flag_comment.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * Contains the flag_comment class.
  5. */
  6. /**
  7. * Implements a comment flag.
  8. */
  9. class flag_comment extends flag_entity {
  10. function options() {
  11. $options = parent::options();
  12. $options += array(
  13. 'access_author' => '',
  14. );
  15. return $options;
  16. }
  17. /**
  18. * Options form extras for comment flags.
  19. */
  20. function options_form(&$form) {
  21. parent::options_form($form);
  22. $form['access']['access_author'] = array(
  23. '#type' => 'radios',
  24. '#title' => t('Flag access by content authorship'),
  25. '#options' => array(
  26. '' => t('No additional restrictions'),
  27. 'comment_own' => t('Users may only flag own comments'),
  28. 'comment_others' => t('Users may only flag comments by others'),
  29. 'node_own' => t('Users may only flag comments of nodes they own'),
  30. 'node_others' => t('Users may only flag comments of nodes by others'),
  31. ),
  32. '#default_value' => $this->access_author,
  33. '#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."),
  34. );
  35. }
  36. function type_access_multiple($entity_ids, $account) {
  37. $access = array();
  38. // If all subtypes are allowed, we have nothing to say here.
  39. if (empty($this->types)) {
  40. return $access;
  41. }
  42. // Ensure node types are granted access. This avoids a
  43. // node_load() on every type, usually done by applies_to_entity_id().
  44. $query = db_select('comment', 'c');
  45. $query->innerJoin('node', 'n', 'c.nid = n.nid');
  46. $result = $query
  47. ->fields('c', array('cid'))
  48. ->condition('c.cid', $entity_ids, 'IN')
  49. ->condition('n.type', $this->types, 'NOT IN')
  50. ->execute();
  51. foreach ($result as $row) {
  52. $access[$row->nid] = FALSE;
  53. }
  54. return $access;
  55. }
  56. function get_entity_id($comment) {
  57. // Store the comment object in the static cache, to avoid getting it
  58. // again unneedlessly.
  59. $this->remember_entity($comment->cid, $comment);
  60. return $comment->cid;
  61. }
  62. function get_labels_token_types() {
  63. return array_merge(array('comment', 'node'), parent::get_labels_token_types());
  64. }
  65. function replace_tokens($label, $contexts, $options, $entity_id) {
  66. if ($entity_id) {
  67. if (($comment = $this->fetch_entity($entity_id)) && ($node = node_load($comment->nid))) {
  68. $contexts['node'] = $node;
  69. $contexts['comment'] = $comment;
  70. }
  71. }
  72. return parent::replace_tokens($label, $contexts, $options, $entity_id);
  73. }
  74. function get_flag_action($entity_id) {
  75. $flag_action = parent::get_flag_action($entity_id);
  76. $comment = $this->fetch_entity($entity_id);
  77. $flag_action->content_title = $comment->subject;
  78. $flag_action->content_url = $this->_flag_url("comment/$comment->cid", "comment-$comment->cid");
  79. return $flag_action;
  80. }
  81. function get_relevant_action_objects($entity_id) {
  82. $comment = $this->fetch_entity($entity_id);
  83. return array(
  84. 'comment' => $comment,
  85. 'node' => node_load($comment->nid),
  86. );
  87. }
  88. }