PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/pages/start.php

https://github.com/masuman/elgg-1
PHP | 355 lines | 211 code | 64 blank | 80 comment | 55 complexity | de8781ab7ed499c3669f5cc63302f1eb MD5 | raw file
  1. <?php
  2. /**
  3. * Elgg Pages
  4. *
  5. * @package ElggPages
  6. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  7. * @author Curverider Ltd
  8. * @copyright Curverider Ltd 2008-2009
  9. * @link http://elgg.com/
  10. */
  11. /**
  12. * Initialise the pages plugin.
  13. *
  14. */
  15. function pages_init()
  16. {
  17. global $CONFIG;
  18. // Set up the menu for logged in users
  19. if (isloggedin())
  20. {
  21. add_menu(elgg_echo('pages'), $CONFIG->wwwroot . "pg/pages/owned/" . $_SESSION['user']->username,'pages');
  22. }
  23. else
  24. {
  25. add_menu(elgg_echo('pages'), $CONFIG->wwwroot . "mod/pages/world.php");
  26. }
  27. // Extend hover-over menu
  28. extend_view('profile/menu/links','pages/menu');
  29. // Register a page handler, so we can have nice URLs
  30. register_page_handler('pages','pages_page_handler');
  31. // Register a url handler
  32. register_entity_url_handler('pages_url','object', 'page_top');
  33. register_entity_url_handler('pages_url','object', 'page');
  34. // Register some actions
  35. register_action("pages/edit",false, $CONFIG->pluginspath . "pages/actions/pages/edit.php");
  36. register_action("pages/editwelcome",false, $CONFIG->pluginspath . "pages/actions/pages/editwelcome.php");
  37. register_action("pages/delete",false, $CONFIG->pluginspath . "pages/actions/pages/delete.php");
  38. // Extend some views
  39. extend_view('css','pages/css');
  40. extend_view('groups/menu/links', 'pages/menu'); // Add to groups context
  41. extend_view('groups/right_column', 'pages/groupprofile_pages'); // Add to groups context
  42. // Register entity type
  43. register_entity_type('object','page');
  44. register_entity_type('object','page_top');
  45. // Register granular notification for this type
  46. if (is_callable('register_notification_object'))
  47. register_notification_object('object', 'page', elgg_echo('pages:new'));
  48. // Listen to notification events and supply a more useful message
  49. register_plugin_hook('notify:entity:message', 'object', 'page_notify_message');
  50. // add the group pages tool option
  51. add_group_tool_option('pages',elgg_echo('groups:enablepages'),true);
  52. //add a widget
  53. add_widget_type('pages',elgg_echo('pages'),elgg_echo('pages:widget:description'));
  54. // For now, we'll hard code the groups profile items as follows:
  55. // TODO make this user configurable
  56. // Language short codes must be of the form "pages:key"
  57. // where key is the array key below
  58. $CONFIG->pages = array(
  59. 'title' => 'text',
  60. 'description' => 'longtext',
  61. 'tags' => 'tags',
  62. 'access_id' => 'access',
  63. 'write_access_id' => 'access',
  64. );
  65. }
  66. function pages_url($entity) {
  67. global $CONFIG;
  68. return $CONFIG->url . "pg/pages/view/{$entity->guid}/";
  69. }
  70. /**
  71. * Sets up submenus for the pages system. Triggered on pagesetup.
  72. *
  73. */
  74. function pages_submenus() {
  75. global $CONFIG;
  76. $page_owner = page_owner_entity();
  77. // Group submenu option
  78. if ($page_owner instanceof ElggGroup && get_context() == 'groups') {
  79. if($page_owner->pages_enable != "no"){
  80. add_submenu_item(sprintf(elgg_echo("pages:group"),$page_owner->name), $CONFIG->wwwroot . "pg/pages/owned/" . $page_owner->username);
  81. }
  82. }
  83. }
  84. /**
  85. * Pages page handler.
  86. *
  87. * @param array $page
  88. */
  89. function pages_page_handler($page)
  90. {
  91. global $CONFIG;
  92. if (isset($page[0]))
  93. {
  94. // See what context we're using
  95. switch($page[0])
  96. {
  97. case "url" :
  98. case "slug":
  99. $pages = get_entities_from_metadata('slug', $page[1], 'object','',0, 40);
  100. if ($pages && sizeof($pages) > 0) {
  101. $body = $pages[0]->description;
  102. } else {
  103. $body = 'page does not exist';
  104. }
  105. $content = elgg_view_layout('one_column', $body);
  106. echo page_draw(null, $content);
  107. break;
  108. case "new" :
  109. include($CONFIG->pluginspath . "pages/new.php");
  110. break;
  111. case "welcome" :
  112. include($CONFIG->pluginspath . "pages/welcome.php");
  113. break;
  114. case "world":
  115. include($CONFIG->pluginspath . "pages/world.php");
  116. break;
  117. case "owned" :
  118. // Owned by a user
  119. if (isset($page[1]))
  120. set_input('username',$page[1]);
  121. include($CONFIG->pluginspath . "pages/index.php");
  122. break;
  123. case "edit" :
  124. if (isset($page[1]))
  125. set_input('page_guid', $page[1]);
  126. $entity = get_entity($page[1]);
  127. add_submenu_item(elgg_echo('pages:label:view'), $CONFIG->url . "pg/pages/view/{$page[1]}", 'pageslinks');
  128. // add_submenu_item(elgg_echo('pages:user'), $CONFIG->wwwroot . "pg/pages/owned/" . $_SESSION['user']->username, 'pageslinksgeneral');
  129. if (($entity) && ($entity->canEdit())) add_submenu_item(elgg_echo('pages:label:edit'), $CONFIG->url . "pg/pages/edit/{$page[1]}", 'pagesactions');
  130. add_submenu_item(elgg_echo('pages:label:history'), $CONFIG->url . "pg/pages/history/{$page[1]}", 'pageslinks');
  131. include($CONFIG->pluginspath . "pages/edit.php");
  132. break;
  133. case "view" :
  134. if (isset($page[1]))
  135. set_input('page_guid', $page[1]);
  136. extend_view('metatags','pages/metatags');
  137. $entity = get_entity($page[1]);
  138. //add_submenu_item(elgg_echo('pages:label:view'), $CONFIG->url . "pg/pages/view/{$page[1]}", 'pageslinks');
  139. if (($entity) && ($entity->canEdit())) add_submenu_item(elgg_echo('pages:label:edit'), $CONFIG->url . "pg/pages/edit/{$page[1]}", 'pagesactions');
  140. add_submenu_item(elgg_echo('pages:label:history'), $CONFIG->url . "pg/pages/history/{$page[1]}", 'pageslinks');
  141. include($CONFIG->pluginspath . "pages/view.php");
  142. break;
  143. case "history" :
  144. if (isset($page[1]))
  145. set_input('page_guid', $page[1]);
  146. extend_view('metatags','pages/metatags');
  147. $entity = get_entity($page[1]);
  148. add_submenu_item(elgg_echo('pages:label:view'), $CONFIG->url . "pg/pages/view/{$page[1]}", 'pageslinks');
  149. if (($entity) && ($entity->canEdit())) add_submenu_item(elgg_echo('pages:label:edit'), $CONFIG->url . "pg/pages/edit/{$page[1]}", 'pagesactions');
  150. add_submenu_item(elgg_echo('pages:label:history'), $CONFIG->url . "pg/pages/history/{$page[1]}", 'pageslinks');
  151. include($CONFIG->pluginspath . "pages/history.php");
  152. break;
  153. default:
  154. include($CONFIG->pluginspath . "pages/new.php");
  155. break;
  156. }
  157. }
  158. }
  159. /**
  160. * Returns a more meaningful message
  161. *
  162. * @param unknown_type $hook
  163. * @param unknown_type $entity_type
  164. * @param unknown_type $returnvalue
  165. * @param unknown_type $params
  166. */
  167. function page_notify_message($hook, $entity_type, $returnvalue, $params)
  168. {
  169. $entity = $params['entity'];
  170. $to_entity = $params['to_entity'];
  171. $method = $params['method'];
  172. if (($entity instanceof ElggEntity) && (($entity->getSubtype() == 'page_top') || ($entity->getSubtype() == 'page')))
  173. {
  174. $descr = $entity->description;
  175. $title = $entity->title;
  176. global $CONFIG;
  177. $url = $CONFIG->wwwroot . "pg/view/" . $entity->guid;
  178. if ($method == 'sms') {
  179. $owner = $entity->getOwnerEntity();
  180. return $owner->username . ' ' . elgg_echo("pages:via") . ': ' . $url . ' (' . $title . ')';
  181. }
  182. if ($method == 'email') {
  183. $owner = $entity->getOwnerEntity();
  184. return $owner->username . ' ' . elgg_echo("pages:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
  185. }
  186. if ($method == 'web') {
  187. $owner = $entity->getOwnerEntity();
  188. return $owner->username . ' ' . elgg_echo("pages:via") . ': ' . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
  189. }
  190. }
  191. return null;
  192. }
  193. /**
  194. * Sets the parent of the current page, for navigation purposes
  195. *
  196. * @param ElggObject $entity
  197. */
  198. function pages_set_navigation_parent(ElggObject $entity) {
  199. $guid = $entity->getGUID();
  200. while ($parent_guid = $entity->parent_guid) {
  201. $entity = get_entity($parent_guid);
  202. if ($entity) {
  203. $guid = $entity->getGUID();
  204. }
  205. }
  206. set_input('treeguid',$guid);
  207. }
  208. function pages_get_path($guid) {
  209. if (!$entity = get_entity($guid)) return array();
  210. $path = array($guid);
  211. while ($parent_guid = $entity->parent_guid) {
  212. $entity = get_entity($parent_guid);
  213. if ($entity) {
  214. $path[] = $entity->getGUID();
  215. }
  216. }
  217. return $path;
  218. }
  219. /**
  220. * Return the correct sidebar for a given entity
  221. *
  222. * @param ElggObject $entity
  223. */
  224. function pages_get_entity_sidebar(ElggObject $entity, $fulltree = 0)
  225. {
  226. $body = "";
  227. $children = get_entities_from_metadata('parent_guid',$entity->guid,'','',0,9999);
  228. $body .= elgg_view('pages/sidebar/sidebarthis', array('entity' => $entity,
  229. 'children' => $children,
  230. 'fulltree' => $fulltree));
  231. //$body = elgg_view('pages/sidebar/wrapper', array('body' => $body));
  232. return $body;
  233. }
  234. /**
  235. * Extend permissions checking to extend can-edit for write users.
  236. *
  237. * @param unknown_type $hook
  238. * @param unknown_type $entity_type
  239. * @param unknown_type $returnvalue
  240. * @param unknown_type $params
  241. */
  242. function pages_write_permission_check($hook, $entity_type, $returnvalue, $params)
  243. {
  244. if ($params['entity']->getSubtype() == 'page'
  245. || $params['entity']->getSubtype() == 'page_top') {
  246. $write_permission = $params['entity']->write_access_id;
  247. $user = $params['user'];
  248. if (($write_permission) && ($user))
  249. {
  250. // $list = get_write_access_array($user->guid);
  251. $list = get_access_array($user->guid); // get_access_list($user->guid);
  252. if (($write_permission!=0) && (in_array($write_permission,$list)))
  253. return true;
  254. }
  255. }
  256. }
  257. /**
  258. * Extend container permissions checking to extend can_write_to_container for write users.
  259. *
  260. * @param unknown_type $hook
  261. * @param unknown_type $entity_type
  262. * @param unknown_type $returnvalue
  263. * @param unknown_type $params
  264. */
  265. function pages_container_permission_check($hook, $entity_type, $returnvalue, $params) {
  266. if (get_context() == "pages") {
  267. if (page_owner()) {
  268. if (can_write_to_container($_SESSION['user']->guid, page_owner())) return true;
  269. }
  270. if ($page_guid = get_input('page_guid',0)) {
  271. $entity = get_entity($page_guid);
  272. } else if ($parent_guid = get_input('parent_guid',0)) {
  273. $entity = get_entity($parent_guid);
  274. }
  275. if ($entity instanceof ElggObject) {
  276. if (
  277. can_write_to_container($_SESSION['user']->guid, $entity->container_guid)
  278. || in_array($entity->write_access_id,get_access_list())
  279. ) {
  280. return true;
  281. }
  282. }
  283. }
  284. }
  285. // write permission plugin hooks
  286. register_plugin_hook('permissions_check', 'object', 'pages_write_permission_check');
  287. register_plugin_hook('container_permissions_check', 'object', 'pages_container_permission_check');
  288. // Make sure the pages initialisation function is called on initialisation
  289. register_elgg_event_handler('init','system','pages_init');
  290. register_elgg_event_handler('pagesetup','system','pages_submenus');
  291. ?>