Mixin_Displayed_Gallery_Renderer

Provides the ability to render a display type.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/nextgen_gallery_display/package.module.nextgen_gallery_display.php  
  1. class Mixin_Displayed_Gallery_Renderer extends Mixin 
  2. /** 
  3. * Displays a "displayed gallery" instance 
  4. * Alias Properties: 
  5. * gallery_ids/album_ids/tag_ids == container_ids 
  6. * image_ids/gallery_ids == entity_ids 
  7. * Default Behavior: 
  8. * - if order_by and order_direction are missing, the default settings 
  9. * are used from the "Other Options" page. The exception to this is 
  10. * when entity_ids are selected, in which the order is custom unless 
  11. * specified. 
  12. * How to use: 
  13. * To retrieve images from gallery 1 & 3, but exclude images 4 & 6: 
  14. * [ngg_images gallery_ids="1, 3" exclusions="4, 6" display_type="photocrati-nextgen_basic_thumbnails"] 
  15. * To retrieve images 1 & 2 from gallery 1: 
  16. * [ngg_images gallery_ids="1" image_ids="1, 2" display_type="photocrati-nextgen_basic_thumbnails"] 
  17. * To retrieve images matching tags "landscapes" and "wedding shoots": 
  18. * [ngg_images tag_ids="landscapes, wedding shoots" display_type="photocrati-nextgen_basic_thumbnails"] 
  19. * To retrieve galleries from albums 1 & #, but exclude sub-album 1: 
  20. * [ngg_images album_ids="1, 2" exclusions="a1" display_type="photocrati-nextgen_basic_compact_album"] 
  21. * To retrieve galleries from albums 1 & 2, but exclude gallery 1: 
  22. * [ngg_images album_ids="1, 2" exclusions="1" display_type="photocrati-nextgen_basic_compact_album"] 
  23. * To retrieve image 2, 3, and 5 - independent of what container is used 
  24. * [ngg_images image_ids="2, 3, 5" display_type="photocrati-nextgen_basic_thumbnails"] 
  25. * To retrieve galleries 3 & 5, custom sorted, in album view 
  26. * [ngg_images source="albums" gallery_ids="3, 5" display_type="photocrati-nextgen_basic_compact_album"] 
  27. * To retrieve recent images, sorted by alt/title text 
  28. * [ngg_images source="recent" order_by="alttext" display_type="photocrati-nextgen_basic_thumbnails"] 
  29. * To retrieve random image 
  30. * [ngg_images source="random" display_type="photocrati-nextgen_basic_thumbnails"] 
  31. * To retrieve a single image 
  32. * [ngg_images image_ids='8' display_type='photocrati-nextgen_basic_singlepic'] 
  33. * To retrieve a tag cloud 
  34. * [ngg_images tagcloud=yes display_type='photocrati-nextgen_basic_tagcloud'] 
  35. */ 
  36. public function display_images($params, $inner_content = NULL, $mode = NULL) 
  37. $retval = ''; 
  38. $displayed_gallery = NULL; 
  39. // Get the NextGEN settings to provide some defaults 
  40. $settings = C_NextGen_Settings::get_instance(); 
  41. // Configure the arguments 
  42. $defaults = array('id' => NULL, 'source' => '', 'container_ids' => array(), 'gallery_ids' => array(), 'album_ids' => array(), 'tag_ids' => array(), 'display_type' => '', 'exclusions' => array(), 'order_by' => $settings->galSort, 'order_direction' => $settings->galSortOrder, 'image_ids' => array(), 'entity_ids' => array(), 'tagcloud' => FALSE, 'inner_content' => $inner_content, 'returns' => 'included', 'slug' => NULL); 
  43. $args = shortcode_atts($defaults, $params); 
  44. // Are we loading a specific displayed gallery that's persisted? 
  45. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  46. if (!is_null($args['id'])) { 
  47. $displayed_gallery = $mapper->find($args['id']); 
  48. unset($mapper); 
  49. } else { 
  50. // Perform some conversions... 
  51. // Galleries? 
  52. if ($args['gallery_ids']) { 
  53. if ($args['source'] != 'albums' and $args['source'] != 'album') { 
  54. $args['source'] = 'galleries'; 
  55. $args['container_ids'] = $args['gallery_ids']; 
  56. if ($args['image_ids']) { 
  57. $args['entity_ids'] = $args['image_ids']; 
  58. } elseif ($args['source'] == 'albums') { 
  59. $args['entity_ids'] = $args['gallery_ids']; 
  60. unset($args['gallery_ids']); 
  61. } elseif ($args['album_ids'] || $args['album_ids'] === '0') { 
  62. $args['source'] = 'albums'; 
  63. $args['container_ids'] = $args['album_ids']; 
  64. unset($args['albums_ids']); 
  65. } elseif ($args['tag_ids']) { 
  66. $args['source'] = 'tags'; 
  67. $args['container_ids'] = $args['tag_ids']; 
  68. unset($args['tag_ids']); 
  69. } elseif ($args['image_ids']) { 
  70. $args['source'] = 'galleries'; 
  71. $args['entity_ids'] = $args['image_ids']; 
  72. unset($args['image_ids']); 
  73. } elseif ($args['tagcloud']) { 
  74. $args['source'] = 'tags'; 
  75. // Convert strings to arrays 
  76. if (!is_array($args['container_ids'])) { 
  77. $args['container_ids'] = preg_split('/, |\\|/', $args['container_ids']); 
  78. if (!is_array($args['exclusions'])) { 
  79. $args['exclusions'] = preg_split('/, |\\|/', $args['exclusions']); 
  80. if (!is_array($args['entity_ids'])) { 
  81. $args['entity_ids'] = preg_split('/, |\\|/', $args['entity_ids']); 
  82. // Get the display settings 
  83. foreach (array_keys($defaults) as $key) { 
  84. unset($params[$key]); 
  85. $args['display_settings'] = $params; 
  86. // Create the displayed gallery 
  87. $factory = C_Component_Factory::get_instance(); 
  88. $displayed_gallery = $factory->create('displayed_gallery', $args, $mapper); 
  89. unset($factory); 
  90. // Validate the displayed gallery 
  91. if ($displayed_gallery) { 
  92. if ($displayed_gallery->validate()) { 
  93. // Display! 
  94. return $this->object->render($displayed_gallery, TRUE, $mode); 
  95. } else { 
  96. if (C_NextGEN_Bootstrap::$debug) { 
  97. $retval = __('We cannot display this gallery', 'nggallery') . $this->debug_msg($displayed_gallery->get_errors()) . $this->debug_msg($displayed_gallery->get_entity()); 
  98. } else { 
  99. $retval = __('We cannot display this gallery', 'nggallery'); 
  100. } else { 
  101. $retval = __('We cannot display this gallery', 'nggallery'); 
  102. return $retval; 
  103. public function debug_msg($msg, $print_r = FALSE) 
  104. $retval = ''; 
  105. if (C_NextGEN_Bootstrap::$debug) { 
  106. ob_start(); 
  107. if ($print_r) { 
  108. echo '<pre>'; 
  109. print_r($msg); 
  110. echo '</pre>'; 
  111. } else { 
  112. var_dump($msg); 
  113. $retval = ob_get_clean(); 
  114. return $retval; 
  115. /** 
  116. * Renders a displayed gallery on the frontend 
  117. * @param C_Displayed_Gallery|stdClass $displayed_gallery 
  118. */ 
  119. public function render($displayed_gallery, $return = FALSE, $mode = null) 
  120. $retval = ''; 
  121. $lookup = TRUE; 
  122. // Simply throwing our rendered gallery into a feed will most likely not work correctly. 
  123. // The MediaRSS option in NextGEN is available as an alternative. 
  124. if (!C_NextGen_Settings::get_instance()->galleries_in_feeds && is_feed()) { 
  125. return sprintf(__(' [<a href="%s">See image gallery at %s</a>] ', 'nggallery'), esc_url(apply_filters('the_permalink_rss', get_permalink())), $_SERVER['SERVER_NAME']); 
  126. if ($mode == null) { 
  127. $mode = 'normal'; 
  128. if (apply_filters('ngg_cache_displayed_galleries', FALSE)) { 
  129. // Save the displayed gallery as a transient if it hasn't already. Allows for ajax operations 
  130. // to add or modify the gallery without losing a retrievable ID 
  131. if (!$displayed_gallery->apply_transient()) { 
  132. $displayed_gallery->to_transient(); 
  133. } else { 
  134. if (is_null($displayed_gallery->id())) { 
  135. $displayed_gallery->id(md5(json_encode($displayed_gallery->get_entity()))); 
  136. // Get the display type controller 
  137. $controller = $this->get_registry()->get_utility('I_Display_Type_Controller', $displayed_gallery->display_type); 
  138. // Get routing info 
  139. $router = C_Router::get_instance(); 
  140. $url = $router->get_url($router->get_request_uri(), TRUE); 
  141. // Should we lookup in cache? 
  142. if (is_array($displayed_gallery->container_ids) && in_array('All', $displayed_gallery->container_ids)) { 
  143. $lookup = FALSE; 
  144. } elseif ($displayed_gallery->source == 'albums' && $controller->param('gallery') or $controller->param('album')) { 
  145. $lookup = FALSE; 
  146. } elseif ($controller->param('show')) { 
  147. $lookup = FALSE; 
  148. } elseif ($controller->is_cachable() === FALSE) { 
  149. $lookup = FALSE; 
  150. } elseif (!NGG_RENDERING_CACHE_ENABLED) { 
  151. $lookup = FALSE; 
  152. // Enqueue any necessary static resources 
  153. if (!defined('NGG_SKIP_LOAD_SCRIPTS') || !NGG_SKIP_LOAD_SCRIPTS) { 
  154. $controller->enqueue_frontend_resources($displayed_gallery); 
  155. // Try cache lookup, if we're to do so 
  156. $key = NULL; 
  157. $html = FALSE; 
  158. if ($lookup) { 
  159. // The display type may need to output some things 
  160. // even when serving from the cache 
  161. if ($controller->has_method('cache_action')) { 
  162. $retval = $controller->cache_action($displayed_gallery); 
  163. // Output debug message 
  164. $retval .= $this->debug_msg('Lookup!'); 
  165. // Some settings affect display types 
  166. $settings = C_NextGen_Settings::get_instance(); 
  167. $key_params = apply_filters('ngg_displayed_gallery_cache_params', array($displayed_gallery->get_entity(), $url, $mode, $settings->activateTags, $settings->appendType, $settings->maxImages, $settings->thumbEffect, $settings->thumbCode, $settings->galSort, $settings->galSortDir)); 
  168. // Any displayed gallery links on the home page will need to be regenerated if the permalink structure 
  169. // changes 
  170. if (is_home() or is_front_page()) { 
  171. $key_params[] = get_option('permalink_structure'); 
  172. // Try getting the rendered HTML from the cache 
  173. $key = C_Photocrati_Transient_Manager::create_key('displayed_gallery_rendering', $key_params); 
  174. $html = C_Photocrati_Transient_Manager::fetch($key, FALSE); 
  175. // Output debug messages 
  176. if ($html) { 
  177. $retval .= $this->debug_msg('HIT!'); 
  178. } else { 
  179. $retval .= $this->debug_msg('MISS!'); 
  180. // TODO: This is hack. We need to figure out a more uniform way of detecting dynamic image urls 
  181. if (strpos($html, C_Photocrati_Settings_Manager::get_instance()->dynamic_thumbnail_slug . '/') !== FALSE) { 
  182. $html = FALSE; 
  183. } else { 
  184. $retval .= $this->debug_msg('Not looking up in cache as per rules'); 
  185. // If we're displaying a variant, I want to know it 
  186. if (isset($displayed_gallery->variation) && is_numeric($displayed_gallery->variation) && $displayed_gallery->variation > 0) { 
  187. $retval .= $this->debug_msg("Using variation #{$displayed_gallery->variation}!"); 
  188. // If a cached version doesn't exist, then create the cache 
  189. if (!$html) { 
  190. $retval .= $this->debug_msg('Rendering displayed gallery'); 
  191. $current_mode = $controller->get_render_mode(); 
  192. $controller->set_render_mode($mode); 
  193. $html = apply_filters('ngg_displayed_gallery_rendering', $controller->index_action($displayed_gallery, TRUE), $displayed_gallery); 
  194. if ($key != null) { 
  195. C_Photocrati_Transient_Manager::update($key, $html, NGG_RENDERING_CACHE_TTL); 
  196. $retval .= $html; 
  197. if (!$return) { 
  198. echo $retval; 
  199. return $retval;