• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

modules/node/views_plugin_argument_validate_node.inc

Go to the documentation of this file.
00001 <?php
00010 class views_plugin_argument_validate_node extends views_plugin_argument_validate {
00011   function option_definition() {
00012     $options = parent::option_definition();
00013     $options['types'] = array('default' => array());
00014     $options['access'] = array('default' => FALSE);
00015     $options['access_op'] = array('default' => 'view');
00016     $options['nid_type'] = array('default' => 'nid');
00017 
00018     return $options;
00019   }
00020 
00021   function options_form(&$form, &$form_state) {
00022     $types = node_type_get_types();
00023     foreach ($types as $type => $info) {
00024       $options[$type] = check_plain(t($info->name));
00025     }
00026 
00027     $form['types'] = array(
00028       '#type' => 'checkboxes',
00029       '#title' => t('Content types'),
00030       '#options' => $options,
00031       '#default_value' => $this->options['types'],
00032       '#description' => t('Choose one or more content types to validate with.'),
00033     );
00034 
00035     $form['access'] = array(
00036       '#type' => 'checkbox',
00037       '#title' => t('Validate user has access to the content'),
00038       '#default_value' => $this->options['access'],
00039     );
00040     $form['access_op'] = array(
00041       '#type' => 'radios',
00042       '#title' => t('Access operation to check'),
00043       '#options' => array('view' => t('View'), 'update' => t('Edit'), 'delete' => t('Delete')),
00044       '#default_value' => $this->options['access_op'],
00045       '#dependency' => array('edit-options-validate-options-node-access' => array(TRUE)),
00046     );
00047 
00048     $form['nid_type'] = array(
00049       '#type' => 'select',
00050       '#title' => t('Filter value format'),
00051       '#options' => array(
00052         'nid' => t('Node ID'),
00053         'nids' => t('Node IDs separated by , or +'),
00054       ),
00055       '#default_value' => $this->options['nid_type'],
00056     );
00057   }
00058 
00059   function options_submit(&$form, &$form_state, &$options = array()) {
00060     // filter trash out of the options so we don't store giant unnecessary arrays
00061     $options['types'] = array_filter($options['types']);
00062   }
00063 
00064   function convert_options(&$options) {
00065     if (!isset($options['types']) && !empty($this->argument->options['validate_argument_node_type'])) {
00066       $options['types'] = isset($this->argument->options['validate_argument_node_type']) ? $this->argument->options['validate_argument_node_type'] : array();
00067       $options['access'] = !empty($this->argument->options['validate_argument_node_access']);
00068       $options['access_op'] = isset($this->argument->options['validate_argument_node_access_op']) ? $this->argument->options['validate_argument_node_access_op'] : 'view';
00069       $options['nid_type'] = isset($this->argument->options['validate_argument_nid_type']) ? $this->argument->options['validate_argument_nid_type'] : array();
00070     }
00071   }
00072 
00073   function validate_argument($argument) {
00074     $types = $this->options['types'];
00075 
00076     switch ($this->options['nid_type']) {
00077       case 'nid':
00078         if (!is_numeric($argument)) {
00079           return FALSE;
00080         }
00081         $node = node_load($argument);
00082         if (!$node) {
00083           return FALSE;
00084         }
00085 
00086         if (!empty($this->options['access'])) {
00087           if (!node_access($this->options['access_op'], $node)) {
00088             return FALSE;
00089           }
00090         }
00091 
00092         // Save the title() handlers some work.
00093         $this->argument->validated_title = check_plain($node->title);
00094 
00095         if (empty($types)) {
00096           return TRUE;
00097         }
00098 
00099         return isset($types[$node->type]);
00100       break;
00101       case 'nids':
00102         $nids = new stdClass();
00103         $nids->value = array($argument);
00104         $nids = views_break_phrase($argument, $nids);
00105         if ($nids->value == array(-1)) {
00106           return FALSE;
00107         }
00108 
00109         $test = drupal_map_assoc($nids->value);
00110         $titles = array();
00111 
00112         $result = db_query("SELECT * FROM {node} WHERE nid IN (:nids)", array(':nids' =>  $nids->value));
00113         foreach ($result as $node) {
00114           if ($types && empty($types[$node->type])) {
00115             return FALSE;
00116           }
00117 
00118           if (!empty($this->options['access'])) {
00119             if (!node_access($this->options['access_op'], $node)) {
00120               return FALSE;
00121             }
00122           }
00123 
00124           $titles[] = check_plain($node->title);
00125           unset($test[$node->nid]);
00126         }
00127 
00128         $this->argument->validated_title = implode($nids->operator == 'or' ? ' + ' : ', ', $titles);
00129         // If this is not empty, we did not find a nid.
00130         return empty($test);
00131     }
00132   }
00133 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1