reverted webform_localization as it depends on webform 4.x
This commit is contained in:
parent
0521608bb7
commit
66fa1d419d
@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Webform localizations for grid component.
|
||||
* Translates the analysis component properties that are translatable.
|
||||
*
|
||||
* These are found in under 'translated_strings' in the 'extra' array of the
|
||||
* component, which is build when the component is inserted / updated, or
|
||||
* when all webform strings are updated from
|
||||
* admin/config/regional/translate/i18n_string.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements _webform_localization_analysis_data_component().
|
||||
*
|
||||
* @param array $data
|
||||
* The data array of component results.
|
||||
* @param array $node
|
||||
* The node
|
||||
* @param array $component
|
||||
* The component.
|
||||
*
|
||||
* @return array
|
||||
* Translated data array of component results.
|
||||
*/
|
||||
function _webform_localization_analysis_data_grid($data, $node, $component) {
|
||||
if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
|
||||
return $data;
|
||||
}
|
||||
$options_key_lookup = _webform_localization_string_to_key($component['extra']['options']);
|
||||
$questions_key_lookup = _webform_localization_string_to_key($component['extra']['questions']);
|
||||
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate options / questions.
|
||||
list (, $key) = explode('-', $name_list[3]);
|
||||
|
||||
if (strpos($name_list[3], 'grid_options') && $name_list[3] !== '#title') {
|
||||
if (isset($options_key_lookup[$key])) {
|
||||
foreach ($data['table_header'] as $index => $row) {
|
||||
if ($row == $options_key_lookup[$key]) {
|
||||
$data['table_header'][$index] = i18n_string($name, $row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strpos($name_list[3], 'grid_questions') && $name_list[3] !== '#title') {
|
||||
if (isset($questions_key_lookup[$key])) {
|
||||
foreach ($data['table_rows'] as $index => $row) {
|
||||
if (trim($row[0]) == trim($questions_key_lookup[$key])) {
|
||||
$data['table_rows'][$index][0] = i18n_string($name, $row[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Webform localizations for select component.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Translate a single option from component.
|
||||
*
|
||||
* @param array $component
|
||||
* The select component
|
||||
* @param string $option
|
||||
* Untranslated option string.
|
||||
*
|
||||
* @return string
|
||||
* The translated option string, if found.
|
||||
*/
|
||||
function webform_localization_translate_select_option($component, $option) {
|
||||
// Find the source for data value and translate it.
|
||||
$item_key_lookup = _webform_localization_string_to_key($component['extra']['items']);
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate options.
|
||||
if (strpos($name_list[3], '-') !== FALSE) {
|
||||
list (, $key) = explode('-', $name_list[3]);
|
||||
if (isset($item_key_lookup[$key]) && $option == $item_key_lookup[$key]) {
|
||||
return i18n_string($name, $option);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements _webform_localization_csv_header_component().
|
||||
*/
|
||||
function _webform_localization_csv_header_select($header, $component) {
|
||||
if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
|
||||
return $header;
|
||||
}
|
||||
// Each component has own methods and tricks to add different items to header
|
||||
// rows. Attempt to translate whatever we can.
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate header from #title property, this is rather common scenario.
|
||||
if ($name_list[3] == '#title' && $component['name'] == $header[2][0]) {
|
||||
$header[2] = i18n_string($name, $component['name']);
|
||||
break;
|
||||
}
|
||||
// Title could be found from position [1][0] and in this case the select
|
||||
// options are on row 2.
|
||||
if ($name_list[3] == '#title' && $component['name'] == $header[1][0]) {
|
||||
$header[1] = i18n_string($name, $component['name']);
|
||||
foreach ($header[2] as $i => $option) {
|
||||
$header[2][$i] = webform_localization_translate_select_option($component, $option);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements _webform_localization_csv_data_component().
|
||||
*/
|
||||
function _webform_localization_csv_data_select($data, $component, $submission) {
|
||||
// If data is an array then answers are being marked as X:es and there is no
|
||||
// need to translate these.
|
||||
if (is_array($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
|
||||
return $data;
|
||||
}
|
||||
return webform_localization_translate_select_option($component, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements _webform_localization_analysis_data_component().
|
||||
*/
|
||||
function _webform_localization_analysis_data_select($data, $node, $component) {
|
||||
if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
|
||||
return $data;
|
||||
}
|
||||
$item_key_lookup = _webform_localization_string_to_key($component['extra']['items']);
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate options.
|
||||
if (strpos($name_list[3], '-') !== FALSE) {
|
||||
list (, $key) = explode('-', $name_list[3]);
|
||||
if (isset($item_key_lookup[$key])) {
|
||||
foreach ($data['table_rows'] as $index => $row) {
|
||||
if ($row[0] == $item_key_lookup[$key]) {
|
||||
$data['table_rows'][$index][0] = i18n_string($name, $row[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
@ -18,42 +18,38 @@
|
||||
*
|
||||
* @staticvar array $component_translations
|
||||
* An array of webform components for each tnid.
|
||||
*
|
||||
* @param array $component
|
||||
* A webform component array.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array of webform components that match a tnid.
|
||||
*
|
||||
*/
|
||||
function webform_localization_component_get_translations($component) {
|
||||
static $component_translations = array();
|
||||
|
||||
$node = node_load($component['nid']);
|
||||
$translations = translation_node_get_translations($node->tnid);
|
||||
$component_translations[$node->tnid] = array();
|
||||
|
||||
if (!empty($translations) && !isset($component_translations[$node->tnid])) {
|
||||
if (!isset($component_translations[$node->tnid])) {
|
||||
$nid_list = array();
|
||||
foreach ($translations as $trans_node) {
|
||||
$nid_list[] = $trans_node->nid;
|
||||
}
|
||||
// Load components for each translated node.
|
||||
if (!empty($nid_list)) {
|
||||
$components = db_select('webform_component')
|
||||
->fields('webform_component')
|
||||
->condition('nid', $nid_list, 'IN')
|
||||
->condition('cid', $component['cid'], '=')
|
||||
->orderBy('nid')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
// Cleanup on each component.
|
||||
foreach ($components as $cid => $c) {
|
||||
$components[$cid]['nid'] = $c['nid'];
|
||||
$components[$cid]['extra'] = unserialize($c['extra']);
|
||||
webform_component_defaults($components[$cid]);
|
||||
}
|
||||
$component_translations[$node->tnid] = $components;
|
||||
$components = db_select('webform_component')
|
||||
->fields('webform_component')
|
||||
->condition('nid', $nid_list, 'IN')
|
||||
->condition('cid', $component['cid'], '=')
|
||||
->orderBy('nid')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
// Cleanup on each component.
|
||||
foreach ($components as $cid => $c) {
|
||||
$components[$cid]['nid'] = $c['nid'];
|
||||
$components[$cid]['extra'] = unserialize($c['extra']);
|
||||
webform_component_defaults($components[$cid]);
|
||||
}
|
||||
$component_translations[$node->tnid] = $components;
|
||||
}
|
||||
|
||||
return $component_translations[$node->tnid];
|
||||
@ -62,17 +58,17 @@ function webform_localization_component_get_translations($component) {
|
||||
/**
|
||||
* Synchronize the changed component with it's translations versions.
|
||||
*
|
||||
* @param array $component
|
||||
* @param $component
|
||||
* A webform component that have been modified.
|
||||
* @param array $translations
|
||||
* @param $translations
|
||||
* An Array of the translated webform components to sync with.
|
||||
*/
|
||||
function webform_localization_component_sync($component, &$translations) {
|
||||
|
||||
// Get properties to sync.
|
||||
// $sync_properties['standar_values'] = array('mandatory', 'weight', 'pid');
|
||||
// $sync_properties['extra_values'] = array('options', 'private');
|
||||
|
||||
/**
|
||||
* Get properties to sync
|
||||
* $sync_properties['standar_values'] = array('mandatory', 'weight', 'pid');
|
||||
* $sync_properties['extra_values'] = array('options', 'private');
|
||||
*/
|
||||
$sync_properties = webform_localization_synchronizable_properties($component);
|
||||
foreach ($translations as $component_key => $translation) {
|
||||
foreach ($sync_properties['standar_values'] as $sync_key) {
|
||||
@ -89,14 +85,13 @@ function webform_localization_component_sync($component, &$translations) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get synchronizable properties for a webform component.
|
||||
* Get synchronizable properties for a webform component
|
||||
*
|
||||
* @param array $component
|
||||
* A webform component.
|
||||
* @param bool $clear_cache
|
||||
* @param boolean $clear_cache
|
||||
* A flag to force a database reading in case that properties are cached.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array with synchronizable properties.
|
||||
*/
|
||||
function webform_localization_synchronizable_properties($component, $clear_cache = FALSE) {
|
||||
@ -107,11 +102,11 @@ function webform_localization_synchronizable_properties($component, $clear_cache
|
||||
if ($clear_cache || !isset($webform_component_localization_options[$nid][$cid])) {
|
||||
// Select webform localization options that match this node ID.
|
||||
$options = db_select('webform_component_localization')
|
||||
->fields('webform_component_localization')
|
||||
->condition('nid', $nid, '=')
|
||||
->condition('cid', $cid, '=')
|
||||
->execute()
|
||||
->fetchObject();
|
||||
->fields('webform_component_localization')
|
||||
->condition('nid', $nid, '=')
|
||||
->condition('cid', $cid, '=')
|
||||
->execute()
|
||||
->fetchObject();
|
||||
if (!$options) {
|
||||
$synchronizable = _webform_localization_default_properties($component);
|
||||
$webform_component_localization_options[$nid][$cid] = $synchronizable;
|
||||
@ -141,8 +136,7 @@ function webform_localization_synchronizable_properties($component, $clear_cache
|
||||
*
|
||||
* @param array $component
|
||||
* A webform component.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array with webform synchronizable default properties.
|
||||
*/
|
||||
function _webform_localization_default_properties($component) {
|
||||
@ -177,23 +171,23 @@ function webform_localization_synchronizable_properties_delete($component) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a Webform Component.
|
||||
* Load a Webform Component
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
* @param int $cid
|
||||
* @param $cid
|
||||
* A webform component Id.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* A webform component array.
|
||||
*
|
||||
*/
|
||||
function webform_localization_component_load($nid, $cid) {
|
||||
$component = db_select('webform_component')
|
||||
->fields('webform_component')
|
||||
->condition('nid', $nid, '=')
|
||||
->condition('cid', $cid, '=')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
->fields('webform_component')
|
||||
->condition('nid', $nid, '=')
|
||||
->condition('cid', $cid, '=')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
$component[$nid]['nid'] = $nid;
|
||||
$component[$nid]['extra'] = unserialize($component[$nid]['extra']);
|
||||
webform_component_defaults($component[$nid]);
|
||||
|
@ -4,7 +4,6 @@
|
||||
* @file
|
||||
* Webform Localization i18n_string integration.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides interface with the i18n_string module.
|
||||
* Based in patch http://drupal.org/node/245424#comment-5244256
|
||||
@ -28,16 +27,9 @@
|
||||
*/
|
||||
function _webform_localization_translate_component(&$element, $component) {
|
||||
if (isset($component['extra']['translated_strings']) && is_array($component['extra']['translated_strings'])) {
|
||||
$node = !empty($component['nid']) ? node_load($component['nid']) : NULL;
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
$current_element = &$element;
|
||||
$current_element_format;
|
||||
if (isset($current_element['#format'])) {
|
||||
$current_element_format = $current_element['#format'];
|
||||
} else {
|
||||
$current_element_format = I18N_STRING_FILTER_XSS_ADMIN;
|
||||
}
|
||||
if (strpos($name_list[3], '[') !== FALSE) {
|
||||
// The property is deeper in the renderable array, we must extract the
|
||||
// the place where it is.
|
||||
@ -60,15 +52,9 @@ function _webform_localization_translate_component(&$element, $component) {
|
||||
}
|
||||
if (strpos($property, '-') !== FALSE) {
|
||||
// If property is array, we extract the key from the property.
|
||||
list ($property, $key) = explode('-', $property, 2);
|
||||
list ($property, $key) = explode('-', $property);
|
||||
if (isset($current_element['#' . $property][$key])) {
|
||||
$text = i18n_string($name, $current_element['#' . $property][$key], array('format' => I18N_STRING_FILTER_XSS));
|
||||
if (module_exists('token')) {
|
||||
$current_element['#' . $property][$key] = webform_replace_tokens($text, $node);
|
||||
}
|
||||
else {
|
||||
$current_element['#' . $property][$key] = $text;
|
||||
}
|
||||
$current_element['#' . $property][$key] = i18n_string($name, $current_element['#' . $property][$key], array('sanitize' => FALSE));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -77,23 +63,11 @@ function _webform_localization_translate_component(&$element, $component) {
|
||||
$option_group = str_replace('/-', '', $name_list[4]);
|
||||
// If it's a element.
|
||||
if (isset($name_list[5])) {
|
||||
$text = i18n_string($name, $current_element['#' . $property][$option_group][$name_list[5]], array('format' => $current_element_format));
|
||||
if (module_exists('token')) {
|
||||
$current_element['#' . $property][$option_group][$name_list[5]] = webform_replace_tokens($text, $node);
|
||||
}
|
||||
else {
|
||||
$current_element['#' . $property][$option_group][$name_list[5]] = $text;
|
||||
}
|
||||
$current_element['#' . $property][$option_group][$name_list[5]] = i18n_string($name, $current_element['#' . $property][$option_group][$name_list[5]]);
|
||||
}
|
||||
else {
|
||||
// If it's a option group we translate the key.
|
||||
$text = i18n_string($name, $option_group, array('format' => $current_element_format));
|
||||
if (module_exists('token')) {
|
||||
$translated_option_group = webform_replace_tokens($text, $node);
|
||||
}
|
||||
else {
|
||||
$translated_option_group = $text;
|
||||
}
|
||||
$translated_option_group = i18n_string($name, $option_group);
|
||||
if ($translated_option_group != $option_group) {
|
||||
_webform_localization_array_key_replace($current_element['#' . $property], $option_group, $translated_option_group);
|
||||
}
|
||||
@ -103,15 +77,84 @@ function _webform_localization_translate_component(&$element, $component) {
|
||||
// Else we can treat the property as string.
|
||||
if (isset($current_element['#' . $property])) {
|
||||
if ($property == 'markup' && $current_element['#type'] == 'markup') {
|
||||
$text = i18n_string($name, $current_element['#' . $property], array('format' => $current_element['#format']));
|
||||
$current_element['#' . $property] = i18n_string($name, $current_element['#' . $property], array('format' => $current_element['#format']));
|
||||
}
|
||||
elseif ($property == 'description') {
|
||||
$text = i18n_string($name, $current_element['#' . $property], array('sanitize' => FALSE));
|
||||
$current_element['#' . $property] = i18n_string($name, $current_element['#' . $property], array('format' => I18N_STRING_FILTER_XSS));
|
||||
}
|
||||
else {
|
||||
$text = i18n_string($name, $current_element['#' . $property], array('sanitize' => FALSE));
|
||||
$current_element['#' . $property] = i18n_string($name, $current_element['#' . $property]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates the analysis component properties that are translatable.
|
||||
*
|
||||
* These are found in under 'translated_strings' in the 'extra' array of the
|
||||
* component, which is build when the component is inserted / updated, or
|
||||
* when all webform strings are updated from
|
||||
* admin/config/regional/translate/i18n_string.
|
||||
*
|
||||
* @param array $data
|
||||
* The data array of component results.
|
||||
* @param array $component
|
||||
* The component.
|
||||
*/
|
||||
function _webform_localization_translate_analysis_component(&$data, &$component) {
|
||||
if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
|
||||
return;
|
||||
}
|
||||
// Attempt to translate select options.
|
||||
if ($component['type'] == 'select') {
|
||||
$item_key_lookup = _webform_localization_string_to_key($component['extra']['items']);
|
||||
}
|
||||
// Attempt to translate grid options / questions.
|
||||
if ($component['type'] == 'grid') {
|
||||
$options_key_lookup = _webform_localization_string_to_key($component['extra']['options']);
|
||||
$questions_key_lookup = _webform_localization_string_to_key($component['extra']['questions']);
|
||||
}
|
||||
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate component name from title property.
|
||||
if ($name_list[3] == '#title') {
|
||||
$component['name'] = i18n_string($name, $component['name']);
|
||||
continue;
|
||||
}
|
||||
// Translate options for select elements.
|
||||
if ($component['type'] == 'select' && strpos($name_list[3], '-') !== FALSE) {
|
||||
list (, $key) = explode('-', $name_list[3]);
|
||||
if (isset($item_key_lookup[$key])) {
|
||||
foreach ($data['table_rows'] as $index => $row) {
|
||||
if ($row[0] == $item_key_lookup[$key]) {
|
||||
$data['table_rows'][$index][0] = i18n_string($name, $row[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Translate options / questions for grid elements.
|
||||
if ($component['type'] == 'grid' && $name_list[3] !== '#title') {
|
||||
list (, $key) = explode('-', $name_list[3]);
|
||||
if (strpos($name_list[3], 'grid_options')) {
|
||||
if (isset($options_key_lookup[$key])) {
|
||||
foreach ($data['table_header'] as $index => $row) {
|
||||
if ($row == $options_key_lookup[$key]) {
|
||||
$data['table_header'][$index] = i18n_string($name, $row);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (strpos($name_list[3], 'grid_questions')) {
|
||||
if (isset($questions_key_lookup[$key])) {
|
||||
foreach ($data['table_rows'] as $index => $row) {
|
||||
if (trim($row[0]) == trim($questions_key_lookup[$key])) {
|
||||
$data['table_rows'][$index][0] = i18n_string($name, $row[0]);
|
||||
}
|
||||
$current_element['#' . $property] = $text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -151,9 +194,9 @@ function webform_localization_component_update_translation_strings(&$component)
|
||||
* The renderable array to be parsed.
|
||||
* @param array $component
|
||||
* The component which was rendered.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array of translatabled webform properties.
|
||||
*
|
||||
*/
|
||||
function _webform_localization_component_translation_parse($element, $component) {
|
||||
$translated_properies = array();
|
||||
@ -161,26 +204,19 @@ function _webform_localization_component_translation_parse($element, $component)
|
||||
$element['#parents'] = array();
|
||||
}
|
||||
|
||||
$element['#translatable'][] = 'placeholder';
|
||||
if (isset($element['#translatable']) && is_array($element['#translatable'])) {
|
||||
foreach ($element['#translatable'] as $key) {
|
||||
if (!empty($element['#' . $key]) || !empty($element['#attributes'][$key])) {
|
||||
if (isset($element['#' . $key]) && $element['#' . $key] != '') {
|
||||
if (isset($element['#parents']) && count($element['#parents'])) {
|
||||
$property = '[' . implode('][', $element['#parents']) . ']#' . $key;
|
||||
}
|
||||
else {
|
||||
$property = '#' . $key;
|
||||
}
|
||||
$element_key = '';
|
||||
if ($key == 'placeholder' && !empty($element['#attributes']['placeholder'])) {
|
||||
$element_key = $element['#attributes']['placeholder'];
|
||||
} elseif ($key != 'placeholder' && !empty($element['#' . $key])) {
|
||||
$element_key = $element['#' . $key];
|
||||
}
|
||||
if (is_array($element_key)) {
|
||||
if (is_array($element['#' . $key])) {
|
||||
// If the translatable property is an array, we translate the
|
||||
// children.
|
||||
foreach ($element_key as $elem_key => $elem_value) {
|
||||
foreach ($element['#' . $key] as $elem_key => $elem_value) {
|
||||
// If the child if an array, we translate the elements.
|
||||
if (is_array($elem_value)) {
|
||||
foreach ($elem_value as $k => $v) {
|
||||
@ -201,11 +237,13 @@ function _webform_localization_component_translation_parse($element, $component)
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If the translatable property is not an array,
|
||||
// it can be treated as a string.
|
||||
/**
|
||||
* If the translatable property is not an array,
|
||||
* it can be treated as a string.
|
||||
*/
|
||||
$name = webform_localization_i18n_string_name($component['nid'], $component['cid'], $property);
|
||||
$translated_properies[] = $name;
|
||||
i18n_string($name, $element_key, array('update' => TRUE));
|
||||
i18n_string($name, $element['#' . $key], array('update' => TRUE));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -218,7 +256,8 @@ function _webform_localization_component_translation_parse($element, $component)
|
||||
$element[$child]['#parents'][] = $child;
|
||||
// Add the translated propertied to the list.
|
||||
$translated_properies = array_merge(
|
||||
$translated_properies, _webform_localization_component_translation_parse($element[$child], $component)
|
||||
$translated_properies,
|
||||
_webform_localization_component_translation_parse($element[$child], $component)
|
||||
);
|
||||
}
|
||||
|
||||
@ -231,18 +270,18 @@ function _webform_localization_component_translation_parse($element, $component)
|
||||
* Additional arguments can be passed to add more depth to context
|
||||
*
|
||||
* @param int $node_identifier
|
||||
* webform nid.
|
||||
* webform nid
|
||||
*
|
||||
* @return string
|
||||
* i18n string name grouped by nid or uuid if module is available.
|
||||
* i18n string name grouped by nid or uuid if module is available
|
||||
*/
|
||||
function webform_localization_i18n_string_name($node_identifier) {
|
||||
if (module_exists('uuid') and !uuid_is_valid($node_identifier)) {
|
||||
if (module_exists('uuid')) {
|
||||
$node_identifier = current(entity_get_uuid_by_id('node', array($node_identifier)));
|
||||
}
|
||||
$name = array('webform', $node_identifier);
|
||||
$args = func_get_args();
|
||||
// Remove $node_identifier from args.
|
||||
// Remove $node_identifier from args
|
||||
array_shift($args);
|
||||
foreach ($args as $arg) {
|
||||
$name[] = $arg;
|
||||
@ -251,7 +290,7 @@ function webform_localization_i18n_string_name($node_identifier) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete translation source for all the translatable poperties.
|
||||
* Delete translation source for all the translatable poperties
|
||||
*
|
||||
* Process components matching webforms configuration.
|
||||
*/
|
||||
@ -313,20 +352,21 @@ function webform_localization_update_translation_strings($properties) {
|
||||
/**
|
||||
* Translate general webform properties.
|
||||
*
|
||||
* @param object $node
|
||||
* @param $node
|
||||
* A node object.
|
||||
*/
|
||||
function webform_localization_translate_strings(&$node, $update = FALSE) {
|
||||
$option = array('update' => $update, array('sanitize' => FALSE, array('format' => I18N_STRING_FILTER_XSS_ADMIN)));
|
||||
if (!array_key_exists('nid', $node->webform)) {
|
||||
$node->webform['nid'] = $node->nid;
|
||||
}
|
||||
$option = array('update' => $update, 'sanitize' => FALSE);
|
||||
$name = webform_localization_i18n_string_name($node->webform['nid'], 'confirmation');
|
||||
$node->webform['confirmation'] = i18n_string(
|
||||
$name, $node->webform['confirmation'], $option);
|
||||
$name,
|
||||
$node->webform['confirmation'],
|
||||
$option);
|
||||
$name = webform_localization_i18n_string_name($node->webform['nid'], 'submit_text');
|
||||
$node->webform['submit_text'] = i18n_string(
|
||||
$name, $node->webform['submit_text'], $option);
|
||||
$name,
|
||||
$node->webform['submit_text'],
|
||||
$option);
|
||||
|
||||
// Allow to translate the redirect url if it's not set to none or the
|
||||
// default confirmation page.
|
||||
@ -367,9 +407,9 @@ function webform_localization_emails_update_translation_string($properties) {
|
||||
/**
|
||||
* Update / create translation source for webform email poperties.
|
||||
*
|
||||
* @param array $emails
|
||||
* @param $emails
|
||||
* An array of webform emails.
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* The node Id of the webform.
|
||||
*/
|
||||
function webform_localization_emails_translation_string_refresh($emails, $nid) {
|
||||
@ -398,7 +438,7 @@ function webform_localization_emails_translation_string_refresh($emails, $nid) {
|
||||
/**
|
||||
* Translate webform email poperties.
|
||||
*
|
||||
* @param object $node
|
||||
* @param $node
|
||||
* A node object.
|
||||
*/
|
||||
function webform_localization_email_translate_strings(&$node) {
|
||||
@ -419,7 +459,7 @@ function webform_localization_email_translate_strings(&$node) {
|
||||
}
|
||||
if (!empty($email['template']) && $email['template'] != 'default') {
|
||||
$name = webform_localization_i18n_string_name($nid, 'email', $eid, 'template');
|
||||
$email['template'] = i18n_string($name, $email['template'], array('sanitize' => FALSE));
|
||||
$email['template'] = i18n_string($name, $email['template']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -427,9 +467,9 @@ function webform_localization_email_translate_strings(&$node) {
|
||||
/**
|
||||
* Remove translation source for webform email poperties.
|
||||
*
|
||||
* @param int $eid
|
||||
* @param $eid
|
||||
* A webform email Id.
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*/
|
||||
function webform_localization_emails_delete_translation_string($eid, $nid) {
|
||||
@ -444,7 +484,7 @@ function webform_localization_emails_delete_translation_string($eid, $nid) {
|
||||
/**
|
||||
* Translate general webform poperties.
|
||||
*
|
||||
* @param object $node
|
||||
* @param $node
|
||||
* A node object.
|
||||
*/
|
||||
function webform_localization_delete_translate_strings($node) {
|
||||
@ -459,11 +499,12 @@ function webform_localization_delete_translate_strings($node) {
|
||||
|
||||
/**
|
||||
* Update i18n string contexts if uuid module is enabled/disabled.
|
||||
*
|
||||
*/
|
||||
function webform_localization_uuid_update_strings($disabling_uuid = FALSE) {
|
||||
module_load_install('i18n_string');
|
||||
$old_ids = db_query('SELECT distinct type FROM {i18n_string} WHERE textgroup = :webform', array(
|
||||
':webform' => 'webform',
|
||||
':webform' => 'webform'
|
||||
))->fetchCol();
|
||||
variable_set('webform_localization_using_uuid', !$disabling_uuid);
|
||||
if (empty($old_ids)) {
|
||||
@ -487,13 +528,13 @@ function webform_localization_uuid_update_strings($disabling_uuid = FALSE) {
|
||||
/**
|
||||
* Helper function that retrieves entity IDs by their UUIDs.
|
||||
*
|
||||
* @param string $entity_type
|
||||
*
|
||||
* @param $entity_type
|
||||
* The entity type we should be dealing with.
|
||||
* @param array $uuids
|
||||
* @param $uuids
|
||||
* An array of UUIDs for which we should find their entity IDs. If $revision
|
||||
* is TRUE this should be revision UUIDs instead.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* Array of entity IDs keyed by their UUIDs. If $revision is TRUE revision
|
||||
* IDs and UUIDs are returned instead.
|
||||
*/
|
||||
@ -511,21 +552,22 @@ function webform_localization_get_id_by_uuid($entity_type, $uuids) {
|
||||
|
||||
// Get all UUIDs in one query.
|
||||
return db_select($table, 't')
|
||||
->fields('t', array($uuid_key, $id_key))
|
||||
->condition($uuid_key, array_values($uuids), 'IN')
|
||||
->execute()
|
||||
->fetchAllKeyed();
|
||||
->fields('t', array($uuid_key, $id_key))
|
||||
->condition($uuid_key, array_values($uuids), 'IN')
|
||||
->execute()
|
||||
->fetchAllKeyed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to replace an array key and its content.
|
||||
*
|
||||
* @param array $array
|
||||
* @param $array
|
||||
* Array To process.
|
||||
* @param string $old_key
|
||||
* @param $old_key
|
||||
* Array key to be replaced.
|
||||
* @param string $new_key
|
||||
* @param $new_key
|
||||
* The new array key.
|
||||
*
|
||||
*/
|
||||
function _webform_localization_array_key_replace(&$array, $old_key, $new_key) {
|
||||
$keys = array_keys($array);
|
||||
@ -541,23 +583,17 @@ function _webform_localization_array_key_replace(&$array, $old_key, $new_key) {
|
||||
/**
|
||||
* Helper function to convert select / grid strings to array.
|
||||
*
|
||||
* @param string $string_array
|
||||
* @param $string_array
|
||||
* Array To process.
|
||||
*
|
||||
* @return array
|
||||
* Processed array.
|
||||
*/
|
||||
function _webform_localization_string_to_key($string_array) {
|
||||
$key_array = array();
|
||||
$items = explode("\n", trim($string_array));
|
||||
foreach ($items as $item) {
|
||||
$item_data = explode('|', $item);
|
||||
if (isset($item_data[1]) && isset($item_data[0])) {
|
||||
$key_array[$item_data[0]] = $item_data[1];
|
||||
}
|
||||
elseif (isset($item_data[1]) && !isset($item_data[0])) {
|
||||
$key_array[$item_data[0]] = '';
|
||||
}
|
||||
$key_array[$item_data[0]] = $item_data[1];
|
||||
}
|
||||
return $key_array;
|
||||
}
|
||||
|
||||
|
@ -14,11 +14,10 @@
|
||||
/**
|
||||
* Sync webform configured properties with its translated versions.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*/
|
||||
function webform_localization_webform_properties_sync($nid) {
|
||||
|
||||
// Gets webform localization options that match this node ID.
|
||||
$webform_localization_options = webform_localization_get_config($nid, TRUE);
|
||||
if (count($webform_localization_options['webform_properties']) > 0) {
|
||||
@ -26,10 +25,10 @@ function webform_localization_webform_properties_sync($nid) {
|
||||
if (count($node_list) > 1) {
|
||||
// Select all webforms that match these node IDs.
|
||||
$result = db_select('webform')
|
||||
->fields('webform')
|
||||
->condition('nid', $node_list, 'IN')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
->fields('webform')
|
||||
->condition('nid', $node_list, 'IN')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
if ($result) {
|
||||
$origin = $result[$nid];
|
||||
unset($result[$nid]);
|
||||
@ -48,16 +47,16 @@ function webform_localization_webform_properties_sync($nid) {
|
||||
/**
|
||||
* Sync webform roles with its translated versions.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*/
|
||||
function webform_localization_roles_sync($nid) {
|
||||
$node_list = _webform_localization_translation_set_node_list($nid);
|
||||
$roles = db_select('webform_roles')
|
||||
->fields('webform_roles', array('rid'))
|
||||
->condition('nid', $nid)
|
||||
->execute()
|
||||
->fetchCol();
|
||||
->fields('webform_roles', array('rid'))
|
||||
->condition('nid', $nid)
|
||||
->execute()
|
||||
->fetchCol();
|
||||
foreach ($node_list as $n) {
|
||||
if ($n != $nid) {
|
||||
db_delete('webform_roles')->condition('nid', $n)->execute();
|
||||
@ -71,7 +70,7 @@ function webform_localization_roles_sync($nid) {
|
||||
/**
|
||||
* Sync webform emails recipients with its translated versions.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*/
|
||||
function webform_localization_emails_sync($nid) {
|
||||
@ -112,18 +111,17 @@ function webform_localization_emails_sync($nid) {
|
||||
/**
|
||||
* Get an Array of webform emails recipients for a Node Id.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array of webform emails.
|
||||
*/
|
||||
function _webform_localization_emails_load($nid) {
|
||||
$emails = db_select('webform_emails')
|
||||
->fields('webform_emails')
|
||||
->condition('nid', $nid)
|
||||
->execute()
|
||||
->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
|
||||
->fields('webform_emails')
|
||||
->condition('nid', $nid)
|
||||
->execute()
|
||||
->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
|
||||
// Unserialize the exclude component list for e-mails.
|
||||
foreach ($emails as $eid => $email) {
|
||||
$emails[$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components']));
|
||||
@ -137,10 +135,9 @@ function _webform_localization_emails_load($nid) {
|
||||
/**
|
||||
* Get a node Id list of a translation set.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
* An array of node ids that share a tnid.
|
||||
*/
|
||||
function _webform_localization_translation_set_node_list($nid) {
|
||||
|
@ -9,61 +9,44 @@ class WebformLocalizationWebTestCase extends DrupalWebTestCase {
|
||||
// Webform test class instance.
|
||||
public $wtc;
|
||||
// Users.
|
||||
public $adminuser;
|
||||
public $admin_user;
|
||||
public $translator;
|
||||
public $normaluser;
|
||||
public $perms;
|
||||
protected $profile = 'testing';
|
||||
public $normal_user;
|
||||
|
||||
/**
|
||||
* Implements setUp().
|
||||
*/
|
||||
function setUp($added_modules = array()) {
|
||||
$modules = array('webform_localization', 'webform', 'views', 'ctools', 'i18n_translation', 'i18n_string', 'i18n', 'variable', 'translation', 'locale', 'block');
|
||||
$added_modules = array_merge($modules, $added_modules);
|
||||
parent::setUp($added_modules);
|
||||
function setUp($modules = array()) {
|
||||
$modules = array_merge($modules, array('locale', 'webform', 'webform_localization'));
|
||||
parent::setUp($modules);
|
||||
|
||||
// We load webform test class to reuse webform and components creation functions.
|
||||
module_load_include('test', 'webform', 'tests/webform');
|
||||
$this->wtc = new WebformTestCase;
|
||||
|
||||
/* Reset the permissions cache prior to calling drupalCreateUser
|
||||
* see notes here: https://api.drupal.org/comment/28739#comment-28739
|
||||
*/
|
||||
$this->checkPermissions(array(), TRUE);
|
||||
// Setup users.
|
||||
$this->adminuser = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content', 'create webform content',
|
||||
$this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content', 'create webform content',
|
||||
'edit any webform content',
|
||||
'access all webform results',
|
||||
'edit all webform submissions',
|
||||
'delete all webform submissions',
|
||||
'edit webform components',
|
||||
'translate interface',
|
||||
'translate user-defined strings'));
|
||||
|
||||
/* Reset the permissions cache prior to calling drupalCreateUser
|
||||
* see notes here: https://api.drupal.org/comment/28739#comment-28739
|
||||
*/
|
||||
$this->checkPermissions(array('access content'), TRUE);
|
||||
$this->translator = $this->drupalCreateUser(array('translate content',
|
||||
'create webform content',
|
||||
'edit any webform content',
|
||||
'access all webform results',
|
||||
'edit webform components',
|
||||
'translate interface',
|
||||
'translate user-defined strings'));
|
||||
|
||||
/* Reset the permissions cache prior to calling drupalCreateUser
|
||||
* see notes here: https://api.drupal.org/comment/28739#comment-28739
|
||||
*/
|
||||
$this->checkPermissions(array('access content'), TRUE);
|
||||
$this->normaluser = $this->drupalCreateUser(array('access content', 'edit own webform submissions'));
|
||||
$this->normal_user = $this->drupalCreateUser(array('access content', 'edit own webform submissions'));
|
||||
|
||||
// Fix for reuse of webform test class.
|
||||
$this->wtc->webform_users['admin'] = $this->adminuser;
|
||||
$this->wtc->webform_users['admin']->gender = array(LANGUAGE_NONE => array(array('value' => 'Female')));
|
||||
$this->wtc->webform_users['admin'] = $this->admin_user;
|
||||
$this->wtc->webform_users['admin']->profile_gender = array('Female', 'Male');
|
||||
|
||||
$this->drupalLogin($this->adminuser);
|
||||
$this->drupalLogin($this->admin_user);
|
||||
|
||||
// Add languages.
|
||||
$this->addLanguage('en');
|
||||
@ -124,9 +107,8 @@ class WebformLocalizationWebTestCase extends DrupalWebTestCase {
|
||||
*/
|
||||
unset($components['select_no_default_zero']);
|
||||
unset($components['radios_zero']);
|
||||
unset($components['checkboxes_zero']);
|
||||
unset($components['grid_keyed']);
|
||||
unset($components['select_zero']);
|
||||
unset($components['select_optgroup']);
|
||||
|
||||
$cid = 0;
|
||||
foreach ($components as $key => $component_info) {
|
||||
@ -304,15 +286,14 @@ class WebformLocalizationStringTranslationTestCase extends WebformLocalizationWe
|
||||
'name' => 'Webform Localization',
|
||||
'description' => 'Webform localization String Translations Tests.',
|
||||
'group' => 'Webform Localization',
|
||||
'dependencies' => array('webform_localization', 'webform', 'views', 'ctools', 'i18n_translation', 'i18n_string', 'i18n', 'variable'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up test.
|
||||
*/
|
||||
function setUp($added_modules = array()) {
|
||||
parent::setUp($added_modules);
|
||||
public function setUp($modules = array()) {
|
||||
parent::setUp(array('translation', 'i18n', 'i18n_string'));
|
||||
|
||||
// Set "Webform" content type to use multilingual support with translation.
|
||||
$this->drupalGet('admin/structure/types/manage/webform');
|
||||
@ -326,7 +307,7 @@ class WebformLocalizationStringTranslationTestCase extends WebformLocalizationWe
|
||||
* Test creating a webform and enabling localization by string translation
|
||||
*/
|
||||
function testWebformLocalizationStringTranslation() {
|
||||
$this->drupalLogin($this->adminuser);
|
||||
$this->drupalLogin($this->admin_user);
|
||||
/**
|
||||
* Create the Webform test node, and enable
|
||||
* localization by string translation feature
|
||||
@ -419,61 +400,10 @@ class WebformLocalizationStringTranslationTestCase extends WebformLocalizationWe
|
||||
}
|
||||
$this->assertRaw($translation, format_string('%c translation is present.', array('%c' => $name)), 'Deutsch Webform translation');
|
||||
}
|
||||
|
||||
// Log in as normal user and add a answer to webform so string
|
||||
// translatability can be tested at various webform report pages.
|
||||
$this->drupalLogin($this->normaluser);
|
||||
$this->drupalPost('node/' . $node->nid, array(), t('Submit'));
|
||||
// Log back to admin account and download results, using Deutsch Webform
|
||||
// translations
|
||||
$this->drupalLogin($this->adminuser);
|
||||
$edit = array(
|
||||
'format' => 'delimited',
|
||||
);
|
||||
$this->drupalPost('de/node/' . $node->nid . '/webform-results/download', $edit, t('Download'));
|
||||
// Previous form submission batch created the export file. Click the link
|
||||
// to access export file itself.
|
||||
$this->clickLink(t('download the file here'));
|
||||
// Export file should contain all element titles translated
|
||||
foreach ($options as $key => $value) {
|
||||
$context = $value['context'];
|
||||
if (substr($context, -5) != ':#title') {
|
||||
continue;
|
||||
}
|
||||
$translation = 'de:' . $value['source'];
|
||||
$this->assertRaw($translation, t('%c translation is present.', array('%c' => $value['source'])), 'Deutsch Webform translation');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The same test as WebformLocalizationStringTranslationTestCase, but with the
|
||||
* Token module enabled.
|
||||
*/
|
||||
class WebformLocalizationStringTranslationTokenTestCase extends WebformLocalizationStringTranslationTestCase {
|
||||
/**
|
||||
* Test info.
|
||||
*/
|
||||
public static function getInfo() {
|
||||
$info = parent::getInfo();
|
||||
$info = array_merge($info, array(
|
||||
'name' => 'Webform Localization Token',
|
||||
'description' => 'Webform localization String Translations with Token Tests.',
|
||||
));
|
||||
$info['dependencies'][] = 'token';
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up test.
|
||||
*/
|
||||
function setUp($added_modules = array()) {
|
||||
$added_modules = array_merge(array('token'), $added_modules);
|
||||
parent::setUp($added_modules);
|
||||
}
|
||||
}
|
||||
|
||||
class WebformLocalizationApiTestCase extends WebformLocalizationWebTestCase {
|
||||
|
||||
/**
|
||||
@ -484,15 +414,14 @@ class WebformLocalizationApiTestCase extends WebformLocalizationWebTestCase {
|
||||
'name' => 'Test Webform Localization API.',
|
||||
'description' => 'Test webform and webform localization interaction at API level.',
|
||||
'group' => 'Webform Localization',
|
||||
'dependencies' => array('webform_localization', 'webform', 'views', 'ctools', 'i18n_translation', 'i18n_string', 'i18n', 'variable'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up test.
|
||||
*/
|
||||
function setUp($added_modules = array()) {
|
||||
parent::setUp($added_modules);
|
||||
public function setUp($modules = array()) {
|
||||
parent::setUp(array('translation', 'i18n', 'i18n_string'));
|
||||
|
||||
// Set "Webform" content type to use multilingual support with translation.
|
||||
$this->drupalGet('admin/structure/types/manage/webform');
|
||||
@ -506,7 +435,7 @@ class WebformLocalizationApiTestCase extends WebformLocalizationWebTestCase {
|
||||
* Test submissions API function with webform localization presence.
|
||||
*/
|
||||
function testWebformLocalizationApi() {
|
||||
$this->drupalLogin($this->adminuser);
|
||||
$this->drupalLogin($this->admin_user);
|
||||
$node = $this->createWebformForm();
|
||||
/**
|
||||
* Enables localization by string translation and reuse the single webform
|
||||
|
@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Webform localization hooks.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Module specific instance of _webform_localization_csv_header().
|
||||
*
|
||||
* This allows each component to translate the analysis report CSV header.
|
||||
* These are found in under 'translated_strings' in the 'extra' array of the
|
||||
* component, which is build when the component is inserted / updated, or
|
||||
* when all webform strings are updated from
|
||||
* admin/config/regional/translate/i18n_string.
|
||||
*
|
||||
* @param array $header
|
||||
* The untranslated CSV header to be altered.
|
||||
* @param array $component
|
||||
* The webform component.
|
||||
*
|
||||
* @return array
|
||||
* The translated header array.
|
||||
*/
|
||||
function _webform_localization_csv_header_component($header, $component) {
|
||||
$header[1] = 'Custom string';
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Module specific instance of _webform_localization_csv_data().
|
||||
*
|
||||
* This allows each component to translate the analysis report CSV data.
|
||||
*
|
||||
* @param array $data
|
||||
* The untranslated CSV data to be altered.
|
||||
* @param array $component
|
||||
* The webform component.
|
||||
* @param array $submission
|
||||
* The webform submission.
|
||||
*
|
||||
* @return array
|
||||
* The translated data array.
|
||||
*/
|
||||
function _webform_localization_csv_data_component($data, $component, $submission) {
|
||||
$data[1] = 'Custom string';
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Module specific instance of _webform_localization_analysis_data().
|
||||
*
|
||||
* This allows each component to translate the analysis report data.
|
||||
* These are found in under 'translated_strings' in the 'extra' array of the
|
||||
* component, which is build when the component is inserted / updated, or
|
||||
* when all webform strings are updated from
|
||||
* admin/config/regional/translate/i18n_string.
|
||||
*
|
||||
* @param array $data
|
||||
* The untranslated data to be altered
|
||||
* @param object $node
|
||||
* The webform node.
|
||||
* @param array $component
|
||||
* The webform component.
|
||||
*
|
||||
* @return array
|
||||
* The translated data array.
|
||||
*/
|
||||
function _webform_localization_analysis_data_component($data, $node, $component) {
|
||||
$data[1] = 'Custom string';
|
||||
return $data;
|
||||
}
|
@ -1,23 +1,16 @@
|
||||
; $Id: $
|
||||
|
||||
name = Webform Localization
|
||||
description = Enables localization features to forms and questionnaires.
|
||||
dependencies[] = webform
|
||||
dependencies[] = i18n_string
|
||||
core = 7.x
|
||||
package = Webform
|
||||
php = 5.3
|
||||
dependencies[] = locale
|
||||
dependencies[] = translation
|
||||
dependencies[] = variable
|
||||
dependencies[] = i18n_translation
|
||||
dependencies[] = i18n_string
|
||||
dependencies[] = webform (4.x)
|
||||
test_dependencies[] = token
|
||||
test_dependencies[] = i18n_block
|
||||
|
||||
files[] = tests/webform_localization.test
|
||||
|
||||
; Information added by Drupal.org packaging script on 2016-10-22
|
||||
version = "7.x-4.9"
|
||||
; Information added by Drupal.org packaging script on 2014-03-28
|
||||
version = "7.x-4.x-dev"
|
||||
core = "7.x"
|
||||
project = "webform_localization"
|
||||
datestamp = "1477171440"
|
||||
datestamp = "1396049366"
|
||||
|
||||
|
@ -9,11 +9,12 @@
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function webform_localization_install() {
|
||||
|
||||
// NOTE:
|
||||
// We add a field to record the language of the submission since when using
|
||||
// "Localization by String Translation" you can get single webform been
|
||||
// submitted by several nodes in different languages.
|
||||
/**
|
||||
* NOTE:
|
||||
* We add a field to record the language of the submission since when using
|
||||
* "Localization by String Translation" you can get single webform been
|
||||
* submitted by several nodes in different languages.
|
||||
*/
|
||||
db_add_field('webform_submissions', 'language', array(
|
||||
'description' => 'The {languages}.language source of this submission.',
|
||||
'type' => 'varchar',
|
||||
|
@ -53,100 +53,53 @@ function webform_localization_i18n_string_info() {
|
||||
* Refresh callback that regenerates all the translatable poperties of the
|
||||
* components of the matching webforms configuration.
|
||||
*/
|
||||
function webform_localization_i18n_string_refresh($group) {
|
||||
if ($group == 'webform') {
|
||||
global $language;
|
||||
$languages = language_list();
|
||||
$source_language = $languages[i18n_string_source_language()];
|
||||
// Refresh the strings using source language.
|
||||
$language = $source_language;
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
function webform_localization_i18n_string_refresh() {
|
||||
|
||||
// In case updating before UUID support.
|
||||
if (module_exists('uuid') && !variable_get('webform_localization_using_uuid', FALSE)) {
|
||||
webform_localization_uuid_update_strings(FALSE);
|
||||
}
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
|
||||
// Get components configured as translatable.
|
||||
$query = db_select('webform_component', 'wc');
|
||||
$query->fields('wc');
|
||||
$query->condition('wl.expose_strings', 0, '>');
|
||||
$query->innerJoin('webform_localization', 'wl', 'wc.nid = wl.nid');
|
||||
$components = $query->execute()->fetchAll();
|
||||
|
||||
foreach ($components as $component) {
|
||||
$component = (array) $component;
|
||||
$component['extra'] = unserialize($component['extra']);
|
||||
|
||||
webform_localization_component_update_translation_strings($component);
|
||||
|
||||
$component['extra'] = serialize($component['extra']);
|
||||
drupal_write_record('webform_component', $component, array('nid', 'cid'));
|
||||
}
|
||||
// Get emails configured as translatable.
|
||||
$query = db_select('webform_localization', 'wl');
|
||||
$query->fields('wl', array('nid'));
|
||||
$query->condition('wl.expose_strings', 0, '>');
|
||||
$nid_list = $query->execute()->fetchAllAssoc('nid');
|
||||
// @todo: Find a more eficient way to manage webform translatable
|
||||
// properties.
|
||||
$nodes = node_load_multiple(array_keys($nid_list));
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
|
||||
foreach ($nid_list as $nid => $value) {
|
||||
$emails = _webform_localization_emails_load($nid);
|
||||
webform_localization_emails_translation_string_refresh($emails, $nid);
|
||||
$node = $nodes[$nid];
|
||||
webform_localization_translate_strings($node, TRUE);
|
||||
}
|
||||
|
||||
// NOTE: Delete string for webforms that has disabled i18n translation.
|
||||
// This is the only moment when we deleted translation for disabled
|
||||
// webforms. This way we provide the feature to temporally disable the
|
||||
// webform i18n string without losing custom translated texts.
|
||||
webform_localization_delete_all_strings();
|
||||
return TRUE;
|
||||
// In case updating before UUID support.
|
||||
if (module_exists('uuid') && !variable_get('webform_localization_using_uuid', FALSE)) {
|
||||
webform_localization_uuid_update_strings(FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a component file into memory.
|
||||
*
|
||||
* @see webform_component_include()
|
||||
*
|
||||
* @param string $component_type
|
||||
* The string machine name of a component.
|
||||
*/
|
||||
function webform_localization_component_include($component_type) {
|
||||
static $included = array();
|
||||
// Get components configured as translatable.
|
||||
$query = db_select('webform_component', 'wc');
|
||||
$query->fields('wc');
|
||||
$query->condition('wl.expose_strings', 0, '>');
|
||||
$query->innerJoin('webform_localization', 'wl', 'wc.nid = wl.nid');
|
||||
$components = $query->execute()->fetchAll();
|
||||
|
||||
// No need to load components that have already been added once.
|
||||
if (!isset($included[$component_type])) {
|
||||
$included[$component_type] = TRUE;
|
||||
module_load_include('inc', 'webform_localization', 'components/' . $component_type);
|
||||
foreach ($components as $component) {
|
||||
$component = (array) $component;
|
||||
$component['extra'] = unserialize($component['extra']);
|
||||
|
||||
webform_localization_component_update_translation_strings($component);
|
||||
|
||||
$component['extra'] = serialize($component['extra']);
|
||||
drupal_write_record('webform_component', $component, array('nid', 'cid'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a component callback.
|
||||
*
|
||||
* @see webform_component_invoke()
|
||||
*
|
||||
* @param string $type
|
||||
* The component type as a string.
|
||||
* @param string $callback
|
||||
* The callback to execute.
|
||||
* @param ...
|
||||
* Any additional parameters required by the $callback.
|
||||
*/
|
||||
function webform_localization_component_invoke($type, $callback) {
|
||||
$args = func_get_args();
|
||||
$type = array_shift($args);
|
||||
$callback = array_shift($args);
|
||||
$function = '_webform_localization_' . $callback . '_' . $type;
|
||||
webform_localization_component_include($type);
|
||||
if (function_exists($function)) {
|
||||
return call_user_func_array($function, $args);
|
||||
// Get emails configured as translatable.
|
||||
$query = db_select('webform_localization', 'wl');
|
||||
$query->fields('wl', array('nid'));
|
||||
$query->condition('wl.expose_strings', 0, '>');
|
||||
$nid_list = $query->execute()->fetchAllAssoc('nid');
|
||||
// @todo: Find a more eficient way to manage webform translatable properties.
|
||||
$nodes = node_load_multiple(array_keys($nid_list));
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
|
||||
foreach ($nid_list as $nid => $value) {
|
||||
$emails = _webform_localization_emails_load($nid);
|
||||
webform_localization_emails_translation_string_refresh($emails, $nid);
|
||||
$node = $nodes[$nid];
|
||||
webform_localization_translate_strings($node, TRUE);
|
||||
}
|
||||
/**
|
||||
* NOTE: Delete string for webforms that has disabled i18n translation.
|
||||
* This is the only moment when we deleted translation for disabled webforms.
|
||||
* This way we provide the feature to temporally disable the webform i18n
|
||||
* string without losing custom translated texts.
|
||||
*/
|
||||
webform_localization_delete_all_strings();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -157,12 +110,10 @@ function webform_localization_webform_component_insert($component) {
|
||||
$wl_options = webform_localization_get_config($component['nid']);
|
||||
|
||||
// Create translation source for i18n_string for all the translatable
|
||||
// properties.
|
||||
// poperties.
|
||||
if ($wl_options['expose_strings']) {
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
webform_localization_component_update_translation_strings($component);
|
||||
$component['extra'] = serialize($component['extra']);
|
||||
drupal_write_record('webform_component', $component, array('nid', 'cid'));
|
||||
}
|
||||
|
||||
if ($wl_options['sync_components'] && _webform_localization_sync()) {
|
||||
@ -205,25 +156,19 @@ function webform_localization_webform_component_presave(&$component) {
|
||||
}
|
||||
|
||||
if ($wl_options['sync_components'] && _webform_localization_sync()) {
|
||||
$test_node = node_load($component['nid']);
|
||||
if ($test_node->tnid > 0) {
|
||||
// Turn Off Sync.
|
||||
_webform_localization_sync(FALSE);
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
|
||||
// Get all versions of the component across all translations.
|
||||
$translations = webform_localization_component_get_translations($component);
|
||||
unset($translations[$component['nid']]);
|
||||
// Sync the changed component with it's translations versions.
|
||||
webform_localization_component_sync($component, $translations);
|
||||
foreach ($translations as $trans_c) {
|
||||
webform_component_update($trans_c);
|
||||
}
|
||||
// Turn On Sync.
|
||||
_webform_localization_sync(TRUE);
|
||||
}
|
||||
else {
|
||||
debug('configuration error: webform_localization_sync enabled AND Keep a single webform across a translation set. but no tnid, to resolve this either disable <Keep a single webform across a translation set> by unchecking this option, or enable node translation on the webform', t('debug'));
|
||||
// Turn Off Sync.
|
||||
_webform_localization_sync(FALSE);
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
|
||||
// Get all versions of the component across all translations.
|
||||
$translations = webform_localization_component_get_translations($component);
|
||||
unset($translations[$component['nid']]);
|
||||
// Sync the changed component with it's translations versions.
|
||||
webform_localization_component_sync($component, $translations);
|
||||
foreach ($translations as $trans_c) {
|
||||
webform_component_update($trans_c);
|
||||
}
|
||||
// Turn On Sync.
|
||||
_webform_localization_sync(TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
@ -246,52 +191,16 @@ function webform_localization_webform_component_delete($component) {
|
||||
// Get all versions of the node.
|
||||
$node = node_load($component['nid']);
|
||||
$translations = translation_node_get_translations($node->tnid);
|
||||
if ($translations) {
|
||||
unset($translations[$node->language]);
|
||||
foreach ($translations as $trans_c) {
|
||||
$component_version = webform_localization_component_load($trans_c->nid, $component['cid']);
|
||||
webform_component_delete($trans_c, $component_version);
|
||||
}
|
||||
unset($translations[$node->language]);
|
||||
foreach ($translations as $trans_c) {
|
||||
$component_version = webform_localization_component_load($trans_c->nid, $component['cid']);
|
||||
webform_component_delete($trans_c, $component_version);
|
||||
}
|
||||
|
||||
// Turn On Sync.
|
||||
_webform_localization_sync(TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A menu to_arg handler explicitly invoked by webform_menu_to_arg().
|
||||
*
|
||||
* If a single webform is used across all translations, use the appropriate
|
||||
* node ID for webform paths.
|
||||
*/
|
||||
function webform_localization_webform_menu_to_arg($arg, $map, $index) {
|
||||
if ($node = node_load($arg)) {
|
||||
if ($nid = webform_localization_single_webform_nid($node)) {
|
||||
$node = node_load($nid);
|
||||
}
|
||||
}
|
||||
return $node ? $node->nid : $arg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find nid of node containing the 'single webform' for this translation set.
|
||||
*/
|
||||
function webform_localization_single_webform_nid($node) {
|
||||
$cache = &drupal_static(__FUNCTION__, array());
|
||||
if (!array_key_exists($node->nid, $cache)) {
|
||||
// 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', 0, '<>');
|
||||
$query->condition('wl.single_webform', $node->tnid, '=');
|
||||
$query->condition('w.nid', $node->nid, '<>');
|
||||
$cache[$node->nid] = $query->execute()->fetchField();
|
||||
}
|
||||
return $cache[$node->nid];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_node_view().
|
||||
*/
|
||||
@ -299,13 +208,23 @@ function webform_localization_node_view($node, $view_mode) {
|
||||
if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
|
||||
return;
|
||||
}
|
||||
if ($nid = webform_localization_single_webform_nid($node)) {
|
||||
// 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', 0, '<>');
|
||||
$query->condition('wl.single_webform', $node->tnid, '=');
|
||||
$query->condition('w.nid', $node->nid, '<>');
|
||||
$result = $query->execute()->fetchField();
|
||||
|
||||
// NOTE:
|
||||
// Perhaps not most eficient way.. a possible alternative
|
||||
// @todo rewrite the webform load and view process as a
|
||||
// independent function to reuse.
|
||||
$source_node = node_load($nid);
|
||||
if ($result) {
|
||||
/**
|
||||
* NOTE:
|
||||
* Perhaps not most eficient way.. a possible alternative
|
||||
* @todo rewrite the webform load and view process as a
|
||||
* independent function to reuse.
|
||||
*/
|
||||
$source_node = node_load($result);
|
||||
// We replace the webform with the node translation source.
|
||||
$node->webform = $source_node->webform;
|
||||
|
||||
@ -346,10 +265,11 @@ function webform_localization_node_load($nodes, $types) {
|
||||
* Implements hook_webform_submission_update().
|
||||
*/
|
||||
function webform_localization_webform_submission_insert($node, $submission) {
|
||||
|
||||
// NOTE:
|
||||
// We have 2 options here: to use the node language or the language of
|
||||
// the page... for now make more sense use the node as language source.
|
||||
/**
|
||||
* NOTE:
|
||||
* We have 2 options here: to use the node language or the language of
|
||||
* the page... for now make more sense use the node as language source
|
||||
*/
|
||||
$language = $node->language;
|
||||
// Update language field when a submission is updated.
|
||||
db_update('webform_submissions')
|
||||
@ -364,8 +284,8 @@ function webform_localization_webform_submission_insert($node, $submission) {
|
||||
* Implements hook_webform_submission_load().
|
||||
*/
|
||||
function webform_localization_webform_submission_load(&$submissions) {
|
||||
if (empty($submissions)) {
|
||||
return;
|
||||
if(empty($submissions)) {
|
||||
return;
|
||||
}
|
||||
$query = db_select('webform_submissions', 's');
|
||||
$query->fields('s', array('language', 'sid'));
|
||||
@ -376,20 +296,12 @@ function webform_localization_webform_submission_load(&$submissions) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_webform_submission_presave().
|
||||
*/
|
||||
function webform_localization_webform_submission_presave(&$node, &$submission) {
|
||||
if (isset($node->tnid) && $node->tnid != 0) {
|
||||
$submission->serial = _webform_submission_serial_next_value($node->tnid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_node_delete().
|
||||
*/
|
||||
function webform_localization_node_delete($node) {
|
||||
if (!in_array($node->type, webform_variable_get('webform_node_types')) || empty($node->webform['components'])) {
|
||||
if (!in_array($node->type, webform_variable_get('webform_node_types'))
|
||||
|| empty($node->webform['components'])) {
|
||||
return;
|
||||
}
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
@ -409,10 +321,6 @@ function webform_localization_webform_component_render_alter(&$element, $compone
|
||||
if ($wl_options['expose_strings']) {
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
_webform_localization_translate_component($element, $component);
|
||||
if (!empty($element['#attributes']['placeholder'])) {
|
||||
$name = webform_localization_i18n_string_name($component['nid'], $component['cid'], '#placeholder');
|
||||
$element['#attributes']['placeholder'] = i18n_string($name, $element['#attributes']['placeholder']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -437,67 +345,22 @@ function webform_localization_webform_analysis_component_data_alter(&$data, $nod
|
||||
$wl_options = webform_localization_get_config($node->nid);
|
||||
// Translate the translatable properties.
|
||||
if ($wl_options['expose_strings']) {
|
||||
// Translate component name, this is not a component spesific property.
|
||||
foreach ($component['extra']['translated_strings'] as $name) {
|
||||
$name_list = explode(':', $name);
|
||||
// Translate component name from title property.
|
||||
if ($name_list[3] == '#title') {
|
||||
$component['name'] = i18n_string($name, $component['name']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Translate data array.
|
||||
$result = webform_localization_component_invoke($component['type'], 'analysis_data', $data, $node, $component);
|
||||
if (!empty($result)) {
|
||||
$data = $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_webform_csv_header_alter().
|
||||
*/
|
||||
function webform_localization_webform_csv_header_alter(&$header, $component) {
|
||||
// Gets webform localization options that match this node ID.
|
||||
$wl_options = webform_localization_get_config($component['nid']);
|
||||
// Translate the translatable properties.
|
||||
if ($wl_options['expose_strings']) {
|
||||
$result = webform_localization_component_invoke($component['type'], 'csv_header', $header, $component);
|
||||
if (!empty($result)) {
|
||||
$header = $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_webform_csv_data_alter().
|
||||
*/
|
||||
function webform_localization_webform_csv_data_alter(&$data, $component, $submission) {
|
||||
// Gets webform localization options that match this node ID.
|
||||
$wl_options = webform_localization_get_config($component['nid']);
|
||||
// Translate the translatable properties.
|
||||
if ($wl_options['expose_strings']) {
|
||||
$result = webform_localization_component_invoke($component['type'], 'csv_data', $data, $component, $submission);
|
||||
if (!empty($result)) {
|
||||
$data = $result;
|
||||
}
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
_webform_localization_translate_analysis_component($data, $component);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*
|
||||
* Add specific localization options to Webform Configure Form.
|
||||
* Add specific localization options to Webform Configure Form
|
||||
*/
|
||||
function webform_localization_form_webform_configure_form_alter(&$form, &$form_state, $form_id) {
|
||||
|
||||
// @see https://api.drupal.org/comment/52443#comment-52443
|
||||
$enabled_languages = locale_language_list('name');
|
||||
|
||||
// Gets webform localization options that match this node ID.
|
||||
$webform_localization_options = webform_localization_get_config($form['nid']['#value']);
|
||||
|
||||
if ($webform_localization_options['expose_strings'] && count($enabled_languages) > 1) {
|
||||
if ($webform_localization_options['expose_strings']) {
|
||||
// Avoid caching for translatable element values.
|
||||
entity_get_controller('node')->resetCache(array($form['nid']['#value']));
|
||||
$form['#node'] = node_load($form['nid']['#value']);
|
||||
@ -506,17 +369,6 @@ function webform_localization_form_webform_configure_form_alter(&$form, &$form_s
|
||||
$form['submission']['redirection']['redirect_url']['#default_value'] = $form['#node']->webform['redirect_url'];
|
||||
}
|
||||
$form['advanced']['submit_text']['#default_value'] = $form['#node']->webform['submit_text'];
|
||||
// Add friendly translation link.
|
||||
$name = webform_localization_i18n_string_name($form['#node']->nid, 'confirmation');
|
||||
$string_source = i18n_string_get_string($name);
|
||||
if (isset($string_source->lid) && $string_source->lid != FALSE) {
|
||||
$link = l(t('here'), 'admin/config/regional/translate/edit/' . $string_source->lid, array('query' => drupal_get_destination()));
|
||||
$description = t('Click !here to translate this string.', array('!here' => $link));
|
||||
}
|
||||
else {
|
||||
$description = t('<i>The message must be filled in before it can be translated.</i>');
|
||||
}
|
||||
$form['submission']['confirmation']['#description'] .= ' ' . $description;
|
||||
}
|
||||
|
||||
$single_webform = 0;
|
||||
@ -643,42 +495,17 @@ function _webform_localization_webform_configure_form_submit($form, &$form_state
|
||||
*/
|
||||
function _webform_localization_webform_configure_form_submit_i18n_tweaks($form, &$form_state) {
|
||||
global $language;
|
||||
$source_language = i18n_string_source_language();
|
||||
if ($source_language != $language->language) {
|
||||
// Webform Configure Form not in source 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');
|
||||
// Collect the form elements that are translatable.
|
||||
$form_elements = array('confirmation', 'submit_text');
|
||||
// Add redirect url if it's not set to none or the default
|
||||
// confirmation page.
|
||||
if (!in_array($form_state['values']['redirect_url'], array('<confirmation>', '<none>'))) {
|
||||
$form_elements[] = 'redirect_url';
|
||||
}
|
||||
foreach ($form_elements as $element) {
|
||||
// Get i18n string for $element.
|
||||
$name = webform_localization_i18n_string_name($form['#node']->webform['nid'], $element);
|
||||
$i18n_string = i18n_string_get_string($name);
|
||||
// Get submitted value for $element (the translation).
|
||||
$string_translation = ($element == 'confirmation' ? $form_state['values'][$element]['value'] : $form_state['values'][$element]);
|
||||
if ($i18n_string->lid) {
|
||||
// There is an i18n string source.
|
||||
$string_source = $i18n_string->get_string();
|
||||
// We reset the source string value before saving the form.
|
||||
if ($element == 'confirmation') {
|
||||
$form_state['values'][$element]['value'] = $string_source;
|
||||
}
|
||||
else {
|
||||
$form_state['values'][$element] = $string_source;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// No i18n string source found: use the current translation as the
|
||||
// string source.
|
||||
$string_source = $string_translation;
|
||||
}
|
||||
// We save the translated string using i18n string.
|
||||
i18n_string_translation_update($name, $string_translation, $language->language, $string_source);
|
||||
}
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -702,13 +529,9 @@ function _webform_localization_webform_email_edit_form_submit($form, &$form_stat
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
|
||||
webform_localization_emails_sync($node->nid);
|
||||
}
|
||||
if (isset($form_state['values']['node'])) {
|
||||
// Above isset avoids problem when using entity_translation.
|
||||
// @see https://www.drupal.org/node/2482521
|
||||
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));
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -726,19 +549,15 @@ function webform_localization_form_webform_email_delete_form_alter(&$form, &$for
|
||||
*/
|
||||
function _webform_localization_webform_email_delete_form_submit($form, &$form_state) {
|
||||
|
||||
$node = $form['node']['#value'];
|
||||
$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 (isset($form_state['values']['node'])) {
|
||||
// Above isset avoids problem when using entity_translation.
|
||||
// @see https://www.drupal.org/node/2482521
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -781,8 +600,10 @@ function webform_localization_form_webform_component_edit_form_alter(&$form, &$f
|
||||
'#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.
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
@ -817,131 +638,28 @@ function webform_localization_field_attach_prepare_translation_alter(&$entity, $
|
||||
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.
|
||||
/**
|
||||
* 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.
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_alter().
|
||||
*/
|
||||
function webform_localization_form_alter(&$form, &$form_state, $form_id) {
|
||||
// n.b. We are not using hook_form_BASE_FORM_ID_alter(), as we need
|
||||
// to interact closely with other hook_form_alter() implementations.
|
||||
// @see webform_localization_module_implements_alter()
|
||||
if (strpos($form_id, 'webform_client_form_') !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enhance webform's mollom support, to handle our 'single_webform' option.
|
||||
if (module_exists('mollom')) {
|
||||
_webform_localization_mollom_form_alter($form, $form_state, $form_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interaction with mollom_form_alter() for 'single_webform' localization.
|
||||
*
|
||||
* If the translation source node's webform is protected by mollom, and
|
||||
* uses our 'single_webform' setting, then we must also protect the other
|
||||
* nodes in the translation set.
|
||||
*
|
||||
* This is because each node in the translation set will still have a
|
||||
* unique client form_id (based on the nid, not the tnid), but mollom will
|
||||
* only know about one of those form_ids.
|
||||
*
|
||||
* @see webform_localization_module_implements_alter()
|
||||
*/
|
||||
function _webform_localization_mollom_form_alter(&$form, &$form_state, $form_id) {
|
||||
// Establish that the current node has a (different) translation source.
|
||||
// (If this is the translation source, mollom will already know about it).
|
||||
$node = $form['#node'];
|
||||
if (empty($node->tnid) || $node->tnid == $node->nid) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prime the static mollom form cache (for manipulation).
|
||||
mollom_form_cache();
|
||||
$mollom_cache = &drupal_static('mollom_form_cache');
|
||||
$protected = &$mollom_cache['protected'];
|
||||
|
||||
// If the source node's webform is not protected by mollom, we can bail
|
||||
// out immediately.
|
||||
$source_form_id = 'webform_client_form_' . $node->tnid;
|
||||
if (!array_key_exists($source_form_id, $protected)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check that the 'single_webform' option is configured for the source.
|
||||
$source_wl_options = webform_localization_get_config($node->tnid);
|
||||
if (empty($source_wl_options['single_webform'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Protect this form using the settings for the single webform.
|
||||
// Firstly we take care of the mollom_form_cache() static cache.
|
||||
$protected[$form_id] = $protected[$source_form_id];
|
||||
|
||||
// Then, if necessary, we update the mollom_form_load() cache.
|
||||
$mollom_form = mollom_form_load($form_id);
|
||||
if (!$mollom_form) {
|
||||
$source_mollom_form = mollom_form_load($source_form_id);
|
||||
$mollom_form = $source_mollom_form;
|
||||
$mollom_form['form_id'] = $form_id;
|
||||
$cid = 'mollom:form:' . $form_id;
|
||||
cache_set($cid, $mollom_form);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_module_implements_alter().
|
||||
*
|
||||
* We need our hook_form_alter() to run before mollom's, so that we can
|
||||
* prime and modify its cache before mollom uses it.
|
||||
*
|
||||
* @see webform_localization_form_alter()
|
||||
* @see mollom_form_alter()
|
||||
*/
|
||||
function webform_localization_module_implements_alter(&$implementations, $hook) {
|
||||
if ($hook != 'form_alter') {
|
||||
return;
|
||||
}
|
||||
|
||||
// If mollom isn't enabled, do nothing.
|
||||
if (!module_exists('mollom')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If our code will run before mollom's, do nothing.
|
||||
$pos = array_flip(array_keys($implementations));
|
||||
if ($pos['webform_localization'] < $pos['mollom']) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make it so our hook implementation runs before mollom's.
|
||||
$webform_localization = array(
|
||||
'webform_localization' => $implementations['webform_localization'],
|
||||
);
|
||||
$implementations = array_diff_key($implementations, $webform_localization);
|
||||
$implementations = array_merge(
|
||||
array_slice($implementations, 0, $pos['mollom'], TRUE), $webform_localization, array_slice($implementations, $pos['mollom'], NULL, TRUE)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets webform localization options that match a node ID.
|
||||
*
|
||||
* @staticvar array $webform_localization_options
|
||||
* An array of webform localization options group by nid.
|
||||
*
|
||||
* @param int $nid
|
||||
* @param $nid
|
||||
* A node Id.
|
||||
* @param bool $clear_cache
|
||||
* A flag to force a database reading in case that properties are cached.
|
||||
@ -961,20 +679,12 @@ function webform_localization_get_config($nid, $clear_cache = FALSE) {
|
||||
unset($webform_properties['roles']);
|
||||
unset($webform_properties['emails']);
|
||||
unset($webform_properties['record_exists']);
|
||||
|
||||
// If webform_uuid is being used since $component['nid'] is really a UUID
|
||||
// get the nid.
|
||||
if (module_exists('webform_uuid') && variable_get('webform_localization_using_uuid', FALSE)) {
|
||||
$record = entity_get_id_by_uuid('node', array($nid));
|
||||
$nid = (!empty($record)) ? array_pop($record) : $nid;
|
||||
}
|
||||
|
||||
// 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);
|
||||
->fields('webform_localization')
|
||||
->condition('nid', $nid, '=')
|
||||
->execute()
|
||||
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
|
||||
if (count($options) == 0) {
|
||||
$webform_localization_options[$nid] = array(
|
||||
'nid' => $nid,
|
||||
@ -1085,212 +795,4 @@ function webform_localization_modules_enabled($modules) {
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
webform_localization_uuid_update_strings(FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_FORM_ID_alter().
|
||||
*
|
||||
* Add related translations to translation form.
|
||||
*/
|
||||
function webform_localization_form_i18n_string_locale_translate_edit_form_alter(&$form, &$form_state, $form_id) {
|
||||
if ($form['textgroup']['#value'] == 'webform') {
|
||||
if (isset($form['context']['#markup']) && preg_match('/#title/', $form['context']['#markup'])) {
|
||||
list($id, $cid, $name) = explode(':', $form['context']['#markup']);
|
||||
$form['#submit'][] = 'webform_localization_i18n_string_locale_translate_edit_form_submit';
|
||||
$rel_key = 'webform:' . $id . ':' . $cid . '%';
|
||||
$related = db_query('SELECT lid, source, context, textgroup, location FROM {locales_source} WHERE location LIKE :key', array(':key' => $rel_key));
|
||||
|
||||
$langs_installed = language_list();
|
||||
|
||||
$langs_enabled = language_list('enabled');
|
||||
$langs_enabled = $langs_enabled[1];
|
||||
|
||||
if (isset($form['translations'])) {
|
||||
foreach ($form['translations'] as $key => $value) {
|
||||
if (array_key_exists($key, $langs_installed) && !array_key_exists($key, $langs_enabled)) {
|
||||
unset($form['translations'][$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form['items'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Related strings'),
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => TRUE,
|
||||
'#weight' => 2,
|
||||
);
|
||||
foreach ($related as $source) {
|
||||
if ($source->context != $form['context']['#markup']) {
|
||||
$lid = $source->lid;
|
||||
// Add original text to the top and some values for form altering.
|
||||
$form['items'][$lid]['original'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Original text'),
|
||||
'#markup' => check_plain(wordwrap($source->source, 0)),
|
||||
);
|
||||
if (!empty($source->context)) {
|
||||
$form['items'][$lid]['context'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Context'),
|
||||
'#markup' => check_plain($source->context),
|
||||
);
|
||||
}
|
||||
$form['items'][$lid]['textgroup'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $source->textgroup,
|
||||
);
|
||||
$form['items'][$lid]['location'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $source->location,
|
||||
);
|
||||
|
||||
$languages = $langs_enabled;
|
||||
// We don't need the default language value, that value is in $source.
|
||||
$omit = $source->textgroup == 'default' ? 'en' : i18n_string_source_language();
|
||||
unset($languages[($omit)]);
|
||||
$form['items'][$lid]['translations-' . $lid] = array('#tree' => TRUE);
|
||||
// Approximate the number of rows to use in the default textarea.
|
||||
$rows = min(ceil(str_word_count($source->source) / 12), 10);
|
||||
foreach ($languages as $langcode => $language) {
|
||||
$form['items'][$lid]['translations-' . $lid][$langcode] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t($language->name),
|
||||
'#rows' => $rows,
|
||||
'#default_value' => '',
|
||||
);
|
||||
}
|
||||
|
||||
// Fetch translations and fill in default values in the form.
|
||||
$result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid AND language <> :omit", array(':lid' => $lid, ':omit' => $omit));
|
||||
foreach ($result as $translation) {
|
||||
$form['items'][$lid]['translations-' . $lid][$translation->language]['#default_value'] = $translation->translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit handler for translations form.
|
||||
*/
|
||||
function webform_localization_i18n_string_locale_translate_edit_form_submit($form, &$form_state) {
|
||||
foreach ($form_state['values'] as $key => $value) {
|
||||
if (preg_match("/translations-(.*)/", $key, $lid)) {
|
||||
foreach ($value as $lang => $translation) {
|
||||
$existing = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid[1], ':language' => $lang))->fetchField();
|
||||
if (!empty($translation)) {
|
||||
if (!empty($existing)) {
|
||||
db_update('locales_target')
|
||||
->fields(array(
|
||||
'translation' => $translation,
|
||||
'i18n_status' => I18N_STRING_STATUS_CURRENT,
|
||||
))
|
||||
->condition('lid', $lid[1])
|
||||
->condition('language', $lang)
|
||||
->execute();
|
||||
}
|
||||
else {
|
||||
db_insert('locales_target')
|
||||
->fields(array(
|
||||
'lid' => $lid[1],
|
||||
'translation' => $translation,
|
||||
'language' => $lang,
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_webform_components_form().
|
||||
*
|
||||
* Adds a translate link to each webform component.
|
||||
*/
|
||||
function webform_localization_preprocess_webform_components_form(&$variables) {
|
||||
$form = $variables['form'];
|
||||
if (!isset($form['#node']->webform['nid'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$header = $variables['header'];
|
||||
$rows = $variables['rows'];
|
||||
$node = $form['#node'];
|
||||
|
||||
$enabled_languages = locale_language_list('name');
|
||||
|
||||
$webform_localization_options = webform_localization_get_config($form['#node']->webform['nid']);
|
||||
$row_data = array();
|
||||
if ($webform_localization_options['expose_strings'] && count($enabled_languages) > 1) {
|
||||
// Change colspan of header and footer.
|
||||
$footer = array_pop($rows);
|
||||
$header[6]['colspan'] = 4;
|
||||
$footer['data'][6]['colspan'] = 4;
|
||||
// Add translate link to rows.
|
||||
foreach ($rows as $key => $row) {
|
||||
$row_data[$key] = $row;
|
||||
if (isset($row['data-cid'])) {
|
||||
$name = webform_localization_i18n_string_name($node->nid, $row['data-cid'], '#title');
|
||||
$string_source = i18n_string_get_string($name);
|
||||
if (isset($string_source->lid)) {
|
||||
$row_data[$key]['data'][] = l(t('Translate'), 'admin/config/regional/translate/edit/' . $string_source->lid, array('query' => drupal_get_destination()));
|
||||
}
|
||||
}
|
||||
}
|
||||
$variables['rows'] = $row_data;
|
||||
$variables['rows'][] = $footer;
|
||||
$variables['header'] = $header;
|
||||
$variables['form'] = $form;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_js_alter().
|
||||
*/
|
||||
function webform_localization_js_alter(&$javascript) {
|
||||
// Only react on webform nodes.
|
||||
if ($node = menu_get_object()) {
|
||||
if (!isset($node->type) || !in_array($node->type, webform_variable_get('webform_node_types'))) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Only react when we are not on the source node.
|
||||
if ($node->nid != $node->tnid && $node->tnid > 0) {
|
||||
// Gets webform localization options that match this node ID.
|
||||
$webform_localization_options = webform_localization_get_config($node->tnid);
|
||||
|
||||
// Only react when keep a single webform across a translation set.
|
||||
if ($webform_localization_options['single_webform'] > 0) {
|
||||
foreach ($javascript['settings']['data'] as &$setting) {
|
||||
if (isset($setting['webform']['conditionals']['webform-client-form-' . $node->tnid])) {
|
||||
$setting['webform']['conditionals']['webform-client-form-' . $node->nid] = $setting['webform']['conditionals']['webform-client-form-' . $node->tnid];
|
||||
unset($setting['webform']['conditionals']['webform-client-form-' . $node->tnid]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entitycache_node_load().
|
||||
*/
|
||||
function webform_localization_entitycache_node_load($nodes) {
|
||||
$webform_types = webform_variable_get('webform_node_types');
|
||||
foreach ($nodes as $nid => &$node) {
|
||||
if (in_array($node->type, $webform_types)) {
|
||||
// Gets webform localization options that match this node ID.
|
||||
$wl_options = webform_localization_get_config($nid);
|
||||
if ($wl_options['expose_strings']) {
|
||||
module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
|
||||
// Translate custom strings.
|
||||
webform_localization_translate_strings($node);
|
||||
webform_localization_email_translate_strings($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user