PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/evogallery/classes/gallery.class.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 442 lines | 350 code | 50 blank | 42 comment | 93 complexity | f924e04763343d715e3a68b42fcbf5f7 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /*---------------------------------------------------------------------------
  3. * Gallery - Contains functions for generating a listing of gallery thumbanils
  4. * while controlling various display aspects.
  5. *--------------------------------------------------------------------------*/
  6. class Gallery
  7. {
  8. var $config; // Array containing snippet configuration values
  9. /**
  10. * Class constructor, set configuration parameters
  11. */
  12. function Gallery($params)
  13. {
  14. global $modx;
  15. $this->config = $params;
  16. $this->galleriesTable = 'portfolio_galleries';
  17. }
  18. /**
  19. * Determine what action was requested and process request
  20. */
  21. function execute()
  22. {
  23. $output = '';
  24. $this->config['type'] = isset($this->config['type']) ? $this->config['type'] : 'simple-list';
  25. if ($this->config['includeAssets'])
  26. $this->getConfig($this->config['type']);
  27. if ($this->config['display'] == 'galleries')
  28. $output = $this->renderGalleries();
  29. elseif ($this->config['display'] == 'single')
  30. $output = $this->renderSingle();
  31. else
  32. $output = $this->renderImages();
  33. return $output;
  34. }
  35. /**
  36. * Generate a listing of document galleries
  37. */
  38. function renderGalleries()
  39. {
  40. global $modx;
  41. // Retrieve chunks/default templates from disk
  42. $tpl = ($this->config['tpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.default.txt') : $modx->getChunk($this->config['tpl']);
  43. $item_tpl = ($this->config['itemTpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.default.txt') : $modx->getChunk($this->config['itemTpl']);
  44. $item_tpl_first = ($this->config['itemTplFirst'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.first.txt') : $modx->getChunk($this->config['itemTplFirst']);
  45. $item_tpl_alt = ($this->config['itemTplAlt'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.alt.txt') : $modx->getChunk($this->config['itemTplAlt']);
  46. $item_tpl_last = ($this->config['itemTplLast'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.last.txt') : $modx->getChunk($this->config['itemTplLast']);
  47. // Hide/show docs based on configuration
  48. $docSelect = '';
  49. if ($this->config['docId'] != '*' && !empty($this->config['docId']))
  50. {
  51. if (strpos($this->config['docId'], ',') !== false)
  52. {
  53. $docSelect = 'parent IN ('.$this->config['docId'].')';
  54. }
  55. else
  56. $docSelect = 'parent = ' . $this->config['docId'];
  57. }
  58. if ($this->config['excludeDocs'] > 0)
  59. {
  60. $excludeDocs = '';
  61. if (strpos($this->config['excludeDocs'], ',') !== false)
  62. {
  63. $excludeDocs = 'parent NOT IN ('.$this->config['excludeDocs'].')';
  64. }
  65. else
  66. $excludeDocs .= 'parent != ' . $this->config['excludeDocs'];
  67. if (!empty($docSelect))
  68. $docSelect.= ' AND ';
  69. $docSelect.= $excludeDocs;
  70. }
  71. $phx = new PHxParser(); // Instantiate PHx
  72. $items = '';
  73. // Retrieve list of documents under the requested id
  74. $filter = " WHERE deleted = '0' AND published = '1' AND type = 'document' AND hidemenu <= '" . $this->config['ignoreHidden'] . "'";
  75. if (!empty($docSelect))
  76. $filter.=' AND '.$docSelect;
  77. if ($this->config['paginate']) {
  78. //Retrieve total records
  79. $totalRows = $modx->db->getValue('select count(*) from '.$modx->getFullTableName('site_content').$filter);
  80. if (!empty($this->config['limit']) && $totalRows>$this->config['limit'])
  81. $totalRows = $this->config['limit'];
  82. $limit = $this->paginate($totalRows);
  83. if (!empty($limit))
  84. $limit = ' limit '.$limit;
  85. } else
  86. $limit = !empty($this->config['limit']) ? ' limit '.$this->config['limit'] : "";
  87. $result = $modx->db->query("select id, pagetitle, longtitle, description, alias, pub_date, introtext, editedby, editedon, publishedon, publishedby, menutitle from " . $modx->getFullTableName('site_content') . $filter. ' order by '. $this->config['gallerySortBy'] . ' ' . $this->config['gallerySortDir'] . $limit);
  88. $recordCount = $modx->db->getRecordCount($result);
  89. if ($recordCount > 0)
  90. {
  91. $count = 1;
  92. while ($row = $modx->fetchRow($result))
  93. {
  94. $item_phx = new PHxParser();
  95. // Get total number of images for total placeholder
  96. $total_result = $modx->db->select("filename", $modx->getFullTableName($this->galleriesTable), "content_id = '" . $row['id'] . "'");
  97. $total = $modx->db->getRecordCount($total_result);
  98. // Fetch first image for each gallery, using the image sort order/direction
  99. $image_result = $modx->db->select("filename", $modx->getFullTableName($this->galleriesTable), "content_id = '" . $row['id'] . "'", $this->config['sortBy'] . ' ' . $this->config['sortDir'], '1');
  100. if ($modx->db->getRecordCount($image_result) > 0)
  101. {
  102. $image = $modx->fetchRow($image_result);
  103. foreach ($image as $name => $value)
  104. if ($name=='filename')
  105. $item_phx->setPHxVariable($name, rawurlencode(trim($value)));
  106. else
  107. $item_phx->setPHxVariable($name, trim($value));
  108. $item_phx->setPHxVariable('images_dir', $this->config['galleriesUrl'] . $row['id'] . '/');
  109. $item_phx->setPHxVariable('thumbs_dir', $this->config['galleriesUrl'] . $row['id'] . '/thumbs/');
  110. $item_phx->setPHxVariable('original_dir', $this->config['galleriesUrl'] . $row['id'] . '/original/');
  111. $item_phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  112. foreach ($row as $name => $value)
  113. $item_phx->setPHxVariable($name, trim($value));
  114. // Get template variable output for row and set variables as needed
  115. $row_tvs = $modx->getTemplateVarOutput('*',$row['id']);
  116. foreach ($row_tvs as $name => $value)
  117. $item_phx->setPHxVariable($name, trim($value));
  118. $item_phx->setPHxVariable('total', $total);
  119. if(!empty($item_tpl_first) && $count == 1){
  120. $items .= $item_phx->Parse($item_tpl_first);
  121. } else if(!empty($item_tpl_last) && $count == $recordCount){
  122. $items .= $item_phx->Parse($item_tpl_last);
  123. } else if(!empty($item_tpl_alt) && $count % $this->config['itemAltNum'] == 0){
  124. $items .= $item_phx->Parse($item_tpl_alt);
  125. } else {
  126. $items .= $item_phx->Parse($item_tpl);
  127. }
  128. }
  129. $count++;
  130. }
  131. }
  132. $phx->setPHxVariable('items', $items);
  133. $phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  134. return $phx->Parse($tpl); // Pass through PHx;
  135. }
  136. /**
  137. * Generate a listing of thumbnails/images for gallery/slideshow display
  138. */
  139. function renderImages()
  140. {
  141. global $modx;
  142. // Retrieve chunks/default templates from disk
  143. $tpl = ($this->config['tpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.default.txt') : $modx->getChunk($this->config['tpl']);
  144. $item_tpl = ($this->config['itemTpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.default.txt') : $modx->getChunk($this->config['itemTpl']);
  145. $item_tpl_first = ($this->config['itemTplFirst'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.first.txt') : $modx->getChunk($this->config['itemTplFirst']);
  146. $item_tpl_alt = ($this->config['itemTplAlt'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.alt.txt') : $modx->getChunk($this->config['itemTplAlt']);
  147. $item_tpl_last = ($this->config['itemTplLast'] == '') ? @file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.last.txt') : $modx->getChunk($this->config['itemTplLast']);
  148. $docSelect = '';
  149. if ($this->config['docId'] != '*' && !empty($this->config['docId']))
  150. {
  151. if (strpos($this->config['docId'], ',') !== false)
  152. {
  153. $docSelect = 'content_id IN ('.$this->config['docId'].')';
  154. }
  155. else
  156. $docSelect = 'content_id = ' . $this->config['docId'];
  157. }
  158. if ($this->config['excludeDocs'] > 0)
  159. {
  160. $excludeDocs = '';
  161. if (strpos($this->config['excludeDocs'], ',') !== false)
  162. {
  163. $excludeDocs = 'content_id NOT IN ('.$this->config['excludeDocs'].')';
  164. }
  165. else
  166. $excludeDocs .= 'content_id != ' . $this->config['excludeDocs'];
  167. if (!empty($docSelect))
  168. $docSelect.= ' AND ';
  169. $docSelect.= $excludeDocs;
  170. }
  171. if (!empty($this->config['tags']))
  172. {
  173. $mode = (!empty($this->config['tagMode']) ? $this->config['tagMode'] : 'AND');
  174. foreach (explode(',', $this->config['tags']) as $tag) {
  175. $tagSelect .= "keywords LIKE '%" . trim($tag) . "%' ".$mode." ";
  176. }
  177. $tagSelect = rtrim($tagSelect, ' '.$mode.' ');
  178. if (!empty($docSelect))
  179. $docSelect.=' AND ';
  180. $docSelect .= "(".$tagSelect.")";
  181. }
  182. $phx = new PHxParser(); // Instantiate PHx
  183. $items = '';
  184. $limit = '';
  185. $where = !empty($docSelect)?' WHERE '.$docSelect.' ':'';
  186. if ($this->config['paginate']) {
  187. //Retrieve total records
  188. $totalRows = $modx->db->getValue('select count(*) from '.$modx->getFullTableName($this->galleriesTable).$where.(!empty($this->config['limit']) ? ' limit '.$this->config['limit'] : ""));
  189. $limit = $this->paginate($totalRows);
  190. if (!empty($limit))
  191. $limit = ' limit '.$limit;
  192. } else
  193. $limit = !empty($this->config['limit']) ? ' limit '.$this->config['limit'] : "";
  194. // Retrieve photos from the database table
  195. $result = $modx->db->query("select * from ". $modx->getFullTableName($this->galleriesTable). $where. ' order by '. $this->config['sortBy'] . ' ' . $this->config['sortDir']. $limit);
  196. $recordCount = $modx->db->getRecordCount($result);
  197. if ($recordCount > 0)
  198. {
  199. $count = 1;
  200. while ($row = $modx->fetchRow($result))
  201. {
  202. $item_phx = new PHxParser();
  203. foreach ($row as $name => $value)
  204. if ($name=='filename')
  205. $item_phx->setPHxVariable($name, rawurlencode(trim($value)));
  206. else
  207. $item_phx->setPHxVariable($name, trim($value));
  208. $imgsize = getimagesize($this->config['galleriesPath'] . $row['content_id'] . '/' . $row['filename']);
  209. $item_phx->setPHxVariable('width',$imgsize[0]);
  210. $item_phx->setPHxVariable('height',$imgsize[1]);
  211. $item_phx->setPHxVariable('image_withpath', $this->config['galleriesUrl'] . $row['content_id'] . '/' . $row['filename']);
  212. $item_phx->setPHxVariable('images_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/');
  213. $item_phx->setPHxVariable('thumbs_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/thumbs/');
  214. $item_phx->setPHxVariable('original_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/original/');
  215. $item_phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  216. if(!empty($item_tpl_first) && $count == 1){
  217. $items .= $item_phx->Parse($item_tpl_first);
  218. } else if(!empty($item_tpl_last) && $count == $recordCount){
  219. $items .= $item_phx->Parse($item_tpl_last);
  220. } else if(!empty($item_tpl_alt) && $count % $this->config['itemAltNum'] == 0){
  221. $items .= $item_phx->Parse($item_tpl_alt);
  222. } else {
  223. $items .= $item_phx->Parse($item_tpl);
  224. }
  225. $count++;
  226. }
  227. }
  228. $phx->setPHxVariable('items', $items);
  229. $phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  230. return $phx->Parse($tpl); // Pass through PHx;
  231. }
  232. /**
  233. * Generate a listing of a single thumbnail/image for gallery/slideshow display
  234. */
  235. function renderSingle()
  236. {
  237. global $modx;
  238. // Retrieve chunks/default templates from disk
  239. $tpl = ($this->config['tpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.default.txt') : $modx->getChunk($this->config['tpl']);
  240. $item_tpl = ($this->config['itemTpl'] == '') ? file_get_contents($this->config['snippetPath'] . $this->config['type'] . '/tpl.item.default.txt') : $modx->getChunk($this->config['itemTpl']);
  241. $picSelect = '';
  242. if ($this->config['picId'] != '*' && !empty($this->config['picId']))
  243. {
  244. $picSelect = "id = '" . $this->config['picId'] . "'";
  245. }
  246. $phx = new PHxParser(); // Instantiate PHx
  247. $items = '';
  248. // Retrieve photos from the database table
  249. $result = $modx->db->select("*", $modx->getFullTableName($this->galleriesTable), $picSelect);
  250. if ($modx->db->getRecordCount($result) > 0)
  251. {
  252. while ($row = $modx->fetchRow($result))
  253. {
  254. $item_phx = new PHxParser();
  255. foreach ($row as $name => $value)
  256. if ($name=='filename')
  257. $item_phx->setPHxVariable($name, rawurlencode(trim($value)));
  258. else
  259. $item_phx->setPHxVariable($name, trim($value));
  260. $item_phx->setPHxVariable('images_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/');
  261. $item_phx->setPHxVariable('thumbs_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/thumbs/');
  262. $item_phx->setPHxVariable('original_dir', $this->config['galleriesUrl'] . $row['content_id'] . '/original/');
  263. $item_phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  264. $items .= $item_phx->Parse($item_tpl);
  265. }
  266. }
  267. $phx->setPHxVariable('items', $items);
  268. $phx->setPHxVariable('plugin_dir', $this->config['snippetUrl'] . $this->config['type'] . '/');
  269. return $phx->Parse($tpl); // Pass through PHx;
  270. }
  271. /**
  272. * Get configuration settings for the selected gallery/slideshow type
  273. */
  274. function getConfig($type)
  275. {
  276. global $modx;
  277. if (file_exists($this->config['snippetPath'] . $type . '/tpl.config.txt'))
  278. {
  279. $register = 0;
  280. $config = file($this->config['snippetPath'] . $type . '/tpl.config.txt');
  281. foreach ($config as $line)
  282. {
  283. $line = trim($line);
  284. if ($line == '')
  285. $register = 0;
  286. elseif (strpos($line, '@SCRIPT') === 0)
  287. $register = 1;
  288. elseif (strpos($line, '@CSS') === 0)
  289. $register = 2;
  290. elseif (strpos($line, '@EXTSCRIPT') === 0)
  291. $register = 3;
  292. elseif (strpos($line, '@EXTCSS') === 0)
  293. $register = 4;
  294. else
  295. {
  296. switch ($register)
  297. {
  298. case 1:
  299. $modx->regClientStartupScript($this->config['snippetUrl'] . $type . '/' . $line);
  300. break;
  301. case 2:
  302. $modx->regClientCSS($this->config['snippetUrl'] . $type . '/' . $line);
  303. break;
  304. case 3:
  305. $modx->regClientStartupScript($line);
  306. break;
  307. case 4:
  308. $modx->regClientCSS($line);
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. /**
  316. * Replace placeholders in template
  317. */
  318. function processTemplate($tpl, $params)
  319. {
  320. //Parse placeholders
  321. foreach($params as $key=>$value)
  322. {
  323. $tpl = str_replace('[+'.$key.'+]', $value, $tpl);
  324. }
  325. return $tpl;
  326. }
  327. /**
  328. * Set pagination's placeholders
  329. * Return string with limit values for SQL query
  330. */
  331. function paginate($totalRows) {
  332. global $modx;
  333. if (!$this->config['paginate'])
  334. return "";
  335. $pageUrl = !empty($this->config['id'])?$this->config['id'].'_page':'page';
  336. $page = isset($_GET[$pageUrl])?intval($_GET[$pageUrl]):1;
  337. $rowsPerPage = $this->config['show'];
  338. $totalPages = ceil($totalRows/$rowsPerPage);
  339. $previous = $page - 1;
  340. $next = $page + 1;
  341. $start = ($page-1)*$rowsPerPage;
  342. if ($start<0)
  343. $start = 0;
  344. $stop = $start + $rowsPerPage - 1;
  345. if ($stop>=$totalRows)
  346. $stop = $totalRows - 1;
  347. $split = "";
  348. if ($previous > 0 && $next <= $totalPages)
  349. $split = $paginateSplitterCharacter;
  350. $previoustpl = '';
  351. $previousplaceholder = '';
  352. if ($previous > 0)
  353. $previoustpl = 'tplPaginatePrevious';
  354. elseif ($this->config['paginateAlwaysShowLinks'])
  355. $previoustpl = 'tplPaginatePreviousOff';
  356. if (!empty($previoustpl))
  357. $previousplaceholder = $this->processTemplate($this->config[$previoustpl],
  358. array('url'=>$modx->makeUrl($modx->documentIdentifier,'',($previous!=1?"$pageUrl=$previous":"")),
  359. 'PaginatePreviousText'=>$this->config['paginatePreviousText']));
  360. $nexttpl = '';
  361. $nextplaceholder = '';
  362. if ($next <= $totalPages)
  363. $nexttpl = 'tplPaginateNext';
  364. elseif ($this->config['paginateAlwaysShowLinks'])
  365. $nexttpl = 'tplPaginateNextOff';
  366. if (!empty($nexttpl))
  367. $nextplaceholder = $this->processTemplate($this->config[$nexttpl],
  368. array('url'=>$modx->makeUrl($modx->documentIdentifier,'',($next!=1?"$pageUrl=$next":"")),
  369. 'PaginateNextText'=>$this->config['paginateNextText']));
  370. $pages = '';
  371. for ($i=1;$i<=$totalPages;$i++) {
  372. if ($i != $page) {
  373. $pages .= $this->processTemplate($this->config['tplPaginatePage'],
  374. array('url'=>$modx->makeUrl($modx->documentIdentifier,'',($i!=1?"$pageUrl=$i":"")),'page'=>$i));
  375. } else {
  376. $modx->setPlaceholder($this->config['id']."currentPage", $i);
  377. $pages .= $this->processTemplate($this->config['tplPaginateCurrentPage'], array('page'=>$i));
  378. }
  379. }
  380. $modx->setPlaceholder($this->config['id']."next", $nextplaceholder);
  381. $modx->setPlaceholder($this->config['id']."previous", $previousplaceholder);
  382. $modx->setPlaceholder($this->config['id']."splitter", $split);
  383. $modx->setPlaceholder($this->config['id']."start", $start+1);
  384. $modx->setPlaceholder($this->config['id']."stop", $stop+1);
  385. $modx->setPlaceholder($this->config['id']."total", $totalRows);
  386. $modx->setPlaceholder($this->config['id']."pages", $pages);
  387. $modx->setPlaceholder($this->config['id']."perPage", $rowsPerPage);
  388. $modx->setPlaceholder($this->config['id']."totalPages", $totalPages);
  389. return $start.','.($stop-$start+1);
  390. }
  391. }
  392. ?>