start.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. elgg_register_event_handler('init', 'system', 'favourites_init');
  3. function favourites_init() {
  4. elgg_extend_view('css/elgg', 'favourites/css');
  5. elgg_extend_view('js/elgg', 'favourites/js');
  6. elgg_register_page_handler('favourites','favourites_page_handler');
  7. // registered with priority < 500 so other plugins can remove favourites
  8. elgg_register_plugin_hook_handler('register', 'menu:river', 'favourites_river_menu_setup', 400);
  9. elgg_register_plugin_hook_handler('register', 'menu:entity', 'favourites_entity_menu_setup', 400);
  10. $actions_base = elgg_get_plugins_path() . 'favourites/actions/favourites';
  11. elgg_register_action('favourites/add', "$actions_base/add.php");
  12. elgg_register_action('favourites/delete', "$actions_base/delete.php");
  13. if ( elgg_is_logged_in() ) {
  14. elgg_register_menu_item(
  15. 'site',
  16. array(
  17. 'name' => 'favourites',
  18. 'text' => elgg_echo('favourites:menu'),
  19. 'href' => "favourites/view/"
  20. )
  21. );
  22. }
  23. }
  24. /**
  25. * Add a mark as favourite to entity menu at end of the menu
  26. */
  27. function favourites_entity_menu_setup($hook, $type, $return, $params) {
  28. if (elgg_in_context('widgets')) {
  29. return $return;
  30. }
  31. $entity = $params['entity'];
  32. if(!is_allowed_type($entity)) {
  33. return $return;
  34. }
  35. // favourites button
  36. $options = array(
  37. 'name' => 'favourite',
  38. 'text' => elgg_view('favourites/button', array('entity' => $entity)),
  39. 'href' => false,
  40. 'priority' => 1002,
  41. );
  42. $return[] = ElggMenuItem::factory($options);
  43. // favourites count
  44. $count = elgg_view('favourites/count', array('entity' => $entity));
  45. if ($count) {
  46. $options = array(
  47. 'name' => 'favourites_count',
  48. 'text' => $count,
  49. 'href' => false,
  50. 'priority' => 1003,
  51. );
  52. $return[] = ElggMenuItem::factory($options);
  53. }
  54. return $return;
  55. }
  56. function is_allowed_type($entity) {
  57. $plugin = elgg_get_plugin_from_id('favourites');
  58. $allowed_object_subtypes_array = explode(', ', $plugin->allowed_object_subtypes);
  59. foreach($allowed_object_subtypes_array as $sub_type) {
  60. if(elgg_instanceof($entity, 'object', $sub_type)) {
  61. return true;
  62. }
  63. }
  64. return true;
  65. }
  66. /**
  67. * Add a mark as favourite button to river actions
  68. */
  69. function favourites_river_menu_setup($hook, $type, $return, $params) {
  70. if (elgg_is_logged_in()) {
  71. $item = $params['item'];
  72. $object = $item->getObjectEntity();
  73. if(!is_allowed_type($object)) {
  74. return $return;
  75. }
  76. if (!elgg_in_context('widgets') && $item->annotation_id == 0) {
  77. if ($object->canAnnotate(0, 'favourite')) {
  78. $options = array(
  79. 'name' => 'favourite',
  80. 'href' => false,
  81. 'text' => elgg_view('favourites/button', array('entity' => $object)),
  82. 'is_action' => true,
  83. 'priority' => 102,
  84. );
  85. $return[] = ElggMenuItem::factory($options);
  86. // favourites count
  87. $count = elgg_view('favourites/count', array('entity' => $object));
  88. if ($count) {
  89. $options = array(
  90. 'name' => 'favourites_count',
  91. 'text' => $count,
  92. 'href' => false,
  93. 'priority' => 103,
  94. );
  95. $return[] = ElggMenuItem::factory($options);
  96. }
  97. }
  98. }
  99. }
  100. return $return;
  101. }
  102. /**
  103. * Count how many people have marked an entity as favourite.
  104. *
  105. * @param ElggEntity $entity
  106. *
  107. * @return int Number of markings
  108. */
  109. function favourites_count($entity) {
  110. $type = $entity->getType();
  111. $params = array('entity' => $entity);
  112. $number = elgg_trigger_plugin_hook('favourites:count', $type, $params, false);
  113. if ($number) {
  114. return $number;
  115. }
  116. else {
  117. return $entity->countAnnotations('favourite');
  118. }
  119. }
  120. /**
  121. * Notify $owner that $user marked his $entity as favourite.
  122. *
  123. * @param type $owner
  124. * @param type $user
  125. * @param type $entity
  126. */
  127. function favourites_notify_user(ElggUser $owner, ElggUser $user, ElggEntity $entity) {
  128. if (!$owner instanceof ElggUser) {
  129. return false;
  130. }
  131. if (!$user instanceof ElggUser) {
  132. return false;
  133. }
  134. if (!$entity instanceof ElggEntity) {
  135. return false;
  136. }
  137. $title_str = $entity->title;
  138. if (!$title_str) {
  139. $title_str = elgg_get_excerpt($entity->description);
  140. }
  141. $site = get_config('site');
  142. $subject = elgg_echo('favourites:notifications:subject', array(
  143. $user->name,
  144. $title_str));
  145. $body = elgg_echo('favourites:notifications:body', array(
  146. $owner->name,
  147. $user->name,
  148. $title_str,
  149. $site->name,
  150. $entity->getURL(),
  151. $user->getURL()));
  152. notify_user($owner->guid,
  153. $user->guid,
  154. $subject,
  155. $body);
  156. }
  157. function favourites_page_handler($page) {
  158. elgg_set_context('favourites');
  159. echo elgg_view_page(
  160. elgg_echo("favourites:items"),
  161. elgg_view_layout(
  162. 'one_sidebar',
  163. array(
  164. 'title' => elgg_echo("favourites:items"),
  165. 'content' => elgg_view('favourites/view'),
  166. 'sidebar' => elgg_view('favourites/sidebar'),
  167. )
  168. )
  169. );
  170. return true;
  171. }