webform_localization.module 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. <?php
  2. /**
  3. * @file
  4. * Webform localization module.
  5. *
  6. * This module provides Localization Features to forms and questionnaires
  7. * generated by the Webform Module.
  8. *
  9. * i18n_string integration based on patch
  10. * http://drupal.org/node/245424#comment-5244256 by Calin Marian.
  11. *
  12. * Further development sponsored by Riot Games.
  13. *
  14. * @author German Martin <gmartin.php@gmail.com>
  15. */
  16. /**
  17. * Implements hook_help().
  18. */
  19. function webform_localization_help($section = 'admin/help#webform_localization', $arg = NULL) {
  20. $output = '';
  21. switch ($section) {
  22. case 'admin/help#webform_localization':
  23. $output = '<p>' . 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.') . '</p>';
  24. $output .= '<p>' . t('You can choose two different ways to manage localization that cover this scenarios:') . '</p>';
  25. $output .= '<p>' . t('A) <strong>If you want to keep a single webform across all nodes in a translation set:</strong><br />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.<br />(You have a "<em>localization by string translation</em>" fieldset in the form settings to enable this)') . '</p>';
  26. $output .= '<p>' . t('B)<strong> If you want to keep a webform per node per language but synchronized:</strong><br />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.<br />(You have a "<em>localization by sync</em>" fieldset in the form settings to enable this)') . '</p>';
  27. break;
  28. }
  29. return $output;
  30. }
  31. /**
  32. * Implements hook_i18n_string_info().
  33. */
  34. function webform_localization_i18n_string_info() {
  35. $groups['webform'] = array(
  36. 'title' => t('Webform Localization'),
  37. 'description' => t('Localizable properties of webforms, like title, select options, and others.'),
  38. // This group doesn't have strings with format.
  39. 'format' => FALSE,
  40. // This group cannot list all strings.
  41. 'list' => FALSE,
  42. 'refresh callback' => 'webform_localization_i18n_string_refresh',
  43. );
  44. return $groups;
  45. }
  46. /**
  47. * Update / create / delete translation source for components.
  48. *
  49. * Refresh callback that regenerates all the translatable poperties of the
  50. * components of the matching webforms configuration.
  51. */
  52. function webform_localization_i18n_string_refresh($group) {
  53. if ($group == 'webform') {
  54. global $language;
  55. $languages = language_list();
  56. $source_language = $languages[i18n_string_source_language()];
  57. // Refresh the strings using source language.
  58. $language = $source_language;
  59. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  60. // In case updating before UUID support.
  61. if (module_exists('uuid') && !variable_get('webform_localization_using_uuid', FALSE)) {
  62. webform_localization_uuid_update_strings(FALSE);
  63. }
  64. // Get components configured as translatable.
  65. $query = db_select('webform_component', 'wc');
  66. $query->fields('wc');
  67. $query->condition('wl.expose_strings', 0, '>');
  68. $query->innerJoin('webform_localization', 'wl', 'wc.nid = wl.nid');
  69. $components = $query->execute()->fetchAll();
  70. foreach ($components as $component) {
  71. $component = (array) $component;
  72. $component['extra'] = unserialize($component['extra']);
  73. webform_localization_component_update_translation_strings($component);
  74. $component['extra'] = serialize($component['extra']);
  75. drupal_write_record('webform_component', $component, array('nid', 'cid'));
  76. }
  77. // Get emails configured as translatable.
  78. $query = db_select('webform_localization', 'wl');
  79. $query->fields('wl', array('nid'));
  80. $query->condition('wl.expose_strings', 0, '>');
  81. $nid_list = $query->execute()->fetchAllAssoc('nid');
  82. // @todo: Find a more eficient way to manage webform translatable
  83. // properties.
  84. $nodes = node_load_multiple(array_keys($nid_list));
  85. module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
  86. foreach ($nid_list as $nid => $value) {
  87. $emails = _webform_localization_emails_load($nid);
  88. webform_localization_emails_translation_string_refresh($emails, $nid);
  89. $node = $nodes[$nid];
  90. webform_localization_translate_strings($node, TRUE);
  91. }
  92. // NOTE: Delete string for webforms that has disabled i18n translation.
  93. // This is the only moment when we deleted translation for disabled
  94. // webforms. This way we provide the feature to temporally disable the
  95. // webform i18n string without losing custom translated texts.
  96. webform_localization_delete_all_strings();
  97. return TRUE;
  98. }
  99. }
  100. /**
  101. * Load a component file into memory.
  102. *
  103. * @see webform_component_include()
  104. *
  105. * @param string $component_type
  106. * The string machine name of a component.
  107. */
  108. function webform_localization_component_include($component_type) {
  109. static $included = array();
  110. // No need to load components that have already been added once.
  111. if (!isset($included[$component_type])) {
  112. $included[$component_type] = TRUE;
  113. module_load_include('inc', 'webform_localization', 'components/' . $component_type);
  114. }
  115. }
  116. /**
  117. * Invoke a component callback.
  118. *
  119. * @see webform_component_invoke()
  120. *
  121. * @param string $type
  122. * The component type as a string.
  123. * @param string $callback
  124. * The callback to execute.
  125. * @param ...
  126. * Any additional parameters required by the $callback.
  127. */
  128. function webform_localization_component_invoke($type, $callback) {
  129. $args = func_get_args();
  130. $type = array_shift($args);
  131. $callback = array_shift($args);
  132. $function = '_webform_localization_' . $callback . '_' . $type;
  133. webform_localization_component_include($type);
  134. if (function_exists($function)) {
  135. return call_user_func_array($function, $args);
  136. }
  137. }
  138. /**
  139. * Implements hook_webform_component_insert().
  140. */
  141. function webform_localization_webform_component_insert($component) {
  142. // Gets webform localization options that match this node ID.
  143. $wl_options = webform_localization_get_config($component['nid']);
  144. // Create translation source for i18n_string for all the translatable
  145. // properties.
  146. if ($wl_options['expose_strings']) {
  147. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  148. webform_localization_component_update_translation_strings($component);
  149. $component['extra'] = serialize($component['extra']);
  150. drupal_write_record('webform_component', $component, array('nid', 'cid'));
  151. }
  152. if ($wl_options['sync_components'] && _webform_localization_sync()) {
  153. // Turn Off Sync.
  154. _webform_localization_sync(FALSE);
  155. // Get all versions of the node.
  156. $node = node_load($component['nid']);
  157. $translations = translation_node_get_translations($node->tnid);
  158. if ($translations) {
  159. unset($translations[$node->language]);
  160. foreach ($translations as $trans_c) {
  161. $new_component = $component;
  162. $new_component['nid'] = $trans_c->nid;
  163. webform_component_insert($new_component);
  164. }
  165. }
  166. // Turn On Sync.
  167. _webform_localization_sync(TRUE);
  168. }
  169. }
  170. /**
  171. * Implements hook_webform_component_presave().
  172. */
  173. function webform_localization_webform_component_presave(&$component) {
  174. // If this is an insert, skip handling as this is handled by
  175. // hook_webform_component_insert().
  176. if (empty($component['cid'])) {
  177. return;
  178. }
  179. // Gets webform localization options that match this node ID.
  180. $wl_options = webform_localization_get_config($component['nid']);
  181. // Create translation source for i18n_string for all the translatable
  182. // poperties.
  183. if ($wl_options['expose_strings']) {
  184. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  185. webform_localization_component_update_translation_strings($component);
  186. }
  187. if ($wl_options['sync_components'] && _webform_localization_sync()) {
  188. $test_node = node_load($component['nid']);
  189. if ($test_node->tnid > 0) {
  190. // Turn Off Sync.
  191. _webform_localization_sync(FALSE);
  192. module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
  193. // Get all versions of the component across all translations.
  194. $translations = webform_localization_component_get_translations($component);
  195. unset($translations[$component['nid']]);
  196. // Sync the changed component with it's translations versions.
  197. webform_localization_component_sync($component, $translations);
  198. foreach ($translations as $trans_c) {
  199. webform_component_update($trans_c);
  200. }
  201. // Turn On Sync.
  202. _webform_localization_sync(TRUE);
  203. }
  204. else {
  205. 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'));
  206. }
  207. }
  208. }
  209. /**
  210. * Implements hook_webform_component_delete().
  211. */
  212. function webform_localization_webform_component_delete($component) {
  213. // Gets webform localization options that match this node ID.
  214. $wl_options = webform_localization_get_config($component['nid']);
  215. if ($wl_options['expose_strings']) {
  216. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  217. webform_localization_component_delete_translation_strings($component);
  218. }
  219. if ($wl_options['sync_components'] && _webform_localization_sync()) {
  220. module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
  221. webform_localization_synchronizable_properties_delete($component);
  222. // Turn Off Sync.
  223. _webform_localization_sync(FALSE);
  224. // Get all versions of the node.
  225. $node = node_load($component['nid']);
  226. $translations = translation_node_get_translations($node->tnid);
  227. if ($translations) {
  228. unset($translations[$node->language]);
  229. foreach ($translations as $trans_c) {
  230. $component_version = webform_localization_component_load($trans_c->nid, $component['cid']);
  231. webform_component_delete($trans_c, $component_version);
  232. }
  233. }
  234. // Turn On Sync.
  235. _webform_localization_sync(TRUE);
  236. }
  237. }
  238. /**
  239. * A menu to_arg handler explicitly invoked by webform_menu_to_arg().
  240. *
  241. * If a single webform is used across all translations, use the appropriate
  242. * node ID for webform paths.
  243. */
  244. function webform_localization_webform_menu_to_arg($arg, $map, $index) {
  245. if ($node = node_load($arg)) {
  246. if ($nid = webform_localization_single_webform_nid($node)) {
  247. $node = node_load($nid);
  248. }
  249. }
  250. return $node ? $node->nid : $arg;
  251. }
  252. /**
  253. * Find nid of node containing the 'single webform' for this translation set.
  254. */
  255. function webform_localization_single_webform_nid($node) {
  256. $cache = &drupal_static(__FUNCTION__, array());
  257. if (!array_key_exists($node->nid, $cache)) {
  258. // Select all webforms that match the localization configuration.
  259. $query = db_select('webform', 'w');
  260. $query->innerJoin('webform_localization', 'wl', 'w.nid = wl.nid');
  261. $query->fields('w', array('nid'));
  262. $query->condition('wl.single_webform', 0, '<>');
  263. $query->condition('wl.single_webform', $node->tnid, '=');
  264. $query->condition('w.nid', $node->nid, '<>');
  265. $cache[$node->nid] = $query->execute()->fetchField();
  266. }
  267. return $cache[$node->nid];
  268. }
  269. /**
  270. * Implements hook_node_view().
  271. */
  272. function webform_localization_node_view($node, $view_mode) {
  273. if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
  274. return;
  275. }
  276. if ($nid = webform_localization_single_webform_nid($node)) {
  277. // NOTE:
  278. // Perhaps not most eficient way.. a possible alternative
  279. // @todo rewrite the webform load and view process as a
  280. // independent function to reuse.
  281. $source_node = node_load($nid);
  282. // We replace the webform with the node translation source.
  283. $node->webform = $source_node->webform;
  284. // This fool webform_node_view to avoid errors with drafts in between pages.
  285. $translation_nid = $node->nid;
  286. if (($node->webform['allow_draft'] || $node->webform['auto_save']) && $user->uid != 0) {
  287. $node->nid = $source_node->nid;
  288. }
  289. // Call node view implementation to update the $node->content.
  290. webform_node_view($node, $view_mode);
  291. // Reset the nid if we used the drafts fix.
  292. $node->nid = $translation_nid;
  293. }
  294. }
  295. /**
  296. * Implements hook_node_load().
  297. */
  298. function webform_localization_node_load($nodes, $types) {
  299. // Quick check to see if we need to do anything at all for these nodes.
  300. $webform_types = webform_variable_get('webform_node_types');
  301. if (count(array_intersect($types, $webform_types)) == 0) {
  302. return;
  303. }
  304. foreach ($nodes as $nid => &$node) {
  305. // Gets webform localization options that match this node ID.
  306. $wl_options = webform_localization_get_config($nid);
  307. if ($wl_options['expose_strings']) {
  308. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  309. // Translate custom strings.
  310. webform_localization_translate_strings($node);
  311. webform_localization_email_translate_strings($node);
  312. }
  313. }
  314. }
  315. /**
  316. * Implements hook_webform_submission_update().
  317. */
  318. function webform_localization_webform_submission_insert($node, $submission) {
  319. // NOTE:
  320. // We have 2 options here: to use the node language or the language of
  321. // the page... for now make more sense use the node as language source.
  322. $language = $node->language;
  323. // Update language field when a submission is updated.
  324. db_update('webform_submissions')
  325. ->fields(array(
  326. 'language' => $language,
  327. ))
  328. ->condition('sid', $submission->sid)
  329. ->execute();
  330. }
  331. /**
  332. * Implements hook_webform_submission_load().
  333. */
  334. function webform_localization_webform_submission_load(&$submissions) {
  335. if (empty($submissions)) {
  336. return;
  337. }
  338. $query = db_select('webform_submissions', 's');
  339. $query->fields('s', array('language', 'sid'));
  340. $query->condition('s.sid', array_keys($submissions), 'IN');
  341. $s_languages = $query->execute()->fetchAllAssoc('sid');
  342. foreach ($submissions as $sid => $submission) {
  343. $submissions[$sid]->language = $s_languages[$sid]->language;
  344. }
  345. }
  346. /**
  347. * Implements hook_webform_submission_presave().
  348. */
  349. function webform_localization_webform_submission_presave(&$node, &$submission) {
  350. if (isset($node->tnid) && $node->tnid != 0) {
  351. $submission->serial = _webform_submission_serial_next_value($node->tnid);
  352. }
  353. }
  354. /**
  355. * Implements hook_node_delete().
  356. */
  357. function webform_localization_node_delete($node) {
  358. if (!in_array($node->type, webform_variable_get('webform_node_types')) || empty($node->webform['components'])) {
  359. return;
  360. }
  361. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  362. // Deletion of webform and emails translatable properties.
  363. webform_localization_delete_translate_strings($node);
  364. // Delete webform localization configuration record.
  365. db_delete('webform_localization')->condition('nid', $node->nid)->execute();
  366. }
  367. /**
  368. * Implements hook_webform_component_render_alter().
  369. */
  370. function webform_localization_webform_component_render_alter(&$element, $component) {
  371. // Gets webform localization options that match this node ID.
  372. $wl_options = webform_localization_get_config($component['nid']);
  373. // Translate the translatable properties.
  374. if ($wl_options['expose_strings']) {
  375. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  376. _webform_localization_translate_component($element, $component);
  377. if (!empty($element['#attributes']['placeholder'])) {
  378. $name = webform_localization_i18n_string_name($component['nid'], $component['cid'], '#placeholder');
  379. $element['#attributes']['placeholder'] = i18n_string($name, $element['#attributes']['placeholder']);
  380. }
  381. }
  382. }
  383. /**
  384. * Implements hook_webform_component_display_alter().
  385. */
  386. function webform_localization_webform_component_display_alter(&$display_element, $component) {
  387. // Gets webform localization options that match this node ID.
  388. $wl_options = webform_localization_get_config($component['nid']);
  389. // Translate the translatable properties.
  390. if ($wl_options['expose_strings']) {
  391. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  392. _webform_localization_translate_component($display_element, $component);
  393. }
  394. }
  395. /**
  396. * Implements hook_webform_analysis_component_data_alter().
  397. */
  398. function webform_localization_webform_analysis_component_data_alter(&$data, $node, &$component) {
  399. // Gets webform localization options that match this node ID.
  400. $wl_options = webform_localization_get_config($node->nid);
  401. // Translate the translatable properties.
  402. if ($wl_options['expose_strings']) {
  403. // Translate component name, this is not a component spesific property.
  404. foreach ($component['extra']['translated_strings'] as $name) {
  405. $name_list = explode(':', $name);
  406. // Translate component name from title property.
  407. if ($name_list[3] == '#title') {
  408. $component['name'] = i18n_string($name, $component['name']);
  409. break;
  410. }
  411. }
  412. // Translate data array.
  413. $result = webform_localization_component_invoke($component['type'], 'analysis_data', $data, $node, $component);
  414. if (!empty($result)) {
  415. $data = $result;
  416. }
  417. }
  418. }
  419. /**
  420. * Implements hook_webform_csv_header_alter().
  421. */
  422. function webform_localization_webform_csv_header_alter(&$header, $component) {
  423. // Gets webform localization options that match this node ID.
  424. $wl_options = webform_localization_get_config($component['nid']);
  425. // Translate the translatable properties.
  426. if ($wl_options['expose_strings']) {
  427. $result = webform_localization_component_invoke($component['type'], 'csv_header', $header, $component);
  428. if (!empty($result)) {
  429. $header = $result;
  430. }
  431. }
  432. }
  433. /**
  434. * Implements hook_webform_csv_data_alter().
  435. */
  436. function webform_localization_webform_csv_data_alter(&$data, $component, $submission) {
  437. // Gets webform localization options that match this node ID.
  438. $wl_options = webform_localization_get_config($component['nid']);
  439. // Translate the translatable properties.
  440. if ($wl_options['expose_strings']) {
  441. $result = webform_localization_component_invoke($component['type'], 'csv_data', $data, $component, $submission);
  442. if (!empty($result)) {
  443. $data = $result;
  444. }
  445. }
  446. }
  447. /**
  448. * Implements hook_form_FORM_ID_alter().
  449. *
  450. * Add specific localization options to Webform Configure Form.
  451. */
  452. function webform_localization_form_webform_configure_form_alter(&$form, &$form_state, $form_id) {
  453. // @see https://api.drupal.org/comment/52443#comment-52443
  454. $enabled_languages = locale_language_list('name');
  455. // Gets webform localization options that match this node ID.
  456. $webform_localization_options = webform_localization_get_config($form['nid']['#value']);
  457. if ($webform_localization_options['expose_strings'] && count($enabled_languages) > 1) {
  458. // Avoid caching for translatable element values.
  459. entity_get_controller('node')->resetCache(array($form['nid']['#value']));
  460. $form['#node'] = node_load($form['nid']['#value']);
  461. $form['submission']['confirmation']['#default_value'] = $form['#node']->webform['confirmation'];
  462. if ($form['submission']['redirection']['redirect']['#default_value'] == 'url') {
  463. $form['submission']['redirection']['redirect_url']['#default_value'] = $form['#node']->webform['redirect_url'];
  464. }
  465. $form['advanced']['submit_text']['#default_value'] = $form['#node']->webform['submit_text'];
  466. // Add friendly translation link.
  467. $name = webform_localization_i18n_string_name($form['#node']->nid, 'confirmation');
  468. $string_source = i18n_string_get_string($name);
  469. if (isset($string_source->lid) && $string_source->lid != FALSE) {
  470. $link = l(t('here'), 'admin/config/regional/translate/edit/' . $string_source->lid, array('query' => drupal_get_destination()));
  471. $description = t('Click !here to translate this string.', array('!here' => $link));
  472. }
  473. else {
  474. $description = t('<i>The message must be filled in before it can be translated.</i>');
  475. }
  476. $form['submission']['confirmation']['#description'] .= ' ' . $description;
  477. }
  478. $single_webform = 0;
  479. if ($webform_localization_options['single_webform'] > 0) {
  480. $single_webform = 1;
  481. }
  482. $form['localization_by_string'] = array(
  483. '#type' => 'fieldset',
  484. '#title' => t('Localization by String Translation'),
  485. '#collapsible' => TRUE,
  486. '#collapsed' => TRUE,
  487. '#weight' => -6,
  488. '#description' => '<p>' . 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 <em>one single webform / node acrossdifferent languages</em>.', array('html' => TRUE)) . '</p>',
  489. );
  490. $form['localization_by_string']['expose_strings'] = array(
  491. '#type' => 'checkbox',
  492. '#title' => t('Expose webform component strings suitable for translation.'),
  493. '#default_value' => $webform_localization_options['expose_strings'],
  494. '#description' => '<p>' . t('Use the i18n module to translate webform component strings.', array('html' => TRUE)) . '</p>',
  495. );
  496. $form['localization_by_string']['single_webform'] = array(
  497. '#type' => 'checkbox',
  498. '#title' => t('Keep a single webform across a translation set.'),
  499. '#default_value' => $single_webform,
  500. '#description' => '<p>' . 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)) . '</p>',
  501. );
  502. $form['localization_by_sync'] = array(
  503. '#type' => 'fieldset',
  504. '#title' => t('Localization by Sync'),
  505. '#collapsible' => TRUE,
  506. '#collapsed' => TRUE,
  507. '#weight' => -5,
  508. '#description' => '<p>' . 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 <em>a different webform /node per language</em> but keeping some settings synchronized.', array('html' => TRUE)) . '</p>',
  509. );
  510. $form['localization_by_sync']['webform_properties_header'] = array(
  511. '#type' => 'markup',
  512. '#prefix' => t('Synchronize webform properties across node translations.'),
  513. '#markup' => '<div class="description"><p>' . t('You can choose to synchronize specific properties of the webform.') . '</p></div>',
  514. );
  515. $form['localization_by_sync']['webform_properties'] = array(
  516. '#type' => 'checkboxes',
  517. '#options' => $webform_localization_options['webform_properties_structure'],
  518. '#default_value' => $webform_localization_options['webform_properties'],
  519. '#description' => '',
  520. );
  521. $form['localization_by_sync']['sync_components'] = array(
  522. '#type' => 'checkbox',
  523. '#title' => t('Synchronize webform components across node translations.'),
  524. '#default_value' => $webform_localization_options['sync_components'],
  525. '#description' => '<p>' . t('Copy the entire webform structure when a node is translated and synchronize changes on selected components properties.', array('html' => TRUE)) . '</p>',
  526. );
  527. $form['localization_by_sync']['sync_roles'] = array(
  528. '#type' => 'checkbox',
  529. '#title' => t('Synchronize webform submission access roles across node translations.'),
  530. '#default_value' => $webform_localization_options['sync_roles'],
  531. '#description' => '<p>' . t('Keep the roles that can submit a webform synchronized in a translation set.') . '</p>',
  532. );
  533. $form['localization_by_sync']['sync_emails'] = array(
  534. '#type' => 'checkbox',
  535. '#title' => t('Synchronize webform e-mail recipients across node translations.'),
  536. '#default_value' => $webform_localization_options['sync_emails'],
  537. '#description' => '<p>' . t('Keep the webform e-mail recipients synchronized in a translation set.') . '</p>',
  538. );
  539. $form['#submit'][] = '_webform_localization_webform_configure_form_submit';
  540. if ($webform_localization_options['expose_strings']) {
  541. // Using i18n string we need to tweak values before submit.
  542. $submit_array = $form['#submit'];
  543. array_unshift($submit_array, '_webform_localization_webform_configure_form_submit_i18n_tweaks');
  544. $form['#submit'] = $submit_array;
  545. }
  546. }
  547. /**
  548. * Handle specific localization options in Webform Configure Form.
  549. */
  550. function _webform_localization_webform_configure_form_submit($form, &$form_state) {
  551. $webform_properties = $form_state['values']['webform_properties'];
  552. foreach ($webform_properties as $key => $value) {
  553. if (!is_string($value)) {
  554. unset($webform_properties[$key]);
  555. }
  556. }
  557. if (count($webform_properties) == 0) {
  558. $webform_properties = '';
  559. }
  560. else {
  561. $webform_properties = serialize($webform_properties);
  562. }
  563. if ($form_state['values']['single_webform'] > 0) {
  564. $form_state['values']['single_webform'] = $form_state['values']['nid'];
  565. }
  566. $webform_localization_options = array(
  567. 'nid' => $form_state['values']['nid'],
  568. 'expose_strings' => $form_state['values']['expose_strings'],
  569. 'sync_components' => $form_state['values']['sync_components'],
  570. 'sync_roles' => $form_state['values']['sync_roles'],
  571. 'sync_emails' => $form_state['values']['sync_emails'],
  572. 'single_webform' => $form_state['values']['single_webform'],
  573. 'webform_properties' => $webform_properties,
  574. );
  575. $prev_options = webform_localization_get_config($form_state['values']['nid']);
  576. if (isset($prev_options['no_persistent'])) {
  577. drupal_write_record('webform_localization', $webform_localization_options);
  578. }
  579. else {
  580. drupal_write_record('webform_localization', $webform_localization_options, array('nid'));
  581. }
  582. module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
  583. webform_localization_webform_properties_sync($form_state['values']['nid']);
  584. $webform_localization_options = webform_localization_get_config($form_state['values']['nid']);
  585. if ($webform_localization_options['sync_roles']) {
  586. webform_localization_roles_sync($form_state['values']['nid']);
  587. }
  588. if ($webform_localization_options['expose_strings']) {
  589. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  590. webform_localization_update_translation_strings($form_state['values']);
  591. }
  592. }
  593. /**
  594. * Handle translated element tweaks in Webform Configure Form.
  595. */
  596. function _webform_localization_webform_configure_form_submit_i18n_tweaks($form, &$form_state) {
  597. global $language;
  598. $source_language = i18n_string_source_language();
  599. if ($source_language != $language->language) {
  600. // Webform Configure Form not in source language.
  601. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  602. // Collect the form elements that are translatable.
  603. $form_elements = array('confirmation', 'submit_text');
  604. // Add redirect url if it's not set to none or the default
  605. // confirmation page.
  606. if (!in_array($form_state['values']['redirect_url'], array('<confirmation>', '<none>'))) {
  607. $form_elements[] = 'redirect_url';
  608. }
  609. foreach ($form_elements as $element) {
  610. // Get i18n string for $element.
  611. $name = webform_localization_i18n_string_name($form['#node']->webform['nid'], $element);
  612. $i18n_string = i18n_string_get_string($name);
  613. // Get submitted value for $element (the translation).
  614. $string_translation = ($element == 'confirmation' ? $form_state['values'][$element]['value'] : $form_state['values'][$element]);
  615. if ($i18n_string->lid) {
  616. // There is an i18n string source.
  617. $string_source = $i18n_string->get_string();
  618. // We reset the source string value before saving the form.
  619. if ($element == 'confirmation') {
  620. $form_state['values'][$element]['value'] = $string_source;
  621. }
  622. else {
  623. $form_state['values'][$element] = $string_source;
  624. }
  625. }
  626. else {
  627. // No i18n string source found: use the current translation as the
  628. // string source.
  629. $string_source = $string_translation;
  630. }
  631. // We save the translated string using i18n string.
  632. i18n_string_translation_update($name, $string_translation, $language->language, $string_source);
  633. }
  634. }
  635. }
  636. /**
  637. * Implements hook_form_FORM_ID_alter().
  638. *
  639. * Add a Submit function to handle localization features.
  640. */
  641. function webform_localization_form_webform_email_edit_form_alter(&$form, &$form_state, $form_id) {
  642. $form['#submit'][] = '_webform_localization_webform_email_edit_form_submit';
  643. }
  644. /**
  645. * Handle emails sync on individual email change.
  646. */
  647. function _webform_localization_webform_email_edit_form_submit($form, &$form_state) {
  648. $node = $form['#node'];
  649. $webform_localization_options = webform_localization_get_config($node->nid);
  650. if ($webform_localization_options['sync_emails']) {
  651. module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
  652. webform_localization_emails_sync($node->nid);
  653. }
  654. if (isset($form_state['values']['node'])) {
  655. // Above isset avoids problem when using entity_translation.
  656. // @see https://www.drupal.org/node/2482521
  657. if ($webform_localization_options['expose_strings']) {
  658. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  659. webform_localization_emails_update_translation_string($form_state['values'] + array('node' => $node));
  660. }
  661. }
  662. }
  663. /**
  664. * Implements hook_form_FORM_ID_alter().
  665. *
  666. * Add a Submit function to handle sync feature.
  667. */
  668. function webform_localization_form_webform_email_delete_form_alter(&$form, &$form_state, $form_id) {
  669. $form['#submit'][] = '_webform_localization_webform_email_delete_form_submit';
  670. }
  671. /**
  672. * Handle emails localization cleanup / sync on email deletion.
  673. */
  674. function _webform_localization_webform_email_delete_form_submit($form, &$form_state) {
  675. $node = $form['node']['#value'];
  676. $webform_localization_options = webform_localization_get_config($node->nid);
  677. if ($webform_localization_options['sync_emails']) {
  678. module_load_include('inc', 'webform_localization', 'includes/webform_localization.sync');
  679. webform_localization_emails_sync($node->nid);
  680. }
  681. if (isset($form_state['values']['node'])) {
  682. // Above isset avoids problem when using entity_translation.
  683. // @see https://www.drupal.org/node/2482521
  684. if ($webform_localization_options['expose_strings']) {
  685. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  686. webform_localization_emails_delete_translation_string($form_state['values']['email']['eid'], $node->nid);
  687. }
  688. }
  689. }
  690. /**
  691. * Implements hook_form_FORM_ID_alter().
  692. *
  693. * Add specific localization options to Webform Component Edit Form.
  694. */
  695. function webform_localization_form_webform_component_edit_form_alter(&$form, &$form_state, $form_id) {
  696. $component = $form_state['build_info']['args'][1];
  697. if (!isset($component['cid'])) {
  698. $component['cid'] = -1;
  699. }
  700. // Gets webform localization options that match this node ID.
  701. $webform_localization_options = webform_localization_get_config($component['nid']);
  702. if ($webform_localization_options['sync_components']) {
  703. module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
  704. $select_options = webform_localization_synchronizable_properties($component);
  705. $form['localization'] = array(
  706. '#type' => 'fieldset',
  707. '#title' => t('Localization by Sync / Component settings'),
  708. '#collapsible' => TRUE,
  709. '#collapsed' => FALSE,
  710. '#weight' => -4,
  711. '#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 <em>"Synchronizewebform components across node translations"</em> option in thewebform that contains this component.', array('html' => TRUE)),
  712. );
  713. $form['localization']['standar_properties'] = array(
  714. '#type' => 'checkboxes',
  715. '#title' => t('Select properties to synchronize across translations.'),
  716. '#options' => $select_options['standar'],
  717. '#default_value' => $select_options['standar_values'],
  718. '#description' => t('Common properties that applies to all types of components.'),
  719. );
  720. $form['localization']['extra_properties'] = array(
  721. '#type' => 'checkboxes',
  722. '#title' => t('Select especial properties to synchronize across translations.'),
  723. '#options' => $select_options['extra'],
  724. '#default_value' => $select_options['extra_values'],
  725. '#description' => t('Special properties that applies only for this type of component.'),
  726. );
  727. // NOTE:
  728. // First we save the sync options to know what to do with the changes.
  729. array_unshift($form['#submit'], '_webform_localization_webform_component_edit_form_submit');
  730. }
  731. }
  732. /**
  733. * Handle specific localization options in Webform Component Edit Form.
  734. */
  735. function _webform_localization_webform_component_edit_form_submit($form, &$form_state) {
  736. module_load_include('inc', 'webform_localization', 'includes/webform_localization.component.sync');
  737. $options = array(
  738. 'nid' => $form_state['values']['nid'],
  739. 'cid' => $form_state['values']['cid'],
  740. 'type' => $form_state['values']['type'],
  741. 'standar_properties' => serialize($form_state['values']['localization']['standar_properties']),
  742. 'extra_properties' => serialize($form_state['values']['localization']['extra_properties']),
  743. );
  744. $prev_options = webform_localization_synchronizable_properties($options);
  745. if (isset($prev_options['no_persistent'])) {
  746. drupal_write_record('webform_component_localization', $options);
  747. }
  748. else {
  749. drupal_write_record('webform_component_localization', $options, array('nid', 'cid'));
  750. }
  751. // We reload cached configuration for this component.
  752. webform_localization_synchronizable_properties($options, TRUE);
  753. }
  754. /**
  755. * Implements hook_field_attach_prepare_translation_alter().
  756. */
  757. function webform_localization_field_attach_prepare_translation_alter(&$entity, $context) {
  758. if ($context['entity_type'] == 'node') {
  759. if (isset($context['source_entity']->webform)) {
  760. $webform_localization_options = webform_localization_get_config($context['source_entity']->nid);
  761. // Copy all Webform settings over to translated versions of this node
  762. // if the configuration match.
  763. if ($webform_localization_options['sync_components']) {
  764. // NOTE:
  765. // Perhaps could be interesting to copy only specific properties
  766. // but for now the entire webform make more sense.
  767. $entity->webform = $context['source_entity']->webform;
  768. }
  769. }
  770. }
  771. }
  772. /**
  773. * Implements hook_form_alter().
  774. */
  775. function webform_localization_form_alter(&$form, &$form_state, $form_id) {
  776. // n.b. We are not using hook_form_BASE_FORM_ID_alter(), as we need
  777. // to interact closely with other hook_form_alter() implementations.
  778. // @see webform_localization_module_implements_alter()
  779. if (strpos($form_id, 'webform_client_form_') !== 0) {
  780. return;
  781. }
  782. // Enhance webform's mollom support, to handle our 'single_webform' option.
  783. if (module_exists('mollom')) {
  784. _webform_localization_mollom_form_alter($form, $form_state, $form_id);
  785. }
  786. }
  787. /**
  788. * Interaction with mollom_form_alter() for 'single_webform' localization.
  789. *
  790. * If the translation source node's webform is protected by mollom, and
  791. * uses our 'single_webform' setting, then we must also protect the other
  792. * nodes in the translation set.
  793. *
  794. * This is because each node in the translation set will still have a
  795. * unique client form_id (based on the nid, not the tnid), but mollom will
  796. * only know about one of those form_ids.
  797. *
  798. * @see webform_localization_module_implements_alter()
  799. */
  800. function _webform_localization_mollom_form_alter(&$form, &$form_state, $form_id) {
  801. // Establish that the current node has a (different) translation source.
  802. // (If this is the translation source, mollom will already know about it).
  803. $node = $form['#node'];
  804. if (empty($node->tnid) || $node->tnid == $node->nid) {
  805. return;
  806. }
  807. // Prime the static mollom form cache (for manipulation).
  808. mollom_form_cache();
  809. $mollom_cache = &drupal_static('mollom_form_cache');
  810. $protected = &$mollom_cache['protected'];
  811. // If the source node's webform is not protected by mollom, we can bail
  812. // out immediately.
  813. $source_form_id = 'webform_client_form_' . $node->tnid;
  814. if (!array_key_exists($source_form_id, $protected)) {
  815. return;
  816. }
  817. // Check that the 'single_webform' option is configured for the source.
  818. $source_wl_options = webform_localization_get_config($node->tnid);
  819. if (empty($source_wl_options['single_webform'])) {
  820. return;
  821. }
  822. // Protect this form using the settings for the single webform.
  823. // Firstly we take care of the mollom_form_cache() static cache.
  824. $protected[$form_id] = $protected[$source_form_id];
  825. // Then, if necessary, we update the mollom_form_load() cache.
  826. $mollom_form = mollom_form_load($form_id);
  827. if (!$mollom_form) {
  828. $source_mollom_form = mollom_form_load($source_form_id);
  829. $mollom_form = $source_mollom_form;
  830. $mollom_form['form_id'] = $form_id;
  831. $cid = 'mollom:form:' . $form_id;
  832. cache_set($cid, $mollom_form);
  833. }
  834. }
  835. /**
  836. * Implements hook_module_implements_alter().
  837. *
  838. * We need our hook_form_alter() to run before mollom's, so that we can
  839. * prime and modify its cache before mollom uses it.
  840. *
  841. * @see webform_localization_form_alter()
  842. * @see mollom_form_alter()
  843. */
  844. function webform_localization_module_implements_alter(&$implementations, $hook) {
  845. if ($hook != 'form_alter') {
  846. return;
  847. }
  848. // If mollom isn't enabled, do nothing.
  849. if (!module_exists('mollom')) {
  850. return;
  851. }
  852. // If our code will run before mollom's, do nothing.
  853. $pos = array_flip(array_keys($implementations));
  854. if ($pos['webform_localization'] < $pos['mollom']) {
  855. return;
  856. }
  857. // Make it so our hook implementation runs before mollom's.
  858. $webform_localization = array(
  859. 'webform_localization' => $implementations['webform_localization'],
  860. );
  861. $implementations = array_diff_key($implementations, $webform_localization);
  862. $implementations = array_merge(
  863. array_slice($implementations, 0, $pos['mollom'], TRUE), $webform_localization, array_slice($implementations, $pos['mollom'], NULL, TRUE)
  864. );
  865. }
  866. /**
  867. * Gets webform localization options that match a node ID.
  868. *
  869. * @staticvar array $webform_localization_options
  870. * An array of webform localization options group by nid.
  871. *
  872. * @param int $nid
  873. * A node Id.
  874. * @param bool $clear_cache
  875. * A flag to force a database reading in case that properties are cached.
  876. *
  877. * @return array
  878. * Webform localization options that match the nid.
  879. */
  880. function webform_localization_get_config($nid, $clear_cache = FALSE) {
  881. static $webform_localization_options = array();
  882. if ($clear_cache || !isset($webform_localization_options[$nid])) {
  883. $defaults = array_keys(webform_node_defaults());
  884. $webform_properties = array();
  885. foreach ($defaults as $key) {
  886. $webform_properties[$key] = $key;
  887. }
  888. unset($webform_properties['components']);
  889. unset($webform_properties['roles']);
  890. unset($webform_properties['emails']);
  891. unset($webform_properties['record_exists']);
  892. // If webform_uuid is being used since $component['nid'] is really a UUID
  893. // get the nid.
  894. if (module_exists('webform_uuid') && variable_get('webform_localization_using_uuid', FALSE)) {
  895. $record = entity_get_id_by_uuid('node', array($nid));
  896. $nid = (!empty($record)) ? array_pop($record) : $nid;
  897. }
  898. // Select webform localization options that match this node ID.
  899. $options = db_select('webform_localization')
  900. ->fields('webform_localization')
  901. ->condition('nid', $nid, '=')
  902. ->execute()
  903. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  904. if (count($options) == 0) {
  905. $webform_localization_options[$nid] = array(
  906. 'nid' => $nid,
  907. 'expose_strings' => 0,
  908. 'single_webform' => 0,
  909. 'sync_components' => 0,
  910. 'sync_roles' => 0,
  911. 'sync_emails' => 0,
  912. 'webform_properties' => array(),
  913. 'webform_properties_structure' => $webform_properties,
  914. 'no_persistent' => TRUE,
  915. );
  916. }
  917. else {
  918. $options[$nid]['webform_properties_structure'] = $webform_properties;
  919. if (empty($options[$nid]['webform_properties'])) {
  920. $options[$nid]['webform_properties'] = array();
  921. }
  922. else {
  923. $options[$nid]['webform_properties'] = unserialize($options[$nid]['webform_properties']);
  924. }
  925. $webform_localization_options[$nid] = $options[$nid];
  926. }
  927. }
  928. return $webform_localization_options[$nid];
  929. }
  930. /**
  931. * Global switch to enable / disable syncing.
  932. *
  933. * This function also check whether we are synching at the moment.
  934. *
  935. * @return bool
  936. * TRUE if we need to run sync operations. FALSE during syncing
  937. * so we don't have recursion.
  938. */
  939. function _webform_localization_sync($status = NULL) {
  940. static $current = TRUE;
  941. if (isset($status)) {
  942. $current = $status;
  943. }
  944. return $current;
  945. }
  946. /**
  947. * Translate webform confirmation field.
  948. */
  949. function webform_localization_preprocess_webform_confirmation(&$vars) {
  950. if (empty($vars['node']->tnid)) {
  951. return;
  952. }
  953. // Select all webforms that match the localization configuration.
  954. $query = db_select('webform', 'w');
  955. $query->innerJoin('webform_localization', 'wl', 'w.nid = wl.nid');
  956. $query->fields('w', array('nid'));
  957. $query->condition('wl.single_webform', $vars['node']->tnid, '=');
  958. $query->condition('w.nid', $vars['node']->nid, '<>');
  959. $result = $query->execute()->fetchField();
  960. if ($result) {
  961. $source_node = node_load($result);
  962. // We replace the webform with the node translation source.
  963. $vars['node']->webform = $source_node->webform;
  964. }
  965. else {
  966. return;
  967. }
  968. $confirmation = check_markup($vars['node']->webform['confirmation'], $vars['node']->webform['confirmation_format'], '', TRUE);
  969. // Strip out empty tags added by WYSIWYG editors if needed.
  970. $vars['confirmation_message'] = drupal_strlen(trim(strip_tags($confirmation))) ? $confirmation : '';
  971. }
  972. /**
  973. * Implements hook_form_webform_client_form_alter().
  974. */
  975. function webform_localization_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
  976. if (!isset($form['#node']->webform['nid'])) {
  977. return;
  978. }
  979. $webform_localization_options = webform_localization_get_config($form['#node']->webform['nid']);
  980. if ($webform_localization_options['single_webform']) {
  981. if (isset($form['details']['nid']['#value']) && $form['#node']->webform['nid'] == $form['#node']->tnid) {
  982. // We keep current language node nid.
  983. $form['details']['current_language_nid'] = array(
  984. '#type' => 'value',
  985. '#value' => $form['details']['nid']['#value'],
  986. );
  987. // Nid from the source webform for webform_validation.
  988. $form['details']['nid']['#value'] = $form['#node']->webform['nid'];
  989. }
  990. }
  991. }
  992. /**
  993. * Implements hook_modules_disabled().
  994. */
  995. function webform_localization_modules_disabled($modules) {
  996. if (in_array('uuid', $modules)) {
  997. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  998. webform_localization_uuid_update_strings(TRUE);
  999. }
  1000. }
  1001. /**
  1002. * Implements hook_modules_enabled().
  1003. */
  1004. function webform_localization_modules_enabled($modules) {
  1005. if (in_array('uuid', $modules)) {
  1006. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  1007. webform_localization_uuid_update_strings(FALSE);
  1008. }
  1009. }
  1010. /**
  1011. * Implements hook_form_FORM_ID_alter().
  1012. *
  1013. * Add related translations to translation form.
  1014. */
  1015. function webform_localization_form_i18n_string_locale_translate_edit_form_alter(&$form, &$form_state, $form_id) {
  1016. if ($form['textgroup']['#value'] == 'webform') {
  1017. if (isset($form['context']['#markup']) && preg_match('/#title/', $form['context']['#markup'])) {
  1018. list($id, $cid, $name) = explode(':', $form['context']['#markup']);
  1019. $form['#submit'][] = 'webform_localization_i18n_string_locale_translate_edit_form_submit';
  1020. $rel_key = 'webform:' . $id . ':' . $cid . '%';
  1021. $related = db_query('SELECT lid, source, context, textgroup, location FROM {locales_source} WHERE location LIKE :key', array(':key' => $rel_key));
  1022. $langs_installed = language_list();
  1023. $langs_enabled = language_list('enabled');
  1024. $langs_enabled = $langs_enabled[1];
  1025. if (isset($form['translations'])) {
  1026. foreach ($form['translations'] as $key => $value) {
  1027. if (array_key_exists($key, $langs_installed) && !array_key_exists($key, $langs_enabled)) {
  1028. unset($form['translations'][$key]);
  1029. }
  1030. }
  1031. }
  1032. $form['items'] = array(
  1033. '#type' => 'fieldset',
  1034. '#title' => t('Related strings'),
  1035. '#collapsible' => TRUE,
  1036. '#collapsed' => TRUE,
  1037. '#weight' => 2,
  1038. );
  1039. foreach ($related as $source) {
  1040. if ($source->context != $form['context']['#markup']) {
  1041. $lid = $source->lid;
  1042. // Add original text to the top and some values for form altering.
  1043. $form['items'][$lid]['original'] = array(
  1044. '#type' => 'item',
  1045. '#title' => t('Original text'),
  1046. '#markup' => check_plain(wordwrap($source->source, 0)),
  1047. );
  1048. if (!empty($source->context)) {
  1049. $form['items'][$lid]['context'] = array(
  1050. '#type' => 'item',
  1051. '#title' => t('Context'),
  1052. '#markup' => check_plain($source->context),
  1053. );
  1054. }
  1055. $form['items'][$lid]['textgroup'] = array(
  1056. '#type' => 'value',
  1057. '#value' => $source->textgroup,
  1058. );
  1059. $form['items'][$lid]['location'] = array(
  1060. '#type' => 'value',
  1061. '#value' => $source->location,
  1062. );
  1063. $languages = $langs_enabled;
  1064. // We don't need the default language value, that value is in $source.
  1065. $omit = $source->textgroup == 'default' ? 'en' : i18n_string_source_language();
  1066. unset($languages[($omit)]);
  1067. $form['items'][$lid]['translations-' . $lid] = array('#tree' => TRUE);
  1068. // Approximate the number of rows to use in the default textarea.
  1069. $rows = min(ceil(str_word_count($source->source) / 12), 10);
  1070. foreach ($languages as $langcode => $language) {
  1071. $form['items'][$lid]['translations-' . $lid][$langcode] = array(
  1072. '#type' => 'textarea',
  1073. '#title' => t($language->name),
  1074. '#rows' => $rows,
  1075. '#default_value' => '',
  1076. );
  1077. }
  1078. // Fetch translations and fill in default values in the form.
  1079. $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid AND language <> :omit", array(':lid' => $lid, ':omit' => $omit));
  1080. foreach ($result as $translation) {
  1081. $form['items'][$lid]['translations-' . $lid][$translation->language]['#default_value'] = $translation->translation;
  1082. }
  1083. }
  1084. }
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * Submit handler for translations form.
  1090. */
  1091. function webform_localization_i18n_string_locale_translate_edit_form_submit($form, &$form_state) {
  1092. foreach ($form_state['values'] as $key => $value) {
  1093. if (preg_match("/translations-(.*)/", $key, $lid)) {
  1094. foreach ($value as $lang => $translation) {
  1095. $existing = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid[1], ':language' => $lang))->fetchField();
  1096. if (!empty($translation)) {
  1097. if (!empty($existing)) {
  1098. db_update('locales_target')
  1099. ->fields(array(
  1100. 'translation' => $translation,
  1101. 'i18n_status' => I18N_STRING_STATUS_CURRENT,
  1102. ))
  1103. ->condition('lid', $lid[1])
  1104. ->condition('language', $lang)
  1105. ->execute();
  1106. }
  1107. else {
  1108. db_insert('locales_target')
  1109. ->fields(array(
  1110. 'lid' => $lid[1],
  1111. 'translation' => $translation,
  1112. 'language' => $lang,
  1113. ))
  1114. ->execute();
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. /**
  1122. * Implements hook_preprocess_webform_components_form().
  1123. *
  1124. * Adds a translate link to each webform component.
  1125. */
  1126. function webform_localization_preprocess_webform_components_form(&$variables) {
  1127. $form = $variables['form'];
  1128. if (!isset($form['#node']->webform['nid'])) {
  1129. return;
  1130. }
  1131. $header = $variables['header'];
  1132. $rows = $variables['rows'];
  1133. $node = $form['#node'];
  1134. $enabled_languages = locale_language_list('name');
  1135. $webform_localization_options = webform_localization_get_config($form['#node']->webform['nid']);
  1136. $row_data = array();
  1137. if ($webform_localization_options['expose_strings'] && count($enabled_languages) > 1) {
  1138. // Change colspan of header and footer.
  1139. $footer = array_pop($rows);
  1140. $header[6]['colspan'] = 4;
  1141. $footer['data'][6]['colspan'] = 4;
  1142. // Add translate link to rows.
  1143. foreach ($rows as $key => $row) {
  1144. $row_data[$key] = $row;
  1145. if (isset($row['data-cid'])) {
  1146. $name = webform_localization_i18n_string_name($node->nid, $row['data-cid'], '#title');
  1147. $string_source = i18n_string_get_string($name);
  1148. if (isset($string_source->lid)) {
  1149. $row_data[$key]['data'][] = l(t('Translate'), 'admin/config/regional/translate/edit/' . $string_source->lid, array('query' => drupal_get_destination()));
  1150. }
  1151. }
  1152. }
  1153. $variables['rows'] = $row_data;
  1154. $variables['rows'][] = $footer;
  1155. $variables['header'] = $header;
  1156. $variables['form'] = $form;
  1157. }
  1158. }
  1159. /**
  1160. * Implements hook_js_alter().
  1161. */
  1162. function webform_localization_js_alter(&$javascript) {
  1163. // Only react on webform nodes.
  1164. if ($node = menu_get_object()) {
  1165. if (!isset($node->type) || !in_array($node->type, webform_variable_get('webform_node_types'))) {
  1166. return FALSE;
  1167. }
  1168. // Only react when we are not on the source node.
  1169. if ($node->nid != $node->tnid && $node->tnid > 0) {
  1170. // Gets webform localization options that match this node ID.
  1171. $webform_localization_options = webform_localization_get_config($node->tnid);
  1172. // Only react when keep a single webform across a translation set.
  1173. if ($webform_localization_options['single_webform'] > 0) {
  1174. foreach ($javascript['settings']['data'] as &$setting) {
  1175. if (isset($setting['webform']['conditionals']['webform-client-form-' . $node->tnid])) {
  1176. $setting['webform']['conditionals']['webform-client-form-' . $node->nid] = $setting['webform']['conditionals']['webform-client-form-' . $node->tnid];
  1177. unset($setting['webform']['conditionals']['webform-client-form-' . $node->tnid]);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. * Implements hook_entitycache_node_load().
  1186. */
  1187. function webform_localization_entitycache_node_load($nodes) {
  1188. $webform_types = webform_variable_get('webform_node_types');
  1189. foreach ($nodes as $nid => &$node) {
  1190. if (in_array($node->type, $webform_types)) {
  1191. // Gets webform localization options that match this node ID.
  1192. $wl_options = webform_localization_get_config($nid);
  1193. if ($wl_options['expose_strings']) {
  1194. module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
  1195. // Translate custom strings.
  1196. webform_localization_translate_strings($node);
  1197. webform_localization_email_translate_strings($node);
  1198. }
  1199. }
  1200. }
  1201. }