[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/ctools/plugins/access/ -> term.inc (source)

   1  <?php
   2  // $Id: term.inc,v 1.1.2.6 2010/10/15 22:44:31 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Plugin to provide access control based upon specific terms.
   7   */
   8  
   9  /**
  10   * Plugins are described by creating a $plugin array which will be used
  11   * by the system that includes this file.
  12   */
  13  $plugin = array(
  14    'title' => t("Taxonomy: term"),
  15    'description' => t('Control access by a specific term.'),
  16    'callback' => 'ctools_term_ctools_access_check',
  17    'default' => array('vids' => array()),
  18    'settings form' => 'ctools_term_ctools_access_settings',
  19    'settings form validation' => 'ctools_term_ctools_access_settings_validate',
  20    'settings form submit' => 'ctools_term_ctools_access_settings_submit',
  21    'summary' => 'ctools_term_ctools_access_summary',
  22    'required context' => new ctools_context_required(t('Term'), array('term', 'terms')),
  23  );
  24  
  25  /**
  26   * Settings form for the 'by term' access plugin
  27   */
  28  function ctools_term_ctools_access_settings(&$form, &$form_state, $conf) {
  29    // If no configuration was saved before, set some defaults.
  30    if (empty($conf)) {
  31      $conf = array(
  32        'vid' => 0,
  33      );
  34    }
  35    if (!isset($conf['vid'])) {
  36      $conf['vid'] = 0;
  37    }
  38  
  39    $form['settings']['vid'] = array(
  40      '#title' => t('Vocabulary'),
  41      '#type' => 'select',
  42      '#options' => array(),
  43      '#description' => t('Select the vocabulary for this form.'),
  44      '#id' => 'ctools-select-vid',
  45      '#default_value' => $conf['vid'],
  46      '#required' => TRUE,
  47    );
  48  
  49    ctools_include('dependent');
  50    $options = array();
  51  
  52    // A note: Dependency works strangely on these forms as they have never been
  53    // updated to a more modern system so they are not individual forms of their
  54    // own like the content types.
  55  
  56    $form['settings']['#tree'] = TRUE;
  57  
  58    // Loop over each of the configured vocabularies.
  59    foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  60      $options[$vid] = $vocabulary->name;
  61      $form['settings'][$vocabulary->vid] = array(
  62        '#title' => t('Terms'),
  63        '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description,
  64        '#process' => array('ctools_dependent_process'),
  65        '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
  66        '#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '',
  67        '#multiple' => TRUE,
  68      );
  69  
  70      // If it's a tag, use an autocomplete.
  71      if ($vocabulary->tags) {
  72        $form['settings'][$vocabulary->vid]['#type'] = 'textfield';
  73        $form['settings'][$vocabulary->vid]['#autocomplete_path'] = 'taxonomy/autocomplete/' . $vocabulary->vid;
  74      }
  75  
  76      // Other vocabs just show a list.
  77      else {
  78        $terms = array();
  79        foreach (taxonomy_get_tree($vid) as $x => $term) {
  80          $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name;
  81        }
  82        $form['settings'][$vocabulary->vid]['#type'] = 'select';
  83        $form['settings'][$vocabulary->vid]['#options'] = $terms;
  84        unset($terms);
  85      }
  86    }
  87    $form['settings']['vid']['#options'] = $options;
  88  }
  89  
  90  /**
  91   * Check for access.
  92   */
  93  function ctools_term_ctools_access_check($conf, $context) {
  94    // As far as I know there should always be a context at this point, but this
  95    // is safe.
  96    if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  97      return FALSE;
  98    }
  99  
 100    // Get the $vid.
 101    if (!isset($conf['vid'])) {
 102      return FALSE;
 103    }
 104    $vid = $conf['vid'];
 105  
 106    // Get the terms.
 107    if (!isset($conf[$vid])) {
 108      return FALSE;
 109    }
 110  
 111    $return = FALSE;
 112    // Tags get saved as an imploded array of strings.
 113    if (!is_array($conf[$vid])) {
 114      $terms = explode(', ', $conf[$vid]);
 115      // For multi-term with names, we'll only accept the first term because
 116      // that is the name we have.
 117      return in_array($context->data->name, $terms);
 118    }
 119    else {
 120      $terms = array_filter($conf[$vid]);
 121      // For multi-term if any terms coincide, let's call that good enough:
 122      if (isset($context->tids)) {
 123        return (bool) array_intersect($terms, $context->tids);
 124      }
 125      else {
 126        return in_array($context->data->tid, $terms);
 127      }
 128    }
 129  }
 130  
 131  /**
 132   * Provide a summary description based upon the checked terms.
 133   */
 134  function ctools_term_ctools_access_summary($conf, $context) {
 135    $vocab = taxonomy_vocabulary_load($conf['vid']);
 136    if ($vocab->tags) {
 137      $terms = explode(', ', $conf[$vocab->vid]);
 138    }
 139    else {
 140      $terms = array();
 141      foreach ($conf[$vocab->vid] as $tid) {
 142        $term = taxonomy_get_term($tid);
 143        $terms[] = $term->name;
 144      }
 145    }
 146  
 147    return format_plural(count($terms),
 148      '@term can be the term "@terms"',
 149      '@term can be one of these terms: @terms',
 150      array('@terms' => implode(', ', $terms),
 151        '@term' => $context->identifier));
 152  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7