discussion.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Discussion function library
  4. */
  5. /**
  6. * List all discussion topics
  7. */
  8. function discussion_handle_all_page() {
  9. elgg_pop_breadcrumb();
  10. elgg_push_breadcrumb(elgg_echo('discussion'));
  11. $content = elgg_list_entities(array(
  12. 'type' => 'object',
  13. 'subtype' => 'groupforumtopic',
  14. 'order_by' => 'e.last_action desc',
  15. 'limit' => max(20, elgg_get_config('default_limit')),
  16. 'full_view' => false,
  17. 'no_results' => elgg_echo('discussion:none'),
  18. 'preload_owners' => true,
  19. 'preload_containers' => true,
  20. ));
  21. $title = elgg_echo('discussion:latest');
  22. $params = array(
  23. 'content' => $content,
  24. 'title' => $title,
  25. 'sidebar' => elgg_view('discussion/sidebar'),
  26. 'filter' => '',
  27. );
  28. $body = elgg_view_layout('content', $params);
  29. echo elgg_view_page($title, $body);
  30. }
  31. /**
  32. * List discussion topics in a group
  33. *
  34. * @param int $guid Group entity GUID
  35. */
  36. function discussion_handle_list_page($guid) {
  37. elgg_set_page_owner_guid($guid);
  38. elgg_group_gatekeeper();
  39. $group = get_entity($guid);
  40. if (!elgg_instanceof($group, 'group')) {
  41. forward('', '404');
  42. }
  43. elgg_push_breadcrumb($group->name, $group->getURL());
  44. elgg_push_breadcrumb(elgg_echo('item:object:groupforumtopic'));
  45. elgg_register_title_button();
  46. $title = elgg_echo('item:object:groupforumtopic');
  47. $options = array(
  48. 'type' => 'object',
  49. 'subtype' => 'groupforumtopic',
  50. 'limit' => max(20, elgg_get_config('default_limit')),
  51. 'order_by' => 'e.last_action desc',
  52. 'container_guid' => $guid,
  53. 'full_view' => false,
  54. 'no_results' => elgg_echo('discussion:none'),
  55. 'preload_owners' => true,
  56. );
  57. $content = elgg_list_entities($options);
  58. $params = array(
  59. 'content' => $content,
  60. 'title' => $title,
  61. 'sidebar' => elgg_view('discussion/sidebar'),
  62. 'filter' => '',
  63. );
  64. $body = elgg_view_layout('content', $params);
  65. echo elgg_view_page($title, $body);
  66. }
  67. /**
  68. * Edit or add a discussion topic
  69. *
  70. * @param string $type 'add' or 'edit'
  71. * @param int $guid GUID of group or topic
  72. */
  73. function discussion_handle_edit_page($type, $guid) {
  74. elgg_gatekeeper();
  75. if ($type == 'add') {
  76. $group = get_entity($guid);
  77. if (!elgg_instanceof($group, 'group')) {
  78. register_error(elgg_echo('group:notfound'));
  79. forward();
  80. }
  81. // make sure user has permissions to add a topic to container
  82. if (!$group->canWriteToContainer(0, 'object', 'groupforumtopic')) {
  83. register_error(elgg_echo('groups:permissions:error'));
  84. forward($group->getURL());
  85. }
  86. $title = elgg_echo('groups:addtopic');
  87. elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
  88. elgg_push_breadcrumb($title);
  89. $body_vars = discussion_prepare_form_vars();
  90. $content = elgg_view_form('discussion/save', array(), $body_vars);
  91. } else {
  92. $topic = get_entity($guid);
  93. if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) {
  94. register_error(elgg_echo('discussion:topic:notfound'));
  95. forward();
  96. }
  97. $group = $topic->getContainerEntity();
  98. if (!elgg_instanceof($group, 'group')) {
  99. register_error(elgg_echo('group:notfound'));
  100. forward();
  101. }
  102. $title = elgg_echo('groups:edittopic');
  103. elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
  104. elgg_push_breadcrumb($topic->title, $topic->getURL());
  105. elgg_push_breadcrumb($title);
  106. $body_vars = discussion_prepare_form_vars($topic);
  107. $content = elgg_view_form('discussion/save', array(), $body_vars);
  108. }
  109. $params = array(
  110. 'content' => $content,
  111. 'title' => $title,
  112. 'sidebar' => elgg_view('discussion/sidebar/edit'),
  113. 'filter' => '',
  114. );
  115. $body = elgg_view_layout('content', $params);
  116. echo elgg_view_page($title, $body);
  117. }
  118. /**
  119. * Edit discussion reply
  120. *
  121. * @param string $type 'edit'
  122. * @param int $guid GUID of group or topic
  123. */
  124. function discussion_handle_reply_edit_page($type, $guid) {
  125. elgg_gatekeeper();
  126. if ($type == 'edit') {
  127. $reply = get_entity($guid);
  128. if (!elgg_instanceof($reply, 'object', 'discussion_reply', 'ElggDiscussionReply') || !$reply->canEdit()) {
  129. register_error(elgg_echo('discussion:reply:error:notfound'));
  130. forward();
  131. }
  132. $topic = $reply->getContainerEntity();
  133. if (!elgg_instanceof($topic, 'object', 'groupforumtopic')) {
  134. register_error(elgg_echo('discussion:topic:notfound'));
  135. forward();
  136. }
  137. $group = $topic->getContainerEntity();
  138. if (!elgg_instanceof($group, 'group')) {
  139. register_error(elgg_echo('group:notfound'));
  140. forward();
  141. }
  142. $title = elgg_echo('discussion:reply:edit');
  143. elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
  144. elgg_push_breadcrumb($topic->title, $topic->getURL());
  145. elgg_push_breadcrumb($title);
  146. $params = array(
  147. 'guid' => $reply->guid,
  148. 'hidden' => false,
  149. );
  150. $content = elgg_view('ajax/discussion/reply/edit', $params);
  151. }
  152. $params = array(
  153. 'content' => $content,
  154. 'title' => $title,
  155. 'sidebar' => elgg_view('discussion/sidebar/edit'),
  156. 'filter' => '',
  157. );
  158. $body = elgg_view_layout('content', $params);
  159. echo elgg_view_page($title, $body);
  160. }
  161. /**
  162. * View a discussion topic
  163. *
  164. * @param int $guid GUID of topic
  165. */
  166. function discussion_handle_view_page($guid) {
  167. // We now have RSS on topics
  168. global $autofeed;
  169. $autofeed = true;
  170. elgg_entity_gatekeeper($guid, 'object', 'groupforumtopic');
  171. $topic = get_entity($guid);
  172. $group = $topic->getContainerEntity();
  173. if (!elgg_instanceof($group, 'group')) {
  174. register_error(elgg_echo('group:notfound'));
  175. forward();
  176. }
  177. elgg_load_js('elgg.discussion');
  178. elgg_set_page_owner_guid($group->getGUID());
  179. elgg_group_gatekeeper();
  180. elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
  181. elgg_push_breadcrumb($topic->title);
  182. $params = array(
  183. 'topic' => $topic,
  184. 'show_add_form' => false,
  185. );
  186. $content = elgg_view_entity($topic, array('full_view' => true));
  187. if ($topic->status == 'closed') {
  188. $content .= elgg_view('discussion/replies', $params);
  189. $content .= elgg_view('discussion/closed');
  190. } elseif ($group->canWriteToContainer(0, 'object', 'groupforumtopic') || elgg_is_admin_logged_in()) {
  191. $params['show_add_form'] = true;
  192. $content .= elgg_view('discussion/replies', $params);
  193. } else {
  194. $content .= elgg_view('discussion/replies', $params);
  195. }
  196. $params = array(
  197. 'content' => $content,
  198. 'title' => $topic->title,
  199. 'sidebar' => elgg_view('discussion/sidebar'),
  200. 'filter' => '',
  201. );
  202. $body = elgg_view_layout('content', $params);
  203. echo elgg_view_page($topic->title, $body);
  204. }
  205. /**
  206. * Prepare discussion topic form variables
  207. *
  208. * @param ElggObject $topic Topic object if editing
  209. * @return array
  210. */
  211. function discussion_prepare_form_vars($topic = NULL) {
  212. // input names => defaults
  213. $values = array(
  214. 'title' => '',
  215. 'description' => '',
  216. 'status' => '',
  217. 'access_id' => ACCESS_DEFAULT,
  218. 'tags' => '',
  219. 'container_guid' => elgg_get_page_owner_guid(),
  220. 'guid' => null,
  221. 'topic' => $topic,
  222. );
  223. if ($topic) {
  224. foreach (array_keys($values) as $field) {
  225. if (isset($topic->$field)) {
  226. $values[$field] = $topic->$field;
  227. }
  228. }
  229. }
  230. if (elgg_is_sticky_form('topic')) {
  231. $sticky_values = elgg_get_sticky_values('topic');
  232. foreach ($sticky_values as $key => $value) {
  233. $values[$key] = $value;
  234. }
  235. }
  236. elgg_clear_sticky_form('topic');
  237. return $values;
  238. }