snippet.galleryitem.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Display a single Gallery Item
  24. *
  25. * @package gallery
  26. */
  27. $gallery = $modx->getService('gallery','Gallery',$modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/gallery/',$scriptProperties);
  28. if (!($gallery instanceof Gallery)) return '';
  29. /* get ID of item */
  30. $id = (int)$modx->getOption('id',$scriptProperties,false);
  31. if ($modx->getOption('checkForRequestVar',$scriptProperties,true)) {
  32. $getParam = $modx->getOption('getParam',$scriptProperties,'galItem');
  33. if (!empty($_REQUEST[$getParam])) { $id = (int)$_REQUEST[$getParam]; }
  34. }
  35. if (empty($id)) return '';
  36. /* setup default properties */
  37. $tpl = $modx->getOption('tpl',$scriptProperties,'galItem');
  38. $toPlaceholders = $modx->getOption('toPlaceholders',$scriptProperties,true);
  39. $toPlaceholdersPrefix = $modx->getOption('toPlaceholdersPrefix',$scriptProperties,'galitem');
  40. $albumTpl = $modx->getOption('albumTpl',$scriptProperties,'galItemAlbum');
  41. $albumSeparator = $modx->getOption('albumSeparator',$scriptProperties,',&nbsp;');
  42. $albumRequestVar = $modx->getOption('albumRequestVar',$scriptProperties,'galAlbum');
  43. $tagTpl = $modx->getOption('tagTpl',$scriptProperties,'galItemTag');
  44. $tagSeparator = $modx->getOption('tagSeparator',$scriptProperties,',&nbsp;');
  45. $tagSortDir = $modx->getOption('tagSortDir',$scriptProperties,'DESC');
  46. $tagRequestVar = $modx->getOption('tagRequestVar',$scriptProperties,'galTag');
  47. /* get item */
  48. $c = $modx->newQuery('galItem');
  49. $c->select($modx->getSelectColumns('galItem','galItem'));
  50. $c->where(array(
  51. 'id' => $id,
  52. ));
  53. $item = $modx->getObject('galItem',$c);
  54. if (empty($item)) return '';
  55. /* setup placeholders */
  56. $itemArray = $item->toArray();
  57. $itemArray['filename'] = basename($item->get('filename'));
  58. $itemArray['filesize'] = $item->get('filesize');
  59. /* get image+thumbnail */
  60. $thumbProperties = $modx->getOption('thumbProperties',$scriptProperties,'');
  61. $thumbProperties = !empty($thumbProperties) ? $modx->fromJSON($thumbProperties) : array();
  62. $thumbProperties = array_merge(array(
  63. 'w' => (int)$modx->getOption('thumbWidth',$scriptProperties,100),
  64. 'h' => (int)$modx->getOption('thumbHeight',$scriptProperties,100),
  65. 'zc' => (boolean)$modx->getOption('thumbZoomCrop',$scriptProperties,0),
  66. 'far' => (string)$modx->getOption('thumbFar',$scriptProperties,'C'),
  67. 'q' => (int)$modx->getOption('thumbQuality',$scriptProperties,90),
  68. ),$thumbProperties);
  69. $itemArray['thumbnail'] = $item->get('thumbnail',$thumbProperties);
  70. $imageProperties = $modx->getOption('imageProperties',$scriptProperties,'');
  71. $imageProperties = !empty($imageProperties) ? $modx->fromJSON($imageProperties) : array();
  72. $imageProperties = array_merge(array(
  73. 'w' => (int)$modx->getOption('imageWidth',$scriptProperties,500),
  74. 'h' => (int)$modx->getOption('imageHeight',$scriptProperties,500),
  75. 'zc' => (boolean)$modx->getOption('imageZoomCrop',$scriptProperties,0),
  76. 'far' => (string)$modx->getOption('imageFar',$scriptProperties,false),
  77. 'q' => (int)$modx->getOption('imageQuality',$scriptProperties,90),
  78. ),$imageProperties);
  79. $itemArray['image'] = $item->get('thumbnail',$imageProperties);
  80. /* get albums */
  81. $c = $modx->newQuery('galAlbum');
  82. $c->innerJoin('galAlbumItem','AlbumItems',$modx->getSelectColumns('galAlbumItem','AlbumItems','',array('album')).' = '.$modx->getSelectColumns('galAlbum','galAlbum','',array('id'))
  83. .' AND '.$modx->getSelectColumns('galAlbumItem','AlbumItems','',array('item')).' = '.$item->get('id'));
  84. $c->sortby('AlbumItems.rank','ASC');
  85. $albums = $modx->getCollection('galAlbum',$c);
  86. $itemArray['albums'] = array();
  87. $i = 0;
  88. foreach ($albums as $album) {
  89. $albumArray = $album->toArray('',true,true);
  90. $albumArray['idx'] = $i;
  91. $albumArray['albumRequestVar'] = $albumRequestVar;
  92. $itemArray['albums'][] = $gallery->getChunk($albumTpl,$albumArray);
  93. $i++;
  94. }
  95. $itemArray['albums'] = implode($albumSeparator,$itemArray['albums']);
  96. /* get tags */
  97. $c = $modx->newQuery('galTag');
  98. $c->where(array(
  99. 'item' => $item->get('id'),
  100. ));
  101. $c->sortby('tag',$tagSortDir);
  102. $tags = $modx->getCollection('galTag',$c);
  103. $i = 0;
  104. $itemArray['tags'] = array();
  105. foreach ($tags as $tag) {
  106. $tagArray = $tag->toArray();
  107. $tagArray['idx'] = $i;
  108. $tagArray['tagRequestVar'] = $tagRequestVar;
  109. $itemArray['tags'][] = $gallery->getChunk($tagTpl,$tagArray);
  110. $i++;
  111. }
  112. $itemArray['tags'] = implode($tagSeparator,$itemArray['tags']);
  113. /* if outputting to placeholders, use this, otherwise, use tpl */
  114. if ($toPlaceholders) {
  115. $modx->toPlaceholders($itemArray,$toPlaceholdersPrefix);
  116. return '';
  117. }
  118. if (empty($tpl)) return '';
  119. $output .= $gallery->getChunk($tpl,$itemArray);
  120. return $output;