nodequeue.actions.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @file nodequeue.actions.inc
  4. * Provides actions integration for nodequeues.
  5. */
  6. /**
  7. * Implements hook_action_info().
  8. */
  9. function nodequeue_action_info() {
  10. return array(
  11. 'nodequeue_add_action' => array(
  12. 'type' => 'node',
  13. 'label' => t('Add to Nodequeues'),
  14. 'configurable' => TRUE,
  15. 'triggers' => array(
  16. 'node_presave',
  17. 'node_insert',
  18. 'node_update',
  19. ),
  20. ),
  21. 'nodequeue_remove_action' => array(
  22. 'type' => 'node',
  23. 'label' => t('Remove from Nodequeues'),
  24. 'configurable' => TRUE,
  25. 'triggers' => array(
  26. 'node_delete',
  27. ),
  28. ),
  29. );
  30. }
  31. /**
  32. * Configuration form for Add to Nodequeues action.
  33. */
  34. function nodequeue_add_action_form($context) {
  35. // Default values for form.
  36. if (!isset($context['qids'])) {
  37. $context['qids'] = '';
  38. }
  39. $queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
  40. foreach ($queues as $qid => $queue) {
  41. $options[$qid] = $queue->title;
  42. }
  43. $form = array();
  44. if (count($options)) {
  45. // Add form components.
  46. $form['qids'] = array(
  47. '#type' => 'select',
  48. '#title' => t("Queue"),
  49. '#default_value' => $context['qids'],
  50. '#multiple' => TRUE,
  51. '#options' => $options,
  52. '#required' => TRUE,
  53. '#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
  54. );
  55. }
  56. else {
  57. drupal_set_message(t('Please <a href="@url">create</a> a nodequeue first.', array('@url' => url('admin/structure/nodequeue'))));
  58. }
  59. return $form;
  60. }
  61. /**
  62. * Submit handler for Add to Nodequeues action configuration.
  63. */
  64. function nodequeue_add_action_submit($form, &$form_state) {
  65. return array(
  66. 'qids' => $form_state['values']['qids'],
  67. );
  68. }
  69. /**
  70. * Action to add a node to a queue.
  71. */
  72. function nodequeue_add_action($node, $context) {
  73. $queues = nodequeue_load_queues($context['qids'], TRUE);
  74. // Filter out queues by node type. We choose not to use nodequeue_get_qids() because it checks for access control which only matters if we administering a queue.
  75. $eligible_queues = array();
  76. foreach ($queues as $queue) {
  77. if (in_array($node->type, $queue->types)) {
  78. $eligible_queues[$queue->qid] = $queue;
  79. }
  80. }
  81. if (!empty($eligible_queues)) {
  82. // Remove the node from the eligible queues (if needed).
  83. nodequeue_remove_action($node, array('qids' => array_keys($eligible_queues)));
  84. // Use API to get the eligible subqueues
  85. $eligible_subqueues = nodequeue_get_subqueues_by_node($eligible_queues, $node);
  86. // Add node to each subqueue.
  87. foreach ($eligible_subqueues as $subqueue) {
  88. nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
  89. }
  90. }
  91. }
  92. /**
  93. * Old-style action to add a node to a queue.
  94. */
  95. function action_nodequeue_add($op, $edit = array(), $node) {
  96. switch ($op) {
  97. case 'metadata':
  98. return array(
  99. 'description' => t('Add to Nodequeues'),
  100. 'type' => t('node'),
  101. 'batchable' => TRUE,
  102. 'configurable' => TRUE,
  103. );
  104. break;
  105. case 'do':
  106. $queues = nodequeue_load_queues($edit['qids'], TRUE);
  107. // Filter out queues by node type. We choose not to use nodequeue_get_qids() because it checks for access control which only matters if we administering a queue.
  108. $eligible_queues = array();
  109. foreach ($queues as $queue) {
  110. if (in_array($node->type, $queue->types)) {
  111. $eligible_queues[$queue->qid] = $queue;
  112. }
  113. }
  114. if (!empty($eligible_queues)) {
  115. // Remove the node from the eligible queues (if needed).
  116. action_nodequeue_remove('do', array('qids' => array_keys($eligible_queues)), $node);
  117. // Use API to get the eligible subqueues
  118. $eligible_subqueues = nodequeue_get_subqueues_by_node($eligible_queues, $node);
  119. // Add node to each subqueue.
  120. foreach ($eligible_subqueues as $subqueue) {
  121. nodequeue_subqueue_add($queues[$subqueue->qid], $subqueue, $node->nid);
  122. }
  123. }
  124. break;
  125. // return an HTML config form for the action
  126. case 'form':
  127. // default values for form
  128. if (!isset($edit['qids'])) {
  129. $edit['qids'] = '';
  130. }
  131. $queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
  132. foreach ($queues as $qid => $queue) {
  133. $options[$qid] = $queue->title;
  134. }
  135. $form = array();
  136. if (count($options)) {
  137. // add form components
  138. $form['qids'] = array(
  139. '#type' => 'select',
  140. '#title' => t("Queue"),
  141. '#default_value' => $edit['qids'],
  142. '#multiple' => TRUE,
  143. '#options' => $options,
  144. '#required' => TRUE,
  145. '#description' => t('Specify the queues into which the node should be submitted. If the queue is a smartqueue, the node shall be placed into every subqueue for which it is eligible.')
  146. );
  147. }
  148. else {
  149. drupal_set_message(t('Please <a href="@url">create</a> a nodequeue first.', array('@url' => url('admin/structure/nodequeue'))));
  150. }
  151. return $form;
  152. // validate the HTML form
  153. // process the HTML form to store configuration
  154. case 'submit':
  155. $params = array(
  156. 'qids' => $edit['qids']
  157. );
  158. return $params;
  159. break;
  160. }
  161. }
  162. /**
  163. * Configuration form for Remove from Nodequeues action.
  164. */
  165. function nodequeue_remove_action_form($context) {
  166. // Default values for form.
  167. if (!isset($context['qids'])) {
  168. $context['qids'] = array();
  169. }
  170. $queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
  171. foreach ($queues as $qid => $queue) {
  172. $options[$qid] = $queue->title;
  173. }
  174. // Add form components.
  175. $form['qids'] = array(
  176. '#type' => 'select',
  177. '#title' => t("Queues"),
  178. '#default_value' => $context['qids'],
  179. '#multiple' => TRUE,
  180. '#decription' => t('Specify the queues from which the node should be removed. If the queue is a smartqueue, the node shall be removed from all subqueues.'),
  181. '#required' => TRUE,
  182. '#options' => $options,
  183. );
  184. return $form;
  185. }
  186. /**
  187. * Submit handler for Remove from Nodequeues action configuration.
  188. */
  189. function nodequeue_remove_action_submit($form, &$form_state) {
  190. return array(
  191. 'qids' => $form_state['values']['qids'],
  192. );
  193. }
  194. function nodequeue_remove_action($node, $context) {
  195. $qids = $context['qids'];
  196. // If a node is being deleted, ensure it's also removed from any queues.
  197. $args = $qids;
  198. $result = db_select('nodequeue_nodes', 'n')
  199. ->fields('n')
  200. ->condition('nid', $node->nid)
  201. ->condition('qid', $args)
  202. ->execute()
  203. ->fetchAll();
  204. foreach ($result as $obj) {
  205. // This removes by nid, not position, because if we happen to have a
  206. // node in a queue twice, the 2nd position would be wrong.
  207. nodequeue_subqueue_remove_node($obj->sqid, $node->nid);
  208. }
  209. }
  210. /**
  211. * Old-style action to remove a node from a queue.
  212. */
  213. function action_nodequeue_remove($op, $edit = array(), $node) {
  214. switch ($op) {
  215. case 'metadata':
  216. return array(
  217. 'description' => t('Remove from Nodequeues'),
  218. 'type' => t('node'),
  219. 'batchable' => TRUE,
  220. 'configurable' => TRUE,
  221. );
  222. break;
  223. case 'do':
  224. $qids = $edit['qids'];
  225. // If a node is being deleted, ensure it's also removed from any queues.
  226. $args = $qids;
  227. $result = db_select('nodequeue_nodes', 'n')
  228. ->fields('n')
  229. ->condition('nid', $node->nid)
  230. ->condition('qid', $args)
  231. ->execute()
  232. ->fetchAll();
  233. foreach ($result as $obj) {
  234. // This removes by nid, not position, because if we happen to have a
  235. // node in a queue twice, the 2nd position would be wrong.
  236. nodequeue_subqueue_remove_node($obj->sqid, $node->nid);
  237. }
  238. break;
  239. // return an HTML config form for the action
  240. case 'form':
  241. // default values for form
  242. if (!isset($edit['qids'])) {
  243. $edit['qids'] = array();
  244. }
  245. $queues = nodequeue_load_queues(nodequeue_get_all_qids(0, 0, TRUE), TRUE);
  246. foreach ($queues as $qid => $queue) {
  247. $options[$qid] = $queue->title;
  248. }
  249. // add form components
  250. $form['qids'] = array(
  251. '#type' => 'select',
  252. '#title' => t("Queues"),
  253. '#default_value' => $edit['qids'],
  254. '#multiple' => TRUE,
  255. '#decription' => t('Specify the queues from which the node should be removed. If the queue is a smartqueue, the node shall be removed from all subqueues.'),
  256. '#required' => TRUE,
  257. '#options' => $options,
  258. );
  259. return $form;
  260. break;
  261. // validate the HTML form
  262. // process the HTML form to store configuration
  263. case 'submit':
  264. $params = array(
  265. 'qids' => $edit['qids']
  266. );
  267. return $params;
  268. break;
  269. }
  270. }