CrudObject.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * CRUD -- Define a RESTful resource entity
  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. class CrudObject extends ElggObject {
  25. /*
  26. * Get the crud template for current entity
  27. */
  28. function getCrudTemplate() {
  29. return crud_get_handler($this->getSubType());
  30. }
  31. /*
  32. * Returns the parent entity
  33. */
  34. function getParentEntity() {
  35. if ($this->parent_guid)
  36. return get_entity($this->parent_guid);
  37. }
  38. /*
  39. * Get the title for current entity
  40. */
  41. function getTitle($full_view=false) {
  42. $template = $this->getCrudTemplate();
  43. $title = $this->title;
  44. if (empty($title)) {
  45. $title = $template->getDefaultValue('title', '');
  46. }
  47. if ($template->title_extend && !$full_view) {
  48. $varname = $template->title_extend;
  49. $value = date(elgg_echo('crud:date_format'), $this->$varname);
  50. if ($title)
  51. $title .= ", $value";
  52. else
  53. $title = $value;
  54. }
  55. return $title;
  56. }
  57. /*
  58. * Get the title formatted with a link to current entity
  59. */
  60. function getTitleLink($full_view=false) {
  61. $title = $this->getTitle($full_view);
  62. $title_link = elgg_view('output/url', array(
  63. 'href' => $this->getURL(),
  64. 'text' => $title,
  65. ));
  66. return $title_link;
  67. }
  68. /**
  69. * List children for given entity
  70. *
  71. * @param entity $entity Entity to operate on
  72. */
  73. function listChildren() {
  74. $crud = $this->getCrudTemplate();
  75. if ($crud->children_categories) {
  76. return $this->listChildrenCategories();
  77. }
  78. else {
  79. return $this->listChildrenRaw();
  80. }
  81. }
  82. /**
  83. * List children for given entity
  84. *
  85. * @param entity $entity Entity to operate on
  86. */
  87. function listChildrenRaw() {
  88. $crud = $this->getCrudTemplate();
  89. $child_subtype = $crud->children_type;
  90. $child_options = array('full_view' => FALSE,
  91. 'types' => 'object',
  92. 'subtypes' => $child_subtype,
  93. 'limit' => 10,
  94. 'list_class' => 'list-'.$child_subtype,
  95. 'metadata_name_value_pairs' => array(
  96. array('name' => 'parent_guid',
  97. 'value' => $this->guid)
  98. ),
  99. );
  100. $children = elgg_list_entities_from_metadata($child_options);
  101. return $children;
  102. }
  103. /**
  104. * List children for given entity per category
  105. *
  106. * @param entity $entity Entity to operate on
  107. */
  108. function listChildrenCategories() {
  109. global $CONFIG;
  110. $crud = $this->getCrudTemplate();
  111. $category_property = $crud->children_categories;
  112. $categories = $this->$category_property;
  113. if (empty($categories)) {
  114. return $this->listChildrenRaw();
  115. }
  116. $child_subtype = $crud->children_type;
  117. foreach($categories as $category) {
  118. $children .= "<h4>".ucfirst($category)."</h4>";
  119. $child_options = array('full_view' => FALSE,
  120. 'types' => 'object',
  121. 'subtypes' => $child_subtype,
  122. 'limit' => 10,
  123. 'list_class' => 'list-'.$child_subtype,
  124. 'metadata_name_value_pairs' => array(
  125. array('name' => 'parent_guid',
  126. 'value' => $this->guid),
  127. array('name' => $category_property,
  128. 'value' => $category)
  129. ),
  130. );
  131. $children .= elgg_list_entities_from_metadata($child_options);
  132. }
  133. $children .= "<h4>".elgg_echo('crud:categories:other')."</h4>";
  134. $child_options = array('full_view' => FALSE,
  135. 'types' => 'object',
  136. 'subtypes' => $child_subtype,
  137. 'limit' => 10,
  138. 'list_class' => 'list-'.$child_subtype,
  139. 'metadata_name_value_pairs' => array(
  140. array('name' => 'parent_guid',
  141. 'value' => $this->guid))
  142. );
  143. $name_metastring_id = get_metastring_id($category_property);
  144. $value_metastring_ids = array();
  145. foreach($categories as $category) {
  146. $value_metastring_ids[] = get_metastring_id($category);
  147. }
  148. if (empty($value_metastring_ids)) {
  149. return "";
  150. }
  151. $value_metastring_id = implode(",", $value_metastring_ids);
  152. $child_options['wheres'][] = "NOT EXISTS (
  153. SELECT 1 FROM {$CONFIG->dbprefix}metadata md
  154. WHERE md.entity_guid = e.guid
  155. AND md.name_id = $name_metastring_id
  156. AND md.value_id IN ($value_metastring_id))";
  157. $children .= elgg_list_entities_from_metadata($child_options);
  158. return $children;
  159. }
  160. /**
  161. * Return an icon for the entity
  162. */
  163. function getCrudIcon($size='tiny') {
  164. $crud = $this->getCrudTemplate();
  165. // Override icon completely for now
  166. if ($crud->icon_var) {
  167. $var_name = $crud->icon_var;
  168. $status = $this->$var_name;
  169. if(empty($status)) {
  170. $status = 'new';
  171. }
  172. $icon = "mod/$crud->module/graphics/$crud->crud_type-icons/$status.png";
  173. }
  174. else {
  175. $icon = NULL;
  176. }
  177. return $icon;
  178. }
  179. /**
  180. * Get children for given entity
  181. */
  182. function getChildren($count=FALSE) {
  183. $limit = 10;
  184. if ($count)
  185. $limit = 0;
  186. $crud = $this->getCrudTemplate();
  187. $child_subtype = $crud->children_type;
  188. $child_options = array('full_view' => FALSE,
  189. 'types' => 'object',
  190. 'subtypes' => $child_subtype,
  191. 'limit' => $limit,
  192. 'count' => $count,
  193. 'metadata_name_value_pairs' => array(
  194. array('name' => 'parent_guid',
  195. 'value' => $this->guid)
  196. )
  197. );
  198. $children = elgg_get_entities_from_metadata($child_options);
  199. return $children;
  200. }
  201. /**
  202. * Get the only embedded child for an entity.
  203. *
  204. * note: if there are more than one children, then nothing will be
  205. * returned.
  206. */
  207. function getEmbeddedChild() {
  208. $embedded_children = $this->getChildren();
  209. if (!empty($embedded_children)) {
  210. if (count($embedded_children) == 1)
  211. $embedded_child = $embedded_children[0];
  212. }
  213. return $embedded_child;
  214. }
  215. }