CrudTemplate.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * CRUD -- Define RESTful template with all CRUD class information
  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 CrudTemplate {
  25. function __construct($type) {
  26. // type of the described crud
  27. $this->crud_type = $type;
  28. // array describing member variables
  29. $this->variables = array();
  30. // module this class resides on
  31. $this->module = $type;
  32. // type for children, if not defined no children controls
  33. // will be generated
  34. $this->children_type = false;
  35. // property that defines categories for children
  36. $this->children_categories = false;
  37. // variable to use for generating an icon representation
  38. $this->icon_var = false;
  39. // whether children should be embedded
  40. $this->embed = false;
  41. // List variables
  42. // variable to use for ordering entity listings
  43. $this->list_order = false;
  44. // variable to use for direction in ordering entity listings
  45. $this->list_order_direction = 'ASC';
  46. // variable to use for generating tabs on entity listings
  47. $this->list_tabs = false;
  48. }
  49. /**
  50. * Get the string prefix for language strings.
  51. */
  52. function getStringPrefix() {
  53. return "$this->module:$this->crud_type";
  54. }
  55. /**
  56. * Get the default value for some variable.
  57. */
  58. function getDefaultValue($name, $default=NULL) {
  59. if (isset($this->variables[$name]) && isset($this->variables[$name]['default_value'])) {
  60. return $this->variables[$name]['default_value'];
  61. }
  62. return $default;
  63. }
  64. /**
  65. * Get data for the given variable
  66. */
  67. function getVariableData($name) {
  68. if (isset($this->variables[$name])) {
  69. return $this->variables[$name];
  70. }
  71. }
  72. /**
  73. * Get the content for a list tab based on selected tab
  74. */
  75. function getDateTabContent() {
  76. $tab_var = $this->variables[$this->list_tabs];
  77. $first_option = $tab_var->options[0];
  78. $selected_tab = get_input('filter', $first_option);
  79. $container_guid = elgg_get_page_owner_guid();
  80. $options = array(
  81. 'type' => 'object',
  82. 'subtype' => $this->crud_type,
  83. 'limit' => 10,
  84. # 'order_by' => 'e.last_action desc',
  85. 'container_guid' => $container_guid,
  86. 'full_view' => false,
  87. );
  88. if ($selected_tab == 'next') {
  89. $operand = '>=';
  90. $direction = 'ASC';
  91. $metadata_search = true;
  92. }
  93. elseif ($selected_tab == 'previous') {
  94. $operand = '<';
  95. $direction = 'DESC';
  96. $metadata_search = true;
  97. }
  98. if ($metadata_search) {
  99. $options['metadata_name_value_pairs'] = array(
  100. array('name' => 'date',
  101. 'operand' => $operand,
  102. 'value' => time())
  103. );
  104. $metadata_search = true;
  105. $options['order_by_metadata'] = array('name' => 'date',
  106. 'direction' => $direction);
  107. $content = elgg_list_entities_from_metadata($options);
  108. }
  109. else {
  110. $content = elgg_list_entities($options);
  111. }
  112. if (!$content) {
  113. $content = elgg_echo($this->module.':'.$this->crud_type.':none');
  114. }
  115. return $content;
  116. }
  117. /**
  118. * Get the content for a list tab based on selected tab
  119. */
  120. function getListTabContent() {
  121. if ($this->list_tabs == 'date') {
  122. return $this->getDateTabContent();
  123. }
  124. $tab_var = $this->variables[$this->list_tabs];
  125. $first_option = $tab_var->options[0];
  126. $selected_tab = get_input('filter', $first_option);
  127. $container_guid = elgg_get_page_owner_guid();
  128. $options = array(
  129. 'type' => 'object',
  130. 'subtype' => $this->crud_type,
  131. 'limit' => 10,
  132. # 'order_by' => 'e.last_action desc',
  133. 'container_guid' => $container_guid,
  134. 'full_view' => false,
  135. );
  136. if ($this->list_tabs && $selected_tab != 'all') {
  137. $metadata_search = true;
  138. $options['metadata_name_value_pairs'] = array(
  139. array('name' => $this->list_tabs,
  140. 'value' => $selected_tab)
  141. );
  142. }
  143. if ($this->list_order) {
  144. $metadata_search = true;
  145. $options['order_by_metadata'] = array('name' => $this->list_order,
  146. 'direction' => $this->list_order_direction);
  147. }
  148. if ($metadata_search) {
  149. $content = elgg_list_entities_from_metadata($options);
  150. }
  151. else {
  152. $content = elgg_list_entities($options);
  153. }
  154. if (!$content) {
  155. $content = elgg_echo($this->module.':'.$this->crud_type.':none');
  156. }
  157. return $content;
  158. }
  159. /**
  160. * Show the header for a list tab based on date
  161. */
  162. function getDateTabFilter() {
  163. $tab_var = $this->variables[$this->list_tabs];
  164. $first_option = $tab_var->options[0];
  165. $selected_tab = get_input('filter', $first_option);
  166. $filter = elgg_view('crud/crud_date_sort_menu',
  167. array('selected' => $selected_tab,
  168. 'crud' => $this));
  169. return $filter;
  170. }
  171. /**
  172. * Show the header for a list tab based on selected tab
  173. */
  174. function getListTabFilter() {
  175. if (!$this->list_tabs)
  176. return '';
  177. if ($this->list_tabs == 'date')
  178. return $this->getDateTabFilter();;
  179. $tab_var = $this->variables[$this->list_tabs];
  180. $first_option = $tab_var->options[0];
  181. $selected_tab = get_input('filter', $first_option);
  182. $filter = elgg_view('crud/crud_sort_menu',
  183. array('selected' => $selected_tab,
  184. 'crud' => $this));
  185. return $filter;
  186. }
  187. }