views_plugin_argument_validate_node.inc

  1. 3.x modules/node/views_plugin_argument_validate_node.inc
  2. 2.x modules/node/views_plugin_argument_validate_node.inc

Contains the 'node' argument validator plugin.

File

modules/node/views_plugin_argument_validate_node.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the 'node' argument validator plugin.
  5. */
  6. /**
  7. * Validate whether an argument is an acceptable node.
  8. */
  9. class views_plugin_argument_validate_node extends views_plugin_argument_validate {
  10. var $option_name = 'validate_argument_node_type';
  11. function validate_form(&$form, &$form_state) {
  12. $types = node_get_types();
  13. foreach ($types as $type => $info) {
  14. $options[$type] = check_plain(t($info->name));
  15. }
  16. $arg = $this->get_argument();
  17. if (empty($arg)) {
  18. $arg = array();
  19. }
  20. $form[$this->option_name] = array(
  21. '#type' => 'checkboxes',
  22. '#prefix' => '<div id="edit-options-validate-argument-node-type-wrapper">',
  23. '#suffix' => '</div>',
  24. '#title' => t('Types'),
  25. '#options' => $options,
  26. '#default_value' => $arg,
  27. '#description' => t('If you wish to validate for specific node types, check them; if none are checked, all nodes will pass.'),
  28. '#process' => array('expand_checkboxes', 'views_process_dependency'),
  29. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  30. );
  31. $form['validate_argument_node_access'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Validate user has access to the node'),
  34. '#default_value' => !empty($this->argument->options['validate_argument_node_access']),
  35. '#process' => array('views_process_dependency'),
  36. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  37. );
  38. $form['validate_argument_nid_type'] = array(
  39. '#type' => 'select',
  40. '#title' => t('Argument type'),
  41. '#options' => array(
  42. 'nid' => t('Node ID'),
  43. 'nids' => t('Node IDs separated by , or +'),
  44. ),
  45. '#default_value' => isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : 'nid',
  46. '#process' => array('views_process_dependency'),
  47. '#dependency' => array('edit-options-validate-type' => array($this->id)),
  48. );
  49. }
  50. function validate_argument($argument) {
  51. $types = array_filter($this->argument->options[$this->option_name]);
  52. $type = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : 'nid';
  53. switch ($type) {
  54. case 'nid':
  55. if (!is_numeric($argument)) {
  56. return FALSE;
  57. }
  58. $node = node_load($argument);
  59. if (!$node) {
  60. return FALSE;
  61. }
  62. if (!empty($this->argument->options['validate_argument_node_access'])) {
  63. if (!node_access('view', $node)) {
  64. return FALSE;
  65. }
  66. }
  67. // Save the title() handlers some work.
  68. $this->argument->validated_title = check_plain($node->title);
  69. if (empty($types)) {
  70. return TRUE;
  71. }
  72. return isset($types[$node->type]);
  73. break;
  74. case 'nids':
  75. $nids = new stdClass();
  76. $nids->value = array($argument);
  77. $nids = views_break_phrase($argument, $nids);
  78. if ($nids->value == -1) {
  79. return FALSE;
  80. }
  81. $placeholders = implode(', ', array_fill(0, sizeof($nids->value), '%d'));
  82. $test = drupal_map_assoc($nids->value);
  83. $titles = array();
  84. $result = db_query("SELECT * FROM {node} WHERE nid IN ($placeholders)", $nids->value);
  85. while ($node = db_fetch_object($result)) {
  86. if ($types && empty($types[$node->type])) {
  87. return FALSE;
  88. }
  89. if (!empty($this->argument->options['validate_argument_node_access'])) {
  90. if (!node_access('view', $node)) {
  91. return FALSE;
  92. }
  93. }
  94. $titles[] = check_plain($node->title);
  95. unset($test[$node->nid]);
  96. }
  97. $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
  98. // If this is not empty, we did not find a nid.
  99. return empty($test);
  100. }
  101. }
  102. }