webform_localization.component.sync.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Webform Localization Component Sync Functions.
  5. */
  6. /**
  7. * Development sponsored by Riot Games.
  8. *
  9. * @author German Martin <gmartin.php@gmail.com>
  10. */
  11. /**
  12. * Get an array with translated versions of a component.
  13. *
  14. * This array is later used to run sync operations on components.
  15. *
  16. * @staticvar array $component_translations
  17. * An array of webform components for each tnid.
  18. * @param array $component
  19. * A webform component array.
  20. * @return
  21. * An array of webform components that match a tnid.
  22. *
  23. */
  24. function webform_localization_component_get_translations($component) {
  25. static $component_translations = array();
  26. $node = node_load($component['nid']);
  27. $translations = translation_node_get_translations($node->tnid);
  28. if (!isset($component_translations[$node->tnid])) {
  29. $nid_list = array();
  30. foreach ($translations as $trans_node) {
  31. $nid_list[] = $trans_node->nid;
  32. }
  33. // Load components for each translated node.
  34. $components = db_select('webform_component')
  35. ->fields('webform_component')
  36. ->condition('nid', $nid_list, 'IN')
  37. ->condition('cid', $component['cid'], '=')
  38. ->orderBy('nid')
  39. ->execute()
  40. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  41. // Cleanup on each component.
  42. foreach ($components as $cid => $c) {
  43. $components[$cid]['nid'] = $c['nid'];
  44. $components[$cid]['extra'] = unserialize($c['extra']);
  45. webform_component_defaults($components[$cid]);
  46. }
  47. $component_translations[$node->tnid] = $components;
  48. }
  49. return $component_translations[$node->tnid];
  50. }
  51. /**
  52. * Synchronize the changed component with it's translations versions.
  53. *
  54. * @param $component
  55. * A webform component that have been modified.
  56. * @param $translations
  57. * An Array of the translated webform components to sync with.
  58. */
  59. function webform_localization_component_sync($component, &$translations) {
  60. /**
  61. * Get properties to sync
  62. * $sync_properties['standar_values'] = array('mandatory', 'weight', 'pid');
  63. * $sync_properties['extra_values'] = array('options', 'private');
  64. */
  65. $sync_properties = webform_localization_synchronizable_properties($component);
  66. foreach ($translations as $component_key => $translation) {
  67. foreach ($sync_properties['standar_values'] as $sync_key) {
  68. if (is_string($sync_key)) {
  69. $translations[$component_key][$sync_key] = $component[$sync_key];
  70. }
  71. }
  72. foreach ($sync_properties['extra_values'] as $extra_key) {
  73. if (is_string($extra_key) && isset($component['extra'][$extra_key])) {
  74. $translations[$component_key]['extra'][$extra_key] = $component['extra'][$extra_key];
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * Get synchronizable properties for a webform component
  81. *
  82. * @param array $component
  83. * A webform component.
  84. * @param boolean $clear_cache
  85. * A flag to force a database reading in case that properties are cached.
  86. * @return
  87. * An array with synchronizable properties.
  88. */
  89. function webform_localization_synchronizable_properties($component, $clear_cache = FALSE) {
  90. static $webform_component_localization_options = array();
  91. $nid = $component['nid'];
  92. $cid = $component['cid'];
  93. if ($clear_cache || !isset($webform_component_localization_options[$nid][$cid])) {
  94. // Select webform localization options that match this node ID.
  95. $options = db_select('webform_component_localization')
  96. ->fields('webform_component_localization')
  97. ->condition('nid', $nid, '=')
  98. ->condition('cid', $cid, '=')
  99. ->execute()
  100. ->fetchObject();
  101. if (!$options) {
  102. $synchronizable = _webform_localization_default_properties($component);
  103. $webform_component_localization_options[$nid][$cid] = $synchronizable;
  104. }
  105. else {
  106. $synchronizable = array();
  107. $synchronizable['standar_values'] = unserialize($options->standar_properties);
  108. $synchronizable['extra_values'] = unserialize($options->extra_properties);
  109. $synchronizable['extra'] = $synchronizable['extra_values'];
  110. foreach ($synchronizable['extra'] as $k => $value) {
  111. $synchronizable['extra'][$k] = $k;
  112. }
  113. $synchronizable['standar'] = $synchronizable['standar_values'];
  114. foreach ($synchronizable['standar'] as $k => $value) {
  115. $synchronizable['standar'][$k] = $k;
  116. }
  117. }
  118. }
  119. else {
  120. $synchronizable = $webform_component_localization_options[$nid][$cid];
  121. }
  122. return $synchronizable;
  123. }
  124. /**
  125. * Get webform component default properties in synchronizable options format.
  126. *
  127. * @param array $component
  128. * A webform component.
  129. * @return
  130. * An array with webform synchronizable default properties.
  131. */
  132. function _webform_localization_default_properties($component) {
  133. $defaults = webform_component_invoke($component['type'], 'defaults');
  134. $sync = array();
  135. foreach (array_keys($defaults) as $key) {
  136. if ($key != 'extra') {
  137. $sync['standar'][$key] = $key;
  138. }
  139. }
  140. foreach (array_keys($defaults['extra']) as $key) {
  141. $sync['extra'][$key] = $key;
  142. }
  143. // To inform that there is no table record for this options.
  144. $sync['no_persistent'] = TRUE;
  145. $sync['standar_values'] = array();
  146. $sync['extra_values'] = array();
  147. return $sync;
  148. }
  149. /**
  150. * Delete database record for component localization options.
  151. *
  152. * @param array $component
  153. * A webform component.
  154. */
  155. function webform_localization_synchronizable_properties_delete($component) {
  156. db_delete('webform_component_localization')
  157. ->condition('nid', $component['nid'], '=')
  158. ->condition('cid', $component['cid'], '=')
  159. ->execute();
  160. }
  161. /**
  162. * Load a Webform Component
  163. *
  164. * @param $nid
  165. * A node Id.
  166. * @param $cid
  167. * A webform component Id.
  168. * @return
  169. * A webform component array.
  170. *
  171. */
  172. function webform_localization_component_load($nid, $cid) {
  173. $component = db_select('webform_component')
  174. ->fields('webform_component')
  175. ->condition('nid', $nid, '=')
  176. ->condition('cid', $cid, '=')
  177. ->execute()
  178. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  179. $component[$nid]['nid'] = $nid;
  180. $component[$nid]['extra'] = unserialize($component[$nid]['extra']);
  181. webform_component_defaults($component[$nid]);
  182. return $component[$nid];
  183. }