You are here

class flag_user in Flag 6.2

Same name and namespace in other branches
  1. 5 flag.inc \flag_user
  2. 6 flag.inc \flag_user
  3. 7.3 includes/flag/flag_user.inc \flag_user
  4. 7.2 flag.inc \flag_user

Implements a user flag.

Hierarchy

Expanded class hierarchy of flag_user

1 string reference to 'flag_user'
flag_flag_definitions in ./flag.inc
Implementation of hook_flag_definitions().

File

./flag.inc, line 1614
Implements various flags. Uses object oriented style inspired by that of Views 2.

View source
class flag_user extends flag_flag {
  function options() {
    $options = parent::options();
    $options += array(
      'show_on_profile' => TRUE,
      'access_uid' => '',
    );
    return $options;
  }
  function options_form(&$form) {
    parent::options_form($form);
    $form['access']['types'] = array(
      // A user flag doesn't support node types.
      // TODO: Maybe support roles instead of node types.
      '#type' => 'value',
      '#value' => array(
        0 => 0,
      ),
    );
    $form['access']['access_uid'] = array(
      '#type' => 'checkbox',
      '#title' => t('Users may flag themselves'),
      '#description' => t('Disabling this option may be useful when setting up a "friend" flag, when a user flagging themself does not make sense.'),
      '#default_value' => $this->access_uid ? 0 : 1,
    );
    $form['display']['show_on_profile'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on user profile page'),
      '#default_value' => $this->show_on_profile,
      '#access' => empty($this->locked['show_on_profile']),
    );
  }
  function form_input($form_values) {
    parent::form_input($form_values);

    // The access_uid value is intentionally backwards from the UI, to avoid
    // confusion caused by checking a box to disable a feature.
    $this->access_uid = empty($form_values['access_uid']) ? 'others' : '';
  }
  function _load_content($content_id) {
    return user_load($content_id);
  }
  function applies_to_content_object($user) {

    // This user flag doesn't currently support subtypes so we return TRUE for
    // any user.
    if ($user) {
      return TRUE;
    }
    return FALSE;
  }
  function type_access($content_id, $action, $account) {

    // Prevent users from flagging themselves.
    if ($this->access_uid == 'others' && $content_id == $account->uid) {
      return FALSE;
    }
  }
  function type_access_multiple($content_ids, $account) {
    $access = array();

    // Exclude anonymous.
    if (array_key_exists(0, $content_ids)) {
      $access[0] = FALSE;
    }

    // Prevent users from flagging themselves.
    if ($this->access_uid == 'others' && array_key_exists($account->uid, $content_ids)) {
      $access[$account->uid] = FALSE;
    }
    return $access;
  }
  function get_content_id($user) {
    return $user->uid;
  }
  function uses_hook_link($teaser) {
    if ($this->show_on_profile) {
      return TRUE;
    }
    return FALSE;
  }
  function get_labels_token_types() {
    return array_merge(array(
      'user',
    ), parent::get_labels_token_types());
  }
  function replace_tokens($label, $contexts, $content_id) {
    if ($content_id && ($user = $this
      ->fetch_content($content_id))) {
      $contexts['user'] = $user;
    }
    return parent::replace_tokens($label, $contexts, $content_id);
  }
  function get_flag_action($content_id) {
    $flag_action = parent::get_flag_action($content_id);
    $user = $this
      ->fetch_content($content_id);
    $flag_action->content_title = $user->name;
    $flag_action->content_url = _flag_url('user/' . $user->uid);
    return $flag_action;
  }
  function get_relevant_action_objects($content_id) {
    return array(
      'user' => $this
        ->fetch_content($content_id),
    );
  }
  function rules_get_event_arguments_definition() {
    return array(
      'account' => array(
        'type' => 'user',
        'label' => t('flagged user'),
        'handler' => 'flag_rules_get_event_argument',
      ),
    );
  }
  function rules_get_element_argument_definition() {
    return array(
      'type' => 'user',
      'label' => t('Flagged user'),
    );
  }
  function get_views_info() {
    return array(
      'views table' => 'users',
      'join field' => 'uid',
      'title field' => 'name',
      'title' => t('User flag'),
      'help' => t('Limit results to only those users flagged by a certain flag; Or display information about the flag set on a user.'),
      'counter title' => t('User flag counter'),
      'counter help' => t('Include this to gain access to the flag counter field.'),
    );
  }

}

Members