flag_user.inc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * Contains the flag_user class.
  5. */
  6. /**
  7. * Implements a user flag.
  8. */
  9. class flag_user extends flag_entity {
  10. function options() {
  11. $options = parent::options();
  12. $options += array(
  13. 'show_on_profile' => TRUE,
  14. 'access_uid' => '',
  15. );
  16. return $options;
  17. }
  18. /**
  19. * Options form extras for user flags.
  20. */
  21. function options_form(&$form) {
  22. parent::options_form($form);
  23. $form['access']['types'] = array(
  24. // A user flag doesn't support node types.
  25. // TODO: Maybe support roles instead of node types.
  26. '#type' => 'value',
  27. '#value' => array(0 => 0),
  28. );
  29. $form['access']['access_uid'] = array(
  30. '#type' => 'checkbox',
  31. '#title' => t('Users may flag themselves'),
  32. '#description' => t('Disabling this option may be useful when setting up a "friend" flag, when a user flagging themself does not make sense.'),
  33. '#default_value' => $this->access_uid ? 0 : 1,
  34. );
  35. $form['display']['show_on_profile'] = array(
  36. '#type' => 'checkbox',
  37. '#title' => t('Display link on user profile page'),
  38. '#description' => t('Show the link formatted as a user profile element.'),
  39. '#default_value' => $this->show_on_profile,
  40. // Put this above 'show on entity'.
  41. '#weight' => -1,
  42. );
  43. }
  44. function form_input($form_values) {
  45. parent::form_input($form_values);
  46. // The access_uid value is intentionally backwards from the UI, to avoid
  47. // confusion caused by checking a box to disable a feature.
  48. $this->access_uid = empty($form_values['access_uid']) ? 'others' : '';
  49. }
  50. function type_access($entity_id, $action, $account) {
  51. // Prevent users from flagging themselves.
  52. if ($this->access_uid == 'others' && $entity_id == $account->uid) {
  53. return FALSE;
  54. }
  55. }
  56. function type_access_multiple($entity_ids, $account) {
  57. $access = array();
  58. // Exclude anonymous.
  59. if (array_key_exists(0, $entity_ids)) {
  60. $access[0] = FALSE;
  61. }
  62. // Prevent users from flagging themselves.
  63. if ($this->access_uid == 'others' && array_key_exists($account->uid, $entity_ids)) {
  64. $access[$account->uid] = FALSE;
  65. }
  66. return $access;
  67. }
  68. function get_flag_action($entity_id) {
  69. $flag_action = parent::get_flag_action($entity_id);
  70. $user = $this->fetch_entity($entity_id);
  71. $flag_action->content_title = $user->name;
  72. $flag_action->content_url = $this->_flag_url('user/' . $user->uid);
  73. return $flag_action;
  74. }
  75. function get_relevant_action_objects($entity_id) {
  76. return array(
  77. 'user' => $this->fetch_entity($entity_id),
  78. );
  79. }
  80. function get_views_info() {
  81. $views_info = parent::get_views_info();
  82. $views_info['title field'] = 'name';
  83. return $views_info;
  84. }
  85. }