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