Projet

Général

Profil

Paste
Télécharger (4,11 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / flag / includes / flag / flag_node.inc @ 018e218c

1
<?php
2

    
3
/**
4
 * @file
5
 *   Contains the flag_node class.
6
 */
7

    
8
/**
9
 * Implements a node flag.
10
 */
11
class flag_node extends flag_entity {
12
  function options() {
13
    $options = parent::options();
14
    // Use own display settings in the meanwhile.
15
    $options += array(
16
      'i18n' => 0,
17
    );
18
    return $options;
19
  }
20

    
21
  /**
22
   * Options form extras for node flags.
23
   */
24
  function options_form(&$form) {
25
    parent::options_form($form);
26

    
27
    $form['access']['access_author'] = array(
28
      '#type' => 'radios',
29
      '#title' => t('Flag access by content authorship'),
30
      '#options' => array(
31
        '' => t('No additional restrictions'),
32
        'own' => t('Users may only flag content they own'),
33
        'others' => t('Users may only flag content of others'),
34
      ),
35
      '#default_value' => $this->access_author,
36
      '#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."),
37
    );
38

    
39
    // Support for i18n flagging requires Translation helpers module.
40
    $form['i18n'] = array(
41
      '#type' => 'radios',
42
      '#title' => t('Internationalization'),
43
      '#options' => array(
44
        '1' => t('Flag translations of content as a group'),
45
        '0' => t('Flag each translation of content separately'),
46
      ),
47
      '#default_value' => $this->i18n,
48
      '#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.'),
49
      '#access' => module_exists('translation_helpers'),
50
      '#weight' => 5,
51
    );
52

    
53
    // Override the UI texts for nodes.
54
    $form['display']['show_on_form'] = array(
55
      '#title' => t('Display checkbox on node edit form'),
56
      '#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/structure/types'))),
57
    ) + $form['display']['show_on_form'];
58
  }
59

    
60
  function type_access_multiple($entity_ids, $account) {
61
    $access = array();
62

    
63
    // If all subtypes are allowed, we have nothing to say here.
64
    if (empty($this->types)) {
65
      return $access;
66
    }
67

    
68
    // Ensure that only flaggable node types are granted access. This avoids a
69
    // node_load() on every type, usually done by applies_to_entity_id().
70
    $result = db_select('node', 'n')->fields('n', array('nid'))
71
      ->condition('nid', array_keys($entity_ids), 'IN')
72
      ->condition('type', $this->types, 'NOT IN')
73
      ->execute();
74
    foreach ($result as $row) {
75
      $access[$row->nid] = FALSE;
76
    }
77

    
78
    return $access;
79
  }
80

    
81
  /**
82
   * Adjust the Content ID to find the translation parent if i18n-enabled.
83
   *
84
   * @param $entity_id
85
   *   The nid for the content.
86
   * @return
87
   *   The tnid if available, the nid otherwise.
88
   */
89
  function get_translation_id($entity_id) {
90
    if ($this->i18n) {
91
      $node = $this->fetch_entity($entity_id);
92
      if (!empty($node->tnid)) {
93
        $entity_id = $node->tnid;
94
      }
95
    }
96
    return $entity_id;
97
  }
98

    
99
  function flag($action, $entity_id, $account = NULL, $skip_permission_check = FALSE, $flagging = NULL) {
100
    $entity_id = $this->get_translation_id($entity_id);
101
    return parent::flag($action, $entity_id, $account, $skip_permission_check, $flagging);
102
  }
103

    
104
  // Instead of overriding is_flagged() we override get_flagging_record(),
105
  // which is the underlying method.
106
  function get_flagging_record($entity_id, $uid = NULL, $sid = NULL) {
107
    $entity_id = $this->get_translation_id($entity_id);
108
    return parent::get_flagging_record($entity_id, $uid, $sid);
109
  }
110

    
111
  /**
112
   * This is overridden for no other purpose than to document that $entity_id
113
   * can be one of the following fake IDs in certain contexts:
114
   *  - 'new': On a new node form.
115
   *  - 'fake': On the node type admin form.
116
   */
117
  function replace_tokens($label, $contexts, $options, $entity_id) {
118
    return parent::replace_tokens($label, $contexts, $options, $entity_id);
119
  }
120
}