[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: term_vocabulary.inc,v 1.4.2.4 2010/07/14 01:30:19 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Plugin to provide access control based upon term vocabulary
   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: vocabulary"),
  15    'description' => t('Control access by vocabulary.'),
  16    'callback' => 'ctools_term_vocabulary_ctools_access_check',
  17    'default' => array('vids' => array()),
  18    'settings form' => 'ctools_term_vocabulary_ctools_access_settings',
  19    'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit',
  20    'summary' => 'ctools_term_vocabulary_ctools_access_summary',
  21    'required context' => new ctools_context_required(t('Vocabulary'), array('term', 'terms', 'vocabulary')),
  22  );
  23  
  24  /**
  25   * Settings form for the 'by term_vocabulary' access plugin
  26   */
  27  function ctools_term_vocabulary_ctools_access_settings(&$form, &$form_state, $conf) {
  28    $options = array();
  29    $vocabularies = taxonomy_get_vocabularies();
  30    foreach ($vocabularies as $voc) {
  31      $options[$voc->vid] = check_plain($voc->name);
  32    }
  33  
  34    $form['settings']['vids'] = array(
  35      '#type' => 'checkboxes',
  36      '#title' => t('Vocabularies'),
  37      '#options' => $options,
  38      '#description' => t('Only the checked vocabularies will be valid.'),
  39      '#default_value' => $conf['vids'],
  40    );
  41  }
  42  
  43  /**
  44   * Compress the term_vocabularys allowed to the minimum.
  45   */
  46  function ctools_term_vocabulary_ctools_access_settings_submit(&$form, &$form_state) {
  47    $form_state['values']['settings']['vids'] = array_filter($form_state['values']['settings']['vids']);
  48  }
  49  
  50  /**
  51   * Check for access.
  52   */
  53  function ctools_term_vocabulary_ctools_access_check($conf, $context) {
  54    // As far as I know there should always be a context at this point, but this
  55    // is safe.
  56    if (empty($context) || empty($context->data) || empty($context->data->vid)) {
  57      return FALSE;
  58    }
  59  
  60    if (array_filter($conf['vids']) && empty($conf['vids'][$context->data->vid])) {
  61      return FALSE;
  62    }
  63  
  64    return TRUE;
  65  }
  66  
  67  /**
  68   * Provide a summary description based upon the checked term_vocabularys.
  69   */
  70  function ctools_term_vocabulary_ctools_access_summary($conf, $context) {
  71    if (!isset($conf['type'])) {
  72      $conf['type'] = array();
  73    }
  74    $vocabularies = taxonomy_get_vocabularies();
  75  
  76    $names = array();
  77    foreach (array_filter($conf['vids']) as $vid) {
  78      $names[] = check_plain($vocabularies[$vid]->name);
  79    }
  80  
  81    if (empty($names)) {
  82      return t('@identifier is any vocabulary', array('@identifier' => $context->identifier));
  83    }
  84  
  85    return format_plural(count($names), '@identifier vocabulary is "@vids"', '@identifier vocabulary is one of "@vids"', array('@vids' => implode(', ', $names), '@identifier' => $context->identifier));
  86  }
  87  


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