node_type.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based upon node type.
  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. $plugin = array(
  11. 'title' => t("Node: type"),
  12. 'description' => t('Control access by node_type.'),
  13. 'callback' => 'ctools_node_type_ctools_access_check',
  14. 'default' => array('type' => array()),
  15. 'settings form' => 'ctools_node_type_ctools_access_settings',
  16. 'settings form submit' => 'ctools_node_type_ctools_access_settings_submit',
  17. 'summary' => 'ctools_node_type_ctools_access_summary',
  18. 'required context' => new ctools_context_required(t('Node'), 'node'),
  19. 'restrictions' => 'ctools_node_type_ctools_access_restrictions',
  20. );
  21. /**
  22. * Settings form for the 'by node_type' access plugin
  23. */
  24. function ctools_node_type_ctools_access_settings($form, &$form_state, $conf) {
  25. $types = node_type_get_types();
  26. foreach ($types as $type => $info) {
  27. $options[$type] = check_plain($info->name);
  28. }
  29. $form['settings']['type'] = array(
  30. '#title' => t('Node type'),
  31. '#type' => 'checkboxes',
  32. '#options' => $options,
  33. '#description' => t('Only the checked node types will be valid.'),
  34. '#default_value' => $conf['type'],
  35. );
  36. return $form;
  37. }
  38. /**
  39. * Compress the node_types allowed to the minimum.
  40. */
  41. function ctools_node_type_ctools_access_settings_submit($form, &$form_state) {
  42. $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
  43. }
  44. /**
  45. * Check for access.
  46. */
  47. function ctools_node_type_ctools_access_check($conf, $context) {
  48. // As far as I know there should always be a context at this point, but this
  49. // is safe.
  50. if (empty($context) || empty($context->data) || empty($context->data->type)) {
  51. return FALSE;
  52. }
  53. if (array_filter($conf['type']) && empty($conf['type'][$context->data->type])) {
  54. return FALSE;
  55. }
  56. return TRUE;
  57. }
  58. /**
  59. * Inform the UI that we've eliminated a bunch of possibilities for this
  60. * context.
  61. */
  62. function ctools_node_type_ctools_access_restrictions($conf, &$context) {
  63. if (isset($context->restrictions['type'])) {
  64. $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
  65. }
  66. else {
  67. $context->restrictions['type'] = array_keys(array_filter($conf['type']));
  68. }
  69. }
  70. /**
  71. * Provide a summary description based upon the checked node_types.
  72. */
  73. function ctools_node_type_ctools_access_summary($conf, $context) {
  74. if (!isset($conf['type'])) {
  75. $conf['type'] = array();
  76. }
  77. $types = node_type_get_types();
  78. $names = array();
  79. // If a node type doesn't exist, let the user know, but prevent a notice.
  80. $missing_types = array();
  81. foreach (array_filter($conf['type']) as $type) {
  82. if (!empty($types[$type])) {
  83. $names[] = check_plain($types[$type]->name);
  84. }
  85. else {
  86. $missing_types[] = check_plain($type);
  87. }
  88. }
  89. if (empty($names) && empty($missing_types)) {
  90. return t('@identifier is any node type', array('@identifier' => $context->identifier));
  91. }
  92. if (!empty($missing_types)) {
  93. $output = array();
  94. if (!empty($names)) {
  95. $output[] = format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
  96. }
  97. $output[] = format_plural(count($missing_types), 'Missing/ deleted type "@types"', 'Missing/ deleted type is one of "@types"', array('@types' => implode(', ', $missing_types)));
  98. return implode(' | ', $output);
  99. }
  100. return format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
  101. }