pages.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Pages function library
  4. */
  5. /**
  6. * Prepare the add/edit form variables
  7. *
  8. * @param ElggObject $page
  9. * @param int $parent_guid
  10. * @param ElggAnnotation $revision
  11. * @return array
  12. */
  13. function pages_prepare_form_vars($page = null, $parent_guid = 0, $revision = null) {
  14. // input names => defaults
  15. $values = array(
  16. 'title' => '',
  17. 'description' => '',
  18. 'access_id' => ACCESS_DEFAULT,
  19. 'write_access_id' => ACCESS_DEFAULT,
  20. 'tags' => '',
  21. 'container_guid' => elgg_get_page_owner_guid(),
  22. 'guid' => null,
  23. 'entity' => $page,
  24. 'parent_guid' => $parent_guid,
  25. );
  26. if ($page) {
  27. foreach (array_keys($values) as $field) {
  28. if (isset($page->$field)) {
  29. $values[$field] = $page->$field;
  30. }
  31. }
  32. }
  33. if (elgg_is_sticky_form('page')) {
  34. $sticky_values = elgg_get_sticky_values('page');
  35. foreach ($sticky_values as $key => $value) {
  36. $values[$key] = $value;
  37. }
  38. }
  39. elgg_clear_sticky_form('page');
  40. // load the revision annotation if requested
  41. if ($revision instanceof ElggAnnotation && $revision->entity_guid == $page->getGUID()) {
  42. $values['description'] = $revision->value;
  43. }
  44. return $values;
  45. }
  46. /**
  47. * Recurses the page tree and adds the breadcrumbs for all ancestors
  48. *
  49. * @param ElggObject $page Page entity
  50. */
  51. function pages_prepare_parent_breadcrumbs($page) {
  52. if ($page && $page->parent_guid) {
  53. $parents = array();
  54. $parent = get_entity($page->parent_guid);
  55. while ($parent) {
  56. array_push($parents, $parent);
  57. $parent = get_entity($parent->parent_guid);
  58. }
  59. while ($parents) {
  60. $parent = array_pop($parents);
  61. elgg_push_breadcrumb($parent->title, $parent->getURL());
  62. }
  63. }
  64. }
  65. /**
  66. * Produce the navigation tree
  67. *
  68. * @param ElggEntity $container Container entity for the pages
  69. *
  70. * @return array
  71. */
  72. function pages_get_navigation_tree($container) {
  73. if (!elgg_instanceof($container)) {
  74. return;
  75. }
  76. $top_pages = new ElggBatch('elgg_get_entities', array(
  77. 'type' => 'object',
  78. 'subtype' => 'page_top',
  79. 'container_guid' => $container->getGUID(),
  80. 'limit' => false,
  81. ));
  82. /* @var ElggBatch $top_pages Batch of top level pages */
  83. $tree = array();
  84. $depths = array();
  85. foreach ($top_pages as $page) {
  86. $tree[] = array(
  87. 'guid' => $page->getGUID(),
  88. 'title' => $page->title,
  89. 'url' => $page->getURL(),
  90. 'depth' => 0,
  91. );
  92. $depths[$page->guid] = 0;
  93. $stack = array();
  94. array_push($stack, $page);
  95. while (count($stack) > 0) {
  96. $parent = array_pop($stack);
  97. $children = new ElggBatch('elgg_get_entities_from_metadata', array(
  98. 'type' => 'object',
  99. 'subtype' => 'page',
  100. 'metadata_name' => 'parent_guid',
  101. 'metadata_value' => $parent->getGUID(),
  102. 'limit' => false,
  103. ));
  104. foreach ($children as $child) {
  105. $tree[] = array(
  106. 'guid' => $child->getGUID(),
  107. 'title' => $child->title,
  108. 'url' => $child->getURL(),
  109. 'parent_guid' => $parent->getGUID(),
  110. 'depth' => $depths[$parent->guid] + 1,
  111. );
  112. $depths[$child->guid] = $depths[$parent->guid] + 1;
  113. array_push($stack, $child);
  114. }
  115. }
  116. }
  117. return $tree;
  118. }
  119. /**
  120. * Register the navigation menu
  121. *
  122. * @param ElggEntity $container Container entity for the pages
  123. */
  124. function pages_register_navigation_tree($container) {
  125. $pages = pages_get_navigation_tree($container);
  126. if ($pages) {
  127. foreach ($pages as $page) {
  128. elgg_register_menu_item('pages_nav', array(
  129. 'name' => $page['guid'],
  130. 'text' => $page['title'],
  131. 'href' => $page['url'],
  132. 'parent_name' => $page['parent_guid'],
  133. ));
  134. }
  135. }
  136. }
  137. /**
  138. * Function checking delete permission
  139. *
  140. * @package ElggPages
  141. * @param mixed $value
  142. *
  143. * @return bool
  144. */
  145. function pages_can_delete_page($page) {
  146. if (! $page) {
  147. return false;
  148. } else {
  149. $container = get_entity($page->container_guid);
  150. return $container ? $container->canEdit() : false;
  151. }
  152. }