fe_nodequeue.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function fe_nodequeue_features_api() {
  6. return array(
  7. 'fe_nodequeue' => array(
  8. 'name' => t('Nodequeues'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'fe_nodequeue_export_fields',
  11. 'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
  12. )
  13. );
  14. }
  15. /**
  16. * Implements hook_features_export_options().
  17. */
  18. function fe_nodequeue_features_export_options() {
  19. $options = array();
  20. $table = 'nodequeue_queue';
  21. $query = "SELECT * FROM {{$table}}";
  22. $fields = db_query($query);
  23. while ($obj = $fields->fetchObject()) {
  24. $options[$obj->name] = t('@name [@machine_name]', array('@name' => $obj->title, '@machine_name' => $obj->name));
  25. }
  26. return $options;
  27. }
  28. /**
  29. * Implements hook_features_export().
  30. */
  31. function fe_nodequeue_features_export($data, &$export, $module_name = '') {
  32. $pipe = array();
  33. $map = features_get_default_map('fe_nodequeue');
  34. foreach ($data as $name) {
  35. $export['dependencies']['fe_nodequeue'] = 'fe_nodequeue';
  36. // If another module provides this style, add it as a dependency
  37. if (isset($map[$name]) && $map[$name] != $module_name) {
  38. $module = $map[$name];
  39. $export['dependencies'][$module] = $module;
  40. }
  41. // Otherwise, export the nodequeue
  42. elseif (nodequeue_load_queue_by_name($name)) {
  43. $export['features']['fe_nodequeue'][$name] = $name;
  44. }
  45. }
  46. return $pipe;
  47. }
  48. /**
  49. * Implements hook_features_export_render().
  50. */
  51. function fe_nodequeue_features_export_render($module_name = '', $data) {
  52. $code = array();
  53. $code[] = ' $nodequeues = array();';
  54. $code[] = '';
  55. foreach ($data as $name) {
  56. // Clone the nodequeue object so that our changes aren't statically
  57. // cached.
  58. $nodequeue_static = nodequeue_load_queue_by_name($name);
  59. if (!empty($nodequeue_static) && $nodequeue = clone $nodequeue_static) {
  60. // sort roles and types arrays into ascending order so that we can
  61. // check for overridden db data without being affected by the
  62. // order in which this array gets stored/loaded.
  63. if (!empty($nodequeue->roles)) {
  64. sort($nodequeue->roles);
  65. }
  66. if (!empty($nodequeue->types)) {
  67. sort($nodequeue->types);
  68. }
  69. // Remove variables which are specific to the current site and should not
  70. // be exported.
  71. unset($nodequeue->qid);
  72. unset($nodequeue->subqueues);
  73. $nodequeue_export = features_var_export($nodequeue, ' ');
  74. $code[] = " // Exported nodequeues: {$nodequeue->name}";
  75. $code[] = " \$nodequeues['{$name}'] = {$nodequeue_export};";
  76. $code[] = "";
  77. }
  78. }
  79. $code[] = ' return $nodequeues;';
  80. $code = implode("\n", $code);
  81. return array('fe_nodequeue_export_fields' => $code);
  82. }
  83. /**
  84. * Implements hook_features_revert().
  85. */
  86. function fe_nodequeue_features_revert($module) {
  87. $defaults = features_get_default('fe_nodequeue', $module);
  88. // Revert.
  89. foreach ($defaults as $object) {
  90. if (empty($object['name'])) {
  91. continue;
  92. }
  93. if ($nodequeue_queue = nodequeue_load_queue_by_name($object['name'])) {
  94. // Update existing nodequeue in the situation that a nodequeue with the
  95. // same name already exists.
  96. $object['qid'] = $nodequeue_queue->qid;
  97. }
  98. $result = _fe_nodequeue_save_queue((array) $object);
  99. }
  100. return TRUE;
  101. }
  102. /**
  103. * Implements hook_features_revert().
  104. */
  105. function fe_nodequeue_features_rebuild($module) {
  106. fe_nodequeue_features_revert($module);
  107. }
  108. /**
  109. * Implements hook_features_enable_feature().
  110. */
  111. function fe_nodequeue_features_enable_feature($module) {
  112. fe_nodequeue_features_revert($module);
  113. }
  114. /**
  115. * Save a nodequeue queue.
  116. *
  117. * @param $settings
  118. * @return array
  119. */
  120. function _fe_nodequeue_save_queue($settings = array()) {
  121. // Simulate checkboxes.
  122. $settings['roles'] = drupal_map_assoc($settings['roles']);
  123. $settings['types'] = drupal_map_assoc($settings['types']);
  124. // Simulate submitting.
  125. $form_state = array();
  126. $form_state['values'] = $settings;
  127. module_load_include('inc', 'nodequeue', 'includes/nodequeue.admin');
  128. nodequeue_edit_queue_form_submit(NULL, $form_state);
  129. return $settings;
  130. }