PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/system/zlframework/zlframework/elements/repeatablepro/repeatablepro.php

https://gitlab.com/vnsoftdev/amms
PHP | 301 lines | 130 code | 39 blank | 132 comment | 29 complexity | e7afe9a132f172b0ceb24c3365b57b21 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * @package ZOOlanders
  4. * @version 3.3.15
  5. * @author ZOOlanders - http://zoolanders.com
  6. * @license GNU General Public License v2 or later
  7. */
  8. defined('_JEXEC') or die();
  9. App::getInstance('zoo')->loader->register('ElementRepeatable', 'elements:repeatable/repeatable.php');
  10. /*
  11. Class: ElementRepeatablePro
  12. The repeatable element class
  13. */
  14. abstract class ElementRepeatablePro extends ElementRepeatable {
  15. protected $_rendered_values = array();
  16. /*
  17. Function: Constructor
  18. */
  19. public function __construct() {
  20. // call parent constructor
  21. parent::__construct();
  22. // set callbacks
  23. $this->registerCallback('returndata');
  24. $this->registerCallback('getemptylayout');
  25. // load default and current language
  26. $this->app->system->language->load('plg_system_zoo_zlelements_'.$this->getElementType(), JPATH_ADMINISTRATOR, 'en-GB');
  27. $this->app->system->language->load('plg_system_zoo_zlelements_'.$this->getElementType(), JPATH_ADMINISTRATOR);
  28. }
  29. /*
  30. Function: setType
  31. Set related type object.
  32. Added a checkInstallation call to allow for extra steps of checking installation
  33. on advanced elements. Here and not in the constructor to be sure to have type and
  34. therefore config available
  35. Parameters:
  36. $type - type object
  37. Returns:
  38. Void
  39. */
  40. public function setType($type) {
  41. parent::setType($type);
  42. $this->checkInstallation();
  43. }
  44. /*
  45. Function: checkInstallation
  46. Allow for extra steps of checkin installation
  47. on advanced elements.
  48. Returns:
  49. Void
  50. */
  51. protected function checkInstallation(){
  52. }
  53. /*
  54. Function: hasValue
  55. Override. Checks if the element's value is set.
  56. Parameters:
  57. $params - render parameter
  58. Returns:
  59. Boolean - true, on success
  60. */
  61. public function hasValue($params = array()) {
  62. $value = $this->getRenderedValues( $this->app->data->create($params) );
  63. return !empty($value['result']);
  64. }
  65. /*
  66. Function: getEmptyLayout
  67. Load Element specified Edit Layout
  68. */
  69. public function getEmptyLayout($layout = null)
  70. {
  71. // get layout from var or request
  72. $layout = $layout ? $layout : $this->app->request->getString('layout', '');
  73. if ($layout = $this->getLayout($layout)) {
  74. echo preg_replace('/(elements\[\S+])\[(\d+)\]/', '$1[-1]', $this->renderLayout($layout, array('loadinglayout' => true)));
  75. }
  76. }
  77. /*
  78. Function: getLayout
  79. Get element layout path and use override if exists.
  80. Returns:
  81. String - Layout path
  82. */
  83. public function getLayout($layout = null)
  84. {
  85. // init vars
  86. $type = $this->getElementType();
  87. // set default
  88. if ($layout == null) {
  89. $layout = "default.php";
  90. }
  91. // find layout
  92. if ($path = $this->app->path->path("elements:{$type}/tmpl/{$layout}"))
  93. {
  94. return $path;
  95. }
  96. else if ($path = $this->app->path->path("elements:repeatablepro/tmpl/{$layout}")) // if no specific use common layout
  97. {
  98. return $path;
  99. }
  100. // if no layout found, search on pro element
  101. return $this->app->path->path("elements:pro/tmpl/{$layout}");
  102. }
  103. /*
  104. Function: returnData
  105. Renders the element data - use for ajax requests
  106. */
  107. public function returnData($layout, $separator = '', $filter = '', $specific = '') {
  108. $separator = json_decode($separator, true); $filter = json_decode($filter, true); $specific = json_decode($specific, true);
  109. $params = compact('layout', 'separator', 'filter', 'specific');
  110. return $this->render($params);
  111. }
  112. /*
  113. Function: loadAssets
  114. Load elements css/js assets.
  115. Returns:
  116. Void
  117. */
  118. public function loadAssets() {
  119. if ($this->config->get('repeatable')) {
  120. $this->app->document->addScript('elements:repeatablepro/repeatablepro.js');
  121. }
  122. $this->app->document->addStylesheet('zlfw:assets/css/zl_ui.css');
  123. return $this;
  124. }
  125. /*
  126. Function: _edit
  127. Renders the repeatable edit form field.
  128. Returns:
  129. String - html
  130. */
  131. protected function _edit(){
  132. // render layout
  133. $_edit = $this->config->find('specific._edit_sublayout', '_edit.php');
  134. return (($layout = $this->getLayout("edit/$_edit")) || ($layout = $this->getLayout("edit/_edit.php"))) ? $this->renderLayout($layout) : '';
  135. }
  136. /*
  137. Function: getRenderedValues
  138. render repeatable values
  139. Returns:
  140. array
  141. */
  142. public function getRenderedValues($params=array(), $mode=false, $opts=array())
  143. {
  144. // create a unique hash for this element position
  145. $hash = md5(serialize(array(
  146. $opts,
  147. $this->getType()->getApplication()->getGroup(),
  148. $this->getType()->id,
  149. $params->get('element').$params->get('_layout'),
  150. $params->get('_position').$params->get('_index')
  151. )));
  152. // check for value, if not exist render it
  153. if (!array_key_exists($hash, $this->_rendered_values))
  154. {
  155. // of limit 0, abort
  156. if ($params->find('filter._limit') == '0') return null;
  157. $report = array();
  158. $data_is_subarray = isset($opts['data_is_subarray']) ? $opts['data_is_subarray'] : false;
  159. // render
  160. $result = array();
  161. $this->seek(0); // let's be sure is starting from first index
  162. foreach ($this as $self) if ($this->_hasValue($params) && $values = $this->_render($params, $mode, $opts)) {
  163. if($data_is_subarray) foreach ($values as $value) {
  164. $result[] = $value; // filespro compatibility
  165. } else {
  166. $result[] = $values;
  167. }
  168. }
  169. if (empty($result)) return null; // if no results abort
  170. // set offset/limit
  171. $offset = ( ($params->find('filter._offset', '') == '') || (!is_numeric($params->find('filter._offset', 0))) ) ? 0 : $params->find('filter._offset', 0);
  172. $limit = ( ($params->find('filter._limit', '') == '') || (!is_numeric($params->find('filter._limit', ''))) ) ? null : $params->find('filter._limit', null);
  173. $report['limited'] = $limit != null ? $limit < count($result) : false;
  174. $result = array_slice($result, $offset, $limit);
  175. // set prefix/suffix
  176. if ($prefix = $params->find('specific._prefix')) array_unshift($result, '<span class="prefix">'.$prefix.'</span>');
  177. if ($suffix = $params->find('specific._suffix')) {
  178. if (count($result) > 1) $result[] = '<span class="suffix">'.$suffix.'</span>';
  179. else $result[0] .= '<span class="suffix">'.$suffix.'</span>';
  180. }
  181. $report['hash'] = $hash;
  182. $this->_rendered_values[$hash] = compact('report', 'result');
  183. }
  184. return $this->_rendered_values[$hash];
  185. }
  186. /*
  187. Function: render
  188. Renders the element.
  189. Parameters:
  190. $params - AppData render parameter
  191. Returns:
  192. String - html
  193. */
  194. public function render($params = array())
  195. {
  196. $params = $this->app->data->create($params);
  197. // render layout
  198. if ($layout = $this->getLayout('render/'.$params->find('layout._layout', 'default.php'))) {
  199. return $this->renderLayout($layout, compact('params'));
  200. }
  201. else
  202. {
  203. // for old elements
  204. $result = array();
  205. foreach ($this as $self) {
  206. $result[] = $this->_render($params);
  207. }
  208. return $this->app->zlfw->applySeparators($params->find('separator._by'), $result, $params->find('separator._class'));
  209. }
  210. }
  211. /*
  212. Function: _render
  213. Renders the repeatable element.
  214. Parameters:
  215. $params - AppData render parameter
  216. Returns:
  217. String - html
  218. */
  219. protected function _render($params = array())
  220. {
  221. // render layout or value
  222. $main_layout = basename($params->find('layout._layout', 'default.php'), '.php');
  223. if($layout = $this->getLayout('render/'.$main_layout.'/_sublayouts/'.$params->find('layout._sublayout', '_default.php'))){
  224. return $this->renderLayout($layout, compact('params'));
  225. } else {
  226. return $this->get('value');
  227. }
  228. }
  229. /*
  230. Function: _renderRepeatable
  231. Renders the repeatable
  232. Returns:
  233. String - output
  234. */
  235. protected function _renderRepeatable($function, $params = array()) {
  236. return $this->renderLayout($this->app->path->path('elements:'.$this->getElementType().'/tmpl/edit/edit.php'), compact('function', 'params'));
  237. }
  238. /*
  239. Function: getControlName
  240. Gets the controle name for given name.
  241. Returns:
  242. String - the control name
  243. */
  244. public function getControlName($name, $array = false) {
  245. return "elements[{$this->identifier}][{$this->index()}][{$name}]" . ($array ? "[]":"");
  246. }
  247. }