flag_node.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @file
  4. * Contains the flag_node class.
  5. */
  6. /**
  7. * Implements a node flag.
  8. */
  9. class flag_node extends flag_entity {
  10. function options() {
  11. $options = parent::options();
  12. // Use own display settings in the meanwhile.
  13. $options += array(
  14. 'i18n' => 0,
  15. );
  16. return $options;
  17. }
  18. /**
  19. * Options form extras for node flags.
  20. */
  21. function options_form(&$form) {
  22. parent::options_form($form);
  23. $form['access']['access_author'] = array(
  24. '#type' => 'radios',
  25. '#title' => t('Flag access by content authorship'),
  26. '#options' => array(
  27. '' => t('No additional restrictions'),
  28. 'own' => t('Users may only flag content they own'),
  29. 'others' => t('Users may only flag content of others'),
  30. ),
  31. '#default_value' => $this->access_author,
  32. '#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."),
  33. );
  34. // Support for i18n flagging requires Translation helpers module.
  35. $form['i18n'] = array(
  36. '#type' => 'radios',
  37. '#title' => t('Internationalization'),
  38. '#options' => array(
  39. '1' => t('Flag translations of content as a group'),
  40. '0' => t('Flag each translation of content separately'),
  41. ),
  42. '#default_value' => $this->i18n,
  43. '#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.'),
  44. '#access' => module_exists('translation_helpers'),
  45. '#weight' => 5,
  46. );
  47. // Override the UI texts for nodes.
  48. $form['display']['show_on_form'] = array(
  49. '#title' => t('Display checkbox on node edit form'),
  50. '#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'))),
  51. ) + $form['display']['show_on_form'];
  52. // Add the 'teaser' view mode as a default value for the entity link display
  53. // option if this is a new flag.
  54. if (empty($this->fid)) {
  55. $form['display']['show_in_links']['#default_value']['teaser'] = 'teaser';
  56. }
  57. }
  58. function type_access_multiple($entity_ids, $account) {
  59. $access = array();
  60. // If all subtypes are allowed, we have nothing to say here.
  61. if (empty($this->types)) {
  62. return $access;
  63. }
  64. // Ensure that only flaggable node types are granted access. This avoids a
  65. // node_load() on every type, usually done by applies_to_entity_id().
  66. $result = db_select('node', 'n')->fields('n', array('nid'))
  67. ->condition('nid', array_keys($entity_ids), 'IN')
  68. ->condition('type', $this->types, 'NOT IN')
  69. ->execute();
  70. foreach ($result as $row) {
  71. $access[$row->nid] = FALSE;
  72. }
  73. return $access;
  74. }
  75. /**
  76. * Adjust the Content ID to find the translation parent if i18n-enabled.
  77. *
  78. * @param int $entity_id
  79. * The nid for the content.
  80. *
  81. * @return int
  82. * The tnid if available, the nid otherwise.
  83. */
  84. function get_translation_id($entity_id) {
  85. if ($this->i18n) {
  86. $node = $this->fetch_entity($entity_id);
  87. if (!empty($node->tnid)) {
  88. $entity_id = $node->tnid;
  89. }
  90. }
  91. return $entity_id;
  92. }
  93. function flag($action, $entity_id, $account = NULL, $skip_permission_check = FALSE, $flagging = NULL) {
  94. $entity_id = $this->get_translation_id($entity_id);
  95. return parent::flag($action, $entity_id, $account, $skip_permission_check, $flagging);
  96. }
  97. // Instead of overriding is_flagged() we override get_flagging_record(),
  98. // which is the underlying method.
  99. function get_flagging_record($entity_id, $uid = NULL, $sid = NULL) {
  100. $entity_id = $this->get_translation_id($entity_id);
  101. return parent::get_flagging_record($entity_id, $uid, $sid);
  102. }
  103. /**
  104. * This is overridden for no other purpose than to document that $entity_id
  105. * can be one of the following fake IDs in certain contexts:
  106. * - 'new': On a new node form.
  107. * - 'fake': On the node type admin form.
  108. */
  109. function replace_tokens($label, $contexts, $options, $entity_id) {
  110. return parent::replace_tokens($label, $contexts, $options, $entity_id);
  111. }
  112. }