You are here

class flag_node in Flag 5

Same name and namespace in other branches
  1. 6.2 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

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

File

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

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

    // Note there isn't a translation helpers module in Drupal 5,
    // this feature is essentially Drupal 6 only.
    $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['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 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);
  }
  function is_flagged($content_id, $uid = NULL) {
    $content_id = $this
      ->get_translation_id($content_id);
    return parent::is_flagged($content_id, $uid);
  }
  function get_labels_token_types() {
    return array(
      'node',
    );
  }
  function replace_tokens($label, $contexts, $content_id) {
    if ($content_id && ($node = $this
      ->fetch_content($content_id))) {
      $contexts['node'] = $node;
    }
    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',
      ),
    );
  }
  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.'),
    );
  }
  function applies_to_content_id_array($content_ids) {
    $passed = array();
    $content_ids = implode(',', array_map('intval', $content_ids));
    $placeholders = implode(',', array_fill(0, sizeof($this->types), "'%s'"));
    $result = db_query("SELECT nid FROM {node} WHERE nid IN ({$content_ids}) AND type in ({$placeholders})", $this->types);
    while ($row = db_fetch_object($result)) {
      $passed[$row->nid] = TRUE;
    }
    return $passed;
  }

}

Members