galleryalbumsmediasource.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * @package gallery
  4. */
  5. class GalleryAlbumsMediaSource extends modMediaSource implements modMediaSourceInterface {
  6. /** @var Gallery $gallery */
  7. public $gallery;
  8. /**
  9. * Initialize the source, preparing it for usage.
  10. *
  11. * @return boolean
  12. */
  13. public function initialize() {
  14. $this->gallery = $this->xpdo->getService('gallery','Gallery',$this->xpdo->getOption('gallery.core_path',null,$this->xpdo->getOption('core_path').'components/gallery/').'model/gallery/');
  15. if (!($this->gallery instanceof Gallery)) return false;
  16. $this->xpdo->lexicon->load('gallery:default','gallery:source');
  17. return true;
  18. }
  19. /**
  20. * Return an array of containers at this current level in the container structure. Used for the tree
  21. * navigation on the files tree.
  22. *
  23. * @abstract
  24. * @param string $path
  25. * @return array
  26. */
  27. public function getContainerList($path) {
  28. $properties = $this->getPropertyList();
  29. $list = array();
  30. if ($path == '/') {
  31. if (!empty($properties['album'])) {
  32. } else {
  33. $c = $this->xpdo->newQuery('galAlbum');
  34. $c->where(array('parent' => 0));
  35. $c->sortby('rank','ASC');
  36. $albums = $this->xpdo->getCollection('galAlbum',$c);
  37. /** @var galAlbum $album */
  38. foreach ($albums as $album) {
  39. $list[] = array(
  40. 'id' => 'album-'.$album->get('id'),
  41. 'text' => $album->get('name'),
  42. 'cls' => 'icon-album icon-avi',
  43. 'type' => 'gallery-album',
  44. 'data' => $album->toArray(),
  45. 'leaf' => false,
  46. 'treeHandler' => 'gal-tree-handler',
  47. );
  48. }
  49. }
  50. } else {
  51. $id = explode('-',$path);
  52. $id = (int)$id[1];
  53. /* get albums */
  54. $c = $this->xpdo->newQuery('galAlbum');
  55. $c->where(array('parent' => $id));
  56. $c->sortby('rank','ASC');
  57. $albums = $this->xpdo->getCollection('galAlbum',$c);
  58. /** @var galAlbum $album */
  59. foreach ($albums as $album) {
  60. $list[] = array(
  61. 'id' => 'album-'.$album->get('id'),
  62. 'text' => $album->get('name'),
  63. 'cls' => 'icon-album icon-avi',
  64. 'type' => 'gallery-album',
  65. 'data' => $album->toArray(),
  66. 'leaf' => false,
  67. 'treeHandler' => 'gal-tree-handler',
  68. );
  69. }
  70. /* get items */
  71. $c = $this->xpdo->newQuery('galItem');
  72. $c->innerJoin('galAlbumItem','AlbumItems');
  73. $c->leftJoin('galTag','Tags');
  74. $c->select($this->xpdo->getSelectColumns('galItem','galItem'));
  75. $c->select(array(
  76. 'AlbumItems.rank',
  77. '(SELECT GROUP_CONCAT(Tags.tag) FROM '.$this->xpdo->getTableName('galTag').' Tags WHERE Tags.item = galItem.id) AS tags',
  78. ));
  79. $c->select(array());
  80. $c->where(array(
  81. 'AlbumItems.album' => $id,
  82. ));
  83. $c->sortby('AlbumItems.rank','ASC');
  84. $items = $this->xpdo->getCollection('galItem',$c);
  85. /** @var galItem $item */
  86. foreach ($items as $item) {
  87. $itemArray = $item->toArray();
  88. $itemArray['image'] = $item->get('image');
  89. $itemArray['absoluteImage'] = $item->get('absoluteImage');
  90. $itemArray['relativeImage'] = $item->get('relativeImage');
  91. $list[] = array(
  92. 'id' => 'item-'.$item->get('id'),
  93. 'text' => $item->get('name'),
  94. 'cls' => 'icon-album-item icon-jpg',
  95. 'type' => 'gallery-item',
  96. 'leaf' => true,
  97. 'data' => $itemArray,
  98. 'qtip' => '<img src="'.$item->get('image').'" alt="'.$item->get('name').'" />',
  99. 'treeHandler' => 'gal-tree-handler',
  100. );
  101. }
  102. }
  103. return $list;
  104. }
  105. /**
  106. * Return a detailed list of objects in a specific path. Used for thumbnails in the Browser.
  107. *
  108. * @param string $path
  109. * @return array
  110. */
  111. public function getObjectsInContainer($path) {
  112. $properties = $this->getPropertyList();
  113. $list = array();
  114. if ($path == '/') {
  115. // Nothing to do? Perhaps displaying all gallery items, or a list of albums might be good.
  116. }
  117. else {
  118. $id = explode('-',$path);
  119. $id = (int)$id[1];
  120. /* get items */
  121. $c = $this->xpdo->newQuery('galItem');
  122. $c->innerJoin('galAlbumItem','AlbumItems');
  123. $c->leftJoin('galTag','Tags');
  124. $c->select($this->xpdo->getSelectColumns('galItem','galItem'));
  125. $c->select(array(
  126. 'AlbumItems.rank',
  127. '(SELECT GROUP_CONCAT(Tags.tag) FROM '.$this->xpdo->getTableName('galTag').' Tags WHERE Tags.item = galItem.id) AS tags',
  128. ));
  129. $c->select(array());
  130. $c->where(array(
  131. 'AlbumItems.album' => $id,
  132. ));
  133. $c->sortby('AlbumItems.rank','ASC');
  134. $items = $this->xpdo->getCollection('galItem',$c);
  135. $imageWidth = $this->ctx->getOption('filemanager_image_width', 400);
  136. $imageHeight = $this->ctx->getOption('filemanager_image_height', 300);
  137. $thumbHeight = $this->ctx->getOption('filemanager_thumb_height', 60);
  138. $thumbWidth = $this->ctx->getOption('filemanager_thumb_width', 80);
  139. /** @var galItem $item */
  140. foreach ($items as $item) {
  141. $itemArray = $item->toArray();
  142. $itemArray['image'] = $item->get('image');
  143. $itemArray['absoluteImage'] = $item->get('absoluteImage');
  144. $itemArray['relativeImage'] = $item->get('relativeImage');
  145. $size = @getimagesize($itemArray['image']);
  146. if (is_array($size)) {
  147. $imageWidth = $size[0] > 800 ? 800 : $size[0];
  148. $imageHeight = $size[1] > 600 ? 600 : $size[1];
  149. }
  150. /* ensure max h/w */
  151. if ($thumbWidth > $imageWidth) $thumbWidth = $imageWidth;
  152. if ($thumbHeight > $imageHeight) $thumbHeight = $imageHeight;
  153. $list[] = array(
  154. 'id' => 'item-'.$item->get('id'),
  155. 'name' => $item->get('name'),
  156. 'cls' => 'icon-album-item icon-jpg',
  157. 'type' => 'gallery-item',
  158. 'leaf' => true,
  159. 'image' => $item->get('image'),
  160. 'image_width' => $imageWidth,
  161. 'image_height' => $imageHeight,
  162. 'thumb' => $item->get('thumbnail'),
  163. 'thumb_width' => $thumbWidth,
  164. 'thumb_height' => $thumbHeight,
  165. 'url' => $itemArray['image'],
  166. 'relativeUrl' => $itemArray['relativeImage'],
  167. 'fullRelativeUrl' => $itemArray['relativeImage'],
  168. );
  169. }
  170. }
  171. return $list;
  172. }
  173. /**
  174. * Create a container at the passed location with the passed name
  175. *
  176. * @abstract
  177. * @param string $name
  178. * @param string $parentContainer
  179. * @return boolean
  180. */
  181. public function createContainer($name,$parentContainer) {
  182. /** @var galAlbum $album */
  183. $album = $this->xpdo->newObject('galAlbum');
  184. $album->set('name',$name);
  185. $album->set('createdby',$this->xpdo->user->get('id'));
  186. $total = $this->xpdo->getCount('galAlbum');
  187. $album->set('rank',$total);
  188. if (!($saved = $album->save())) {
  189. $this->xpdo->log(modX::LOG_LEVEL_ERROR,'[Gallery] Could not create album: '.print_r($album->toArray(),true));
  190. }
  191. return $saved;
  192. }
  193. /**
  194. * Remove the specified container
  195. *
  196. * @abstract
  197. * @param string $path
  198. * @return boolean
  199. */
  200. public function removeContainer($path) {
  201. $removed = false;
  202. /** @var galAlbum $album */
  203. $album = $this->xpdo->getObject('galAlbum',$path);
  204. if ($album) {
  205. if (!($removed = $album->remove())) {
  206. $this->xpdo->log(modX::LOG_LEVEL_ERROR,'[Gallery] Could not remove album.');
  207. }
  208. }
  209. return $removed;
  210. }
  211. /**
  212. * Rename a container
  213. *
  214. * @abstract
  215. * @param string $oldPath
  216. * @param string $newName
  217. * @return boolean
  218. */
  219. public function renameContainer($oldPath,$newName) { }
  220. /**
  221. * Upload objects to a specific container
  222. *
  223. * @abstract
  224. * @param string $container
  225. * @param array $objects
  226. * @return boolean
  227. */
  228. public function uploadObjectsToContainer($container,array $objects = array()) { }
  229. /**
  230. * Get the contents of an object
  231. *
  232. * @abstract
  233. * @param string $objectPath
  234. * @return boolean
  235. */
  236. public function getObjectContents($objectPath) { }
  237. /**
  238. * Update the contents of a specific object
  239. *
  240. * @abstract
  241. * @param string $objectPath
  242. * @param string $content
  243. * @return boolean
  244. */
  245. public function updateObject($objectPath,$content) { }
  246. /**
  247. * Create an object from a path
  248. *
  249. * @param string $objectPath
  250. * @param string $name
  251. * @param string $content
  252. * @return boolean|string
  253. */
  254. public function createObject($objectPath,$name,$content) { }
  255. /**
  256. * Remove an object
  257. *
  258. * @abstract
  259. * @param string $objectPath
  260. * @return boolean
  261. */
  262. public function removeObject($objectPath) { }
  263. /**
  264. * Rename a file/object
  265. *
  266. * @abstract
  267. * @param string $oldPath
  268. * @param string $newName
  269. * @return bool
  270. */
  271. public function renameObject($oldPath,$newName) { }
  272. /**
  273. * Get the openTo path for this source, used with TV input types and Static Elements/Resources
  274. *
  275. * @param string $value
  276. * @param array $parameters
  277. * @return string
  278. */
  279. public function getOpenTo($value,array $parameters = array()) { }
  280. /**
  281. * Get the base path for this source. Only applicable to sources that are streams, used for determining
  282. * the base path with Static objects.
  283. *
  284. * @param string $object An optional file to find the base path with
  285. * @return string
  286. */
  287. public function getBasePath($object = '') { }
  288. /**
  289. * Get the base URL for this source. Only applicable to sources that are streams; used for determining the base
  290. * URL with Static objects and downloading objects.
  291. *
  292. * @abstract
  293. * @param string $object
  294. * @return void
  295. */
  296. public function getBaseUrl($object = '') { }
  297. /**
  298. * Get the URL for an object in this source. Only applicable to sources that are streams; used for determining
  299. * the base URL with Static objects and downloading objects.
  300. *
  301. * @abstract
  302. * @param string $object
  303. * @return void
  304. */
  305. public function getObjectUrl($object = '') { }
  306. /**
  307. * Move a file or folder to a specific location
  308. *
  309. * @param string $from The location to move from
  310. * @param string $to The location to move to
  311. * @param string $point
  312. * @return boolean
  313. */
  314. public function moveObject($from,$to,$point = 'append') {
  315. $from = explode('-',$from);
  316. if (empty($from[1])) return false;
  317. $to = explode('-',$to);
  318. if (empty($to[1])) return false;
  319. /** If reordering */
  320. if ($from[0] == 'item' && $to[0] == 'item') {
  321. /** @TODO Eventually find a way to send the item; may need to refactor Revo code to do this. This current
  322. * code will break when we add multi-album support for items */
  323. /** @var galAlbumItem $fromItem */
  324. $fromItem = $this->xpdo->getObject('galAlbumItem',array('item' => $from[1]));
  325. if (empty($fromItem)) return false;
  326. /** @var galAlbumItem $toItem */
  327. $toItem = $this->xpdo->getObject('galAlbumItem',array('item' => $to[1]));
  328. if (empty($toItem)) return false;
  329. /* if dragging between items in an album */
  330. if ($fromItem->get('album') != $toItem->get('album')) {
  331. $from = implode('-',$from);
  332. return $this->moveObject($from,'album-'.$toItem->get('album'),$point);
  333. }
  334. switch ($point) {
  335. case 'below':
  336. /* move FROM to below TO */
  337. $newRank = $toItem->get('rank');
  338. $fromItem->reorder($newRank);
  339. break;
  340. case 'above':
  341. /* move FROM to above TO */
  342. $newRank = $toItem->get('rank')-1;
  343. $newRank = $newRank > 0 ? $newRank : 0;
  344. $fromItem->reorder($newRank);
  345. break;
  346. }
  347. /** Moving item to different album */
  348. } else if ($from[0] == 'item' && $to[0] == 'album') {
  349. /** @var galItem $fromItem */
  350. $fromItem = $this->xpdo->getObject('galItem',$from[1]);
  351. if (empty($fromItem)) return false;
  352. /** @var galAlbum $toAlbum */
  353. $toAlbum = $this->xpdo->getObject('galAlbum',$to[1]);
  354. if (empty($toAlbum)) return false;
  355. $fromItem->move($toAlbum->get('id'));
  356. /** Moving album */
  357. } else if ($from[0] == 'album') {
  358. /** @var galAlbum $fromAlbum */
  359. $fromAlbum = $this->xpdo->getObject('galAlbum',$from[1]);
  360. if (empty($fromAlbum)) return false;
  361. /** @var galAlbum $toAlbum */
  362. $toAlbum = $this->xpdo->getObject('galAlbum',$to[1]);
  363. if (empty($toAlbum)) return false;
  364. switch ($point) {
  365. case 'below':
  366. /* move FROM to below TO */
  367. $newRank = $toAlbum->get('rank');
  368. $fromAlbum->reorder($newRank);
  369. break;
  370. case 'above':
  371. /* move FROM to above TO */
  372. $newRank = $toAlbum->get('rank')-1;
  373. $fromAlbum->reorder($newRank);
  374. break;
  375. case 'append':
  376. /* move FROM inside TO */
  377. $fromAlbum->move($toAlbum->get('id'));
  378. break;
  379. }
  380. }
  381. return true;
  382. }
  383. /**
  384. * Get the name of this source type, ie, "File System"
  385. * @return string
  386. */
  387. public function getTypeName() {
  388. $this->xpdo->lexicon->load('gallery:source');
  389. return $this->xpdo->lexicon('gallery.source_name');
  390. }
  391. /**
  392. * Get a short description of this source type
  393. * @return string
  394. */
  395. public function getTypeDescription() {
  396. $this->xpdo->lexicon->load('gallery:source');
  397. return $this->xpdo->lexicon('gallery.source_desc');
  398. }
  399. /**
  400. * Get the default properties for this source. Override this in your custom source driver to provide custom
  401. * properties for your source type.
  402. * @return array
  403. */
  404. public function getDefaultProperties() {
  405. return array(
  406. );
  407. }
  408. }