blog.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * Blog helper functions
  4. *
  5. * @package Blog
  6. */
  7. /**
  8. * Get page components to view a blog post.
  9. *
  10. * @param int $guid GUID of a blog entity.
  11. * @return array
  12. */
  13. function blog_get_page_content_read($guid = NULL) {
  14. $return = array();
  15. elgg_entity_gatekeeper($guid, 'object', 'blog');
  16. $blog = get_entity($guid);
  17. // no header or tabs for viewing an individual blog
  18. $return['filter'] = '';
  19. elgg_set_page_owner_guid($blog->container_guid);
  20. elgg_group_gatekeeper();
  21. $return['title'] = $blog->title;
  22. $container = $blog->getContainerEntity();
  23. $crumbs_title = $container->name;
  24. if (elgg_instanceof($container, 'group')) {
  25. elgg_push_breadcrumb($crumbs_title, "blog/group/$container->guid/all");
  26. } else {
  27. elgg_push_breadcrumb($crumbs_title, "blog/owner/$container->username");
  28. }
  29. elgg_push_breadcrumb($blog->title);
  30. $return['content'] = elgg_view_entity($blog, array('full_view' => true));
  31. // check to see if we should allow comments
  32. if ($blog->comments_on != 'Off' && $blog->status == 'published') {
  33. $return['content'] .= elgg_view_comments($blog);
  34. }
  35. return $return;
  36. }
  37. /**
  38. * Get page components to list a user's or all blogs.
  39. *
  40. * @param int $container_guid The GUID of the page owner or NULL for all blogs
  41. * @return array
  42. */
  43. function blog_get_page_content_list($container_guid = NULL) {
  44. $return = array();
  45. $return['filter_context'] = $container_guid ? 'mine' : 'all';
  46. $options = array(
  47. 'type' => 'object',
  48. 'subtype' => 'blog',
  49. 'full_view' => false,
  50. 'no_results' => elgg_echo('blog:none'),
  51. 'preload_owners' => true,
  52. 'distinct' => false,
  53. );
  54. $current_user = elgg_get_logged_in_user_entity();
  55. if ($container_guid) {
  56. // access check for closed groups
  57. elgg_group_gatekeeper();
  58. $options['container_guid'] = $container_guid;
  59. $container = get_entity($container_guid);
  60. if (!$container) {
  61. }
  62. $return['title'] = elgg_echo('blog:title:user_blogs', array($container->name));
  63. $crumbs_title = $container->name;
  64. elgg_push_breadcrumb($crumbs_title);
  65. if ($current_user && ($container_guid == $current_user->guid)) {
  66. $return['filter_context'] = 'mine';
  67. } else if (elgg_instanceof($container, 'group')) {
  68. $return['filter'] = false;
  69. } else {
  70. // do not show button or select a tab when viewing someone else's posts
  71. $return['filter_context'] = 'none';
  72. }
  73. } else {
  74. $options['preload_containers'] = true;
  75. $return['filter_context'] = 'all';
  76. $return['title'] = elgg_echo('blog:title:all_blogs');
  77. elgg_pop_breadcrumb();
  78. elgg_push_breadcrumb(elgg_echo('blog:blogs'));
  79. }
  80. elgg_register_title_button();
  81. $return['content'] = elgg_list_entities($options);
  82. return $return;
  83. }
  84. /**
  85. * Get page components to list of the user's friends' posts.
  86. *
  87. * @param int $user_guid
  88. * @return array
  89. */
  90. function blog_get_page_content_friends($user_guid) {
  91. $user = get_user($user_guid);
  92. if (!$user) {
  93. forward('blog/all');
  94. }
  95. $return = array();
  96. $return['filter_context'] = 'friends';
  97. $return['title'] = elgg_echo('blog:title:friends');
  98. $crumbs_title = $user->name;
  99. elgg_push_breadcrumb($crumbs_title, "blog/owner/{$user->username}");
  100. elgg_push_breadcrumb(elgg_echo('friends'));
  101. elgg_register_title_button();
  102. $options = array(
  103. 'type' => 'object',
  104. 'subtype' => 'blog',
  105. 'full_view' => false,
  106. 'relationship' => 'friend',
  107. 'relationship_guid' => $user_guid,
  108. 'relationship_join_on' => 'container_guid',
  109. 'no_results' => elgg_echo('blog:none'),
  110. 'preload_owners' => true,
  111. 'preload_containers' => true,
  112. );
  113. $return['content'] = elgg_list_entities_from_relationship($options);
  114. return $return;
  115. }
  116. /**
  117. * Get page components to show blogs with publish dates between $lower and $upper
  118. *
  119. * @param int $owner_guid The GUID of the owner of this page
  120. * @param int $lower Unix timestamp
  121. * @param int $upper Unix timestamp
  122. * @return array
  123. */
  124. function blog_get_page_content_archive($owner_guid, $lower = 0, $upper = 0) {
  125. $owner = get_entity($owner_guid);
  126. elgg_set_page_owner_guid($owner_guid);
  127. $crumbs_title = $owner->name;
  128. if (elgg_instanceof($owner, 'user')) {
  129. $url = "blog/owner/{$owner->username}";
  130. } else {
  131. $url = "blog/group/$owner->guid/all";
  132. }
  133. elgg_push_breadcrumb($crumbs_title, $url);
  134. elgg_push_breadcrumb(elgg_echo('blog:archives'));
  135. if ($lower) {
  136. $lower = (int)$lower;
  137. }
  138. if ($upper) {
  139. $upper = (int)$upper;
  140. }
  141. $options = array(
  142. 'type' => 'object',
  143. 'subtype' => 'blog',
  144. 'full_view' => false,
  145. 'no_results' => elgg_echo('blog:none'),
  146. 'preload_owners' => true,
  147. 'distinct' => false,
  148. );
  149. if ($owner_guid) {
  150. $options['container_guid'] = $owner_guid;
  151. }
  152. if ($lower) {
  153. $options['created_time_lower'] = $lower;
  154. }
  155. if ($upper) {
  156. $options['created_time_upper'] = $upper;
  157. }
  158. $content = elgg_list_entities($options);
  159. $title = elgg_echo('date:month:' . date('m', $lower), array(date('Y', $lower)));
  160. return array(
  161. 'content' => $content,
  162. 'title' => $title,
  163. 'filter' => '',
  164. );
  165. }
  166. /**
  167. * Get page components to edit/create a blog post.
  168. *
  169. * @param string $page 'edit' or 'new'
  170. * @param int $guid GUID of blog post or container
  171. * @param int $revision Annotation id for revision to edit (optional)
  172. * @return array
  173. */
  174. function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) {
  175. elgg_require_js('elgg/blog/save_draft');
  176. $return = array(
  177. 'filter' => '',
  178. );
  179. $vars = array();
  180. $vars['id'] = 'blog-post-edit';
  181. $vars['class'] = 'elgg-form-alt';
  182. $sidebar = '';
  183. if ($page == 'edit') {
  184. $blog = get_entity((int)$guid);
  185. $title = elgg_echo('blog:edit');
  186. if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
  187. $vars['entity'] = $blog;
  188. $title .= ": \"$blog->title\"";
  189. if ($revision) {
  190. $revision = elgg_get_annotation_from_id((int)$revision);
  191. $vars['revision'] = $revision;
  192. $title .= ' ' . elgg_echo('blog:edit_revision_notice');
  193. if (!$revision || !($revision->entity_guid == $guid)) {
  194. $content = elgg_echo('blog:error:revision_not_found');
  195. $return['content'] = $content;
  196. $return['title'] = $title;
  197. return $return;
  198. }
  199. }
  200. $body_vars = blog_prepare_form_vars($blog, $revision);
  201. elgg_push_breadcrumb($blog->title, $blog->getURL());
  202. elgg_push_breadcrumb(elgg_echo('edit'));
  203. elgg_require_js('elgg/blog/save_draft');
  204. $content = elgg_view_form('blog/save', $vars, $body_vars);
  205. $sidebar = elgg_view('blog/sidebar/revisions', $vars);
  206. } else {
  207. $content = elgg_echo('blog:error:cannot_edit_post');
  208. }
  209. } else {
  210. elgg_push_breadcrumb(elgg_echo('blog:add'));
  211. $body_vars = blog_prepare_form_vars(null);
  212. $title = elgg_echo('blog:add');
  213. $content = elgg_view_form('blog/save', $vars, $body_vars);
  214. }
  215. $return['title'] = $title;
  216. $return['content'] = $content;
  217. $return['sidebar'] = $sidebar;
  218. return $return;
  219. }
  220. /**
  221. * Pull together blog variables for the save form
  222. *
  223. * @param ElggBlog $post
  224. * @param ElggAnnotation $revision
  225. * @return array
  226. */
  227. function blog_prepare_form_vars($post = NULL, $revision = NULL) {
  228. // input names => defaults
  229. $values = array(
  230. 'title' => NULL,
  231. 'description' => NULL,
  232. 'status' => 'published',
  233. 'access_id' => ACCESS_DEFAULT,
  234. 'comments_on' => 'On',
  235. 'excerpt' => NULL,
  236. 'tags' => NULL,
  237. 'container_guid' => NULL,
  238. 'guid' => NULL,
  239. 'draft_warning' => '',
  240. );
  241. if ($post) {
  242. foreach (array_keys($values) as $field) {
  243. if (isset($post->$field)) {
  244. $values[$field] = $post->$field;
  245. }
  246. }
  247. if ($post->status == 'draft') {
  248. $values['access_id'] = $post->future_access;
  249. }
  250. }
  251. if (elgg_is_sticky_form('blog')) {
  252. $sticky_values = elgg_get_sticky_values('blog');
  253. foreach ($sticky_values as $key => $value) {
  254. $values[$key] = $value;
  255. }
  256. }
  257. elgg_clear_sticky_form('blog');
  258. if (!$post) {
  259. return $values;
  260. }
  261. // load the revision annotation if requested
  262. if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) {
  263. $values['revision'] = $revision;
  264. $values['description'] = $revision->value;
  265. }
  266. // display a notice if there's an autosaved annotation
  267. // and we're not editing it.
  268. $auto_save_annotations = $post->getAnnotations(array(
  269. 'annotation_name' => 'blog_auto_save',
  270. 'limit' => 1,
  271. ));
  272. if ($auto_save_annotations) {
  273. $auto_save = $auto_save_annotations[0];
  274. } else {
  275. $auto_save = false;
  276. }
  277. if ($auto_save && $auto_save->id != $revision->id) {
  278. $values['draft_warning'] = elgg_echo('blog:messages:warning:draft');
  279. }
  280. return $values;
  281. }