nodequeue.admin.inc

Admin page callbacks for the nodequeue module.

Functions

Namesort descending Description
nodequeue_admin_add_node Page callback to add a node to a queue.
nodequeue_admin_delete Confirm form to delete a queue
nodequeue_admin_delete_submit Submit function for nodequeue delete
nodequeue_admin_remove Page callback to remove an item from a queue. This will be used only if javascript is disabled in the client, and is a fallback technique. This differs from nodequeue_admin_remove_node in that it removes a specific position, which is necessary in case…
nodequeue_admin_remove_node Page callback to remove a node from a queue.
nodequeue_admin_settings Form builder for the nodequeue settings tab.
nodequeue_admin_view Page callback to view a queue.
nodequeue_arrange_subqueue View the contents of a subqueue, with links to re-order the queue.
nodequeue_arrange_subqueue_form Form definition for nodequeue drag'n'drop form.
nodequeue_arrange_subqueue_form_add_submit
nodequeue_arrange_subqueue_form_clear_submit
nodequeue_arrange_subqueue_form_reverse_submit Submit function for nodequeue_arrange_subqueue_form on 'Reverse' button.
nodequeue_arrange_subqueue_form_shuffle_submit
nodequeue_arrange_subqueue_form_submit Submit handler for nodequeue drag'n'drop form. Updates node positions in {nodequeue_nodes}.
nodequeue_arrange_subqueue_form_validate Validate handler for nodequeue_arrange_subqueue_form.
nodequeue_autocomplete Page callback for autocomplete.
nodequeue_clear_confirm Confirm form to clear a queue.
nodequeue_clear_confirm_submit Submit function for nodequeue clear confirm
nodequeue_edit_queue_form Add or edit a queue.
nodequeue_edit_queue_form_delete_submit Delete-submit function for the nodequeue_queue form.
nodequeue_edit_queue_form_delete_validate Delete-validate function for the nodequeue_queue form.
nodequeue_edit_queue_form_submit Submit function for the nodequeue_queue form.
nodequeue_edit_queue_form_validate Validate function for the nodequeue_queue form.
nodequeue_node_tab Display the queue controls for a node.
nodequeue_save_subqueue_order Validates new subqueue order information and if it passes validation, deletes the old subqueue data from the database and saves the new data.
nodequeue_view_queues Display a list of queues and their status for the administrator.
nodequeue_view_subqueues Display a list of subqueues for a queue and their sizes
_nodequeue_autocomplete
_nodequeue_dragdrop_get_nodes Return a list of nodes in a specific subqueue.

File

