*/ /** * Implements hook_help(). */ function webform_localization_help($section = 'admin/help#webform_localization', $arg = NULL) { $output = ''; switch ($section) { case 'admin/help#webform_localization': $output = '
' . t('The Webform Localization module provides multilingual features to the Webform Module. Special options in the webform and component configuration let you enable different ways to manage translation of forms and questionnaires.') . '
'; $output .= '' . t('You can choose two different ways to manage localization that cover this scenarios:') . '
'; $output .= '' . t('A) If you want to keep a single webform across all nodes in a translation set:
Use i18n_string integration to translate webform strings. This module expose webform properties, components and emails strings through the i18n module. All submissions results are related to the original node only.
(You have a "localization by string translation" fieldset in the form settings to enable this)') . '
' . t('B) If you want to keep a webform per node per language but synchronized:
The entire webform structure is replicated when a translated node is created then you can customize it at will. You can add specific options or components per language and choose to keep sync: webform properties, components properties, roles and emails recipients. In this scenario make no sense of having results attached to one node since each webform could have a different structure with only a few components in sync.
(You have a "localization by sync" fieldset in the form settings to enable this)') . '
' . t('This feature implements an i18n_string integration and let you keep a single webform across several nodes in a translation set.This feature is useful when you want one single webform / node acrossdifferent languages.', array('html' => TRUE)) . '
', ); $form['localization_by_string']['expose_strings'] = array( '#type' => 'checkbox', '#title' => t('Expose webform component strings suitable for translation.'), '#default_value' => $webform_localization_options['expose_strings'], '#description' => '' . t('Use the i18n module to translate webform component strings.', array('html' => TRUE)) . '
', ); $form['localization_by_string']['single_webform'] = array( '#type' => 'checkbox', '#title' => t('Keep a single webform across a translation set.'), '#default_value' => $single_webform, '#description' => '' . t('When you use the i18n module to translate webform component strings, you may like to keep a single webform to attach on each node from a translation set.', array('html' => TRUE)) . '
', ); $form['localization_by_sync'] = array( '#type' => 'fieldset', '#title' => t('Localization by Sync'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -5, '#description' => '' . t('This feature let you have a webform per language and keeping all of them synchronized at different levels.You can choose to synchronize webform properties, submission roles, e-mail recipients and component properties.This feature is useful when you want a different webform /node per language but keeping some settings synchronized.', array('html' => TRUE)) . '
', ); $form['localization_by_sync']['webform_properties_header'] = array( '#type' => 'markup', '#prefix' => t('Synchronize webform properties across node translations.'), '#markup' => '' . t('You can choose to synchronize specific properties of the webform.') . '
' . t('Copy the entire webform structure when a node is translated and synchronize changes on selected components properties.', array('html' => TRUE)) . '
', ); $form['localization_by_sync']['sync_roles'] = array( '#type' => 'checkbox', '#title' => t('Synchronize webform submission access roles across node translations.'), '#default_value' => $webform_localization_options['sync_roles'], '#description' => '' . t('Keep the roles that can submit a webform synchronized in a translation set.') . '
', ); $form['localization_by_sync']['sync_emails'] = array( '#type' => 'checkbox', '#title' => t('Synchronize webform e-mail recipients across node translations.'), '#default_value' => $webform_localization_options['sync_emails'], '#description' => '' . t('Keep the webform e-mail recipients synchronized in a translation set.') . '
', ); $form['#submit'][] = '_webform_localization_webform_configure_form_submit'; if ($webform_localization_options['expose_strings']) { // Using i18n string we need to tweak values before submit. $submit_array = $form['#submit']; array_unshift($submit_array, '_webform_localization_webform_configure_form_submit_i18n_tweaks'); $form['#submit'] = $submit_array; } } /** * Handle specific localization options in Webform Configure Form. */ function _webform_localization_webform_configure_form_submit($form, &$form_state) { $webform_properties = $form_state['values']['webform_properties']; foreach ($webform_properties as $key => $value) { if (!is_string($value)) { unset($webform_properties[$key]); } } if (count($webform_properties) == 0) { $webform_properties = ''; } else { $webform_properties = serialize($webform_properties); } if ($form_state['values']['single_webform'] > 0) { $form_state['values']['single_webform'] = $form_state['values']['nid']; } $webform_localization_options = array( 'nid' => $form_state['values']['nid'], 'expose_strings' => $form_state['values']['expose_strings'], 'sync_components' => $form_state['values']['sync_components'], 'sync_roles' => $form_state['values']['sync_roles'], 'sync_emails' => $form_state['values']['sync_emails'], 'single_webform' => $form_state['values']['single_webform'], 'webform_properties' => $webform_properties, ); $prev_options = webform_localization_get_config($form_state['values']['nid']); if (isset($prev_options['no_persistent'])) { drupal_write_record('webform_localization', $webform_localization_options); } else { drupal_write_record('webform_localization', $webform_localization_options, array('nid')); } module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync'); webform_localization_webform_properties_sync($form_state['values']['nid']); $webform_localization_options = webform_localization_get_config($form_state['values']['nid']); if ($webform_localization_options['sync_roles']) { webform_localization_roles_sync($form_state['values']['nid']); } if ($webform_localization_options['expose_strings']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); webform_localization_update_translation_strings($form_state['values']); } } /** * Handle translated element tweaks in Webform Configure Form. */ function _webform_localization_webform_configure_form_submit_i18n_tweaks($form, &$form_state) { global $language; $default_language = language_default(); if ($default_language->language != $language->language) { // Webform Configure Form not in default language. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); $name = webform_localization_i18n_string_name($form['#node']->webform['nid'], 'confirmation'); $string_source = i18n_string_get_string($name); $string_translation = $form_state['values']['confirmation']['value']; // We reset the source string value before saving the form. $form_state['values']['confirmation']['value'] = $string_source->get_string(); // We save the translated string using i18n string. i18n_string_translation_update($name, $string_translation, $language->language); } } /** * Implements hook_form_FORM_ID_alter(). * * Add a Submit function to handle localization features. */ function webform_localization_form_webform_email_edit_form_alter(&$form, &$form_state, $form_id) { $form['#submit'][] = '_webform_localization_webform_email_edit_form_submit'; } /** * Handle emails sync on individual email change. */ function _webform_localization_webform_email_edit_form_submit($form, &$form_state) { $node = $form['#node']; $webform_localization_options = webform_localization_get_config($node->nid); if ($webform_localization_options['sync_emails']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync'); webform_localization_emails_sync($node->nid); } if ($webform_localization_options['expose_strings']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); webform_localization_emails_update_translation_string($form_state['values'] + array('node' => $node)); } } /** * Implements hook_form_FORM_ID_alter(). * * Add a Submit function to handle sync feature. */ function webform_localization_form_webform_email_delete_form_alter(&$form, &$form_state, $form_id) { $form['#submit'][] = '_webform_localization_webform_email_delete_form_submit'; } /** * Handle emails localization cleanup / sync on email deletion. */ function _webform_localization_webform_email_delete_form_submit($form, &$form_state) { $node = $form['#node']; $webform_localization_options = webform_localization_get_config($node->nid); if ($webform_localization_options['sync_emails']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync'); webform_localization_emails_sync($node->nid); } if ($webform_localization_options['expose_strings']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); webform_localization_emails_delete_translation_string($form_state['values']['email']['eid'], $node->nid); } } /** * Implements hook_form_FORM_ID_alter(). * * Add specific localization options to Webform Component Edit Form. */ function webform_localization_form_webform_component_edit_form_alter(&$form, &$form_state, $form_id) { $component = $form_state['build_info']['args'][1]; if (!isset($component['cid'])) { $component['cid'] = -1; } // Gets webform localization options that match this node ID. $webform_localization_options = webform_localization_get_config($component['nid']); if ($webform_localization_options['sync_components']) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync'); $select_options = webform_localization_synchronizable_properties($component); $form['localization'] = array( '#type' => 'fieldset', '#title' => t('Localization by Sync / Component settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#weight' => -4, '#description' => t('Here you can specified what properties need to be sync for thiscomponent across the several nodes in a translation set. If youare seeing this means that you have enabled the "Synchronizewebform components across node translations" option in thewebform that contains this component.', array('html' => TRUE)), ); $form['localization']['standar_properties'] = array( '#type' => 'checkboxes', '#title' => t('Select properties to synchronize across translations.'), '#options' => $select_options['standar'], '#default_value' => $select_options['standar_values'], '#description' => t('Common properties that applies to all types of components.'), ); $form['localization']['extra_properties'] = array( '#type' => 'checkboxes', '#title' => t('Select especial properties to synchronize across translations.'), '#options' => $select_options['extra'], '#default_value' => $select_options['extra_values'], '#description' => t('Special properties that applies only for this type of component.'), ); /** * NOTE: * First we save the sync options to know what to do with the changes. */ array_unshift($form['#submit'], '_webform_localization_webform_component_edit_form_submit'); } } /** * Handle specific localization options in Webform Component Edit Form. */ function _webform_localization_webform_component_edit_form_submit($form, &$form_state) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync'); $options = array( 'nid' => $form_state['values']['nid'], 'cid' => $form_state['values']['cid'], 'type' => $form_state['values']['type'], 'standar_properties' => serialize($form_state['values']['localization']['standar_properties']), 'extra_properties' => serialize($form_state['values']['localization']['extra_properties']), ); $prev_options = webform_localization_synchronizable_properties($options); if (isset($prev_options['no_persistent'])) { drupal_write_record('webform_component_localization', $options); } else { drupal_write_record('webform_component_localization', $options, array('nid', 'cid')); } // We reload cached configuration for this component. webform_localization_synchronizable_properties($options, TRUE); } /** * Implements hook_field_attach_prepare_translation_alter(). */ function webform_localization_field_attach_prepare_translation_alter(&$entity, $context) { if ($context['entity_type'] == 'node') { if (isset($context['source_entity']->webform)) { $webform_localization_options = webform_localization_get_config($context['source_entity']->nid); /** * Copy all Webform settings over to translated versions of this node * if the configuration match. */ if ($webform_localization_options['sync_components']) { /** * NOTE: * Perhaps could be interesting to copy only specific properties * but for now the entire webform make more sense. */ $entity->webform = $context['source_entity']->webform; } } } } /** * Gets webform localization options that match a node ID. * * @staticvar array $webform_localization_options * An array of webform localization options group by nid. * @param $nid * A node Id. * @param bool $clear_cache * A flag to force a database reading in case that properties are cached. * * @return array * Webform localization options that match the nid. */ function webform_localization_get_config($nid, $clear_cache = FALSE) { static $webform_localization_options = array(); if ($clear_cache || !isset($webform_localization_options[$nid])) { $defaults = array_keys(webform_node_defaults()); $webform_properties = array(); foreach ($defaults as $key) { $webform_properties[$key] = $key; } unset($webform_properties['components']); unset($webform_properties['roles']); unset($webform_properties['emails']); unset($webform_properties['record_exists']); // Select webform localization options that match this node ID. $options = db_select('webform_localization') ->fields('webform_localization') ->condition('nid', $nid, '=') ->execute() ->fetchAllAssoc('nid', PDO::FETCH_ASSOC); if (count($options) == 0) { $webform_localization_options[$nid] = array( 'nid' => $nid, 'expose_strings' => 0, 'single_webform' => 0, 'sync_components' => 0, 'sync_roles' => 0, 'sync_emails' => 0, 'webform_properties' => array(), 'webform_properties_structure' => $webform_properties, 'no_persistent' => TRUE, ); } else { $options[$nid]['webform_properties_structure'] = $webform_properties; if (empty($options[$nid]['webform_properties'])) { $options[$nid]['webform_properties'] = array(); } else { $options[$nid]['webform_properties'] = unserialize($options[$nid]['webform_properties']); } $webform_localization_options[$nid] = $options[$nid]; } } return $webform_localization_options[$nid]; } /** * Global switch to enable / disable syncing. * * This function also check whether we are synching at the moment. * * @return bool * TRUE if we need to run sync operations. FALSE during syncing * so we don't have recursion. */ function _webform_localization_sync($status = NULL) { static $current = TRUE; if (isset($status)) { $current = $status; } return $current; } /** * Translate webform confirmation field. */ function webform_localization_preprocess_webform_confirmation(&$vars) { if (empty($vars['node']->tnid)) { return; } // Select all webforms that match the localization configuration. $query = db_select('webform', 'w'); $query->innerJoin('webform_localization', 'wl', 'w.nid = wl.nid'); $query->fields('w', array('nid')); $query->condition('wl.single_webform', $vars['node']->tnid, '='); $query->condition('w.nid', $vars['node']->nid, '<>'); $result = $query->execute()->fetchField(); if ($result) { $source_node = node_load($result); // We replace the webform with the node translation source. $vars['node']->webform = $source_node->webform; } else { return; } $confirmation = check_markup($vars['node']->webform['confirmation'], $vars['node']->webform['confirmation_format'], '', TRUE); // Strip out empty tags added by WYSIWYG editors if needed. $vars['confirmation_message'] = drupal_strlen(trim(strip_tags($confirmation))) ? $confirmation : ''; } /** * Implements hook_form_webform_client_form_alter(). */ function webform_localization_form_webform_client_form_alter(&$form, &$form_state, $form_id) { if (!isset($form['#node']->webform['nid'])) { return; } $webform_localization_options = webform_localization_get_config($form['#node']->webform['nid']); if ($webform_localization_options['single_webform']) { if (isset($form['details']['nid']['#value']) && $form['#node']->webform['nid'] == $form['#node']->tnid) { // We keep current language node nid. $form['details']['current_language_nid'] = array( '#type' => 'value', '#value' => $form['details']['nid']['#value'], ); // Nid from the source webform for webform_validation. $form['details']['nid']['#value'] = $form['#node']->webform['nid']; } } } /** * Implements hook_modules_disabled(). */ function webform_localization_modules_disabled($modules) { if (in_array('uuid', $modules)) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); webform_localization_uuid_update_strings(TRUE); } } /** * Implements hook_modules_enabled(). */ function webform_localization_modules_enabled($modules) { if (in_array('uuid', $modules)) { module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n'); webform_localization_uuid_update_strings(FALSE); } }