term_merge.batch.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Batch process callbacks for Term Merge module.
  5. */
  6. /**
  7. * Process batch function.
  8. *
  9. * Trigger action 'term_merge_action' on each pair of term branch:term trunk.
  10. *
  11. * @param array $term_branch
  12. * An array of term tids to be merged, aka term branches
  13. * @param int $term_trunk
  14. * The tid of the term to merge term branches into, aka term trunk
  15. * @param array $merge_settings
  16. * Array of settings that control how merging should happen. Currently
  17. * supported settings are:
  18. * - term_branch_keep: (bool) Whether the term branches should not be
  19. * deleted, also known as "merge only occurrences" option
  20. * - merge_fields: (array) Array of field names whose values should be
  21. * merged into the values of corresponding fields of term trunk (until
  22. * each field's cardinality limit is reached)
  23. * - keep_only_unique: (bool) Whether after merging within one field only
  24. * unique taxonomy term references should be kept in other entities. If
  25. * before merging your entity had 2 values in its taxonomy term reference
  26. * field and one was pointing to term branch while another was pointing to
  27. * term trunk, after merging you will end up having your entity
  28. * referencing to the same term trunk twice. If you pass TRUE in this
  29. * parameter, only a single reference will be stored in your entity after
  30. * merging
  31. * - redirect: (int) HTTP code for redirect from $term_branch to
  32. * $term_trunk, 0 stands for the default redirect defined in Redirect
  33. * module. Use constant TERM_MERGE_NO_REDIRECT to denote not creating any
  34. * HTTP redirect. Note: this parameter requires Redirect module enabled,
  35. * otherwise it will be disregarded
  36. * - synonyms: (array) Array of field names of trunk term into which branch
  37. * terms should be added as synonyms (until each field's cardinality limit
  38. * is reached). Note: this parameter requires Synonyms module enabled,
  39. * otherwise it will be disregarded
  40. * - step: (int) How many term branches to merge per script run in batch. If
  41. * you are hitting time or memory limits, decrease this parameter
  42. * @param array $context
  43. * Drupal Batch API context array
  44. */
  45. function _term_merge_batch_process($term_branch, $term_trunk, $merge_settings, &$context) {
  46. // Initializing sandbox.
  47. if (!isset($context['sandbox']['current'])) {
  48. $context['sandbox']['current'] = 0;
  49. }
  50. // Populating $merge_settings with defaults.
  51. $merge_settings += array(
  52. 'term_branch_keep' => FALSE,
  53. 'merge_fields' => array(),
  54. 'keep_only_unique' => TRUE,
  55. 'redirect' => TERM_MERGE_NO_REDIRECT,
  56. 'synonyms' => array(),
  57. 'step' => 40,
  58. );
  59. $total = count($term_branch);
  60. // To speed up the process we take advantage of taxonomy_term_load_multiple()
  61. // instead of just repeating calls to taxonomy_term_load().
  62. $till = min($total, $context['sandbox']['current'] + $merge_settings['step']);
  63. $length = $till - $context['sandbox']['current'];
  64. $term_branch = array_slice($term_branch, $context['sandbox']['current'], $length);
  65. $term_branch = array_values(taxonomy_term_load_multiple($term_branch));
  66. for ($i = 0; $i < $merge_settings['step'] && $context['sandbox']['current'] < $total; $i++) {
  67. actions_do('term_merge_action', $term_branch[$i], array(
  68. 'term_trunk' => $term_trunk,
  69. 'term_branch_keep' => $merge_settings['term_branch_keep'],
  70. 'merge_fields' => $merge_settings['merge_fields'],
  71. 'keep_only_unique' => $merge_settings['keep_only_unique'],
  72. 'redirect' => $merge_settings['redirect'],
  73. 'synonyms' => $merge_settings['synonyms'],
  74. ));
  75. $context['sandbox']['current']++;
  76. }
  77. if ($context['sandbox']['current'] != $total) {
  78. $context['finished'] = $context['sandbox']['current'] / $total;
  79. $term = $term_branch[$i - 1];
  80. $context['message'] = t('Merged up to @term', array('@term' => $term->name));
  81. }
  82. }
  83. /**
  84. * Batch 'finished' callback.
  85. *
  86. * Process results of Term Merge batch.
  87. */
  88. function term_merge_batch_finished($success, $results, $operations) {
  89. if ($success) {
  90. drupal_set_message(t('The terms have been successfully merged.'));
  91. }
  92. else {
  93. // An error happened. We have to notify the user.
  94. drupal_set_message(t('An error occurred. We are sorry, please, report this error to the maintainers of Term Merge module.'), 'error');
  95. }
  96. }