PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/media/widgetkit/widgets/gallery/gallery.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 511 lines | 266 code | 97 blank | 148 comment | 53 complexity | 752c167c51ac2cb3def687b21ead896d MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Widgetkit
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: GalleryWidgetkitHelper
  10. Photo gallery helper class
  11. */
  12. class GalleryWidgetkitHelper extends WidgetkitHelper {
  13. /* type */
  14. public $type;
  15. /* options */
  16. public $options;
  17. /*
  18. Function: Constructor
  19. Class Constructor.
  20. */
  21. public function __construct($widgetkit) {
  22. parent::__construct($widgetkit);
  23. // init vars
  24. $this->type = strtolower(str_replace('WidgetkitHelper', '', get_class($this)));
  25. $this->options = $this['system']->options;
  26. // create cache
  27. $cache = $this['path']->path('cache:');
  28. if ($cache && !file_exists($cache.'/gallery')) {
  29. mkdir($cache.'/gallery', 0777, true);
  30. }
  31. // register path
  32. $this['path']->register(dirname(__FILE__), $this->type);
  33. $this['path']->register($this['path']->path('cache:gallery'), 'gallery.cache');
  34. }
  35. /*
  36. Function: get
  37. Get widget
  38. Returns:
  39. Array
  40. */
  41. public function get($id = 0) {
  42. // get widget
  43. if ($id) {
  44. $widget = $this['widget']->get($id);
  45. }
  46. // set defaults
  47. foreach (array('id' => 0, 'type' => $this->type, 'name' => null, 'settings' => array(), 'paths' => array(), 'captions' => array()) as $var => $val) {
  48. if (!isset($widget->$var)) {
  49. $widget->$var = $val;
  50. }
  51. }
  52. return $widget;
  53. }
  54. /*
  55. Function: site
  56. Site init actions
  57. Returns:
  58. Void
  59. */
  60. public function site() {
  61. // add javascripts
  62. $this['asset']->addFile('js', 'gallery:js/lazyloader.js');
  63. // add style stylesheets
  64. foreach ($this['path']->dirs('gallery:styles') as $style) {
  65. // style
  66. if ($this['path']->path("gallery:styles/$style/style.css")) {
  67. $this['asset']->addFile('css', "gallery:styles/$style/style.css");
  68. }
  69. // rtl
  70. if ($this['system']->options->get('direction') == 'rtl' && $this['path']->path("gallery:styles/$style/rtl.css")) {
  71. $this['asset']->addFile('css', "gallery:styles/$style/rtl.css");
  72. }
  73. }
  74. }
  75. /*
  76. Function: render
  77. Render widget on site
  78. Returns:
  79. String
  80. */
  81. public function render($widget) {
  82. // get images and style
  83. $style = isset($widget->settings['style']) ? $widget->settings['style'] : 'default';
  84. return $this['template']->render("gallery:styles/$style/template", array('widget' => $widget));
  85. }
  86. /*
  87. Function: images
  88. Get widget images
  89. Returns:
  90. Array
  91. */
  92. public function images($widget, $overrides = array()) {
  93. $images = array();
  94. $settings = $widget->settings;
  95. $captions = $widget->captions;
  96. $links = $widget->links;
  97. $cache = $this['path']->path('gallery.cache:');
  98. // merge setting overrides
  99. if (is_array($overrides)) {
  100. $settings = array_merge($settings, $overrides);
  101. }
  102. // create cache
  103. if ($cache && !file_exists($cache.'/'.$widget->id)) {
  104. mkdir($cache.'/'.$widget->id, 0777, true);
  105. }
  106. if (is_array($widget->paths)) {
  107. foreach ($widget->paths as $path) {
  108. foreach ($this['path']->files("media:$path") as $file) {
  109. if (preg_match("/(\.gif|\.jpg|\.jpeg|\.png)$/i", $file)) {
  110. // image data
  111. $image['name'] = basename($file);
  112. $image['ext'] = pathinfo($file, PATHINFO_EXTENSION);
  113. $image['filename'] = pathinfo($file, PATHINFO_FILENAME);
  114. $image['caption'] = isset($captions[$path.'/'.$file]) ? $captions[$path.'/'.$file] : null;
  115. $image['link'] = isset($links[$path.'/'.$file]) ? $links[$path.'/'.$file] : null;
  116. $image['file'] = sprintf("media:%s/%s", $path, $file);
  117. $image['cache'] = sprintf("gallery.cache:/%s/%s-%s.%s", $widget->id, $image['filename'], substr(md5($image['file'].$settings['width'].$settings['height']), 0, 10), $image['ext']);
  118. $image['url'] = $this['path']->url($image['file']);
  119. // cache image
  120. if ($cache && !$this['path']->path($image['cache'])) {
  121. if (isset($settings['animated']) && $settings['animated'] == 'kenburns') {
  122. $kbw = is_numeric($settings['width']) ? $settings['width']*1.2:$settings['width'];
  123. $kbh = is_numeric($settings['height']) ? $settings['height']*1.2:$settings['height'];
  124. $this['image']->create($this['path']->path($image['file']))->output(array('width' => $kbw, 'height' => $kbh, 'file' => $cache.preg_replace('/^gallery\.cache:/', '', $image['cache'], 1)));
  125. } else {
  126. $this['image']->create($this['path']->path($image['file']))->output(array('width' => $settings['width'], 'height' => $settings['height'], 'file' => $cache.preg_replace('/^gallery\.cache:/', '', $image['cache'], 1)));
  127. }
  128. }
  129. $image['cache_url'] = $this['path']->url($image['cache']);
  130. if ($p = $this['path']->path($image['cache'])) {
  131. if (!is_numeric($settings['width']) || !is_numeric($settings['height'])) {
  132. list($image['width'], $image['height']) = @getimagesize($p);
  133. } else {
  134. $image['width'] = $settings['width'];
  135. $image['height'] = $settings['height'];
  136. }
  137. }
  138. array_push($images, $image);
  139. }
  140. }
  141. }
  142. }
  143. // random order
  144. if (count($images) && isset($settings['order']) && $settings['order'] =="random") {
  145. shuffle($images);
  146. }
  147. return $images;
  148. }
  149. /*
  150. Function: dashboard
  151. Render dashboard layout
  152. Returns:
  153. Void
  154. */
  155. public function dashboard() {
  156. // init vars
  157. $xml = simplexml_load_file($this['path']->path('gallery:gallery.xml'));
  158. $galleries = $this['widget']->all($this->type);
  159. // add js
  160. $this['asset']->addFile('js', 'gallery:js/dashboard.js');
  161. // render dashboard
  162. echo $this['template']->render('gallery:layouts/dashboard', compact('xml', 'galleries'));
  163. }
  164. /*
  165. Function: config
  166. Save configuration
  167. Returns:
  168. Void
  169. */
  170. public function config() {
  171. // save configuration
  172. foreach ($this['request']->get('post:', 'array') as $option => $value) {
  173. if (preg_match('/^gallery_/', $option)) {
  174. $this['system']->options->set($option, $value);
  175. }
  176. }
  177. $this['system']->saveOptions();
  178. }
  179. /*
  180. Function: edit
  181. Edit action
  182. Returns:
  183. Void
  184. */
  185. public function edit($id = null) {
  186. // add css/js
  187. $this['asset']->addFile('css', 'gallery:css/admin.css');
  188. $this['asset']->addFile('js', 'gallery:js/edit.js');
  189. // get xml/widget
  190. $xml = simplexml_load_file(dirname(__FILE__).'/gallery.xml');
  191. $widget = $this->get($id ? $id : $this['request']->get('id', 'int'));
  192. if (isset($widget->paths) && is_array($widget->paths)) {
  193. $widget->paths = array_filter($widget->paths, create_function('$path','
  194. $wk = Widgetkit::getInstance();
  195. return $wk["path"]->path("media:/$path") ? true : false;
  196. '));
  197. }
  198. // get style and xml
  199. $style = isset($widget->settings['style']) ? $widget->settings['style'] : 'default';
  200. if (!$this['path']->path("gallery:styles/$style/config.xml")) {
  201. $style = 'default';
  202. }
  203. $style_xml = simplexml_load_file($this['path']->path("gallery:styles/$style/config.xml"));
  204. // render edit form
  205. echo $this['template']->render('gallery:layouts/edit', compact('widget', 'xml', 'style_xml'));
  206. }
  207. /*
  208. Function: dirs
  209. Get directory list JSON formatted
  210. Returns:
  211. Void
  212. */
  213. public function dirs() {
  214. $dirs = array();
  215. $path = $this['request']->get('path', 'string');
  216. foreach ($this['path']->dirs('media:'.$path) as $dir) {
  217. $dirs[] = array('name' => basename($dir), 'path' => $path.'/'.$dir, 'type' => 'folder');
  218. }
  219. echo json_encode($dirs);
  220. }
  221. /*
  222. Function: files
  223. Get image file list JSON formatted
  224. Returns:
  225. Void
  226. */
  227. public function files() {
  228. $files = array();
  229. $path = $this['request']->get('path', 'string');
  230. foreach ($this['path']->files('media:'.$path) as $file) {
  231. if (preg_match("/(\.gif|\.jpg|\.jpeg|\.png)$/i", $file)) {
  232. $files[] = array('name' => basename($file), 'path' => $path.'/'.$file, 'type' => 'file');
  233. }
  234. }
  235. echo json_encode($files);
  236. }
  237. /*
  238. Function: uploadFiles
  239. Upload files to gallery
  240. Returns:
  241. Void
  242. */
  243. public function uploadFiles() {
  244. $files = isset($_FILES['files']) ? $_FILES['files'] : array();
  245. $path = $this['request']->get('path', 'string');
  246. $uploaded = array();
  247. if ($this['system']->checkToken($this['request']->get('token', 'string')) && isset($files['name']) && strlen($path) && $targetpath = $this['path']->path('media:'.$path)) {
  248. for ($i = 0; $i < count($files['name']); $i++) {
  249. if (!$files['error'][$i] && preg_match('/image/i', $files['type'][$i]) && @move_uploaded_file($files['tmp_name'][$i], $targetpath.'/'.$files['name'][$i])) {
  250. $uploaded[] = $files['name'][$i];
  251. }
  252. }
  253. }
  254. echo json_encode($uploaded);
  255. }
  256. /*
  257. Function: deleteFile
  258. Delete files from gallery folders
  259. Returns:
  260. Void
  261. */
  262. public function deleteFile() {
  263. $status = 0;
  264. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $file = $this['path']->path('media:'.$this['request']->get('file', 'string')) and is_file($file)) {
  265. $status = (int) @unlink($file);
  266. }
  267. echo json_encode(compact('status'));
  268. }
  269. /*
  270. Function: createFolder
  271. Create new gallery folder
  272. Returns:
  273. Void
  274. */
  275. public function createFolder() {
  276. $status = 0;
  277. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $path = preg_replace('#^([^/])#', '/$1', str_replace('\\', '/', $this['request']->get('path', 'string'))) and strpos($path, '/..') === false) {
  278. $status = (int) @mkdir($this['path']->path('media:').$path, 0775);
  279. }
  280. echo json_encode(compact('status'));
  281. }
  282. /*
  283. Function: deleteFolder
  284. Delete gallery folder
  285. Returns:
  286. Void
  287. */
  288. public function deleteFolder() {
  289. $status = 0;
  290. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $path = $this['path']->path('media:'.$this['request']->get('path', 'string'))) {
  291. if ($this['path']->path('media:') != $path) {
  292. $status = (int) $this->_deleteFolder($path);
  293. }
  294. }
  295. echo json_encode(compact('status'));
  296. }
  297. /*
  298. Function: _deleteFolder
  299. Delete folder recursively and don't recurse into symlinks
  300. Returns:
  301. Boolean
  302. */
  303. protected function _deleteFolder($path) {
  304. $directory = new DirectoryIterator($path);
  305. foreach ($directory as $file) {
  306. if ($file->isFile() || $file->isLink()) {
  307. unlink($file->getPathName());
  308. } elseif (!$file->isDot() && $file->isDir()) {
  309. $this->_deleteFolder($file->getPathName());
  310. }
  311. }
  312. return @rmdir($path);
  313. }
  314. /*
  315. Function: save
  316. Save action
  317. Returns:
  318. Void
  319. */
  320. public function save() {
  321. // save data
  322. $data['type'] = $this->type;
  323. $data['id'] = $this['request']->get('id', 'int');
  324. $data['name'] = $this['request']->get('name', 'string');
  325. $data['settings'] = $this['request']->get('settings', 'array');
  326. $data['style'] = $this['request']->get('settings.style', 'array');
  327. $data['paths'] = $this['request']->get('paths', 'array', array());
  328. $data['captions'] = $this['request']->get('captions', 'array', array());
  329. $data['links'] = $this['request']->get('links', 'array', array());
  330. // force numeric values if not auto
  331. if ($data['settings']['width']!= 'auto') {
  332. $data['settings']['width'] = intval($data['settings']['width']);
  333. }
  334. if ($data['settings']['height']!= 'auto') {
  335. $data['settings']['height'] = intval($data['settings']['height']);
  336. }
  337. // merge style settings defaults
  338. if ($path = $this['path']->path("gallery:styles/{$data['style']}/config.xml")) {
  339. $xml = simplexml_load_file($path);
  340. foreach ($xml->xpath('settings/setting') as $setting) {
  341. $name = (string) $setting->attributes()->name;
  342. $value = (string) $setting->attributes()->default;
  343. if (!array_key_exists($name, $data['settings'])) {
  344. $data['settings'][$name] = $value;
  345. }
  346. }
  347. }
  348. $this->edit($this['widget']->save($data));
  349. }
  350. /*
  351. Function: delete
  352. Delete action
  353. Returns:
  354. Void
  355. */
  356. public function delete(){
  357. $data['id'] = false;
  358. if ($id = $this['request']->get('id', 'int')) {
  359. if ($this['widget']->delete($id)) {
  360. $data['id'] = $id;
  361. }
  362. }
  363. echo json_encode($data);
  364. }
  365. /*
  366. Function: docopy
  367. Copy action
  368. Returns:
  369. Void
  370. */
  371. public function docopy(){
  372. if ($id = $this['request']->get('id', 'int')) {
  373. $this['widget']->copy($id);
  374. }
  375. echo $this['template']->render('dashboard');
  376. }
  377. }
  378. // bind events
  379. $widgetkit = Widgetkit::getInstance();
  380. $widgetkit['event']->bind('site', array($widgetkit['gallery'], 'site'));
  381. $widgetkit['event']->bind('dashboard', array($widgetkit['gallery'], 'dashboard'));
  382. $widgetkit['event']->bind('task:config_gallery', array($widgetkit['gallery'], 'config'));
  383. $widgetkit['event']->bind('task:edit_gallery', array($widgetkit['gallery'], 'edit'));
  384. $widgetkit['event']->bind('task:dirs_gallery', array($widgetkit['gallery'], 'dirs'));
  385. $widgetkit['event']->bind('task:files_gallery', array($widgetkit['gallery'], 'files'));
  386. $widgetkit['event']->bind('task:upload_files_gallery', array($widgetkit['gallery'], 'uploadFiles'));
  387. $widgetkit['event']->bind('task:delete_file_gallery', array($widgetkit['gallery'], 'deleteFile'));
  388. $widgetkit['event']->bind('task:create_folder_gallery', array($widgetkit['gallery'], 'createFolder'));
  389. $widgetkit['event']->bind('task:delete_folder_gallery', array($widgetkit['gallery'], 'deleteFolder'));
  390. $widgetkit['event']->bind('task:save_gallery', array($widgetkit['gallery'], 'save'));
  391. $widgetkit['event']->bind('task:delete_gallery', array($widgetkit['gallery'], 'delete'));
  392. $widgetkit['event']->bind('task:copy_gallery', array($widgetkit['gallery'], 'docopy'));