[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/ctools/plugins/contexts/ -> user.inc (source)

   1  <?php
   2  // $Id: user.inc,v 1.4.2.6 2010/08/03 18:45:21 merlinofchaos Exp $
   3  
   4  /**
   5   * @file
   6   *
   7   * Plugin to provide a user context
   8   */
   9  
  10  /**
  11   * Plugins are described by creating a $plugin array which will be used
  12   * by the system that includes this file.
  13   */
  14  $plugin = array(
  15    'title' => t("User"),
  16    'description' => t('A single user object.'),
  17    'context' => 'ctools_context_create_user',
  18    'settings form' => 'ctools_context_user_settings_form',
  19    'settings form validate' => 'ctools_context_user_settings_form_validate',
  20    'settings form submit' => 'ctools_context_user_settings_form_submit',
  21    'keyword' => 'user',
  22    'defaults' => array('type' => 'select', 'uid' => ''),
  23    'context name' => 'user',
  24    'convert list' => 'ctools_context_user_convert_list',
  25    'convert' => 'ctools_context_user_convert',
  26    'convert default' => 'name',
  27    'js' => array('misc/autocomplete.js'),
  28  );
  29  
  30  /**
  31   * It's important to remember that $conf is optional here, because contexts
  32   * are not always created from the UI.
  33   */
  34  function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
  35    $context = new ctools_context('user');
  36    $context->plugin = 'user';
  37  
  38    if ($empty) {
  39      return $context;
  40    }
  41  
  42    if ($conf) {
  43      if ($data['type'] == 'current') {
  44        global $user;
  45        $data = $user;
  46        $data->logged_in_user = TRUE;
  47      }
  48      else {
  49        $data = user_load(array('uid' => $data['uid']));
  50      }
  51    }
  52  
  53    if (!empty($data)) {
  54      $context->data     = $data;
  55      $context->title    = isset($data->name) ? $data->name : t('Anonymous');
  56      $context->argument = $data->uid;
  57      return $context;
  58    }
  59  }
  60  
  61  function ctools_context_user_settings_form($conf) {
  62    ctools_include('dependent');
  63    $form['type'] = array(
  64      '#title' => t('Enter the context type'),
  65      '#type' => 'radios',
  66      '#options' => array(
  67        'select' => t('Select a user'),
  68        'current' => t('Logged in user'),
  69      ),
  70      '#default_value' => $conf['type'],
  71    );
  72  
  73    $form['user'] = array(
  74      '#title' => t('Enter a user name'),
  75      '#type' => 'textfield',
  76      '#maxlength' => 512,
  77      '#autocomplete_path' => 'user/autocomplete',
  78      '#process' => array('ctools_dependent_process'),
  79      '#dependency' => array('radio:context[context_settings][type]' => array('select')),
  80    );
  81  
  82    if (!empty($conf['uid'])) {
  83      $info = user_load($conf['uid']);
  84      if ($info) {
  85        $form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', $info)));
  86      }
  87    }
  88  
  89    $form['uid'] = array(
  90      '#type' => 'value',
  91      '#value' => $conf['uid'],
  92    );
  93  
  94    $form['set_identifier'] = array(
  95      '#type' => 'checkbox',
  96      '#default_value' => FALSE,
  97      '#title' => t('Reset identifier to username'),
  98      '#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
  99      '#process' => array('ctools_dependent_process'),
 100      '#dependency' => array('radio:context[context_settings][type]' => array('select')),
 101    );
 102  
 103    return $form;
 104  }
 105  
 106  /**
 107   * Validate a user.
 108   */
 109  function ctools_context_user_settings_form_validate($form, &$form_values, &$form_state) {
 110    if ($form_values['type'] != 'select') {
 111      return;
 112    }
 113  
 114    // Validate the autocomplete
 115    if (empty($form_values['uid']) && empty($form_values['user'])) {
 116      form_error($form['user'], t('You must select a user.'));
 117      return;
 118    }
 119  
 120    if (empty($form_values['user'])) {
 121      return;
 122    }
 123  
 124    $account = user_load(array('name' => $form_values['user']));
 125  
 126    if (!$account) {
 127      form_error($form['user'], t('Invalid user selected.'));
 128    }
 129    else {
 130      form_set_value($form['uid'], $account->uid, $form_state);
 131    }
 132  }
 133  
 134  function ctools_context_user_settings_form_submit($form, &$form_values, &$form_state) {
 135    if ($form_values['set_identifier']) {
 136      $account = user_load($form_values['uid']);
 137      $form_state['values']['context']['identifier'] = $account->name;
 138    }
 139  
 140    // Don't let this be stored.
 141    unset($form_values['set_identifier']);
 142  }
 143  
 144  /**
 145   * Provide a list of replacements.
 146   */
 147  function ctools_context_user_convert_list() {
 148    $list = array(
 149      'uid' => t('User ID'),
 150      'name' => t('User name'),
 151    );
 152    if (module_exists('token')) {
 153      $list += reset(token_get_list(array('user')));
 154    }
 155    return $list;
 156  }
 157  
 158  /**
 159   * Convert a context into a string.
 160   */
 161  function ctools_context_user_convert($context, $type) {
 162    switch ($type) {
 163      case 'uid':
 164        return $context->data->uid;
 165      case 'name':
 166        return $context->data->name;
 167    }
 168    if (module_exists('token')) {
 169      $values = token_get_values('user', $context->data);
 170      if ($key = array_search($type, $values->tokens)) {
 171        return $values->values[$key];
 172      }
 173    }
 174  }


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