You are here

class flag_node in Flag 6.2

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

Implements a node flag.

Hierarchy

Expanded class hierarchy of flag_node

2 string references to 'flag_node'
flag_flag_definitions in ./flag.inc
Implementation of hook_flag_definitions().
hook_flag_definitions in ./flag.api.php
Define one or more flag types.

File

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

View source
class flag_node extends flag_flag {
  function options() {
    $options = parent::options();
    $options += array(
      'show_on_page' => TRUE,
      'show_on_teaser' => TRUE,
      'show_on_form' => FALSE,
      'access_author' => '',
      'i18n' => 0,
    );
    return $options;
  }
  function options_form(&$form) {
    parent::options_form($form);

    // Support for i18n flagging requires Translation helpers module.
    $form['i18n'] = array(
      '#type' => 'radios',
      '#title' => t('Internationalization'),
      '#options' => array(
        '1' => t('Flag translations of content as a group'),
        '0' => t('Flag each translation of content separately'),
      ),
      '#default_value' => $this->i18n,
      '#description' => t('Flagging translations as a group effectively allows users to flag the original piece of content regardless of the translation they are viewing. Changing this setting will <strong>not</strong> update content that has been flagged already.'),
      '#access' => module_exists('translation_helpers'),
      '#weight' => 5,
    );
    $form['access']['access_author'] = array(
      '#type' => 'radios',
      '#title' => t('Flag access by content authorship'),
      '#options' => array(
        '' => t('No additional restrictions'),
        'own' => t('Users may only flag content they own'),
        'others' => t('Users may only flag content of others'),
      ),
      '#default_value' => $this->access_author,
      '#description' => t("Restrict access to this flag based on the user's ownership of the content. Users must also have access to the flag through the role settings."),
    );
    $form['display']['show_on_teaser'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on node teaser'),
      '#default_value' => $this->show_on_teaser,
      '#access' => empty($this->locked['show_on_teaser']),
    );
    $form['display']['show_on_page'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display link on node page'),
      '#default_value' => $this->show_on_page,
      '#access' => empty($this->locked['show_on_page']),
    );
    $form['display']['show_on_form'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display checkbox on node edit form'),
      '#default_value' => $this->show_on_form,
      '#description' => t('If you elect to have a checkbox on the node edit form, you may specify its initial state in the settings form <a href="@content-types-url">for each content type</a>.', array(
        '@content-types-url' => url('admin/content/types'),
      )),
      '#access' => empty($this->locked['show_on_form']),
    );
  }
  function _load_content($content_id) {
    return is_numeric($content_id) ? node_load($content_id) : NULL;
  }
  function applies_to_content_object($node) {
    if ($node && in_array($node->type, $this->types)) {
      return TRUE;
    }
    return FALSE;
  }
  function type_access_multiple($content_ids, $account) {
    $access = array();

    // Ensure that only flaggable node types are granted access. This avoids a
    // node_load() on every type, usually done by applies_to_content_id().
    $nids = implode(',', array_map('intval', array_keys($content_ids)));
    $placeholders = implode(',', array_fill(0, sizeof($this->types), "'%s'"));
    $result = db_query("SELECT nid as content_id FROM {node} WHERE nid IN ({$nids}) AND type NOT IN ({$placeholders})", $this->types);
    while ($row = db_fetch_object($result)) {
      $access[$row->content_id] = FALSE;
    }
    return $access;
  }
  function get_content_id($node) {
    return $node->nid;
  }

  /**
   * Adjust the Content ID to find the translation parent if i18n-enabled.
   *
   * @param $content_id
   *   The nid for the content.
   * @return
   *   The tnid if available, the nid otherwise.
   */
  function get_translation_id($content_id) {
    if ($this->i18n) {
      $node = $this
        ->fetch_content($content_id);
      if (!empty($node->tnid)) {
        $content_id = $node->tnid;
      }
    }
    return $content_id;
  }
  function uses_hook_link($teaser) {
    if ($teaser && $this->show_on_teaser || !$teaser && $this->show_on_page) {
      return TRUE;
    }
    return FALSE;
  }
  function flag($action, $content_id, $account = NULL, $skip_permission_check = FALSE) {
    $content_id = $this
      ->get_translation_id($content_id);
    return parent::flag($action, $content_id, $account, $skip_permission_check);
  }

  // Instead of overriding is_flagged() we override get_flagging_record(),
  // which is the underlying method.
  function get_flagging_record($content_id, $uid = NULL, $sid = NULL) {
    $content_id = $this
      ->get_translation_id($content_id);
    return parent::get_flagging_record($content_id, $uid, $sid);
  }
  function get_labels_token_types() {
    return array_merge(array(
      'node',
    ), parent::get_labels_token_types());
  }
  function replace_tokens($label, $contexts, $content_id) {
    if (is_numeric($content_id) && ($node = $this
      ->fetch_content($content_id))) {
      $contexts['node'] = $node;
    }
    elseif (!empty($content_id) && ($type = node_get_types('type', $content_id))) {
      $content_id = NULL;
      $contexts['node'] = (object) array(
        'nid' => NULL,
        'type' => $type->type,
        'title' => '',
      );
    }
    return parent::replace_tokens($label, $contexts, $content_id);
  }
  function get_flag_action($content_id) {
    $flag_action = parent::get_flag_action($content_id);
    $node = $this
      ->fetch_content($content_id);
    $flag_action->content_title = $node->title;
    $flag_action->content_url = _flag_url('node/' . $node->nid);
    return $flag_action;
  }
  function get_valid_actions() {
    $actions = module_invoke_all('action_info');
    foreach ($actions as $callback => $action) {
      if ($action['type'] != 'node' && !isset($action['hooks']['nodeapi'])) {
        unset($actions[$callback]);
      }
    }
    return $actions;
  }
  function get_relevant_action_objects($content_id) {
    return array(
      'node' => $this
        ->fetch_content($content_id),
    );
  }
  function rules_get_event_arguments_definition() {
    return array(
      'node' => array(
        'type' => 'node',
        'label' => t('flagged content'),
        'handler' => 'flag_rules_get_event_argument',
      ),
      'node_author' => array(
        'type' => 'user',
        'label' => t('flagged content author'),
        'handler' => 'flag_rules_get_node_author',
      ),
    );
  }
  function rules_get_element_argument_definition() {
    return array(
      'type' => 'node',
      'label' => t('Flagged content'),
    );
  }
  function get_views_info() {
    return array(
      'views table' => 'node',
      'join field' => 'nid',
      'title field' => 'title',
      'title' => t('Node flag'),
      'help' => t('Limit results to only those nodes flagged by a certain flag; Or display information about the flag set on a node.'),
      'counter title' => t('Node flag counter'),
      'counter help' => t('Include this to gain access to the flag counter field.'),
    );
  }

}

Members