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

/plugins/system/zoo_zlelements/zoo_zlelements/elements/imagepro/imagepro.php

https://gitlab.com/vnsoftdev/amms
PHP | 307 lines | 169 code | 43 blank | 95 comment | 35 complexity | e8bedfdcdb50e283cdddce7965fd19f8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * @package ZL Elements
  4. * @author JOOlanders, SL http://www.zoolanders.com
  5. * @copyright Copyright (C) JOOlanders, SL
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die('Restricted access');
  10. /*
  11. Class: ElementImagePro
  12. The image element class
  13. */
  14. class ElementImagePro extends ElementFilesPro implements iRepeatSubmittable {
  15. protected $_extensions = 'bmp|gif|jpg|jpeg|png';
  16. /*
  17. Function: Constructor
  18. */
  19. public function __construct() {
  20. // call parent constructor
  21. parent::__construct();
  22. // register events
  23. $this->app->event->dispatcher->connect('element:afterdisplay', array($this, 'afterDisplay'));
  24. // load default and current language
  25. $this->app->system->language->load("plg_system_zoo_zlelements_imagepro", $this->app->path->path('zlpath:'), 'en-GB', true);
  26. $this->app->system->language->load("plg_system_zoo_zlelements_imagepro", $this->app->path->path('zlpath:'), null, true);
  27. }
  28. /*
  29. Function: afterDisplay
  30. Change the element layout after it has been displayed
  31. Parameters:
  32. $event - object
  33. */
  34. public static function afterDisplay($event)
  35. {
  36. $event['html'] = str_replace('class="element element-imagepro', 'class="element element-image element-imagepro', $event['html']);
  37. }
  38. /*
  39. Function: getSearchData
  40. Get elements search data.
  41. Returns:
  42. String - Search data
  43. */
  44. public function _getSearchData() {
  45. if ($this->config->get('custom_title')) {
  46. return $this->get('title');
  47. }
  48. return null;
  49. }
  50. /*
  51. Function: getPreview
  52. Return file preview
  53. Returns:
  54. HTML
  55. */
  56. public function getPreview($file = null)
  57. {
  58. $sourcepath = $this->app->path->path('root:'.$file);
  59. if (is_file($sourcepath)){
  60. return '<img src="'.$this->app->path->url('root:'.$file).'">';
  61. } else if (is_dir($sourcepath)){
  62. return '<img src="'.$this->app->path->url('elements:filespro/assets/images/folder_horiz.png').'">';
  63. }
  64. }
  65. /*
  66. Function: getRenderedValues
  67. render repeatable values
  68. Returns:
  69. array
  70. */
  71. public function getRenderedValues($params=array(), $wk=false, $opts=array())
  72. {
  73. $result = parent::getRenderedValues($params, $wk, $opts);
  74. if (empty($result)) return null; // if no results abort
  75. // perform the _sublayout if is not WidgetKit
  76. if($wk == false) foreach ($result['result'] as $key => $image)
  77. {
  78. if($layout = $this->getLayout('render/default/_sublayouts/'.$params->find('layout._sublayout', '_default.php'))){
  79. $result['result'][$key] = $this->renderLayout($layout, array('img' => $image, 'params' => $params));
  80. }
  81. }
  82. return $result;
  83. }
  84. /*
  85. Function: render
  86. Renders the repeatable element.
  87. Parameters:
  88. $params - AppData render parameter
  89. Returns:
  90. String - html
  91. */
  92. protected function _render($params=array(), $mode='', $opts=array())
  93. {
  94. // init vars
  95. $opts = $this->app->data->create($opts); // overide options
  96. $width = $opts->get('width', $params->find('specific._width', 0));
  97. $height = $opts->get('height', $params->find('specific._height', 0));
  98. $ac = $opts->get('avoid_cropping', $params->find('specific._avoid_cropping', 0));
  99. $file2 = $this->get('file2', 0);
  100. $result = array();
  101. if ($opts->get('file2') && $file2 && $sourcepath = $this->app->path->path('root:'.$file2)) // get file 2
  102. {
  103. if (is_file($sourcepath) && is_readable($sourcepath)){
  104. $result[] = $this->getImgObject($file2, $params, $width, $height, $ac);
  105. }
  106. }
  107. else foreach ($this->getFiles($this->get('file')) as $file) // get default file or files from folder
  108. {
  109. $result[] = $this->getImgObject($file, $params, $width, $height, $ac);
  110. }
  111. return $result;
  112. }
  113. /*
  114. Function: getImgObject
  115. Renders the repeatable element
  116. Returns:
  117. String - array
  118. */
  119. protected function getImgObject($file=null, $params=array(), $width=0, $height=0, $ac=0)
  120. {
  121. $sourcefile = 'root:'.$this->app->path->relative($file);
  122. $ext = $this->getExtension($file);
  123. $file = $this->app->zlfw->resizeImage(JPATH_ROOT.'/'.$file, $width, $height, $ac);
  124. $url = JURI::base().$this->app->path->relative($file); // using base is important
  125. $info = getimagesize($file);
  126. $title = $this->get('title');
  127. $link = $target = $rel = '';
  128. if ($params->find('specific._link_to_item', false) && $this->getItem()->getState())
  129. {
  130. $link = $this->app->route->item($this->_item, false);
  131. $title = empty($title) ? $this->_item->name : $title;
  132. }
  133. // override link from item
  134. if ($this->get('link'))
  135. {
  136. $link = $this->get('link');
  137. $target = $this->get('target');
  138. $rel = $this->get('rel');
  139. }
  140. // get alt
  141. $alt = empty($title) ? $this->_item->name : $title;
  142. // encode posible double quotes
  143. $title = str_replace('"', '&#34;', $title);
  144. $alt = str_replace('"', '&#34;', $alt);
  145. $relative = 'root:'.$this->app->path->relative($file);
  146. $img = array();
  147. $img['sourcefile'] = $sourcefile;
  148. $img['sourceurl'] = $this->app->path->url($sourcefile);
  149. $img['file'] = $relative;
  150. $img['fileurl'] = $this->app->path->url($relative);
  151. $img['filename'] = basename($sourcefile, '.'.$ext);
  152. $img['title'] = $title;
  153. $img['caption'] = $this->get('caption') ? $this->get('caption') : $title;
  154. $img['alt'] = $alt;
  155. $img['ext'] = $ext;
  156. $img['link'] = $link;
  157. $img['target'] = $target;
  158. $img['rel'] = $rel;
  159. $img['name'] = basename($sourcefile);
  160. $img['width'] = $info[0];
  161. $img['height'] = $info[1];
  162. $img['spotlight_effect'] = $this->get('spotlight_effect', '');
  163. return $img;
  164. }
  165. /*
  166. Function: _edit
  167. Renders the repeatable edit form field.
  168. Returns:
  169. String - html
  170. */
  171. public function _edit(){
  172. if ($layout = $this->getLayout("edit/_edit.php")) {
  173. return $this->renderLayout($layout, array());
  174. }
  175. }
  176. /*
  177. Function: _validateSubmission
  178. Validates the submitted element
  179. Parameters:
  180. $value - AppData value
  181. $params - AppData submission parameters
  182. Returns:
  183. Array - cleaned value
  184. */
  185. public function _validateSubmission($value, $params)
  186. {
  187. // init vars
  188. $trusted_mode = $params->get('trusted_mode');
  189. $required = $params->get('required');
  190. $userfile = $value->get('userfile', null);
  191. $file = '';
  192. if ($params->find('layout._layout') == 'advanced' && $trusted_mode) // advanced
  193. {
  194. $file = $this->app->validator->create('string', array('required' => $required))->clean($value->find('values.file'));
  195. $title = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.title'));
  196. $rel = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.rel'));
  197. $link = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.link'));
  198. $target = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.target'));
  199. // wk integration
  200. $file2 = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.file2'));
  201. $spotlight_effect = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.spotlight_effect'));
  202. $caption = $this->app->validator->create('string', array('required' => false))->clean($value->find('values.caption'));
  203. return compact('file', 'title', 'rel', 'link', 'target', 'spotlight_effect', 'caption', 'file2');
  204. }
  205. else if ($value->find('values.image') != null && $old_file = $value->get('old_file')) // use old file
  206. {
  207. // get the correct old file as it has could been reordered
  208. $old_file = $old_file[$value->find('values.image')];
  209. // clean the file path
  210. $file = $this->app->validator->create('string', array('required' => $required))->clean($old_file);
  211. if ($required && !JFile::exists($file)) {
  212. throw new AppValidatorException(sprintf('This file does not exist.'));
  213. }
  214. }
  215. else if (!isset($userfile['error'])) // default
  216. {
  217. try {
  218. // get legal extensions
  219. $extensions = array_map(create_function('$ext', 'return strtolower(trim($ext));'), explode('|', $this->config->find('files._extensions', $this->_extensions)));
  220. //get legal mime types
  221. $legal_mime_types = $this->app->data->create(array_intersect_key($this->app->filesystem->getMimeMapping(), array_flip($extensions)))->flattenRecursive();
  222. $max_upload_size = $this->config->find('files._max_upload_size', '1024') * 1024;
  223. $max_upload_size = empty($max_upload_size) ? null : $max_upload_size;
  224. // validate
  225. $file = $this->app->validator
  226. ->create('filepro', array('mime_types' => $legal_mime_types, 'max_size' => $max_upload_size))
  227. ->addMessage('mime_types', 'Uploaded file is not of a permitted type.')
  228. ->clean($userfile);
  229. // init vars
  230. $ext = strtolower(JFile::getExt($file['name']));
  231. $basename = substr($file['name'], 0, strrpos($file['name'], '.'));
  232. $targetDir = JPATH_ROOT.'/'.$this->getDirectory();
  233. // construct filename
  234. $fileName = "{$basename}.{$ext}";
  235. // Make sure the fileName is unique
  236. if (JFile::exists("$targetDir/$fileName")) {
  237. $count = 1;
  238. while (JFile::exists("{$targetDir}/{$basename}_{$count}.{$ext}"))
  239. $count++;
  240. $fileName = "{$basename}_{$count}.{$ext}";
  241. }
  242. $file = $this->app->path->relative("$targetDir/$fileName");
  243. } catch (AppValidatorException $e) {
  244. if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
  245. throw $e;
  246. }
  247. }
  248. }
  249. if ($params->get('required') && empty($file)) {
  250. throw new AppValidatorException('Please select an image to upload.');
  251. }
  252. return compact('file');
  253. }
  254. }