PageRenderTime 35ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/elgg/mod/blog/lib/blog.php

https://github.com/brettp/Elgg-pagodabox
PHP | 435 lines | 288 code | 81 blank | 66 comment | 70 complexity | 0a5cc8cb4dc580b2b1b554d80be66db3 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause
  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. $blog = get_entity($guid);
  16. // no header or tabs for viewing an individual blog
  17. $return['filter'] = '';
  18. $return['header'] = '';
  19. if (!elgg_instanceof($blog, 'object', 'blog')) {
  20. $return['content'] = elgg_echo('blog:error:post_not_found');
  21. return $return;
  22. }
  23. $return['title'] = htmlspecialchars($blog->title);
  24. $container = $blog->getContainerEntity();
  25. $crumbs_title = $container->name;
  26. if (elgg_instanceof($container, 'group')) {
  27. elgg_push_breadcrumb($crumbs_title, "blog/group/$container->guid/all");
  28. } else {
  29. elgg_push_breadcrumb($crumbs_title, "blog/owner/$container->username");
  30. }
  31. elgg_push_breadcrumb($blog->title);
  32. $return['content'] = elgg_view_entity($blog, TRUE);
  33. //check to see if comment are on
  34. if ($blog->comments_on != 'Off') {
  35. $return['content'] .= elgg_view_comments($blog);
  36. }
  37. return $return;
  38. }
  39. /**
  40. * Get page components to list a user's or all blogs.
  41. *
  42. * @param int $owner_guid The GUID of the page owner or NULL for all blogs
  43. * @return array
  44. */
  45. function blog_get_page_content_list($container_guid = NULL) {
  46. $return = array();
  47. $return['filter_context'] = $container_guid ? 'mine' : 'all';
  48. $options = array(
  49. 'type' => 'object',
  50. 'subtype' => 'blog',
  51. 'full_view' => FALSE,
  52. );
  53. $loggedin_userid = elgg_get_logged_in_user_guid();
  54. if ($container_guid) {
  55. $options['container_guid'] = $container_guid;
  56. $container = get_entity($container_guid);
  57. if (!$container) {
  58. }
  59. $return['title'] = elgg_echo('blog:title:user_blogs', array($container->name));
  60. $crumbs_title = $container->name;
  61. elgg_push_breadcrumb($crumbs_title);
  62. if ($container_guid == $loggedin_userid) {
  63. $return['filter_context'] = 'mine';
  64. } else if (elgg_instanceof($container, 'group')) {
  65. $return['filter'] = false;
  66. } else {
  67. // do not show button or select a tab when viewing someone else's posts
  68. $return['filter_context'] = 'none';
  69. }
  70. } else {
  71. $return['filter_context'] = 'all';
  72. $return['title'] = elgg_echo('blog:title:all_blogs');
  73. }
  74. // show all posts for admin or users looking at their own blogs
  75. // show only published posts for other users.
  76. if (!(elgg_is_admin_logged_in() || (elgg_is_logged_in() && $container_guid == $loggedin_userid))) {
  77. $options['metadata_name_value_pairs'] = array(
  78. array('name' => 'status', 'value' => 'published'),
  79. );
  80. }
  81. $list = elgg_list_entities_from_metadata($options);
  82. if (!$list) {
  83. $return['content'] = elgg_echo('blog:none');
  84. } else {
  85. $return['content'] = $list;
  86. }
  87. return $return;
  88. }
  89. /**
  90. * Get page components to list of the user's friends' posts.
  91. *
  92. * @param int $user_guid
  93. * @return array
  94. */
  95. function blog_get_page_content_friends($user_guid) {
  96. $user = get_user($user_guid);
  97. $return = array();
  98. $return['filter_context'] = 'friends';
  99. $return['title'] = elgg_echo('blog:title:friends');
  100. $crumbs_title = $user->name;
  101. elgg_push_breadcrumb($crumbs_title, "blog/owner/{$user->username}");
  102. elgg_push_breadcrumb(elgg_echo('friends'));
  103. if (!$friends = get_user_friends($user_guid, ELGG_ENTITIES_ANY_VALUE, 0)) {
  104. $return['content'] .= elgg_echo('friends:none:you');
  105. return $return;
  106. } else {
  107. $options = array(
  108. 'type' => 'object',
  109. 'subtype' => 'blog',
  110. 'full_view' => FALSE,
  111. );
  112. foreach ($friends as $friend) {
  113. $options['container_guids'][] = $friend->getGUID();
  114. }
  115. // admin / owners can see any posts
  116. // everyone else can only see published posts
  117. if (!(elgg_is_admin_logged_in() || (elgg_is_logged_in() && $owner_guid == elgg_get_logged_in_user_guid()))) {
  118. if ($upper > $now) {
  119. $upper = $now;
  120. }
  121. $options['metadata_name_value_pairs'][] = array(
  122. array('name' => 'status', 'value' => 'published')
  123. );
  124. }
  125. $list = elgg_list_entities_from_metadata($options);
  126. if (!$list) {
  127. $return['content'] = elgg_echo('blog:none');
  128. } else {
  129. $return['content'] = $list;
  130. }
  131. }
  132. return $return;
  133. }
  134. /**
  135. * Get page components to show blogs with publish dates between $lower and $upper
  136. *
  137. * @param int $owner_guid The GUID of the owner of this page
  138. * @param int $lower Unix timestamp
  139. * @param int $upper Unix timestamp
  140. * @return array
  141. */
  142. function blog_get_page_content_archive($owner_guid, $lower = 0, $upper = 0) {
  143. $now = time();
  144. $user = get_user($owner_guid);
  145. elgg_set_page_owner_guid($owner_guid);
  146. $crumbs_title = $user->name;
  147. elgg_push_breadcrumb($crumbs_title, "blog/owner/{$user->username}");
  148. elgg_push_breadcrumb(elgg_echo('blog:archives'));
  149. if ($lower) {
  150. $lower = (int)$lower;
  151. }
  152. if ($upper) {
  153. $upper = (int)$upper;
  154. }
  155. $options = array(
  156. 'type' => 'object',
  157. 'subtype' => 'blog',
  158. 'full_view' => FALSE,
  159. );
  160. if ($owner_guid) {
  161. $options['owner_guid'] = $owner_guid;
  162. }
  163. // admin / owners can see any posts
  164. // everyone else can only see published posts
  165. if (!(elgg_is_admin_logged_in() || (elgg_is_logged_in() && $owner_guid == elgg_get_logged_in_user_guid()))) {
  166. if ($upper > $now) {
  167. $upper = $now;
  168. }
  169. $options['metadata_name_value_pairs'] = array(
  170. array('name' => 'status', 'value' => 'published')
  171. );
  172. }
  173. if ($lower) {
  174. $options['created_time_lower'] = $lower;
  175. }
  176. if ($upper) {
  177. $options['created_time_upper'] = $upper;
  178. }
  179. $list = elgg_list_entities_from_metadata($options);
  180. if (!$list) {
  181. $content .= elgg_echo('blog:none');
  182. } else {
  183. $content .= $list;
  184. }
  185. $title = elgg_echo('date:month:' . date('m', $lower), array(date('Y', $lower)));
  186. return array(
  187. 'content' => $content,
  188. 'title' => $title,
  189. 'buttons' => '',
  190. 'filter' => '',
  191. );
  192. }
  193. /**
  194. * Get page components to edit/create a blog post.
  195. *
  196. * @param string $page 'edit' or 'new'
  197. * @param int $guid GUID of blog post or container
  198. * @param int $revision Annotation id for revision to edit (optional)
  199. * @return array
  200. */
  201. function blog_get_page_content_edit($page, $guid = 0, $revision = NULL) {
  202. elgg_load_js('elgg.blog');
  203. $return = array(
  204. 'buttons' => '',
  205. 'filter' => '',
  206. );
  207. $vars = array();
  208. $vars['id'] = 'blog-post-edit';
  209. $vars['name'] = 'blog_post';
  210. if ($page == 'edit') {
  211. $blog = get_entity((int)$guid);
  212. $title = elgg_echo('blog:edit');
  213. if (elgg_instanceof($blog, 'object', 'blog') && $blog->canEdit()) {
  214. $vars['entity'] = $blog;
  215. $title .= ": \"$blog->title\"";
  216. if ($revision) {
  217. $revision = elgg_get_annotation_from_id((int)$revision);
  218. $vars['revision'] = $revision;
  219. $title .= ' ' . elgg_echo('blog:edit_revision_notice');
  220. if (!$revision || !($revision->entity_guid == $guid)) {
  221. $content = elgg_echo('blog:error:revision_not_found');
  222. $return['content'] = $content;
  223. $return['title'] = $title;
  224. return $return;
  225. }
  226. }
  227. $body_vars = blog_prepare_form_vars($blog, $revision);
  228. elgg_push_breadcrumb($blog->title, $blog->getURL());
  229. elgg_push_breadcrumb(elgg_echo('edit'));
  230. elgg_load_js('elgg.blog');
  231. $content = elgg_view_form('blog/save', $vars, $body_vars);
  232. $sidebar = elgg_view('blog/sidebar/revisions', $vars);
  233. } else {
  234. $content = elgg_echo('blog:error:cannot_edit_post');
  235. }
  236. } else {
  237. if (!$guid) {
  238. $container = elgg_get_logged_in_user_entity();
  239. } else {
  240. $container = get_entity($guid);
  241. }
  242. elgg_push_breadcrumb(elgg_echo('blog:add'));
  243. $body_vars = blog_prepare_form_vars($blog);
  244. $title = elgg_echo('blog:add');
  245. $content = elgg_view_form('blog/save', $vars, $body_vars);
  246. $blog_js = elgg_get_simplecache_url('js', 'blog/save_draft');
  247. elgg_register_js('elgg.blog', $blog_js);
  248. }
  249. $return['title'] = $title;
  250. $return['content'] = $content;
  251. $return['sidebar'] = $sidebar;
  252. return $return;
  253. }
  254. /**
  255. * Pull together blog variables for the save form
  256. *
  257. * @param ElggBlog $post
  258. * @param ElggAnnotation $revision
  259. * @return array
  260. */
  261. function blog_prepare_form_vars($post = NULL, $revision = NULL) {
  262. // input names => defaults
  263. $values = array(
  264. 'title' => NULL,
  265. 'description' => NULL,
  266. 'status' => 'published',
  267. 'access_id' => ACCESS_DEFAULT,
  268. 'comments_on' => 'On',
  269. 'excerpt' => NULL,
  270. 'tags' => NULL,
  271. 'container_guid' => NULL,
  272. 'guid' => NULL,
  273. 'draft_warning' => '',
  274. );
  275. if ($post) {
  276. foreach (array_keys($values) as $field) {
  277. if (isset($post->$field)) {
  278. $values[$field] = $post->$field;
  279. }
  280. }
  281. }
  282. if (elgg_is_sticky_form('blog')) {
  283. $sticky_values = elgg_get_sticky_values('blog');
  284. foreach ($sticky_values as $key => $value) {
  285. $values[$key] = $value;
  286. }
  287. }
  288. elgg_clear_sticky_form('blog');
  289. if (!$post) {
  290. return $values;
  291. }
  292. // load the revision annotation if requested
  293. if ($revision instanceof ElggAnnotation && $revision->entity_guid == $post->getGUID()) {
  294. $values['revision'] = $revision;
  295. $values['description'] = $revision->value;
  296. }
  297. // display a notice if there's an autosaved annotation
  298. // and we're not editing it.
  299. if ($auto_save_annotations = $post->getAnnotations('blog_auto_save', 1)) {
  300. $auto_save = $auto_save_annotations[0];
  301. } else {
  302. $auto_save == FALSE;
  303. }
  304. if ($auto_save && $auto_save->id != $revision->id) {
  305. $values['draft_warning'] = elgg_echo('blog:messages:warning:draft');
  306. }
  307. return $values;
  308. }
  309. /**
  310. * Forward to the new style of URLs
  311. *
  312. * @param string $page
  313. */
  314. function blog_url_forwarder($page) {
  315. global $CONFIG;
  316. // group usernames
  317. if (substr_count($page[0], 'group:')) {
  318. preg_match('/group\:([0-9]+)/i', $page[0], $matches);
  319. $guid = $matches[1];
  320. $entity = get_entity($guid);
  321. if ($entity) {
  322. $url = "{$CONFIG->wwwroot}blog/group/$guid/all";
  323. register_error(elgg_echo("changebookmark"));
  324. forward($url);
  325. }
  326. }
  327. // user usernames
  328. $user = get_user_by_username($page[0]);
  329. if (!$user) {
  330. return;
  331. }
  332. if (!isset($page[1])) {
  333. $page[1] = 'owner';
  334. }
  335. switch ($page[1]) {
  336. case "read":
  337. $url = "{$CONFIG->wwwroot}blog/view/{$page[2]}/{$page[3]}";
  338. break;
  339. case "archive":
  340. $url = "{$CONFIG->wwwroot}blog/archive/{$page[0]}/{$page[2]}/{$page[3]}";
  341. break;
  342. case "friends":
  343. $url = "{$CONFIG->wwwroot}blog/friends/{$page[0]}";
  344. break;
  345. case "new":
  346. $url = "{$CONFIG->wwwroot}blog/add/$user->guid";
  347. break;
  348. case "owner":
  349. $url = "{$CONFIG->wwwroot}blog/owner/{$page[0]}";
  350. break;
  351. }
  352. register_error(elgg_echo("changebookmark"));
  353. forward($url);
  354. }