snippet.galleryalbums.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * Loads a list of Albums
  24. *
  25. * @var modX $modx
  26. * @var Gallery $gallery
  27. * @package gallery
  28. */
  29. $gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
  30. if (!($gallery instanceof Gallery)) return '';
  31. /* setup default properties */
  32. $rowTpl = $modx->getOption('rowTpl',$scriptProperties,'galAlbumRowTpl');
  33. $rowCls = $modx->getOption('rowCls',$scriptProperties,'');
  34. $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
  35. $showAll = $modx->getOption('showAll',$scriptProperties,false);
  36. $albumRequestVar = $modx->getOption('albumRequestVar',$scriptProperties,'galAlbum');
  37. $albumCoverSort = $modx->getOption('albumCoverSort',$scriptProperties,'rank');
  38. $albumCoverSortDir = $modx->getOption('albumCoverSortDir',$scriptProperties,'ASC');
  39. $showName = $modx->getOption('showName',$scriptProperties,true);
  40. $totalProperties = $scriptProperties;
  41. $totalProperties['limit'] = '0';
  42. $totalProperties['start'] = '0';
  43. $totalAlbums = $modx->call('galAlbum', 'getList', array(&$modx, $totalProperties));
  44. $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
  45. $modx->setPlaceholder($totalVar, count($totalAlbums));
  46. /* build query */
  47. $albums = $modx->call('galAlbum','getList',array(&$modx,$scriptProperties));
  48. /* handle sorting for album cover */
  49. if ($albumCoverSort == 'rank') {
  50. $albumCoverSort = 'AlbumItems.rank';
  51. }
  52. if (in_array(strtolower($albumCoverSort),array('random','rand()','rand'))) {
  53. $albumCoverSort = 'RAND()';
  54. $albumCoverSortDir = '';
  55. }
  56. /* get thumb properties for album cover */
  57. $thumbProperties = $modx->getOption('thumbProperties',$scriptProperties,'');
  58. $thumbProperties = !empty($thumbProperties) ? $modx->fromJSON($thumbProperties) : array();
  59. $thumbProperties = array_merge(array(
  60. 'w' => (int)$modx->getOption('thumbWidth',$scriptProperties,100),
  61. 'h' => (int)$modx->getOption('thumbHeight',$scriptProperties,100),
  62. 'zc' => (string)$modx->getOption('thumbZoomCrop',$scriptProperties,1),
  63. 'far' => (string)$modx->getOption('thumbFar',$scriptProperties,'C'),
  64. 'q' => (int)$modx->getOption('thumbQuality',$scriptProperties,90),
  65. ),$thumbProperties);
  66. /* iterate */
  67. $output = array();
  68. $idx = 0;
  69. $filesUrl = $modx->call('galAlbum','getFilesUrl',array(&$modx));
  70. $nav = array();
  71. /** @var galAlbum $album */
  72. foreach ($albums as $album) {
  73. $albumArray = $album->toArray();
  74. $classes = array($rowCls);
  75. if (!isset($nav['first'])) {
  76. $nav['first'] = $albumArray['id'];
  77. }
  78. if (!isset($nav['next']) && isset($nav['current'])) {
  79. $nav['next'] = $albumArray['id'];
  80. }
  81. if ($_GET[$albumRequestVar] == $albumArray['id']) {
  82. $nav['current'] = $albumArray['id'];
  83. $nav['curIdx'] = $idx + 1;
  84. $classes[] = 'current';
  85. }
  86. if (!isset($nav['current'])) {
  87. $nav['prev'] = $albumArray['id'];
  88. }
  89. $nav['last'] = $albumArray['id'];
  90. $albumArray['cls'] = implode(' ', $classes);
  91. $albumArray['idx'] = $idx;
  92. $albumArray['showName'] = $showName;
  93. $albumArray['albumRequestVar'] = $albumRequestVar;
  94. $coverItem = $album->getCoverItem($albumCoverSort,$albumCoverSortDir);
  95. if ($coverItem) {
  96. $albumArray['image'] = $coverItem->get('thumbnail',$thumbProperties);
  97. $albumArray['image_absolute'] = $filesUrl.$coverItem->get('filename');
  98. $albumArray['total'] = $coverItem->get('total');
  99. }
  100. $albumArray['cls'] = implode(' ', $classes);
  101. $albumArray['idx'] = $idx;
  102. $albumArray['showName'] = $showName;
  103. $albumArray['albumRequestVar'] = $albumRequestVar;
  104. $output[] = $gallery->getChunk($rowTpl,$albumArray);
  105. $idx++;
  106. }
  107. if (!isset($nav['current'])) {
  108. unset($nav['prev']);
  109. }
  110. $nav['count'] = $idx;
  111. /* set output to placeholder or return */
  112. $outputSeparator = $modx->getOption('outputSeparator',$scriptProperties,"\n");
  113. $output = implode($outputSeparator,$output);
  114. /* if set, place in a container tpl */
  115. $containerTpl = $modx->getOption('containerTpl',$scriptProperties,false);
  116. if (!empty($containerTpl)) {
  117. $ct = $gallery->getChunk($containerTpl,array(
  118. 'albums' => $output,
  119. 'nav' => $nav,
  120. 'albumRequestVar' => $albumRequestVar
  121. ));
  122. if (!empty($ct)) $output = $ct;
  123. }
  124. if ($toPlaceholder) {
  125. $modx->setPlaceholder($toPlaceholder,$output);
  126. return '';
  127. }
  128. return $output;