PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/kraymitchell/apex
PHP | 516 lines | 270 code | 98 blank | 148 comment | 55 complexity | fcc1b3d223e86d45205838da5a88e9b8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-3.0
  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(), 'style'=> $this["widget"]->defaultStyle($this->type)) 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 style
  83. $style = isset($widget->settings['style']) ? $widget->settings['style'] : 'default';
  84. if(!$this["path"]->path("{$widget->type}:styles/{$style}")) {
  85. return "Style {$style} for Widget {$widget->type} not found!";
  86. }
  87. return $this['template']->render("gallery:styles/$style/template", array('widget' => $widget));
  88. }
  89. /*
  90. Function: images
  91. Get widget images
  92. Returns:
  93. Array
  94. */
  95. public function images($widget, $overrides = array()) {
  96. $images = array();
  97. $settings = $widget->settings;
  98. $captions = $widget->captions;
  99. $links = $widget->links;
  100. $cache = $this['path']->path('gallery.cache:');
  101. // merge setting overrides
  102. if (is_array($overrides)) {
  103. $settings = array_merge($settings, $overrides);
  104. }
  105. // create cache
  106. if ($cache && !file_exists($cache.'/'.$widget->id)) {
  107. mkdir($cache.'/'.$widget->id, 0777, true);
  108. }
  109. if (is_array($widget->paths)) {
  110. foreach ($widget->paths as $path) {
  111. foreach ($this['path']->files("media:$path") as $file) {
  112. if (preg_match("/(\.gif|\.jpg|\.jpeg|\.png)$/i", $file)) {
  113. // image data
  114. $image['name'] = basename($file);
  115. $image['ext'] = pathinfo($file, PATHINFO_EXTENSION);
  116. $image['filename'] = pathinfo($file, PATHINFO_FILENAME);
  117. $image['caption'] = isset($captions[$path.'/'.$file]) ? $captions[$path.'/'.$file] : null;
  118. $image['link'] = isset($links[$path.'/'.$file]) ? $links[$path.'/'.$file] : null;
  119. $image['file'] = sprintf("media:%s/%s", $path, $file);
  120. $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']);
  121. $image['url'] = $this['path']->url($image['file']);
  122. // cache image
  123. if ($cache && !$this['path']->path($image['cache'])) {
  124. if (isset($settings['animated']) && $settings['animated'] == 'kenburns') {
  125. $kbw = is_numeric($settings['width']) ? $settings['width']*1.2:$settings['width'];
  126. $kbh = is_numeric($settings['height']) ? $settings['height']*1.2:$settings['height'];
  127. $this['image']->create($this['path']->path($image['file']))->output(array('width' => $kbw, 'height' => $kbh, 'file' => $cache.preg_replace('/^gallery\.cache:/', '', $image['cache'], 1)));
  128. } else {
  129. $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)));
  130. }
  131. }
  132. $image['cache_url'] = $this['path']->url($image['cache']);
  133. if ($p = $this['path']->path($image['cache'])) {
  134. if (!is_numeric($settings['width']) || !is_numeric($settings['height'])) {
  135. list($image['width'], $image['height']) = @getimagesize($p);
  136. } else {
  137. $image['width'] = $settings['width'];
  138. $image['height'] = $settings['height'];
  139. }
  140. }
  141. array_push($images, $image);
  142. }
  143. }
  144. }
  145. }
  146. // random order
  147. if (count($images) && isset($settings['order']) && $settings['order'] =="random") {
  148. shuffle($images);
  149. }
  150. return $images;
  151. }
  152. /*
  153. Function: dashboard
  154. Render dashboard layout
  155. Returns:
  156. Void
  157. */
  158. public function dashboard() {
  159. // init vars
  160. $xml = simplexml_load_file($this['path']->path('gallery:gallery.xml'));
  161. $galleries = $this['widget']->all($this->type);
  162. // add js
  163. $this['asset']->addFile('js', 'gallery:js/dashboard.js');
  164. // render dashboard
  165. echo $this['template']->render('gallery:layouts/dashboard', compact('xml', 'galleries'));
  166. }
  167. /*
  168. Function: config
  169. Save configuration
  170. Returns:
  171. Void
  172. */
  173. public function config() {
  174. // save configuration
  175. foreach ($this['request']->get('post:', 'array') as $option => $value) {
  176. if (preg_match('/^gallery_/', $option)) {
  177. $this['system']->options->set($option, $value);
  178. }
  179. }
  180. $this['system']->saveOptions();
  181. }
  182. /*
  183. Function: edit
  184. Edit action
  185. Returns:
  186. Void
  187. */
  188. public function edit($id = null) {
  189. // add css/js
  190. $this['asset']->addFile('css', 'gallery:css/admin.css');
  191. $this['asset']->addFile('js', 'gallery:js/edit.js');
  192. // get xml/widget
  193. $xml = simplexml_load_file(dirname(__FILE__).'/gallery.xml');
  194. $widget = $this->get($id ? $id : $this['request']->get('id', 'int'));
  195. if (isset($widget->paths) && is_array($widget->paths)) {
  196. $widget->paths = array_filter($widget->paths, create_function('$path','
  197. $wk = Widgetkit::getInstance();
  198. return $wk["path"]->path("media:/$path") ? true : false;
  199. '));
  200. }
  201. // get style and xml
  202. $style = $widget->style;
  203. if (!$style) {
  204. echo 'No styles found';
  205. return;
  206. }
  207. $style_xml = simplexml_load_file($this['path']->path("gallery:styles/$style/config.xml"));
  208. // render edit form
  209. echo $this['template']->render('gallery:layouts/edit', compact('widget', 'xml', 'style_xml'));
  210. }
  211. /*
  212. Function: dirs
  213. Get directory list JSON formatted
  214. Returns:
  215. Void
  216. */
  217. public function dirs() {
  218. $dirs = array();
  219. $path = $this['request']->get('path', 'string');
  220. foreach ($this['path']->dirs('media:'.$path) as $dir) {
  221. $dirs[] = array('name' => basename($dir), 'path' => $path.'/'.$dir, 'type' => 'folder');
  222. }
  223. echo json_encode($dirs);
  224. }
  225. /*
  226. Function: files
  227. Get image file list JSON formatted
  228. Returns:
  229. Void
  230. */
  231. public function files() {
  232. $files = array();
  233. $path = $this['request']->get('path', 'string');
  234. foreach ($this['path']->files('media:'.$path) as $file) {
  235. if (preg_match("/(\.gif|\.jpg|\.jpeg|\.png)$/i", $file)) {
  236. $files[] = array('name' => basename($file), 'path' => $path.'/'.$file, 'type' => 'file');
  237. }
  238. }
  239. echo json_encode($files);
  240. }
  241. /*
  242. Function: uploadFiles
  243. Upload files to gallery
  244. Returns:
  245. Void
  246. */
  247. public function uploadFiles() {
  248. $files = isset($_FILES['files']) ? $_FILES['files'] : array();
  249. $path = $this['request']->get('path', 'string');
  250. $uploaded = array();
  251. if ($this['system']->checkToken($this['request']->get('token', 'string')) && isset($files['name']) && strlen($path) && $targetpath = $this['path']->path('media:'.$path)) {
  252. for ($i = 0; $i < count($files['name']); $i++) {
  253. if (!$files['error'][$i] && preg_match('/image/i', $files['type'][$i]) && @move_uploaded_file($files['tmp_name'][$i], $targetpath.'/'.$files['name'][$i])) {
  254. $uploaded[] = $files['name'][$i];
  255. }
  256. }
  257. }
  258. echo json_encode($uploaded);
  259. }
  260. /*
  261. Function: deleteFile
  262. Delete files from gallery folders
  263. Returns:
  264. Void
  265. */
  266. public function deleteFile() {
  267. $status = 0;
  268. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $file = $this['path']->path('media:'.$this['request']->get('file', 'string')) and is_file($file)) {
  269. $status = (int) @unlink($file);
  270. }
  271. echo json_encode(compact('status'));
  272. }
  273. /*
  274. Function: createFolder
  275. Create new gallery folder
  276. Returns:
  277. Void
  278. */
  279. public function createFolder() {
  280. $status = 0;
  281. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $path = preg_replace('#^([^/])#', '/$1', str_replace('\\', '/', $this['request']->get('path', 'string'))) and strpos($path, '/..') === false) {
  282. $status = (int) @mkdir($this['path']->path('media:').$path, 0775);
  283. }
  284. echo json_encode(compact('status'));
  285. }
  286. /*
  287. Function: deleteFolder
  288. Delete gallery folder
  289. Returns:
  290. Void
  291. */
  292. public function deleteFolder() {
  293. $status = 0;
  294. if ($this['system']->checkToken($this['request']->get('token', 'string')) && $path = $this['path']->path('media:'.$this['request']->get('path', 'string'))) {
  295. if ($this['path']->path('media:') != $path) {
  296. $status = (int) $this->_deleteFolder($path);
  297. }
  298. }
  299. echo json_encode(compact('status'));
  300. }
  301. /*
  302. Function: _deleteFolder
  303. Delete folder recursively and don't recurse into symlinks
  304. Returns:
  305. Boolean
  306. */
  307. protected function _deleteFolder($path) {
  308. $directory = new DirectoryIterator($path);
  309. foreach ($directory as $file) {
  310. if ($file->isFile() || $file->isLink()) {
  311. unlink($file->getPathName());
  312. } elseif (!$file->isDot() && $file->isDir()) {
  313. $this->_deleteFolder($file->getPathName());
  314. }
  315. }
  316. return @rmdir($path);
  317. }
  318. /*
  319. Function: save
  320. Save action
  321. Returns:
  322. Void
  323. */
  324. public function save() {
  325. // save data
  326. $data['type'] = $this->type;
  327. $data['id'] = $this['request']->get('id', 'int');
  328. $data['name'] = $this['request']->get('name', 'string');
  329. $data['settings'] = $this['request']->get('settings', 'array');
  330. $data['style'] = $this['request']->get('settings.style', 'array');
  331. $data['paths'] = $this['request']->get('paths', 'array', array());
  332. $data['captions'] = $this['request']->get('captions', 'array', array());
  333. $data['links'] = $this['request']->get('links', 'array', array());
  334. // force numeric values if not auto
  335. if ($data['settings']['width']!= 'auto') {
  336. $data['settings']['width'] = intval($data['settings']['width']);
  337. }
  338. if ($data['settings']['height']!= 'auto') {
  339. $data['settings']['height'] = intval($data['settings']['height']);
  340. }
  341. // merge style settings defaults
  342. if ($path = $this['path']->path("gallery:styles/{$data['style']}/config.xml")) {
  343. $xml = simplexml_load_file($path);
  344. foreach ($xml->xpath('settings/setting') as $setting) {
  345. $name = (string) $setting->attributes()->name;
  346. $value = (string) $setting->attributes()->default;
  347. if (!array_key_exists($name, $data['settings'])) {
  348. $data['settings'][$name] = $value;
  349. }
  350. }
  351. }
  352. $this->edit($this['widget']->save($data));
  353. }
  354. /*
  355. Function: delete
  356. Delete action
  357. Returns:
  358. Void
  359. */
  360. public function delete(){
  361. $data['id'] = false;
  362. if ($id = $this['request']->get('id', 'int')) {
  363. if ($this['widget']->delete($id)) {
  364. $data['id'] = $id;
  365. }
  366. }
  367. echo json_encode($data);
  368. }
  369. /*
  370. Function: docopy
  371. Copy action
  372. Returns:
  373. Void
  374. */
  375. public function docopy(){
  376. if ($id = $this['request']->get('id', 'int')) {
  377. $this['widget']->copy($id);
  378. }
  379. echo $this['template']->render('dashboard');
  380. }
  381. }
  382. // bind events
  383. $widgetkit = Widgetkit::getInstance();
  384. $widgetkit['event']->bind('site', array($widgetkit['gallery'], 'site'));
  385. $widgetkit['event']->bind('dashboard', array($widgetkit['gallery'], 'dashboard'));
  386. $widgetkit['event']->bind('task:config_gallery', array($widgetkit['gallery'], 'config'));
  387. $widgetkit['event']->bind('task:edit_gallery', array($widgetkit['gallery'], 'edit'));
  388. $widgetkit['event']->bind('task:dirs_gallery', array($widgetkit['gallery'], 'dirs'));
  389. $widgetkit['event']->bind('task:files_gallery', array($widgetkit['gallery'], 'files'));
  390. $widgetkit['event']->bind('task:upload_files_gallery', array($widgetkit['gallery'], 'uploadFiles'));
  391. $widgetkit['event']->bind('task:delete_file_gallery', array($widgetkit['gallery'], 'deleteFile'));
  392. $widgetkit['event']->bind('task:create_folder_gallery', array($widgetkit['gallery'], 'createFolder'));
  393. $widgetkit['event']->bind('task:delete_folder_gallery', array($widgetkit['gallery'], 'deleteFolder'));
  394. $widgetkit['event']->bind('task:save_gallery', array($widgetkit['gallery'], 'save'));
  395. $widgetkit['event']->bind('task:delete_gallery', array($widgetkit['gallery'], 'delete'));
  396. $widgetkit['event']->bind('task:copy_gallery', array($widgetkit['gallery'], 'docopy'));