129.include.cache.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Gallery
  4. *
  5. * Copyright 2010-2012 by Shaun McCormick <shaun@modx.com>
  6. *
  7. * Gallery is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Gallery is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Gallery; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package gallery
  21. */
  22. /**
  23. * The main Gallery snippet.
  24. *
  25. * @var modX $modx
  26. * @var Gallery $gallery
  27. *
  28. * @package gallery
  29. */
  30. $gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
  31. if (!($gallery instanceof Gallery)) return '';
  32. $modx->lexicon->load('gallery:web');
  33. /* check for REQUEST vars if property set */
  34. $imageGetParam = $modx->getOption('imageGetParam',$scriptProperties,'galItem');
  35. $albumRequestVar = $modx->getOption('albumRequestVar',$scriptProperties,'galAlbum');
  36. $tagRequestVar = $modx->getOption('tagRequestVar',$scriptProperties,'galTag');
  37. if ($modx->getOption('checkForRequestAlbumVar',$scriptProperties,true)) {
  38. if (!empty($_REQUEST[$albumRequestVar])) $scriptProperties['album'] = $_REQUEST[$albumRequestVar];
  39. }
  40. if ($modx->getOption('checkForRequestTagVar',$scriptProperties,true)) {
  41. if (!empty($_REQUEST[$tagRequestVar])) $scriptProperties['tag'] = $_REQUEST[$tagRequestVar];
  42. }
  43. if (empty($scriptProperties['album']) && empty($scriptProperties['tag'])) return '';
  44. $data = $modx->call('galItem','getList',array(&$modx,$scriptProperties));
  45. $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
  46. $modx->setPlaceholder($totalVar,$data['total']);
  47. /* load plugins */
  48. $plugin = $modx->getOption('plugin',$scriptProperties,'');
  49. if (!empty($plugin)) {
  50. $pluginPath = $modx->getOption('pluginPath',$scriptProperties,'');
  51. if (empty($pluginPath)) {
  52. $pluginPath = $gallery->config['modelPath'].'gallery/plugins/';
  53. }
  54. /** @var GalleryPlugin $plugin */
  55. if (($className = $modx->loadClass($plugin,$pluginPath,true,true))) {
  56. $plugin = new $className($gallery,$scriptProperties);
  57. $plugin->load();
  58. $scriptProperties = $plugin->adjustSettings($scriptProperties);
  59. } else {
  60. return $modx->lexicon('gallery.plugin_err_load',array('name' => $plugin,'path' => $pluginPath));
  61. }
  62. } else {
  63. if ($modx->getOption('useCss',$scriptProperties,true)) {
  64. $modx->regClientCSS($gallery->config['cssUrl'].'web.css');
  65. }
  66. }
  67. /* iterate */
  68. $imageProperties = $modx->getOption('imageProperties',$scriptProperties,'');
  69. $imageProperties = !empty($imageProperties) ? $modx->fromJSON($imageProperties) : array();
  70. $imageProperties = array_merge(array(
  71. 'w' => (int)$modx->getOption('imageWidth',$scriptProperties,500),
  72. 'h' => (int)$modx->getOption('imageHeight',$scriptProperties,500),
  73. 'zc' => (boolean)$modx->getOption('imageZoomCrop',$scriptProperties,0),
  74. 'far' => (string)$modx->getOption('imageFar',$scriptProperties,false),
  75. 'q' => (int)$modx->getOption('imageQuality',$scriptProperties,90),
  76. ),$imageProperties);
  77. $thumbProperties = $modx->getOption('thumbProperties',$scriptProperties,'');
  78. $thumbProperties = !empty($thumbProperties) ? $modx->fromJSON($thumbProperties) : array();
  79. $thumbProperties = array_merge(array(
  80. 'w' => (int)$modx->getOption('thumbWidth',$scriptProperties,100),
  81. 'h' => (int)$modx->getOption('thumbHeight',$scriptProperties,100),
  82. 'zc' => (boolean)$modx->getOption('thumbZoomCrop',$scriptProperties,1),
  83. 'far' => (string)$modx->getOption('thumbFar',$scriptProperties,'C'),
  84. 'q' => (int)$modx->getOption('thumbQuality',$scriptProperties,90),
  85. ),$thumbProperties);
  86. $idx = 0;
  87. $output = array();
  88. $filesUrl = $modx->call('galAlbum','getFilesUrl',array(&$modx));
  89. $filesPath = $modx->call('galAlbum','getFilesPath',array(&$modx));
  90. $itemCls = $modx->getOption('itemCls',$scriptProperties,'gal-item');
  91. $imageAttributes = $modx->getOption('imageAttributes',$scriptProperties,'');
  92. $linkAttributes = $modx->getOption('linkAttributes',$scriptProperties,'');
  93. $linkToImage = $modx->getOption('linkToImage',$scriptProperties,false);
  94. $activeCls = $modx->getOption('activeCls',$scriptProperties,'gal-item-active');
  95. $highlightItem = $modx->getOption($imageGetParam,$_REQUEST,false);
  96. $defaultThumbTpl = $modx->getOption('thumbTpl',$scriptProperties,'galItemThumb');
  97. /** @var galItem $item */
  98. if (!is_array($data)) return '';
  99. // prep for &thumbTpl_N
  100. $keys = array_keys($scriptProperties);
  101. $nthTpls = array();
  102. foreach($keys as $key) {
  103. $keyBits = $gallery->explodeAndClean($key, '_');
  104. if (isset($keyBits[0]) && $keyBits[0] === 'thumbTpl') {
  105. if ($i = (int) $keyBits[1]) $nthTpls[$i] = $scriptProperties[$key];
  106. }
  107. }
  108. ksort($nthTpls);
  109. foreach ($data['items'] as $item) {
  110. $itemArray = $item->toArray();
  111. $itemArray['idx'] = $idx;
  112. $itemArray['cls'] = $itemCls;
  113. if ($itemArray['id'] == $highlightItem) {
  114. $itemArray['cls'] .= ' '.$activeCls;
  115. }
  116. $itemArray['filename'] = basename($item->get('filename'));
  117. $itemArray['image_absolute'] = $item->get('base_url').$filesUrl.$item->get('filename');
  118. $itemArray['fileurl'] = $itemArray['image_absolute'];
  119. $itemArray['filepath'] = $filesPath.$item->get('filename');
  120. $itemArray['filesize'] = $item->get('filesize');
  121. $itemArray['thumbnail'] = $item->get('thumbnail',$thumbProperties);
  122. $itemArray['image'] = $item->get('thumbnail',$imageProperties);
  123. $itemArray['image_attributes'] = $imageAttributes;
  124. $itemArray['link_attributes'] = $linkAttributes;
  125. if (!empty($scriptProperties['album'])) $itemArray['album'] = $scriptProperties['album'];
  126. if (!empty($scriptProperties['tag'])) $itemArray['tag'] = $scriptProperties['tag'];
  127. $itemArray['linkToImage'] = $linkToImage;
  128. $itemArray['url'] = $item->get('url');
  129. $itemArray['imageGetParam'] = $imageGetParam;
  130. $itemArray['albumRequestVar'] = $albumRequestVar;
  131. $itemArray['tagRequestVar'] = $tagRequestVar;
  132. $itemArray['tag'] = '';
  133. if ($plugin) {
  134. $plugin->renderItem($itemArray);
  135. }
  136. $thumbTpl = $defaultThumbTpl;
  137. if (isset($nthTpls[$idx])) {
  138. $thumbTpl = $nthTpls[$idx];
  139. } else {
  140. foreach ($nthTpls as $int => $tpl) {
  141. if ( ($idx % $int) === 0 ) $thumbTpl = $tpl;
  142. }
  143. }
  144. $output[] = $gallery->getChunk($thumbTpl,$itemArray);
  145. $idx++;
  146. }
  147. $output = implode("\n",$output);
  148. /* if set, place in a container tpl */
  149. $containerTpl = $modx->getOption('containerTpl',$scriptProperties,false);
  150. if (!empty($containerTpl)) {
  151. $ct = $gallery->getChunk($containerTpl,array(
  152. 'thumbnails' => $output,
  153. 'album_name' => $data['album']['name'],
  154. 'album_description' => $data['album']['description'],
  155. 'album_year' => isset($data['album']['year']) ? $data['album']['year'] : '',
  156. 'albumRequestVar' => $albumRequestVar,
  157. 'albumId' => $data['album']['id'],
  158. ));
  159. if (!empty($ct)) $output = $ct;
  160. }
  161. /* set to placeholders or output directly */
  162. $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
  163. if (!empty($toPlaceholder)) {
  164. $modx->toPlaceholders(array(
  165. $toPlaceholder => $output,
  166. $toPlaceholder.'.id' => $data['album']['id'],
  167. $toPlaceholder.'.name' => $data['album']['name'],
  168. $toPlaceholder.'.year' => isset($data['album']['year']) ? $data['album']['year'] : '',
  169. $toPlaceholder.'.description' => $data['album']['description'],
  170. $toPlaceholder.'.total' => $data['total'],
  171. $toPlaceholder.'.next' => $data['album']['id'] + 1,
  172. $toPlaceholder.'.prev' => $data['album']['id'] - 1,
  173. ));
  174. } else {
  175. $placeholderPrefix = $modx->getOption('placeholderPrefix',$scriptProperties,'gallery.');
  176. $modx->toPlaceholders(array(
  177. $placeholderPrefix.'id' => $data['album']['id'],
  178. $placeholderPrefix.'name' => $data['album']['name'],
  179. $placeholderPrefix.'year' => isset($data['album']['year']) ? $data['album']['year'] : '',
  180. $placeholderPrefix.'description' => $data['album']['description'],
  181. $placeholderPrefix.'total' => $data['total'],
  182. $placeholderPrefix.'next' => $data['album']['id'] + 1,
  183. $placeholderPrefix.'prev' => $data['album']['id'] - 1,
  184. ));
  185. return $output;
  186. }
  187. return '';
  188. return;