webform_localization.sync.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * Webform Localization General Properties, Roles and Emails Sync Functions.
  5. */
  6. /**
  7. * Development sponsored by Riot Games.
  8. *
  9. * @author German Martin <gmartin.php@gmail.com>
  10. */
  11. /**
  12. * Sync webform configured properties with its translated versions.
  13. *
  14. * @param $nid
  15. * A node Id.
  16. */
  17. function webform_localization_webform_properties_sync($nid) {
  18. // Gets webform localization options that match this node ID.
  19. $webform_localization_options = webform_localization_get_config($nid, TRUE);
  20. if (count($webform_localization_options['webform_properties']) > 0) {
  21. $node_list = _webform_localization_translation_set_node_list($nid);
  22. if (count($node_list) > 1) {
  23. // Select all webforms that match these node IDs.
  24. $result = db_select('webform')
  25. ->fields('webform')
  26. ->condition('nid', $node_list, 'IN')
  27. ->execute()
  28. ->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
  29. if ($result) {
  30. $origin = $result[$nid];
  31. unset($result[$nid]);
  32. // Sync each translated version.
  33. foreach ($result as $webform) {
  34. foreach ($webform_localization_options['webform_properties'] as $property) {
  35. $webform[$property] = $origin[$property];
  36. }
  37. drupal_write_record('webform', $webform, array('nid'));
  38. }
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * Sync webform roles with its translated versions.
  45. *
  46. * @param $nid
  47. * A node Id.
  48. */
  49. function webform_localization_roles_sync($nid) {
  50. $node_list = _webform_localization_translation_set_node_list($nid);
  51. $roles = db_select('webform_roles')
  52. ->fields('webform_roles', array('rid'))
  53. ->condition('nid', $nid)
  54. ->execute()
  55. ->fetchCol();
  56. foreach ($node_list as $n) {
  57. if ($n != $nid) {
  58. db_delete('webform_roles')->condition('nid', $n)->execute();
  59. foreach ($roles as $rid) {
  60. db_insert('webform_roles')->fields(array('nid' => $n, 'rid' => $rid))->execute();
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Sync webform emails recipients with its translated versions.
  67. *
  68. * @param $nid
  69. * A node Id.
  70. */
  71. function webform_localization_emails_sync($nid) {
  72. $node_list = _webform_localization_translation_set_node_list($nid);
  73. $origin = _webform_localization_emails_load($nid);
  74. foreach ($node_list as $n) {
  75. if ($n != $nid) {
  76. $version = _webform_localization_emails_load($n);
  77. if ($origin != $version) {
  78. module_load_include('inc', 'webform', 'includes/webform.emails');
  79. $original_eids = array_keys($version);
  80. $current_eids = array_keys($origin);
  81. $all_eids = array_unique(array_merge($original_eids, $current_eids));
  82. $deleted_eids = array_diff($original_eids, $current_eids);
  83. $inserted_eids = array_diff($current_eids, $original_eids);
  84. foreach ($all_eids as $eid) {
  85. if (in_array($eid, $inserted_eids)) {
  86. $temp = $origin[$eid];
  87. $temp['nid'] = $n;
  88. webform_email_insert($temp);
  89. }
  90. elseif (in_array($eid, $deleted_eids)) {
  91. $node = node_load($n);
  92. webform_email_delete($node, $version[$eid]);
  93. }
  94. elseif ($origin[$eid] != $version[$eid]) {
  95. $temp = $origin[$eid];
  96. $temp['nid'] = $n;
  97. webform_email_update($temp);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Get an Array of webform emails recipients for a Node Id.
  106. *
  107. * @param $nid
  108. * A node Id.
  109. * @return
  110. * An array of webform emails.
  111. */
  112. function _webform_localization_emails_load($nid) {
  113. $emails = db_select('webform_emails')
  114. ->fields('webform_emails')
  115. ->condition('nid', $nid)
  116. ->execute()
  117. ->fetchAllAssoc('eid', PDO::FETCH_ASSOC);
  118. // Unserialize the exclude component list for e-mails.
  119. foreach ($emails as $eid => $email) {
  120. $emails[$eid]['excluded_components'] = array_filter(explode(',', $email['excluded_components']));
  121. if (variable_get('webform_format_override', 0)) {
  122. $emails[$eid]['html'] = variable_get('webform_default_format', 0);
  123. }
  124. }
  125. return $emails;
  126. }
  127. /**
  128. * Get a node Id list of a translation set.
  129. *
  130. * @param $nid
  131. * A node Id.
  132. * @return
  133. * An array of node ids that share a tnid.
  134. */
  135. function _webform_localization_translation_set_node_list($nid) {
  136. static $node_list = array();
  137. if (!isset($node_list[$nid])) {
  138. // Get all versions of the node.
  139. $node = node_load($nid);
  140. if (!isset($node->tnid) || $node->tnid == 0) {
  141. $node_list[$nid] = array();
  142. return $node_list[$nid];
  143. }
  144. $translations = translation_node_get_translations($node->tnid);
  145. $list = array();
  146. foreach ($translations as $n) {
  147. $list[] = $n->nid;
  148. }
  149. $node_list[$nid] = $list;
  150. }
  151. return $node_list[$nid];
  152. }