sites/all/modules/local_contrib/nodequeue/includes/nodequeue.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the nodequeue module.
  5. */
  6. /**
  7. * Form builder for the nodequeue settings tab.
  8. */
  9. function nodequeue_admin_settings($form, &$form_state) {
  10. $form = array();
  11. $form['nodequeue_use_tab'] = array(
  12. '#type' => 'checkbox',
  13. '#title' => t('Create a menu tab for each node that could belong to any queues'),
  14. '#default_value' => variable_get('nodequeue_use_tab', 1),
  15. );
  16. $form['nodequeue_tab_name'] = array(
  17. '#type' => 'textfield',
  18. '#title' => t('Nodequeue tab label'),
  19. '#default_value' => variable_get('nodequeue_tab_name', t('Nodequeue')),
  20. '#description' => t('If nodes will have a menu tab for manipulating related nodequeues, what should that tab be called?'),
  21. );
  22. $form['nodequeue_autocomplete_limit'] = array(
  23. '#type' => 'textfield',
  24. '#title' => t('Nodequeue autocomplete limit'),
  25. '#size'=> 2,
  26. '#default_value' => variable_get('nodequeue_autocomplete_limit', 10),
  27. '#description' => t('Number of node titles to show in the autocomplete search results.'),
  28. );
  29. $form['nodequeue_view_per_queue'] = array(
  30. '#type' => 'checkbox',
  31. '#title' => t('Automatically create one view per queue'),
  32. '#description' => t("Nodequeue can create a view for each queue. \n
  33. If you wish to have fewer views, you can disable this option and use a single view with an argument for the qid."),
  34. '#default_value' => variable_get('nodequeue_view_per_queue', 1),
  35. );
  36. return system_settings_form($form);
  37. }
  38. /**
  39. * Page callback to add a node to a queue.
  40. */
  41. function nodequeue_admin_add_node($queue, $subqueue, $node) {
  42. if (!nodequeue_check_token($node->nid)) {
  43. return drupal_goto();
  44. }
  45. $id = nodequeue_get_content_id($queue, $node);
  46. nodequeue_subqueue_add($queue, $subqueue, $id);
  47. // Provide a response if this is javascript.
  48. if (!empty($_POST['js'])) {
  49. if (isset($_GET['tab'])) {
  50. nodequeue_js_output(t('Remove from queue'),
  51. url("nodequeue/$queue->qid/remove-node/$subqueue->sqid/$node->nid", array('query' => nodequeue_get_query_string($node->nid, TRUE, array('tab' => '1')))),
  52. nodequeue_subqueue_size_text($queue->size, $queue->size ? min($subqueue->count, $queue->size) : $subqueue->count, FALSE),
  53. $subqueue->sqid);
  54. }
  55. else {
  56. nodequeue_js_output(nodequeue_title_substitute($queue->link_remove, $queue, $subqueue),
  57. url("nodequeue/$queue->qid/remove-node/$subqueue->sqid/$node->nid", array('query' => nodequeue_get_query_string($node->nid, TRUE))));
  58. }
  59. }
  60. // There should always be a destination set for this, so just goto wherever.
  61. drupal_goto();
  62. }
  63. /**
  64. * Page callback to remove a node from a queue.
  65. */
  66. function nodequeue_admin_remove_node($queue, $subqueue, $node) {
  67. if (!nodequeue_check_token($node->nid)) {
  68. return drupal_goto();
  69. }
  70. $id = nodequeue_get_content_id($queue, $node);
  71. nodequeue_subqueue_remove_node($subqueue->sqid, $id);
  72. // Provide a response if this is javascript.
  73. if (!empty($_POST['js'])) {
  74. if (isset($_GET['tab'])) {
  75. nodequeue_js_output(t('Add to queue'),
  76. url("nodequeue/$queue->qid/add-node/$subqueue->sqid/$node->nid", array('query' => nodequeue_get_query_string($node->nid, TRUE, array('tab' => '1')))),
  77. nodequeue_subqueue_size_text($queue->size, $subqueue->count - 1, FALSE),
  78. $subqueue->sqid);
  79. }
  80. else {
  81. nodequeue_js_output(nodequeue_title_substitute($queue->link, $queue, $subqueue),
  82. url("nodequeue/$queue->qid/add-node/$subqueue->sqid/$node->nid", array('query' => nodequeue_get_query_string($node->nid, TRUE))));
  83. }
  84. }
  85. // There should always be a destination set for this, so just goto wherever.
  86. drupal_goto();
  87. }
  88. /**
  89. * Display the queue controls for a node.
  90. *
  91. * @param $node
  92. * The loaded $node; will be loaded by the hook_menu.
  93. */
  94. function nodequeue_node_tab($node) {
  95. $output = '';
  96. // moved from hook_menu due to architecture change. This function seems to only be called from menu anyway...
  97. $queues = nodequeue_load_queues_by_type($node->type, 'tab');
  98. if (!$queues) {
  99. return FALSE;
  100. }
  101. $header = array();
  102. $header[] = array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc', 'class' => array('nodequeue-title'));
  103. $header[] = array('data' => t('Max nodes'), 'field' => 'size', 'class' => array('nodequeue-max-nodes'));
  104. $header[] = array('data' => t('In queue'), 'field' => 'count', 'class' => array('nodequeue-in-queue'));
  105. $header[] = array('data' => t('Operation'), 'class' => array('nodequeue-operation'));
  106. $table_sort = tablesort_init($header);
  107. $subqueues = nodequeue_get_subqueues_by_node($queues, $node);
  108. nodequeue_set_subqueue_positions($subqueues, $node->nid);
  109. $sort_primary = array();
  110. $sort_secondary = array();
  111. $sort_direction_regular = array('asc' => SORT_ASC, 'desc' => SORT_DESC);
  112. $sort_direction_reverse = array('asc' => SORT_DESC, 'desc' => SORT_ASC);
  113. foreach ($subqueues as $subqueue) {
  114. $queue = $queues[$subqueue->qid];
  115. $sort_secondary[] = $queue->title;
  116. switch ($table_sort['sql']) {
  117. case 'title':
  118. default:
  119. $sort_primary[] = $queue->title;
  120. $sort_direction = $sort_direction_regular;
  121. break;
  122. case 'size':
  123. $sort_primary[] = $queue->size;
  124. $sort_direction = $sort_direction_reverse;
  125. break;
  126. case 'count':
  127. $sort_primary[] = $subqueue->count;
  128. $sort_direction = $sort_direction_reverse;
  129. break;
  130. }
  131. }
  132. if (!empty($table_sort)) {
  133. if (strtolower($table_sort['sort']) == 'desc') {
  134. array_multisort($sort_primary, $sort_direction['desc'], $sort_secondary, $subqueues); // Re-indexes array keys; key no longer equals qid.
  135. }
  136. else {
  137. array_multisort($sort_primary, $sort_direction['asc'], $sort_secondary, $subqueues); // Re-indexes array keys; key no longer equals qid.
  138. }
  139. }
  140. $rows = array();
  141. foreach ($subqueues as $subqueue) {
  142. $queue = $queues[$subqueue->qid];
  143. $id = nodequeue_get_content_id($queue, $node);
  144. if (module_exists('translation')) {
  145. $subqueue = array($subqueue->sqid => $subqueue);
  146. nodequeue_set_subqueue_positions($subqueue, $id);
  147. $subqueue = array_shift($subqueue);
  148. }
  149. if (empty($subqueue->position)) {
  150. $op = l(
  151. t('Add to queue'),
  152. "nodequeue/$queue->qid/add-node/$subqueue->sqid/$id",
  153. array('attributes' => array('class' => array('nodequeue-ajax-toggle')),
  154. 'query' => nodequeue_get_query_string($id, TRUE, array('tab' => '1')),
  155. 'purl' => array('disabled' => TRUE))
  156. );
  157. }
  158. else {
  159. $op = l(
  160. t('Remove from queue'),
  161. "nodequeue/$queue->qid/remove-node/$subqueue->sqid/$id",
  162. array('attributes' => array('class' => array('nodequeue-ajax-toggle')),
  163. 'query' => nodequeue_get_query_string($id, TRUE, array('tab' => '1')),
  164. 'purl' => array('disabled' => TRUE))
  165. );
  166. }
  167. $row = array();
  168. $row[] = array(
  169. 'class' => array('nodequeue-title'),
  170. 'data' => l(nodequeue_title_substitute($queue->subqueue_title, $queue, $subqueue), "admin/structure/nodequeue/$queue->qid/view/$subqueue->sqid"),
  171. );
  172. $row[] = array(
  173. 'class' => array('nodequeue-max-nodes'),
  174. 'data' => $queue->size ? $queue->size : t('Infinite')
  175. );
  176. $row[] = array(
  177. 'id' => 'nodequeue-count-' . $subqueue->sqid,
  178. 'class' => array('nodequeue-in-queue'),
  179. 'data' => nodequeue_subqueue_size_text($queue->size, $subqueue->count, FALSE)
  180. );
  181. $row[] = array('class' => array('nodequeue-operation'), 'data' => $op);
  182. $rows[] = $row;
  183. }
  184. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('nodequeue-table'))));
  185. drupal_add_js(drupal_get_path('module', 'nodequeue') . '/nodequeue.js');
  186. drupal_add_css(drupal_get_path('module', 'nodequeue') . '/nodequeue.css');
  187. return $output;
  188. }
  189. /**
  190. * Display a list of queues and their status for the administrator.
  191. */
  192. function nodequeue_view_queues() {
  193. $output = '';
  194. if (module_exists('advanced_help')) {
  195. $output .= theme('advanced_help_topic', array(
  196. 'module' => 'nodequeue',
  197. 'topic' => 'about',
  198. ));
  199. $output .= '&nbsp;' . theme('advanced_help_topic', array(
  200. 'module' => 'nodequeue',
  201. 'topic' => 'about',
  202. 'type' => t('Click here for information about this module'),
  203. ));
  204. $output = '<p>' . $output . '</p>';
  205. }
  206. // Fetch all of the queues.
  207. $queues = nodequeue_load_queues(nodequeue_get_all_qids(25));
  208. foreach ($queues as $queue) {
  209. if (!nodequeue_queue_access($queue)) {
  210. unset($queues[$queue->qid]);
  211. }
  212. }
  213. if (empty($queues)) {
  214. return $output . t('No nodequeues exist.');
  215. }
  216. $header = array(
  217. array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
  218. array('data' => t('Max nodes'), 'field' => 'size'),
  219. array('data' => t('Subqueues'), 'field' => 'subqueues'),
  220. array('data' => t('Operation')),
  221. );
  222. $table_sort = tablesort_init($header);
  223. $qids = array();
  224. $sort_primary = array();
  225. $sort_secondary = array();
  226. $sort_direction_regular = array('asc' => SORT_ASC, 'desc' => SORT_DESC);
  227. $sort_direction_reverse = array('asc' => SORT_DESC, 'desc' => SORT_ASC);
  228. foreach ($queues as $queue) {
  229. // If a queue has only one subqueue, store the qid so we can display
  230. // the number of nodes in the subqueue.
  231. if ($queue->subqueues == 1) {
  232. $qids[] = $queue->qid;
  233. }
  234. $sort_secondary[] = drupal_strtolower($queue->title);
  235. switch ($table_sort['sql']) {
  236. case 'title':
  237. default:
  238. $sort_primary[] = drupal_strtolower($queue->title);
  239. $sort_direction = $sort_direction_regular;
  240. break;
  241. case 'size':
  242. $sort_primary[] = $queue->size;
  243. $sort_direction = $sort_direction_reverse;
  244. break;
  245. case 'subqueues':
  246. $sort_primary[] = $queue->subqueues;
  247. $sort_direction = $sort_direction_regular;
  248. break;
  249. }
  250. }
  251. $subqueues = nodequeue_load_subqueues_by_queue($qids);
  252. // Relate all the subqueues we loaded back to our queues.
  253. foreach ($subqueues as $subqueue) {
  254. if (nodequeue_api_subqueue_access($subqueue, NULL, $queues[$subqueue->qid])) {
  255. $queues[$subqueue->qid]->subqueue = $subqueue;
  256. }
  257. }
  258. if (!empty($table_sort)) {
  259. if (strtolower($table_sort['sort']) == 'desc') {
  260. array_multisort($sort_primary, $sort_direction['desc'], $sort_secondary, $queues); // Re-indexes array keys; key no longer equals qid.
  261. }
  262. else {
  263. array_multisort($sort_primary, $sort_direction['asc'], $sort_secondary, $queues); // Re-indexes array keys; key no longer equals qid.
  264. }
  265. }
  266. $rows = array();
  267. foreach ($queues as $queue) {
  268. $operations = array();
  269. $sub_text = $queue->subqueues;
  270. // If this queue has only one subqueue.
  271. if ($sub_text == 1) {
  272. $sub_text .= " (" . nodequeue_subqueue_size_text($queue->size, $queue->subqueue->count) . ")";
  273. $operations[] = l(t('View'), "admin/structure/nodequeue/$queue->qid/view/" . $queue->subqueue->sqid);
  274. }
  275. else {
  276. $operations[] = l(t('View'), "admin/structure/nodequeue/$queue->qid/view");
  277. }
  278. if (user_access('administer nodequeue')) {
  279. $operations[] = l(t('Edit'), "admin/structure/nodequeue/$queue->qid/edit");
  280. $operations[] = l(t('Delete'), "admin/structure/nodequeue/$queue->qid/delete");
  281. }
  282. $rows[] = array(
  283. array('class' => array('nodequeue-title'), 'data' => check_plain($queue->title)),
  284. array('class' => array('nodequeue-max-nodes'), 'data' => $queue->size == 0 ? t('Infinite') : $queue->size),
  285. array('class' => array('nodequeue-subqueues'), 'data' => $sub_text),
  286. array('class' => array('nodequeue-operation'), 'data' => implode(' | ', $operations)),
  287. );
  288. }
  289. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  290. $output .= theme('pager', array('tags' => NULL));
  291. return $output;
  292. }
  293. /**
  294. * Display a list of subqueues for a queue and their sizes
  295. */
  296. function nodequeue_view_subqueues($queue) {
  297. // Fetch all of the subqueues.
  298. $subqueues = nodequeue_load_subqueues_by_queue($queue->qid);
  299. $header = array(t('Title'), t('In queue'), t('Operation'));
  300. $rows = array();
  301. foreach ($subqueues as $subqueue) {
  302. if (nodequeue_api_subqueue_access($subqueue, NULL, $queue)) {
  303. $sub_text = nodequeue_subqueue_size_text($queue->size, $subqueue->count, FALSE);
  304. $rows[] = array(
  305. array('class' => array('nodequeue-title'), 'data' => check_plain($subqueue->title)),
  306. array('class' => array('nodequeue-subqueues'), 'data' => $sub_text),
  307. array('class' => array('nodequeue-operation'), 'data' => l(t('View'), "admin/structure/nodequeue/$queue->qid/view/$subqueue->sqid"))
  308. );
  309. }
  310. }
  311. $output = '<p>' . t('Max nodes in queue: @size', array('@size' => $queue->size ? $queue->size : t("Infinite"))) . '</p>';
  312. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  313. $output .= theme('pager', array('tags' => NULL));
  314. return $output;
  315. }
  316. /**
  317. * Add or edit a queue.
  318. */
  319. function nodequeue_edit_queue_form($form, &$form_state, $queue) {
  320. $info = nodequeue_api_info();
  321. // For adding queues.
  322. if (is_string($queue)) {
  323. // If the $queue is a string - name of a queue type, basically - then we test that it's a valid queue type.
  324. $queue = strtolower($queue);
  325. if (!isset($info[$queue])) {
  326. return FALSE;
  327. }
  328. drupal_set_title(t('Add @type', array('@type' => $info[$queue]['title'])), PASS_THROUGH);
  329. $queue = new nodequeue_queue($queue);
  330. }
  331. else {
  332. drupal_set_title(t("Nodequeue '@title'", array('@title' => $queue->title)), PASS_THROUGH);
  333. }
  334. $form['description'] = array(
  335. '#type' => 'fieldset',
  336. '#title' => filter_xss($info[$queue->owner]['title']),
  337. '#description' => filter_xss($info[$queue->owner]['description']),
  338. );
  339. $form['title'] = array(
  340. '#type' => 'textfield',
  341. '#title' => t('Title'),
  342. '#default_value' => $queue->title,
  343. '#size' => 50,
  344. '#required' => TRUE,
  345. '#maxlength' => 64,
  346. '#description' => t('Enter the name of the queue'),
  347. );
  348. $form['name'] = array(
  349. '#type' => 'machine_name',
  350. '#maxlength' => 32,
  351. '#machine_name' => array(
  352. 'exists' => 'nodequeue_machine_name_exists',
  353. 'source' => array('title'),
  354. ),
  355. '#description' => t('A unique machine-readable name for this queue. It must only contain lowercase letters, numbers, and underscores.'),
  356. );
  357. if (!empty($queue->name)) {
  358. $form['name']['#default_value'] = $queue->name;
  359. $form['name']['#disabled'] = TRUE;
  360. $form['name']['#value'] = $queue->name;
  361. }
  362. // This is a value; as the default nodequeue implementation has just one
  363. // queue per nodequeue, this field is totally redundant. Plugins can
  364. // override this.
  365. $form['subqueue_title'] = array(
  366. '#type' => 'value',
  367. '#value' => $queue->subqueue_title,
  368. );
  369. // The placeholder is put here so that modifiers have an easy way to put
  370. // additional form widgets in a prominent spot but not before the title of
  371. // the queue.
  372. $form['placeholder'] = array();
  373. $form['size'] = array(
  374. '#type' => 'textfield',
  375. '#title' => t('Queue size'),
  376. '#default_value' => $queue->size,
  377. '#size' => 2,
  378. '#maxlength' => 2,
  379. '#description' => t('The maximum number of nodes will appear in the queue. Enter 0 for no limit'),
  380. );
  381. $form['reverse'] = array(
  382. '#type' => 'checkbox',
  383. '#title' => t('Reverse in admin view'),
  384. '#default_value' => $queue->reverse,
  385. '#description' => t('Ordinarily queues are arranged with the front of the queue (where items will be removed) on top and the back (where items will be added) on the bottom. If checked, this will display the queue such that items will be added to the top and removed from the bottom.'),
  386. );
  387. $form['link'] = array(
  388. '#type' => 'textfield',
  389. '#title' => t('Link "add to queue" text'),
  390. '#default_value' => $queue->link,
  391. '#size' => 40,
  392. '#maxlength' => 40,
  393. '#description' => t('If you want a link to add a node to a queue in the "links" section (next to "add new comment"), enter the text here. If left blank no link will be given; note that enabling this feature for any queue will cause an extra query to be run every time a node is loaded. "%subqueue" will be replaced with the subqueue title, if applicable.'),
  394. );
  395. $form['link_remove'] = array(
  396. '#type' => 'textfield',
  397. '#title' => t('Link "remove from queue" text'),
  398. '#default_value' => $queue->link_remove,
  399. '#size' => 40,
  400. '#maxlength' => 40,
  401. '#description' => t('Enter the text for the corresponding link to remove a node from a queue. This may be blank (in which case no link will appear) but a remove link will only appear if link, above, is set.'),
  402. );
  403. $manipulate_all_queues = user_roles(FALSE, 'manipulate all queues');
  404. $manipulate_queues = user_roles(FALSE, 'manipulate queues');
  405. $roles = $manipulate_all_queues + $manipulate_queues;
  406. if (!empty($roles)) {
  407. $form['roles'] = array(
  408. '#type' => 'checkboxes',
  409. '#title' => t('Roles'),
  410. '#default_value' => is_array($queue->roles) ? $queue->roles : array(),
  411. '#options' => $roles,
  412. '#description' => t('Check each role that can add nodes to the queue. Be sure that roles you want to appear here have "manipulate queues" access in the main access control panel.'),
  413. );
  414. foreach ($roles as $rid => $role) {
  415. $form['roles'][$rid] = array(
  416. '#type' => 'checkbox',
  417. '#title' => $role,
  418. '#default_value' => (isset($manipulate_all_queues[$rid]) || in_array($rid, $queue->roles)) ? TRUE : FALSE,
  419. '#disabled' => (isset($manipulate_all_queues[$rid])) ? TRUE : FALSE,
  420. );
  421. }
  422. }
  423. else {
  424. $form['roles'] = array(
  425. '#type' => 'value',
  426. '#value' => array(),
  427. );
  428. $form['roles_markup'] = array(
  429. '#type' => 'item',
  430. '#title' => t('Roles'),
  431. '#description' => '<strong>' . t('No roles have the "manipulate queues" permission, so only the administrator account will be able to add or remove items from this queue.') . '</strong>',
  432. );
  433. }
  434. $nodes = node_type_get_names();
  435. $form['types'] = array(
  436. '#type' => 'checkboxes',
  437. '#title' => t('Types'),
  438. '#default_value' => $queue->types,
  439. '#options' => $nodes,
  440. '#description' => t('Check each node type that can be added to this queue.'),
  441. );
  442. $form['i18n'] = array(
  443. '#type' => 'radios',
  444. '#title' => t('Internationalization'),
  445. '#options' => array(
  446. '1' => t('Treat translation nodes as a single node'),
  447. '0' => t('Manually manage translated nodes'),
  448. ),
  449. '#default_value' => empty($queue->qid) && module_exists('translation') ? 0 : $queue->i18n,
  450. '#description' => t('Treating translations as a single node allows users to add, remove and manipulate a node
  451. in the queue regardless of which translation is acted upon; nodequeue will only act on the original node.
  452. When manually managing translation nodes, Nodequeue will ignore the relationship between node translations;
  453. each translation node must be added to the queue separately and will occupy a separate spot in the queue.
  454. Changing this setting will <strong>not</strong> update content that is already in the queue.'),
  455. '#access' => module_exists('translation'),
  456. );
  457. $form['submit'] = array(
  458. '#type' => 'submit',
  459. '#value' => t('Submit'),
  460. );
  461. $form['owner'] = array(
  462. '#type' => 'value',
  463. '#value' => $queue->owner,
  464. );
  465. $form['show_in_links'] = array(
  466. '#type' => 'value',
  467. '#value' => $queue->show_in_links,
  468. );
  469. $form['show_in_tab'] = array(
  470. '#type' => 'value',
  471. '#value' => $queue->show_in_tab,
  472. );
  473. $form['show_in_ui'] = array(
  474. '#type' => 'value',
  475. '#value' => $queue->show_in_ui,
  476. );
  477. $form['reference'] = array(
  478. '#type' => 'value',
  479. '#value' => $queue->reference,
  480. );
  481. $form['subqueues'] = array(
  482. '#type' => 'value',
  483. '#value' => $queue->subqueues,
  484. );
  485. if (isset($queue->qid)) {
  486. $form[] = array(
  487. '#type' => 'submit',
  488. '#value' => t('Delete'),
  489. '#validate' => array('nodequeue_edit_queue_form_delete_validate'),
  490. '#submit' => array('nodequeue_edit_queue_form_delete_submit'),
  491. );
  492. $form['qid'] = array(
  493. '#type' => 'value',
  494. '#value' => $queue->qid,
  495. );
  496. $form['count'] = array(
  497. '#type' => 'value',
  498. '#value' => $queue->count,
  499. );
  500. }
  501. nodequeue_api_queue_form($queue, $form);
  502. return $form;
  503. }
  504. /**
  505. * Validate function for the nodequeue_queue form.
  506. */
  507. function nodequeue_edit_queue_form_validate($form, &$form_state) {
  508. if (empty($form_state['values']['title'])) {
  509. form_set_error('title', t('Please enter a title for this queue.'));
  510. }
  511. $queue = (object) $form_state['values'];
  512. // Fix checkboxes.
  513. $queue->roles = array_keys(array_filter($queue->roles));
  514. $queue->types = array_keys(array_filter($queue->types));
  515. if (!isset($queue->qid)) {
  516. $queue->new = TRUE;
  517. }
  518. nodequeue_api_queue_form_validate($queue, $form_state, $form);
  519. }
  520. /**
  521. * Submit function for the nodequeue_queue form.
  522. */
  523. function nodequeue_edit_queue_form_submit($formid, &$form_state) {
  524. $queue = (object) $form_state['values'];
  525. // Fix checkboxes.
  526. $queue->roles = array_keys(array_filter($queue->roles));
  527. $queue->types = array_keys(array_filter($queue->types));
  528. // Remove roles that have the 'manipulate all queues' permission.
  529. $manipulate_all_queues = array_keys(user_roles(FALSE, 'manipulate all queues'));
  530. $queue->roles = array_diff($queue->roles, $manipulate_all_queues);
  531. if (!isset($queue->qid)) {
  532. $queue->new = TRUE;
  533. }
  534. // Modify show_in_links based on whether or not links are available.
  535. $queue->show_in_links = !empty($queue->link) || !empty($queue->link_remove);
  536. nodequeue_api_queue_form_submit($queue, $form_state);
  537. $qid = nodequeue_save($queue); // sets $queue->qid if needed.
  538. nodequeue_api_queue_form_submit_finish($queue, $form_state);
  539. nodequeue_check_subqueue_sizes($queue);
  540. if (!empty($queue->new)) {
  541. $form_state['values']['qid'] = $qid;
  542. drupal_set_message(t('The queue has been created.'));
  543. }
  544. else {
  545. drupal_set_message(t('The queue has been updated.'));
  546. }
  547. $form_state['redirect'] = 'admin/structure/nodequeue';
  548. }
  549. /**
  550. * Delete-validate function for the nodequeue_queue form.
  551. */
  552. function nodequeue_edit_queue_form_delete_validate($form, &$form_state) {
  553. // No validation for delete step! But we need to have this so the default validation isn't called.
  554. }
  555. /**
  556. * Delete-submit function for the nodequeue_queue form.
  557. */
  558. function nodequeue_edit_queue_form_delete_submit($formid, &$form_state) {
  559. $form_state['redirect'] = "admin/structure/nodequeue/" . $form_state['values']['qid'] . "/delete";
  560. }
  561. /**
  562. * Confirm form to delete a queue
  563. */
  564. function nodequeue_admin_delete($form, &$form_state, $queue) {
  565. $form['qid'] = array('#type' => 'value', '#value' => $queue->qid);
  566. return confirm_form($form,
  567. t('Are you sure you want to delete "%title"?', array('%title' => $queue->title)),
  568. isset($_GET['destination']) ? $_GET['destination'] : 'admin/structure/nodequeue',
  569. t('This action cannot be undone.'),
  570. t('Delete'), t('Cancel')
  571. );
  572. }
  573. /**
  574. * Submit function for nodequeue delete
  575. */
  576. function nodequeue_admin_delete_submit($formid, &$form_state) {
  577. if ($form_state['values']['confirm']) {
  578. nodequeue_delete($form_state['values']['qid']);
  579. drupal_set_message(t('The queue has been deleted.'));
  580. }
  581. $form_state['redirect'] = 'admin/structure/nodequeue';
  582. }
  583. /**
  584. * Page callback to view a queue.
  585. */
  586. function nodequeue_admin_view($queue, $subqueue = array()) {
  587. drupal_set_title(t("Nodequeue '@title'", array('@title' => $queue->title)), PASS_THROUGH);
  588. $qid = $queue->qid;
  589. // If the queue has just one subqueue, it gets special treatment.
  590. if (empty($subqueue->sqid)) {
  591. if ($queue->subqueues == 1) {
  592. $subqueues = nodequeue_load_subqueues_by_queue($queue->qid);
  593. $subqueue = array_shift($subqueues);
  594. }
  595. else {
  596. // display subqueue list page.
  597. return nodequeue_view_subqueues($queue);
  598. }
  599. }
  600. elseif ($subqueue->sqid) {
  601. if (!nodequeue_api_subqueue_access($subqueue, NULL, $queue)) {
  602. return drupal_not_found();
  603. }
  604. // Adjust properties of the page so our subqueue is in the right
  605. // visual place.
  606. drupal_set_title(t("Subqueue '@title'",
  607. array('@title' => nodequeue_title_substitute($queue->subqueue_title, $queue, $subqueue))), PASS_THROUGH);
  608. $breadcrumb = drupal_get_breadcrumb();
  609. $breadcrumb[] = l($queue->title, "admin/structure/nodequeue/$queue->qid");
  610. drupal_set_breadcrumb($breadcrumb);
  611. }
  612. return nodequeue_arrange_subqueue($queue, $subqueue);
  613. }
  614. /**
  615. * View the contents of a subqueue, with links to re-order the queue.
  616. */
  617. function nodequeue_arrange_subqueue($queue, $subqueue = NULL) {
  618. // set title and load subqueue if it's not provided
  619. drupal_set_title(t("Nodequeue '@title'", array('@title' => $queue->title)), PASS_THROUGH);
  620. if (!$subqueue->sqid) {
  621. if ($queue->subqueues == 1) {
  622. $subqueues = nodequeue_load_subqueues_by_queue($queue->qid);
  623. $subqueue = array_shift($subqueues);
  624. }
  625. else {
  626. return drupal_not_found();
  627. }
  628. }
  629. elseif ($subqueue->sqid) {
  630. if (!nodequeue_api_subqueue_access($subqueue, NULL, $queue)) {
  631. return drupal_not_found();
  632. }
  633. drupal_set_title(t("Subqueue '@title'", array('@title' => nodequeue_title_substitute($queue->subqueue_title, $queue, $subqueue))), PASS_THROUGH);
  634. }
  635. // get nodes from the queue
  636. $nodes = _nodequeue_dragdrop_get_nodes($queue, $subqueue);
  637. return drupal_get_form('nodequeue_arrange_subqueue_form_' . $subqueue->sqid, $queue, $nodes, $subqueue);
  638. }
  639. /**
  640. * Return a list of nodes in a specific subqueue.
  641. */
  642. function _nodequeue_dragdrop_get_nodes($queue, $subqueue) {
  643. $order = $queue->reverse ? 'DESC' : 'ASC';
  644. $visible = nodequeue_nids_visible($subqueue->sqid);
  645. // Get a list of all nodes in the subqueue, regardless of access restrictions.
  646. $query = db_select('node', 'n')
  647. ->distinct();
  648. $query->leftJoin('nodequeue_nodes', 'nq', 'nq.nid = n.nid');
  649. $query->fields('n', array('nid'))
  650. ->fields('nq', array('position'))
  651. ->condition('nq.sqid', $subqueue->sqid)
  652. ->orderBy('nq.position', $order);
  653. $result = $query->execute();
  654. $nids = array();
  655. $sq_nodes = array();
  656. foreach ($result as $sq_node) {
  657. $nids[] = $sq_node->nid;
  658. // Save node position in a separate array so we can reference it later.
  659. $sq_nodes[$sq_node->nid]['position'] = $sq_node->position;
  660. }
  661. $nodes = node_load_multiple($nids);
  662. foreach ($nodes as $node) {
  663. $node->visible = isset($visible[$node->nid]) ? TRUE : FALSE;
  664. $node->position = $sq_nodes[$node->nid]['position'];
  665. }
  666. return $nodes;
  667. }
  668. /**
  669. * Form definition for nodequeue drag'n'drop form.
  670. */
  671. function nodequeue_arrange_subqueue_form($form, $form_state, $queue, $nodes, $subqueue) {
  672. $form = array('#tree' => TRUE);
  673. // Prepare the main part of the form which will be themed as a table.
  674. $count = count($nodes);
  675. $form['nodes'] = array();
  676. $form['nodes']['#theme'] = 'nodequeue_arrange_subqueue_form_table';
  677. // Theme function needs these.
  678. $form['nodes']['#queue'] = (array) $queue;
  679. $form['nodes']['#subqueue'] = (array) $subqueue;
  680. foreach ($nodes as $node) {
  681. $form['nodes'][$node->nid]['#node'] = (array) $node;
  682. if ($node->visible) {
  683. $form['nodes'][$node->nid]['#node'] = (array) $node;
  684. $form['nodes'][$node->nid]['title'] = array('#markup' => l($node->title, 'node/' . $node->nid));
  685. $form['nodes'][$node->nid]['author'] = array('#markup' => theme('username', array('account' => $node)));
  686. $form['nodes'][$node->nid]['date'] = array('#markup' => format_date($node->created, 'short'));
  687. }
  688. else {
  689. $form['nodes'][$node->nid]['title'] = array('#value' => t('Restricted node, NID: @nid', array('@nid' => $node->nid)));
  690. $form['nodes'][$node->nid]['author'] = array('#value' => '');
  691. $form['nodes'][$node->nid]['date'] = array('#value' => '');
  692. }
  693. if (node_access('update', $node)) {
  694. $form['nodes'][$node->nid]['edit'] = array('#markup' => l(t('edit'), 'node/' . $node->nid . '/edit', array('attributes' => array('title' => t('Edit this node')))));
  695. }
  696. $form['nodes'][$node->nid]['position'] = array(
  697. '#type' => 'position',
  698. '#delta' => $count,
  699. '#default_value' => $node->position,
  700. '#attributes' => array(
  701. 'class' => array('node-position'),
  702. ),
  703. );
  704. $attr = array(
  705. '#attributes' => array(
  706. 'title' => t('Remove from queue'),
  707. 'style' => 'display: none;',
  708. 'class' => array('nodequeue-remove'),
  709. 'id' => 'nodequeue-remove-' . $node->nid,
  710. ),
  711. 'query' => nodequeue_get_query_string($node->nid, TRUE),
  712. );
  713. $form['nodes'][$node->nid]['remove'] = array('#markup' => l(t('remove'), 'nodequeue/' . $queue->qid . '/remove-node/' . $subqueue->sqid . '/' . $node->nid, $attr));
  714. }
  715. // Textfield for adding nodes to the queue.
  716. $form['add'] = array(
  717. '#type' => 'container',
  718. '#attributes' => array('class' => array('container-inline')),
  719. );
  720. $form['add']['nid'] = array(
  721. '#type' => 'textfield',
  722. '#autocomplete_path' => 'nodequeue/autocomplete/' . $subqueue->sqid,
  723. '#maxlength' => 1024,
  724. '#default_value' => t('Enter the title of a node to add it to the queue'),
  725. '#attributes' => array('class' => array('subqueue-add-nid')),
  726. );
  727. $form['add']['submit'] = array(
  728. '#type' => 'submit',
  729. '#value' => t('Add content'),
  730. '#submit' => array('nodequeue_arrange_subqueue_form_add_submit'),
  731. );
  732. // Submit, reverse, shuffle, and clear actions.
  733. $form['actions'] = array('#type' => 'actions');
  734. $form['actions']['submit'] = array(
  735. '#type' => 'submit',
  736. '#value' => t('Save'),
  737. '#submit' => array('nodequeue_arrange_subqueue_form_submit'),
  738. );
  739. $form['actions']['reverse'] = array(
  740. '#type' => 'submit',
  741. '#value' => t('Reverse'),
  742. '#submit' => array('nodequeue_arrange_subqueue_form_reverse_submit'),
  743. );
  744. $form['actions']['shuffle'] = array(
  745. '#type' => 'submit',
  746. '#value' => t('Shuffle'),
  747. '#submit' => array('nodequeue_arrange_subqueue_form_shuffle_submit'),
  748. );
  749. $form['actions']['clear'] = array(
  750. '#type' => 'submit',
  751. '#value' => t('Clear'),
  752. '#submit' => array('nodequeue_arrange_subqueue_form_clear_submit'),
  753. );
  754. // Disable all buttons if the queue is empty.
  755. if ($count == 0) {
  756. $form['actions']['submit']['#disabled'] = TRUE;
  757. $form['actions']['reverse']['#disabled'] = TRUE;
  758. $form['actions']['shuffle']['#disabled'] = TRUE;
  759. $form['actions']['clear']['#disabled'] = TRUE;
  760. }
  761. return $form;
  762. }
  763. /**
  764. * Validate handler for nodequeue_arrange_subqueue_form.
  765. */
  766. function nodequeue_arrange_subqueue_form_validate($form, &$form_state) {
  767. $positions = array();
  768. foreach ($form_state['values'] as $nid => $element) {
  769. if (is_numeric($nid) && is_numeric($element['position'])) {
  770. $positions[$nid] = $element['position'];
  771. }
  772. }
  773. if (count(array_unique($positions)) < count($positions)) {
  774. $seen = array();
  775. foreach ($positions as $nid => $position) {
  776. if (isset($seen[$position])) {
  777. form_set_error($nid . '][position', t('Duplicate position value.'));
  778. }
  779. $seen[$position] = TRUE;
  780. }
  781. }
  782. }
  783. /**
  784. * Submit function for nodequeue_arrange_subqueue_form on 'Reverse' button.
  785. *
  786. * Yeah, this just calls the below function with a different parameter, but in D6 we're not supposed to use the $form['ops'].
  787. */
  788. function nodequeue_arrange_subqueue_form_reverse_submit($form, &$form_state) {
  789. nodequeue_arrange_subqueue_form_submit($form, $form_state, TRUE, FALSE);
  790. }
  791. function nodequeue_arrange_subqueue_form_shuffle_submit($form, &$form_state) {
  792. nodequeue_arrange_subqueue_form_submit($form, $form_state, FALSE, TRUE);
  793. }
  794. /**
  795. * Submit handler for nodequeue drag'n'drop form. Updates node positions in {nodequeue_nodes}.
  796. */
  797. function nodequeue_arrange_subqueue_form_submit($form, &$form_state, $reverse = FALSE, $shuffle = FALSE) {
  798. $nodes = array();
  799. foreach ($form_state['values']['nodes'] as $nid => $element) {
  800. if (is_numeric($nid)) {
  801. $nodes[$form_state['values']['nodes'][$nid]['position']] = array(
  802. 'nid' => $nid,
  803. 'data' => $form_state['values']['nodes'][$nid]
  804. );
  805. }
  806. }
  807. $message = 'The queue has been updated.';
  808. if ($reverse || $shuffle) {
  809. $keys = array_keys($nodes);
  810. $values = array_values($nodes);
  811. // reverse the list if the reverse button was pressed
  812. if ($reverse) {
  813. $values = array_reverse($values);
  814. $message = 'The queue has been reversed.';
  815. }
  816. // shuffle the list if the shuffle button was pressed.
  817. if ($shuffle) {
  818. shuffle($values);
  819. $message = 'The queue has been shuffled.';
  820. }
  821. $nodes = array_combine($keys, $values);
  822. }
  823. $qid = $form['nodes']['#queue']['qid'];
  824. $sqid = $form['nodes']['#subqueue']['sqid'];
  825. nodequeue_save_subqueue_order($nodes, $qid, $sqid);
  826. drupal_set_message(t('@message', array('@message' => $message)));
  827. }
  828. /**
  829. * Validates new subqueue order information and if it passes validation,
  830. * deletes the old subqueue data from the database and saves the new data.
  831. *
  832. * @param $nodes:
  833. * an array of nodes, keyed on the subqueue position.
  834. * @param $qid
  835. * the queue id
  836. * @param unknown_type $sqid
  837. * the subqueue id
  838. * @return An array where the first element is a numeric status code
  839. * (0 means successfully saved) and the second element is a status message.
  840. */
  841. function nodequeue_save_subqueue_order($nodes, $qid, $sqid) {
  842. $positions = array();
  843. $now = REQUEST_TIME;
  844. $queue = nodequeue_load($qid);
  845. $subqueue = nodequeue_load_subqueue($sqid);
  846. // cleanup the node array
  847. $clean = array();
  848. $count = 1;
  849. ksort($nodes);
  850. drupal_alter('nodequeue_sort', $nodes, $sqid);
  851. foreach ($nodes as $pos => $node) {
  852. if (!is_numeric($node['nid']) || $node['nid'] < 1) {
  853. return array(NODEQUEUE_INVALID_NID, 'Invalid nid value. New subqueue order not saved.');
  854. }
  855. if (is_numeric($pos)) {
  856. $clean[$count] = $node;
  857. $count++;
  858. }
  859. elseif ($pos == 'r') {
  860. // Remove the node from this subqueue.
  861. nodequeue_subqueue_remove_node($sqid, $node['nid']);
  862. }
  863. else {
  864. return array(NODEQUEUE_INVALID_POSITION, 'Invalid position value. New subqueue order not saved.');
  865. }
  866. }
  867. $nodes = $clean;
  868. array_walk($clean, create_function('&$node', '$node = serialize($node);'));
  869. if (count(array_unique($clean)) < count($nodes)) {
  870. return array(NODEQUEUE_DUPLICATE_POSITION, 'Duplicate position values are not allowed. New subqueue order not saved.');
  871. }
  872. // Allow other modules to alter the order of nodes.
  873. foreach (module_implements('nodequeue_save_subqueue_order_alter') as $module) {
  874. $function = $module . '_nodequeue_save_subqueue_order_alter';
  875. $function($sqid, $nodes);
  876. }
  877. // clear the queue and save the new positions
  878. db_delete('nodequeue_nodes')
  879. ->condition('sqid', $sqid)
  880. ->execute();
  881. foreach ($nodes as $pos => $node) {
  882. $args = array();
  883. if ($pos != 'r') {
  884. $query = db_insert('nodequeue_nodes')
  885. ->fields(array(
  886. 'sqid' => $sqid,
  887. 'qid' => $qid,
  888. 'nid' => $node['nid'],
  889. 'position' => $pos,
  890. 'timestamp' => $now,
  891. ))
  892. ->execute();
  893. }
  894. }
  895. if ($queue->size) {
  896. // only necessary if the subqueue is of finite length
  897. nodequeue_check_subqueue_size($queue, $subqueue);
  898. }
  899. return array(NODEQUEUE_OK, 'The queue has been updated.');
  900. }
  901. function nodequeue_arrange_subqueue_form_clear_submit($form, &$form_state) {
  902. $form_state['redirect'] = 'admin/structure/nodequeue/' . $form['nodes']['#queue']['qid'] . '/clear/' . $form['#subqueue']['sqid'];
  903. }
  904. function nodequeue_arrange_subqueue_form_add_submit($form, &$form_state) {
  905. $queue = nodequeue_load($form['nodes']['#queue']['qid']);
  906. $subqueue = nodequeue_load_subqueue($form['nodes']['#subqueue']['sqid']);
  907. if (!empty($form_state['values']['add']['nid'])) {
  908. preg_match('/\[nid: (\d+)\]$/', $form_state['values']['add']['nid'], $matches);
  909. $nid = $matches[1];
  910. if (empty($nid)) {
  911. form_set_error('', t('Please enter a valid node title.'));
  912. }
  913. }
  914. nodequeue_subqueue_add($queue, $subqueue, $nid);
  915. }
  916. /**
  917. * Page callback to remove an item from a queue. This will be used only
  918. * if javascript is disabled in the client, and is a fallback technique.
  919. * This differs from nodequeue_admin_remove_node in that it removes a
  920. * specific position, which is necessary in case a node is in a queue
  921. * multiple times.
  922. */
  923. function nodequeue_admin_remove($queue, $subqueue, $pos) {
  924. if (!is_numeric($pos) || !is_object($subqueue) || !nodequeue_check_token($pos)) {
  925. return drupal_goto();
  926. }
  927. nodequeue_subqueue_remove($subqueue, $pos);
  928. drupal_goto();
  929. }
  930. /**
  931. * Confirm form to clear a queue.
  932. */
  933. function nodequeue_clear_confirm($form, &$form_state, $queue, $subqueue) {
  934. if (empty($subqueue)) {
  935. return;
  936. }
  937. drupal_set_title(t("Nodequeue '@title'", array('@title' => $queue->title)), PASS_THROUGH);
  938. $form['qid'] = array('#type' => 'value', '#value' => $queue->qid);
  939. $form['sqid'] = array('#type' => 'value', '#value' => $subqueue->sqid);
  940. return confirm_form($form,
  941. t('Are you sure you want to clear the nodequeue %queue?', array('%queue' => nodequeue_title_substitute($queue->subqueue_title, $queue, $subqueue))),
  942. isset($_GET['destination']) ? $_GET['destination'] : 'admin/structure/nodequeue/' . $queue->qid . '/view/' . $subqueue->sqid,
  943. t('This action will remove all nodes from the queue and cannot be undone.'),
  944. t('Clear'), t('Cancel')
  945. );
  946. }
  947. /**
  948. * Submit function for nodequeue clear confirm
  949. */
  950. function nodequeue_clear_confirm_submit($form, &$form_state) {
  951. if ($form_state['values']['confirm']) {
  952. nodequeue_queue_clear($form_state['values']['sqid']);
  953. drupal_set_message(t('The queue has been cleared.'));
  954. $form_state['redirect'] = 'admin/structure/nodequeue/' . $form_state['values']['qid'] . '/view/' . $form_state['values']['sqid'];
  955. }
  956. }
  957. /**
  958. * Page callback for autocomplete.
  959. */
  960. function nodequeue_autocomplete() {
  961. $args = func_get_args();
  962. $sqid = array_shift($args);
  963. $string = implode('/', $args);
  964. $matches = _nodequeue_autocomplete($sqid, $string);
  965. drupal_json_output(drupal_map_assoc($matches));
  966. }
  967. function _nodequeue_autocomplete($sqid, $string) {
  968. $output = array();
  969. if (!is_numeric($sqid) || !$string) {
  970. return $output;
  971. }
  972. $subqueue = nodequeue_load_subqueue($sqid);
  973. if (!$subqueue) {
  974. return $output;
  975. }
  976. $queue = nodequeue_load($subqueue->qid);
  977. if (!$queue) {
  978. return $output;
  979. }
  980. $nodes = nodequeue_api_autocomplete($queue, $subqueue, $string);
  981. return $nodes;
  982. }