PageRenderTime 64ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/products/photocrati_nextgen/modules/nextgen_basic_album/adapter.nextgen_basic_album_controller.php

https://bitbucket.org/photocrati/nextgen-gallery
PHP | 423 lines | 308 code | 59 blank | 56 comment | 49 complexity | c9cf97a6c273455e0e1d5657d18d72fa MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Class A_NextGen_Basic_Album_Controller
  4. * @mixin C_Display_Type_Controller
  5. * @adapts I_Display_Type_Controller
  6. */
  7. class A_NextGen_Basic_Album_Controller extends Mixin_NextGen_Basic_Pagination
  8. {
  9. var $albums = array();
  10. /**
  11. * Renders the front-end for the NextGen Basic Album display type
  12. *
  13. * @param $displayed_gallery
  14. * @param bool $return
  15. */
  16. function index_action($displayed_gallery, $return = FALSE)
  17. {
  18. // Ensure that the open_gallery_in_lightbox setting is present
  19. if (!array_key_exists('open_gallery_in_lightbox', $displayed_gallery->display_settings)) {
  20. $displayed_gallery->display_settings['open_gallery_in_lightbox'] = 0;
  21. }
  22. $display_settings = $displayed_gallery->display_settings;
  23. // We need to fetch the album containers selected in the Attach
  24. // to Post interface. We need to do this, because once we fetch the
  25. // included entities, we need to iterate over each entity and assign it
  26. // a parent_id, which is the album that it belongs to. We need to do this
  27. // because the link to the gallery, is not /nggallery/gallery--id, but
  28. // /nggallery/album--id/gallery--id
  29. $parent_albums = $displayed_gallery->get_albums();
  30. // Are we to display a gallery? Ensure our 'gallery' isn't just a paginated album view
  31. $gallery = $gallery_slug = $this->param('gallery');
  32. if ($gallery && strpos($gallery, 'nggpage--') !== 0)
  33. {
  34. // basic albums only support one per post
  35. if (isset($GLOBALS['nggShowGallery']))
  36. return;
  37. $GLOBALS['nggShowGallery'] = TRUE;
  38. // Try finding the gallery by slug first. If nothing is found, we assume that
  39. // the user passed in a gallery id instead
  40. $mapper = C_Gallery_Mapper::get_instance();
  41. $tmp = $mapper->select()->where(array('slug = %s', $gallery))->limit(1)->run_query();
  42. $result = reset($tmp);
  43. unset($tmp);
  44. if ($result) {
  45. $gallery = $result->{$result->id_field};
  46. }
  47. $renderer = C_Displayed_Gallery_Renderer::get_instance('inner');
  48. $gallery_params = array(
  49. 'source' => 'galleries',
  50. 'container_ids' => array($gallery),
  51. 'display_type' => $display_settings['gallery_display_type'],
  52. 'original_display_type' => $displayed_gallery->display_type,
  53. 'original_settings' => $display_settings,
  54. 'original_album_entities' => $parent_albums
  55. );
  56. if (!empty($display_settings['gallery_display_template']))
  57. $gallery_params['template'] = $display_settings['gallery_display_template'];
  58. add_filter('ngg_displayed_gallery_rendering', array($this, 'add_description_to_legacy_templates'), 8, 2);
  59. add_filter('ngg_displayed_gallery_rendering', array($this, 'add_breadcrumbs_to_legacy_templates'), 9, 2);
  60. $output = $renderer->display_images($gallery_params, $return);
  61. remove_filter('ngg_displayed_gallery_rendering', array($this, 'add_breadcrumbs_to_legacy_templates'));
  62. remove_filter('ngg_displayed_gallery_rendering', array($this, 'add_description_to_legacy_templates'));
  63. return $output;
  64. }
  65. // If we're viewing a sub-album, then we use that album as a container instead
  66. else if (($album = $this->param('album'))) {
  67. // Are we to display a sub-album?
  68. {
  69. $mapper = C_Album_Mapper::get_instance();
  70. $result = $mapper->select()->where(array('slug = %s', $album))->limit(1)->run_query();
  71. $result = array_pop($result);
  72. $album_sub = $result ? $result->{$result->id_field} : null;
  73. if ($album_sub != null) {
  74. $album = $album_sub;
  75. }
  76. }
  77. $displayed_gallery->entity_ids = array();
  78. $displayed_gallery->sortorder = array();
  79. $displayed_gallery->container_ids = ($album === '0' OR $album === 'all') ? array() : array($album);
  80. $displayed_gallery->display_settings['original_album_id'] = 'a' . $album_sub;
  81. $displayed_gallery->display_settings['original_album_entities'] = $parent_albums;
  82. }
  83. // Get the albums
  84. // TODO: This should probably be moved to the elseif block above
  85. $this->albums = $displayed_gallery->get_albums();
  86. // None of the above: Display the main album. Get the settings required for display
  87. $current_page = (int)$this->param('page', $displayed_gallery->id(), 1);
  88. $offset = $display_settings['galleries_per_page'] * ($current_page - 1);
  89. $entities = $displayed_gallery->get_included_entities($display_settings['galleries_per_page'], $offset);
  90. // If there are entities to be displayed
  91. if ($entities)
  92. {
  93. $pagination_result = $this->object->create_pagination(
  94. $current_page,
  95. $displayed_gallery->get_entity_count(),
  96. $display_settings['galleries_per_page'],
  97. urldecode($this->object->param('ajax_pagination_referrer'))
  98. );
  99. if (!empty($display_settings['template']) && $display_settings['template'] != 'default')
  100. {
  101. // Add additional parameters
  102. $this->object->remove_param('ajax_pagination_referrer');
  103. $display_settings['current_page'] = $current_page;
  104. $display_settings['entities'] = &$entities;
  105. $display_settings['pagination_prev'] = $pagination_result['prev'];
  106. $display_settings['pagination_next'] = $pagination_result['next'];
  107. $display_settings['pagination'] = $pagination_result['output'];
  108. // Legacy templates lack a good way of injecting content at the right time
  109. $this->object->add_mixin('A_NextGen_Album_Breadcrumbs');
  110. $this->object->add_mixin('A_NextGen_Album_Descriptions');
  111. $breadcrumbs = $this->object->render_legacy_template_breadcrumbs($displayed_gallery, $entities);
  112. $description = $this->object->render_legacy_template_description($displayed_gallery);
  113. // Render legacy template
  114. $this->object->add_mixin('Mixin_NextGen_Basic_Templates');
  115. $display_settings = $this->prepare_legacy_album_params($displayed_gallery->get_entity(), $display_settings);
  116. $retval = $this->object->legacy_render($display_settings['template'], $display_settings, $return, 'album');
  117. if (!empty($description))
  118. $retval = $description . $retval;
  119. if (!empty($breadcrumbs))
  120. $retval = $breadcrumbs . $retval;
  121. return $retval;
  122. }
  123. else {
  124. $params = $display_settings;
  125. $albums = $this->prepare_legacy_album_params($displayed_gallery->get_entity(), array('entities' => $entities));
  126. $params['pagination'] = $pagination_result['output'];
  127. $params['image_gen_params'] = $albums['image_gen_params'];
  128. $params['galleries'] = $albums['galleries'];
  129. foreach ($params['galleries'] as &$gallery) {
  130. $gallery->entity_type = isset($gallery->is_gallery) && intval($gallery->is_gallery) ? 'gallery' : 'album';
  131. // If we're to open a gallery in a lightbox, we need to expose it to the lightbox
  132. // as a displayed gallery
  133. if (isset($params['open_gallery_in_lightbox']) && $gallery->entity_type == 'gallery') {
  134. $gallery->displayed_gallery = new C_Displayed_Gallery();
  135. $gallery->displayed_gallery->container_ids = array($gallery->{$gallery->id_field});
  136. $gallery->displayed_gallery->display_settings = $displayed_gallery->display_settings;
  137. $gallery->displayed_gallery->returns = 'included';
  138. $gallery->displayed_gallery->source = 'galleries';
  139. $gallery->displayed_gallery->images_list_count = $gallery->displayed_gallery->get_entity_count();
  140. $gallery->displayed_gallery->is_album_gallery = TRUE;
  141. $gallery->displayed_gallery->to_transient();
  142. if ($this->does_lightbox_support_displayed_gallery($displayed_gallery)) {
  143. $gallery->displayed_gallery->effect_code = $this->object->get_effect_code($gallery->displayed_gallery);
  144. }
  145. // Add "galleries.gallery_1 = {};"
  146. $this->object->_add_script_data(
  147. 'ngg_common',
  148. 'galleries.gallery_' . $gallery->displayed_gallery->id(),
  149. (array)$gallery->displayed_gallery->get_entity(),
  150. FALSE
  151. );
  152. $this->object->_add_script_data(
  153. 'ngg_common',
  154. 'galleries.gallery_' . $gallery->displayed_gallery->id() . '.wordpress_page_root',
  155. get_permalink(),
  156. FALSE
  157. );
  158. }
  159. }
  160. $params['displayed_gallery'] = $displayed_gallery;
  161. $params = $this->object->prepare_display_parameters($displayed_gallery, $params);
  162. switch ($displayed_gallery->display_type) {
  163. case NGG_BASIC_COMPACT_ALBUM:
  164. $template = 'compact';
  165. break;
  166. case NGG_BASIC_EXTENDED_ALBUM:
  167. $template = 'extended';
  168. break;
  169. }
  170. return $this->object->render_view("photocrati-nextgen_basic_album#{$template}", $params, $return);
  171. }
  172. }
  173. else {
  174. return $this->object->render_partial('photocrati-nextgen_gallery_display#no_images_found', array(), $return);
  175. }
  176. }
  177. function add_breadcrumbs_to_legacy_templates($html, $displayed_gallery)
  178. {
  179. $this->object->add_mixin('A_NextGen_Album_Breadcrumbs');
  180. $original_album_entities = array();
  181. if (isset($displayed_gallery->display_settings['original_album_entities']))
  182. $original_album_entities = $displayed_gallery->display_settings['original_album_entities'];
  183. elseif (isset($displayed_gallery->display_settings['original_settings']) && isset($displayed_gallery->display_settings['original_settings']['original_album_entities']))
  184. $original_album_entities = $displayed_gallery->display_settings['original_settings']['original_album_entities'];
  185. $breadcrumbs = $this->object->render_legacy_template_breadcrumbs(
  186. $displayed_gallery,
  187. $original_album_entities,
  188. $displayed_gallery->container_ids
  189. );
  190. if (!empty($breadcrumbs))
  191. $html = $breadcrumbs . $html;
  192. return $html;
  193. }
  194. function add_description_to_legacy_templates($html, $displayed_gallery)
  195. {
  196. $this->object->add_mixin('A_NextGen_Album_Descriptions');
  197. $description = $this->object->render_legacy_template_description($displayed_gallery);
  198. if (!empty($description))
  199. $html = $description . $html;
  200. return $html;
  201. }
  202. /**
  203. * Gets the parent album for the entity being displayed
  204. * @param int $entity_id
  205. * @return stdClass (album)
  206. */
  207. function get_parent_album_for($entity_id)
  208. {
  209. $retval = NULL;
  210. foreach ($this->albums as $album) {
  211. if (in_array($entity_id, $album->sortorder)) {
  212. $retval = $album;
  213. break;
  214. }
  215. }
  216. return $retval;
  217. }
  218. function prepare_legacy_album_params($displayed_gallery, $params)
  219. {
  220. $image_mapper = C_Image_Mapper::get_instance();
  221. $storage = C_Gallery_Storage::get_instance();
  222. $image_gen = C_Dynamic_Thumbnails_Manager::get_instance();
  223. if (empty($displayed_gallery->display_settings['override_thumbnail_settings']))
  224. {
  225. // legacy templates expect these dimensions
  226. $image_gen_params = array(
  227. 'width' => 91,
  228. 'height' => 68,
  229. 'crop' => TRUE
  230. );
  231. }
  232. else {
  233. // use settings requested by user
  234. $image_gen_params = array(
  235. 'width' => $displayed_gallery->display_settings['thumbnail_width'],
  236. 'height' => $displayed_gallery->display_settings['thumbnail_height'],
  237. 'quality' => isset($displayed_gallery->display_settings['thumbnail_quality']) ? $displayed_gallery->display_settings['thumbnail_quality'] : 100,
  238. 'crop' => isset($displayed_gallery->display_settings['thumbnail_crop']) ? $displayed_gallery->display_settings['thumbnail_crop'] : NULL,
  239. 'watermark' => isset($displayed_gallery->display_settings['thumbnail_watermark']) ? $displayed_gallery->display_settings['thumbnail_watermark'] : NULL
  240. );
  241. }
  242. // so user templates can know how big the images are expected to be
  243. $params['image_gen_params'] = $image_gen_params;
  244. // Transform entities
  245. $params['galleries'] = $params['entities'];
  246. unset($params['entities']);
  247. foreach ($params['galleries'] as &$gallery) {
  248. // Get the preview image url
  249. $gallery->previewurl = '';
  250. if ($gallery->previewpic && $gallery->previewpic > 0)
  251. {
  252. if (($image = $image_mapper->find(intval($gallery->previewpic))))
  253. {
  254. $gallery->previewpic_image = $image;
  255. $gallery->previewpic_fullsized_url = $storage->get_image_url($image, 'full');
  256. $gallery->previewurl = $storage->get_image_url($image, $image_gen->get_size_name($image_gen_params), TRUE);
  257. $gallery->previewname = $gallery->name;
  258. }
  259. }
  260. // Get the page link. If the entity is an album, then the url will
  261. // look like /nggallery/album--slug.
  262. $id_field = $gallery->id_field;
  263. if ($gallery->is_album)
  264. {
  265. if ($gallery->pageid > 0)
  266. $gallery->pagelink = @get_page_link($gallery->pageid);
  267. else {
  268. $pagelink = $this->object->get_routed_url(TRUE);
  269. $pagelink = $this->object->remove_param_for($pagelink, 'album');
  270. $pagelink = $this->object->remove_param_for($pagelink, 'gallery');
  271. $pagelink = $this->object->remove_param_for($pagelink, 'nggpage');
  272. $pagelink = $this->object->set_param_for($pagelink, 'album', $gallery->slug);
  273. $gallery->pagelink = $pagelink;
  274. }
  275. }
  276. // Otherwise, if it's a gallery then it will look like
  277. // /nggallery/album--slug/gallery--slug
  278. else {
  279. if ($gallery->pageid > 0) {
  280. $gallery->pagelink = @get_page_link($gallery->pageid);
  281. }
  282. if (empty($gallery->pagelink)) {
  283. $pagelink = $this->object->get_routed_url(TRUE);
  284. $parent_album = $this->object->get_parent_album_for($gallery->$id_field);
  285. if ($parent_album) {
  286. $pagelink = $this->object->remove_param_for($pagelink, 'album');
  287. $pagelink = $this->object->remove_param_for($pagelink, 'gallery');
  288. $pagelink = $this->object->remove_param_for($pagelink, 'nggpage');
  289. $pagelink = $this->object->set_param_for(
  290. $pagelink,
  291. 'album',
  292. $parent_album->slug
  293. );
  294. }
  295. // Legacy compat: use an album slug of 'all' if we're missing a container_id
  296. else if($displayed_gallery->container_ids === array('0')
  297. || $displayed_gallery->container_ids === array('')) {
  298. $pagelink = $this->object->set_param_for($pagelink, 'album', 'all');
  299. }
  300. else {
  301. $pagelink = $this->object->remove_param_for($pagelink, 'nggpage');
  302. $pagelink = $this->object->set_param_for($pagelink, 'album', 'album');
  303. }
  304. $gallery->pagelink = $this->object->set_param_for(
  305. $pagelink,
  306. 'gallery',
  307. $gallery->slug
  308. );
  309. }
  310. }
  311. // Let plugins modify the gallery
  312. $gallery = apply_filters('ngg_album_galleryobject', $gallery);
  313. }
  314. $params['album'] = reset($this->albums);
  315. $params['albums'] = $this->albums;
  316. // Clean up
  317. unset($storage);
  318. unset($image_mapper);
  319. unset($image_gen);
  320. unset($image_gen_params);
  321. return $params;
  322. }
  323. function _get_js_lib_url()
  324. {
  325. return $this->object->get_static_url('photocrati-nextgen_basic_album#init.js');
  326. }
  327. /**
  328. * Enqueues all static resources required by this display type
  329. *
  330. * @param C_Displayed_Gallery $displayed_gallery
  331. */
  332. function enqueue_frontend_resources($displayed_gallery)
  333. {
  334. $this->call_parent('enqueue_frontend_resources', $displayed_gallery);
  335. wp_enqueue_style(
  336. 'nextgen_basic_album_style',
  337. $this->object->get_static_url('photocrati-nextgen_basic_album#nextgen_basic_album.css'),
  338. FALSE,
  339. NGG_SCRIPT_VERSION
  340. );
  341. wp_enqueue_style(
  342. 'nextgen_pagination_style',
  343. $this->get_static_url('photocrati-nextgen_pagination#style.css'),
  344. FALSE,
  345. NGG_SCRIPT_VERSION
  346. );
  347. wp_enqueue_script(
  348. 'jquery.dotdotdot',
  349. $this->object->get_static_url('photocrati-nextgen_basic_album#jquery.dotdotdot-1.5.7-packed.js'),
  350. array('jquery'),
  351. NGG_SCRIPT_VERSION
  352. );
  353. $ds = $displayed_gallery->display_settings;
  354. if ((!empty($ds['enable_breadcrumbs']) && $ds['enable_breadcrumbs'])
  355. || (!empty($ds['original_settings']['enable_breadcrumbs']) && $ds['original_settings']['enable_breadcrumbs']))
  356. wp_enqueue_style(
  357. 'nextgen_basic_album_breadcrumbs_style',
  358. $this->object->get_static_url('photocrati-nextgen_basic_album#breadcrumbs.css'),
  359. FALSE,
  360. NGG_SCRIPT_VERSION
  361. );
  362. $this->enqueue_ngg_styles();
  363. }
  364. }