start.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * CRUD -- Create, Read, Update, Delete: a RESTful plugin for Elgg.
  4. *
  5. * @package Lorea
  6. * @subpackage CRUD
  7. * @homepage http://lorea.org/plugin/crud
  8. * @copyright 2012-2013 Lorea Faeries <federation@lorea.org>
  9. * @license COPYING, http://www.gnu.org/licenses/agpl
  10. *
  11. * Copyright 2012-2013 Lorea Faeries <federation@lorea.org>
  12. *
  13. * This program is free software: you can redistribute it and/or
  14. * modify it under the terms of the GNU Affero General Public License
  15. * as published by the Free Software Foundation, either version 3 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public
  24. * License along with this program. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. */
  27. elgg_register_event_handler('init', 'system', 'crud_init');
  28. /**
  29. * Format and return the URL for crud object.
  30. *
  31. * @param ElggObject $entity Assembly object
  32. * @return string URL of crud object.
  33. */
  34. function crud_url_handler($entity) {
  35. if (!$entity->getOwnerEntity()) {
  36. // default to a standard view if no owner.
  37. return FALSE;
  38. }
  39. /*if (!$entity->testAssembly()) {
  40. return FALSE;
  41. }*/
  42. //$friendly_title = elgg_get_friendly_title($entity->title);
  43. return $entity->getSubtype()."/view/{$entity->guid}";
  44. }
  45. /**
  46. * CRUD page handler
  47. *
  48. * URLs take the form of
  49. * List crud objects in group: <crud_type>/owner/<guid>
  50. * View crud object: <crud_type>/view/<guid>
  51. * Add crud object: <crud_type>/add/<guid>
  52. * Edit crud object: <crud_type>/edit/<guid>
  53. *
  54. * @param array $page Array of url segments for routing
  55. * @return bool
  56. */
  57. function crud_page_handler($page) {
  58. elgg_load_library('elgg:crud');
  59. if (!isset($page[0])) {
  60. $page[0] = 'all';
  61. }
  62. $page_url = current_page_url();
  63. $site_url = elgg_get_site_url();
  64. $current_url = str_replace($site_url, "", $page_url);
  65. $crud_type = current(explode('/', $current_url));
  66. $crud_handler = crud_get_handler($crud_type);
  67. $crud_module = $crud_handler->module;
  68. elgg_push_breadcrumb(elgg_echo($crud_module), '');
  69. switch ($page[0]) {
  70. case 'owner':
  71. case 'group':
  72. crud_handle_list_page($crud_handler, $page[1]);
  73. break;
  74. case 'add':
  75. crud_handle_edit_page($crud_handler, 'add', $page[1]);
  76. break;
  77. case 'edit':
  78. crud_handle_edit_page($crud_handler, 'edit', $page[1]);
  79. break;
  80. case 'view':
  81. crud_handle_view_page($crud_handler, $page[1]);
  82. break;
  83. case 'edit_general':
  84. crud_handle_edit_page($crud_handler, 'edit_general', $page[1]);
  85. break;
  86. default:
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Register a crud class
  93. */
  94. function crud_register_type($name, $variables, $class=NULL) {
  95. crud_register_subtype($name, $class);
  96. $object = crud_get_handler($name);
  97. $prev_variables = elgg_get_config($name);
  98. if (!empty($prev_variables)) {
  99. $variables = array_merge($prev_variables, $variables);
  100. }
  101. elgg_set_config($name, $variables);
  102. $object->variables = $variables;
  103. if (empty($prev_variables)) {
  104. // Register for search.
  105. elgg_register_entity_type('object', $name);
  106. // routing of urls
  107. elgg_register_page_handler($name, 'crud_page_handler');
  108. // override the default url to view a crud object
  109. elgg_register_entity_url_handler('object', $name, 'crud_url_handler');
  110. $action_path = elgg_get_plugins_path() . 'crud/actions/crud';
  111. elgg_register_action("$name/save", "$action_path/save.php");
  112. elgg_register_action("$name/delete", "$action_path/delete.php");
  113. // icon url override
  114. elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'crud_icon_url_override');
  115. }
  116. return $object;
  117. }
  118. /**
  119. * Override the default entity icon for crud entities
  120. *
  121. * @return string Relative URL
  122. */
  123. function crud_icon_url_override($hook, $type, $returnvalue, $params) {
  124. $entity = $params['entity'];
  125. if ($entity instanceof CrudObject) {
  126. $size = $params['size'];
  127. if (!$size)
  128. $icon = 'tiny';
  129. $icon = $entity->getCrudIcon($size);
  130. }
  131. return $icon;
  132. }
  133. /**
  134. * Get the crud handler for a given type
  135. */
  136. function crud_get_handler($name) {
  137. global $CONFIG;
  138. if (isset($CONFIG->crud->handlers[$name])) {
  139. return $CONFIG->crud->handlers[$name];
  140. }
  141. return $CONFIG->crud->handlers[$name] = new CrudTemplate($name);
  142. }
  143. /**
  144. * Owner block handler
  145. */
  146. function crud_owner_block_menu($hook, $type, $return, $params) {
  147. global $CONFIG;
  148. foreach($CONFIG->crud->handlers as $handler_name => $handler) {
  149. if (!$handler->owner_menu) {
  150. continue;
  151. }
  152. if ($handler->owner_menu != 'group' && elgg_instanceof($params['entity'], 'user')) {
  153. $url = "$handler_name/owner/{$params['entity']->username}";
  154. $item = new ElggMenuItem($handler_name, elgg_echo($handler_name), $url);
  155. $return[] = $item;
  156. } elseif ($handler->owner_menu != 'user') {
  157. if ($handler->module_check)
  158. $enable = "{$handler->module_check}_enable";
  159. else
  160. $enable = "{$handler->module}_enable";
  161. if ($params['entity']->$enable == "yes") {
  162. $owner_label = ($handler->owner_menu == 'group') ? 'owner' : 'group';
  163. $url = "$handler_name/$owner_label/{$params['entity']->guid}/all";
  164. $item = new ElggMenuItem($handler_name, elgg_echo("$handler_name:group"), $url);
  165. $return[] = $item;
  166. }
  167. }
  168. }
  169. return $return;
  170. }
  171. /**
  172. * Init crud plugin.
  173. */
  174. function crud_init() {
  175. global $CONFIG;
  176. $CONFIG->crud = new stdClass();
  177. $CONFIG->crud->handlers = array();
  178. elgg_register_library('elgg:crud', elgg_get_plugins_path() . 'crud/lib/crud.php');
  179. // add to the main css
  180. elgg_extend_view('css/elgg', 'crud/css');
  181. // register actions
  182. $action_path = elgg_get_plugins_path() . 'crud/actions/crud';
  183. elgg_register_action('crud/save', "$action_path/save.php");
  184. elgg_register_action('crud/delete', "$action_path/delete.php");
  185. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'crud_owner_block_menu');
  186. }
  187. /**
  188. * Register a crud subtype class into elgg engine
  189. */
  190. function crud_register_subtype($name, $class_name=NULL) {
  191. if (empty($class_name))
  192. $class_name = "CrudObject";
  193. if (get_subtype_id('object', $name)) {
  194. if (get_subtype_class('object', $name) != $class_name) {
  195. update_subtype('object', $name, $class_name);
  196. }
  197. } else {
  198. add_subtype('object', $name, $class_name);
  199. }
  200. }