book.inc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based on whether a node belongs to a book.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. if (module_exists('book')) {
  11. $plugin = array(
  12. 'title' => t("Book: node is in a book"),
  13. 'description' => t('Control access based upon a node belonging to a book.'),
  14. 'callback' => 'ctools_book_node_ctools_access_check',
  15. 'default' => array('book' => array()),
  16. 'settings form' => 'ctools_book_node_ctools_access_settings',
  17. 'settings form submit' => 'ctools_book_node_ctools_access_settings_submit',
  18. 'summary' => 'ctools_book_node_ctools_access_summary',
  19. 'required context' => new ctools_context_required(t('Node'), 'node'),
  20. );
  21. }
  22. /**
  23. * Settings form for the 'by book_node' access plugin.
  24. */
  25. function ctools_book_node_ctools_access_settings($form, &$form_state, $conf) {
  26. $options = array(
  27. 'any' => t('In any book'),
  28. );
  29. $books = book_get_books();
  30. foreach ($books as $bid => $book) {
  31. $options[$bid] = $book['title'];
  32. }
  33. $form['settings']['book'] = array(
  34. '#title' => t('Book'),
  35. '#type' => 'checkboxes',
  36. '#options' => $options,
  37. '#description' => t('Pass only if the node belongs to one of the selected books'),
  38. '#default_value' => $conf['book'],
  39. '#required' => TRUE,
  40. );
  41. return $form;
  42. }
  43. /**
  44. * Check for access.
  45. */
  46. function ctools_book_node_ctools_access_check($conf, $context) {
  47. // As far as I know there should always be a context at this point, but this
  48. // is safe.
  49. if (empty($context) || empty($context->data) || empty($context->data->book)) {
  50. return FALSE;
  51. }
  52. if ($conf['book']['any']) {
  53. return !empty($context->data->book);
  54. }
  55. foreach ($conf['book'] as $bid => $value) {
  56. if ($bid == 'any') {
  57. continue;
  58. }
  59. if ($value && ($bid == $context->data->book['bid'])) {
  60. return TRUE;
  61. }
  62. }
  63. return FALSE;
  64. }
  65. /**
  66. * Provide a summary description based upon the checked node_languages.
  67. */
  68. function ctools_book_node_ctools_access_summary($conf, $context) {
  69. if ($conf['book']['any']) {
  70. return t('@identifier belongs to a book', array('@identifier' => $context->identifier));
  71. }
  72. $books = array();
  73. foreach ($conf['book'] as $bid => $value) {
  74. if ($value) {
  75. $node = node_load($bid);
  76. $books[] = $node->title;
  77. }
  78. }
  79. if (count($books) == 1) {
  80. return t('@identifier belongs to the book "@book"', array('@book' => $books[0], '@identifier' => $context->identifier));
  81. }
  82. return t('@identifier belongs in multiple books', array('@identifier' => $context->identifier));
  83. }