[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: term_parent.inc,v 1.1.2.3 2010/08/20 21:05:17 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   * Plugin to provide access control based upon a parent term.
   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: parent term"),
  15    'description' => t('Control access by existence of a parent term.'),
  16    'callback' => 'ctools_term_parent_ctools_access_check',
  17    'default' => array('vid' => array(), 'negate' => 0),
  18    'settings form' => 'ctools_term_parent_ctools_access_settings',
  19    'settings form validation' => 'ctools_term_parent_ctools_access_settings_validate',
  20    'settings form submit' => 'ctools_term_parent_ctools_access_settings_submit',
  21    'summary' => 'ctools_term_parent_ctools_access_summary',
  22    'required context' => new ctools_context_required(t('Term'), array('term', 'terms')),
  23  );
  24  
  25  /**
  26   * Settings form for the 'by parent term' access plugin
  27   */
  28  function ctools_term_parent_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. If there exists a parent term in that vocabulary, this access check will succeed.'),
  44      '#id' => 'ctools-select-vid',
  45      '#default_value' => $conf['vid'],
  46      '#required' => TRUE,
  47    );
  48  
  49    $options = array();
  50  
  51    // Loop over each of the configured vocabularies.
  52    foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  53      $options[$vid] = $vocabulary->name;
  54    }
  55    $form['settings']['vid']['#options'] = $options;
  56  }
  57  
  58  /**
  59   * Check for access.
  60   */
  61  function ctools_term_parent_ctools_access_check($conf, $context) {
  62    // As far as I know there should always be a context at this point, but this
  63    // is safe.
  64    if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  65      return FALSE;
  66    }
  67  
  68    // Get the $vid.
  69    if (!isset($conf['vid'])) {
  70      return FALSE;
  71    }
  72    $vid = $conf['vid'];
  73  
  74    $count = db_result(db_query('SELECT COUNT(*) FROM {term_hierarchy} th INNER JOIN {term_data} td ON th.parent = td.tid WHERE th.tid = %d AND td.vid = %d',  $context->data->tid, $vid));
  75  
  76    return $count ? TRUE : FALSE;
  77  }
  78  
  79  /**
  80   * Provide a summary description based upon the checked terms.
  81   */
  82  function ctools_term_parent_ctools_access_summary($conf, $context) {
  83    $vocab = taxonomy_vocabulary_load($conf['vid']);
  84  
  85    return t('"@term" has parent in vocabulary "@vocab"', array('@term' => $context->identifier, '@vocab' => $vocab->name));
  86  }


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