crud.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. /**
  3. * CRUD -- CRUD library
  4. *
  5. * @package Lorea
  6. * @subpackage CRUD
  7. *
  8. * Copyright 2012-2013 Lorea Faeries <federation@lorea.org>
  9. *
  10. * This program is free software: you can redistribute it and/or
  11. * modify it under the terms of the GNU Affero General Public License
  12. * as published by the Free Software Foundation, either version 3 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this program. If not, see
  22. * <http://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * List crud objects in a group
  26. *
  27. * @param CrudTemplate $crud Crud template object
  28. * @param int $guid Group entity GUID
  29. */
  30. function crud_handle_list_page($crud, $guid) {
  31. elgg_set_page_owner_guid($guid);
  32. $crud_type = $crud->crud_type;
  33. $parent = get_entity($guid);
  34. if ($parent instanceof ElggGroup) {
  35. $group = $parent;
  36. $parent = NULL;
  37. }
  38. else {
  39. $group = get_entity($parent->container_guid);
  40. }
  41. if (!$group) {
  42. register_error(elgg_echo('groups:notfound'));
  43. forward();
  44. }
  45. group_gatekeeper();
  46. elgg_push_breadcrumb($group->name);
  47. elgg_register_title_button();
  48. if (elgg_view_exists("forms/$crud->module/{$crud_type}_general")) {
  49. elgg_register_title_button($crud->crud_type, 'edit_general');
  50. }
  51. $title = elgg_echo("item:object:$crud_type");
  52. $params = array(
  53. 'title' => $title,
  54. 'content' => $crud->getListTabContent(),
  55. 'footer' => elgg_view("$crud->module/{$crud_type}_general", array('entity' => $group)),
  56. 'filter' => $crud->getListTabFilter(),
  57. );
  58. $params['sidebar'] .= elgg_view('crud/tagcloud_block', array(
  59. 'subtypes' => $crud->crud_type,
  60. 'owner_guid' => elgg_get_page_owner_guid(),
  61. ));
  62. $body = elgg_view_layout('content', $params);
  63. echo elgg_view_page($title, $body);
  64. }
  65. /**
  66. * Edit or add a crud object
  67. *
  68. * @param CrudTemplate $crud Crud template object
  69. * @param string $type 'add' or 'edit'
  70. * @param int $guid GUID of group or crud object
  71. */
  72. function crud_handle_edit_page($crud, $type, $guid) {
  73. gatekeeper();
  74. $crud_type = $crud->crud_type;
  75. if ($type == 'add') {
  76. $parent = get_entity($guid);
  77. if ($parent instanceof ElggGroup) {
  78. $group = $parent;
  79. $parent = NULL;
  80. }
  81. else {
  82. $group = get_entity($parent->container_guid);
  83. }
  84. if (!$group) {
  85. register_error(elgg_echo('groups:notfound'));
  86. forward();
  87. }
  88. elgg_set_page_owner_guid($group->guid);
  89. // make sure user has permissions to add a crud object to container
  90. if (!$group->canWriteToContainer(0, 'object', $crud_type)) {
  91. register_error(elgg_echo('crud:permissions:error'));
  92. forward($group->getURL());
  93. }
  94. $title = elgg_echo($crud_type . ':add');
  95. if ($parent) {
  96. crud_push_breadcrumb('new', $parent);
  97. }
  98. else
  99. elgg_push_breadcrumb($group->name, $crud_type."/owner/$group->guid");
  100. elgg_push_breadcrumb($title);
  101. $body_vars = crud_prepare_form_vars($crud, NULL, $parent);
  102. $content = elgg_view_form('crud/save', array('crud' => $crud), $body_vars);
  103. } elseif ($type == 'edit_general') {
  104. $entity = get_entity($guid);
  105. if (!$entity || !$entity->canEdit()) {
  106. register_error(elgg_echo('groups:notfound'));
  107. forward();
  108. }
  109. $group = $entity;
  110. $title = elgg_echo($crud_type . ':edit_general');
  111. elgg_push_breadcrumb($group->name, $crud_type . "/owner/$group->guid");
  112. elgg_push_breadcrumb($title);
  113. $body_vars = crud_prepare_form_vars($crud, $entity, $parent);
  114. $content = elgg_view_form("$crud->module/{$crud_type}_general", array('crud' => $crud), $body_vars);
  115. } else {
  116. $entity = get_entity($guid);
  117. if (!$entity || !$entity->canEdit()) {
  118. register_error(elgg_echo('groups:notfound'));
  119. forward();
  120. }
  121. $group = $entity->getContainerEntity();
  122. if (!$group) {
  123. register_error(elgg_echo('groups:notfound'));
  124. forward();
  125. }
  126. $parent = get_entity($entity->parent_guid);
  127. $title = elgg_echo($crud_type . ':edit');
  128. elgg_push_breadcrumb($group->name, $crud_type . "/owner/$group->guid");
  129. elgg_push_breadcrumb($entity->title, $entity->getURL());
  130. elgg_push_breadcrumb($title);
  131. $body_vars = crud_prepare_form_vars($crud, $entity, $parent);
  132. $content = elgg_view_form('crud/save', array('crud' => $crud), $body_vars);
  133. }
  134. $params = array(
  135. 'content' => $content,
  136. 'title' => $title,
  137. 'filter' => '',
  138. );
  139. $body = elgg_view_layout('content', $params);
  140. echo elgg_view_page($title, $body);
  141. }
  142. /**
  143. * Push breadcrumbs from the given crud object going up in the parent hirarchy
  144. *
  145. * @param ElggObject $last Ending object
  146. * @param ElggObject $entity Current object
  147. * @param CrudTemplate $crud Crud template object
  148. */
  149. function crud_push_breadcrumb($last, $entity, $crud = NULL) {
  150. if (empty($crud)) {
  151. $crud = crud_get_handler($entity->getSubtype());
  152. }
  153. if ($entity->parent_guid) {
  154. $parent = get_entity($entity->parent_guid);
  155. crud_push_breadcrumb($last, $parent);
  156. }
  157. else {
  158. $group = $entity->getContainerEntity();
  159. elgg_push_breadcrumb($group->name, "$crud->crud_type/owner/$entity->container_guid");
  160. }
  161. $title = $entity->title;
  162. if (empty($title)) {
  163. $title = elgg_echo("$crud->module:$crud->crud_type");
  164. }
  165. if ($entity == $last)
  166. elgg_push_breadcrumb($title);
  167. else
  168. elgg_push_breadcrumb($title, "$crud->crud_type/view/$entity->guid");
  169. }
  170. /**
  171. * View a crud object
  172. *
  173. * @param CrudTemplate $crud Crud template object
  174. * @param int $guid GUID of a crud object
  175. */
  176. function crud_handle_view_page($crud, $guid) {
  177. // We now have RSS on assemblies
  178. global $autofeed;
  179. $autofeed = true;
  180. $crud_type = $crud->crud_type;
  181. $entity = get_entity($guid);
  182. $parent = $entity->getParentEntity();
  183. if ($parent instanceof ElggGroup) {
  184. $group = $parent;
  185. $parent = NULL;
  186. }
  187. else {
  188. $group = get_entity($entity->container_guid);
  189. }
  190. if (!$group) {
  191. register_error(elgg_echo('noaccess'));
  192. $_SESSION['last_forward_from'] = current_page_url();
  193. forward('');
  194. }
  195. /*$group = $entity->getContainerEntity();
  196. if (!$group) {
  197. register_error(elgg_echo('groups:notfound'));
  198. forward();
  199. }*/
  200. if (!empty($crud->children_type) && $group->canWriteToContainer()) {
  201. elgg_set_page_owner_guid($guid);
  202. elgg_register_menu_item('title', array(
  203. 'name' => 'add',
  204. 'href' => "$crud->children_type/add/$guid",
  205. 'text' => elgg_echo("$crud->children_type:add"),
  206. 'link_class' => 'elgg-button elgg-button-action',
  207. ));
  208. }
  209. elgg_trigger_plugin_hook("crud:$crud_type:view_buttons", 'view_buttons', array('crud'=>$crud, 'type'=>$crud_type, 'entity'=>$entity), $location);
  210. elgg_set_page_owner_guid($entity->container_guid);
  211. group_gatekeeper();
  212. crud_push_breadcrumb($entity, $entity, $crud);
  213. $content = elgg_view_entity($entity, array('full_view' => true));
  214. $content .= elgg_view_comments($entity);
  215. $params = array(
  216. 'content' => $content,
  217. 'title' => $entity->getTitle(true),
  218. 'filter' => '',
  219. );
  220. $sidebar = elgg_view('crud/tagcloud_block', array(
  221. 'subtypes' => $crud->crud_type,
  222. 'owner_guid' => elgg_get_page_owner_guid(),
  223. ));
  224. if (isset($params['sidebar'])) {
  225. $params['sidebar'] .= $sidebar;
  226. } else {
  227. $params['sidebar'] = $sidebar;
  228. }
  229. $body = elgg_view_layout('content', $params);
  230. echo elgg_view_page($entity->title, $body);
  231. }
  232. /**
  233. * Prepare crud object form variables
  234. *
  235. * @param CrudTemplate $crud Crud template object
  236. * @param ElggObject $object Crud object if editing
  237. * @return array
  238. */
  239. function crud_prepare_form_vars($crud, $object = NULL, $parent = NULL) {
  240. $crud_type = $crud->crud_type;
  241. if ($object)
  242. $guid = $object->guid;
  243. // input names => defaults
  244. $values = array(
  245. 'title' => $object->title,
  246. 'description' => $object->description,
  247. 'access_id' => ACCESS_DEFAULT,
  248. 'tags' => '',
  249. 'crud' => $crud,
  250. 'container_guid' => elgg_get_page_owner_guid(),
  251. 'parent_guid' => $parent->guid,
  252. 'guid' => $guid,
  253. 'entity' => $object,
  254. );
  255. $variables = elgg_get_config($crud_type);
  256. foreach ($variables as $name => $type) {
  257. if (empty($values[$name])) {
  258. if ($type == 'date') {
  259. $values[$name] = time();
  260. }
  261. else {
  262. $values[$name] = '';
  263. }
  264. }
  265. }
  266. if ($object) {
  267. foreach (array_keys($values) as $field) {
  268. if (isset($object->$field)) {
  269. $values[$field] = $object->$field;
  270. }
  271. }
  272. }
  273. if (elgg_is_sticky_form($crud_type)) {
  274. $sticky_values = elgg_get_sticky_values($crud_type);
  275. foreach ($sticky_values as $key => $value) {
  276. $values[$key] = $value;
  277. }
  278. }
  279. elgg_clear_sticky_form($crud_type);
  280. return $values;
  281. }