start.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /**
  3. * Blogs
  4. *
  5. * @package Blog
  6. *
  7. * @todo
  8. * - Either drop support for "publish date" or duplicate more entity getter
  9. * functions to work with a non-standard time_created.
  10. * - Pingbacks
  11. * - Notifications
  12. * - River entry for posts saved as drafts and later published
  13. */
  14. elgg_register_event_handler('init', 'system', 'blog_init');
  15. /**
  16. * Init blog plugin.
  17. */
  18. function blog_init() {
  19. elgg_register_library('elgg:blog', elgg_get_plugins_path() . 'blog/lib/blog.php');
  20. // add a site navigation item
  21. $item = new ElggMenuItem('blog', elgg_echo('blog:blogs'), 'blog/all');
  22. elgg_register_menu_item('site', $item);
  23. elgg_register_event_handler('upgrade', 'upgrade', 'blog_run_upgrades');
  24. // add to the main css
  25. elgg_extend_view('css/elgg', 'blog/css');
  26. // routing of urls
  27. elgg_register_page_handler('blog', 'blog_page_handler');
  28. // override the default url to view a blog object
  29. elgg_register_plugin_hook_handler('entity:url', 'object', 'blog_set_url');
  30. // notifications
  31. elgg_register_notification_event('object', 'blog', array('publish'));
  32. elgg_register_plugin_hook_handler('prepare', 'notification:publish:object:blog', 'blog_prepare_notification');
  33. // add blog link to
  34. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'blog_owner_block_menu');
  35. // pingbacks
  36. //elgg_register_event_handler('create', 'object', 'blog_incoming_ping');
  37. //elgg_register_plugin_hook_handler('pingback:object:subtypes', 'object', 'blog_pingback_subtypes');
  38. // Register for search.
  39. elgg_register_entity_type('object', 'blog');
  40. // Add group option
  41. add_group_tool_option('blog', elgg_echo('blog:enableblog'), true);
  42. elgg_extend_view('groups/tool_latest', 'blog/group_module');
  43. // add a blog widget
  44. elgg_register_widget_type('blog', elgg_echo('blog'), elgg_echo('blog:widget:description'));
  45. // register actions
  46. $action_path = elgg_get_plugins_path() . 'blog/actions/blog';
  47. elgg_register_action('blog/save', "$action_path/save.php");
  48. elgg_register_action('blog/auto_save_revision', "$action_path/auto_save_revision.php");
  49. elgg_register_action('blog/delete', "$action_path/delete.php");
  50. // entity menu
  51. elgg_register_plugin_hook_handler('register', 'menu:entity', 'blog_entity_menu_setup');
  52. // ecml
  53. elgg_register_plugin_hook_handler('get_views', 'ecml', 'blog_ecml_views_hook');
  54. }
  55. /**
  56. * Dispatches blog pages.
  57. * URLs take the form of
  58. * All blogs: blog/all
  59. * User's blogs: blog/owner/<username>
  60. * Friends' blog: blog/friends/<username>
  61. * User's archives: blog/archives/<username>/<time_start>/<time_stop>
  62. * Blog post: blog/view/<guid>/<title>
  63. * New post: blog/add/<guid>
  64. * Edit post: blog/edit/<guid>/<revision>
  65. * Preview post: blog/preview/<guid>
  66. * Group blog: blog/group/<guid>/all
  67. *
  68. * Title is ignored
  69. *
  70. * @todo no archives for all blogs or friends
  71. *
  72. * @param array $page
  73. * @return bool
  74. */
  75. function blog_page_handler($page) {
  76. elgg_load_library('elgg:blog');
  77. // push all blogs breadcrumb
  78. elgg_push_breadcrumb(elgg_echo('blog:blogs'), "blog/all");
  79. if (!isset($page[0])) {
  80. $page[0] = 'all';
  81. }
  82. $page_type = $page[0];
  83. switch ($page_type) {
  84. case 'owner':
  85. $user = get_user_by_username($page[1]);
  86. if (!$user) {
  87. forward('', '404');
  88. }
  89. $params = blog_get_page_content_list($user->guid);
  90. break;
  91. case 'friends':
  92. $user = get_user_by_username($page[1]);
  93. if (!$user) {
  94. forward('', '404');
  95. }
  96. $params = blog_get_page_content_friends($user->guid);
  97. break;
  98. case 'archive':
  99. $user = get_user_by_username($page[1]);
  100. if (!$user) {
  101. forward('', '404');
  102. }
  103. $params = blog_get_page_content_archive($user->guid, $page[2], $page[3]);
  104. break;
  105. case 'view':
  106. $params = blog_get_page_content_read($page[1]);
  107. break;
  108. case 'add':
  109. elgg_gatekeeper();
  110. $params = blog_get_page_content_edit($page_type, $page[1]);
  111. break;
  112. case 'edit':
  113. elgg_gatekeeper();
  114. $params = blog_get_page_content_edit($page_type, $page[1], $page[2]);
  115. break;
  116. case 'group':
  117. $group = get_entity($page[1]);
  118. if (!elgg_instanceof($group, 'group')) {
  119. forward('', '404');
  120. }
  121. if (!isset($page[2]) || $page[2] == 'all') {
  122. $params = blog_get_page_content_list($page[1]);
  123. } else {
  124. $params = blog_get_page_content_archive($page[1], $page[3], $page[4]);
  125. }
  126. break;
  127. case 'all':
  128. $params = blog_get_page_content_list();
  129. break;
  130. default:
  131. return false;
  132. }
  133. if (isset($params['sidebar'])) {
  134. $params['sidebar'] .= elgg_view('blog/sidebar', array('page' => $page_type));
  135. } else {
  136. $params['sidebar'] = elgg_view('blog/sidebar', array('page' => $page_type));
  137. }
  138. $body = elgg_view_layout('content', $params);
  139. echo elgg_view_page($params['title'], $body);
  140. return true;
  141. }
  142. /**
  143. * Format and return the URL for blogs.
  144. *
  145. * @param string $hook
  146. * @param string $type
  147. * @param string $url
  148. * @param array $params
  149. * @return string URL of blog.
  150. */
  151. function blog_set_url($hook, $type, $url, $params) {
  152. $entity = $params['entity'];
  153. if (elgg_instanceof($entity, 'object', 'blog')) {
  154. $friendly_title = elgg_get_friendly_title($entity->title);
  155. return "blog/view/{$entity->guid}/$friendly_title";
  156. }
  157. }
  158. /**
  159. * Add a menu item to an ownerblock
  160. */
  161. function blog_owner_block_menu($hook, $type, $return, $params) {
  162. if (elgg_instanceof($params['entity'], 'user')) {
  163. $url = "blog/owner/{$params['entity']->username}";
  164. $item = new ElggMenuItem('blog', elgg_echo('blog'), $url);
  165. $return[] = $item;
  166. } else {
  167. if ($params['entity']->blog_enable != "no") {
  168. $url = "blog/group/{$params['entity']->guid}/all";
  169. $item = new ElggMenuItem('blog', elgg_echo('blog:group'), $url);
  170. $return[] = $item;
  171. }
  172. }
  173. return $return;
  174. }
  175. /**
  176. * Add particular blog links/info to entity menu
  177. */
  178. function blog_entity_menu_setup($hook, $type, $return, $params) {
  179. if (elgg_in_context('widgets')) {
  180. return $return;
  181. }
  182. $entity = $params['entity'];
  183. $handler = elgg_extract('handler', $params, false);
  184. if ($handler != 'blog') {
  185. return $return;
  186. }
  187. if ($entity->status != 'published') {
  188. // draft status replaces access
  189. foreach ($return as $index => $item) {
  190. if ($item->getName() == 'access') {
  191. unset($return[$index]);
  192. }
  193. }
  194. $status_text = elgg_echo("status:{$entity->status}");
  195. $options = array(
  196. 'name' => 'published_status',
  197. 'text' => "<span>$status_text</span>",
  198. 'href' => false,
  199. 'priority' => 150,
  200. );
  201. $return[] = ElggMenuItem::factory($options);
  202. }
  203. return $return;
  204. }
  205. /**
  206. * Prepare a notification message about a published blog
  207. *
  208. * @param string $hook Hook name
  209. * @param string $type Hook type
  210. * @param Elgg\Notifications\Notification $notification The notification to prepare
  211. * @param array $params Hook parameters
  212. * @return Elgg\Notifications\Notification
  213. */
  214. function blog_prepare_notification($hook, $type, $notification, $params) {
  215. $entity = $params['event']->getObject();
  216. $owner = $params['event']->getActor();
  217. $recipient = $params['recipient'];
  218. $language = $params['language'];
  219. $method = $params['method'];
  220. $notification->subject = elgg_echo('blog:notify:subject', array($entity->title), $language);
  221. $notification->body = elgg_echo('blog:notify:body', array(
  222. $owner->name,
  223. $entity->title,
  224. $entity->getExcerpt(),
  225. $entity->getURL()
  226. ), $language);
  227. $notification->summary = elgg_echo('blog:notify:summary', array($entity->title), $language);
  228. return $notification;
  229. }
  230. /**
  231. * Register blogs with ECML.
  232. */
  233. function blog_ecml_views_hook($hook, $entity_type, $return_value, $params) {
  234. $return_value['object/blog'] = elgg_echo('blog:blogs');
  235. return $return_value;
  236. }
  237. /**
  238. * Upgrade from 1.7 to 1.8.
  239. */
  240. function blog_run_upgrades($event, $type, $details) {
  241. $blog_upgrade_version = elgg_get_plugin_setting('upgrade_version', 'blogs');
  242. if (!$blog_upgrade_version) {
  243. // When upgrading, check if the ElggBlog class has been registered as this
  244. // was added in Elgg 1.8
  245. if (!update_subtype('object', 'blog', 'ElggBlog')) {
  246. add_subtype('object', 'blog', 'ElggBlog');
  247. }
  248. elgg_set_plugin_setting('upgrade_version', 1, 'blogs');
  249. }
  250. }