A_NextGen_Album_Child_Entities

Because enqueueing an albums child entities (for use in lightboxes) is slow to do inside of cache_action() and we can't guarantee index_action() will run on every hit (thanks to page caching) we inline those entities into our basic albums templates under a window.load listener.

Defined (1)

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

/products/photocrati_nextgen/modules/nextgen_basic_album/package.module.nextgen_basic_album.php  
  1. class A_NextGen_Album_Child_Entities extends Mixin 
  2. protected static $_runonce = FALSE; 
  3. public static $_entities = array(); 
  4. /** 
  5. * The album controller will invoke this filter when its _render_album() method is called 
  6. */ 
  7. function __construct() 
  8. if (!self::$_runonce) { 
  9. add_filter('ngg_album_prepared_child_entity', array($this, 'register_child_gallery'), 10, 2); 
  10. } else { 
  11. self::$_runonce = TRUE; 
  12. /** 
  13. * Register each gallery belonging to the album that has just been rendered, so that when the MVC controller 
  14. * system 'catches up' and runs $this->render_object() that method knows what galleries to inline as JS. 
  15. * @param array $gallery 
  16. * @param $displayed_gallery 
  17. * @return array mixed 
  18. */ 
  19. function register_child_gallery($galleries, $displayed_gallery) 
  20. if (!$this->is_basic_album($displayed_gallery)) { 
  21. return $galleries; 
  22. $id = $displayed_gallery->ID(); 
  23. foreach ($galleries as $gallery) { 
  24. if ($gallery->is_album) { 
  25. continue; 
  26. self::$_entities[$id][] = $gallery; 
  27. return $galleries; 
  28. function is_basic_album($displayed_gallery) 
  29. return in_array($displayed_gallery->display_type, array(NGG_BASIC_COMPACT_ALBUM, NGG_BASIC_EXTENDED_ALBUM)); 
  30. /** 
  31. * Determine if we need to append the JS to the current template. This method static for the basic album controller to access. 
  32. * @param $display_settings 
  33. * @return bool 
  34. */ 
  35. static function are_child_entities_enabled($display_settings) 
  36. $retval = FALSE; 
  37. if (empty($display_settings['open_gallery_in_lightbox'])) { 
  38. $display_settings['open_gallery_in_lightbox'] = 0; 
  39. if ($display_settings['open_gallery_in_lightbox'] == 1) { 
  40. $retval = TRUE; 
  41. return $retval; 
  42. /** 
  43. * Search inside the template for the inside of the container and append our inline JS 
  44. */ 
  45. function render_object() 
  46. $root_element = $this->call_parent('render_object'); 
  47. if ($displayed_gallery = $this->object->get_param('displayed_gallery')) { 
  48. if (!$this->is_basic_album($displayed_gallery)) { 
  49. return $root_element; 
  50. $ds = $displayed_gallery->display_settings; 
  51. if (self::are_child_entities_enabled($ds)) { 
  52. $id = $displayed_gallery->ID(); 
  53. foreach ($root_element->find('nextgen_gallery.gallery_container', TRUE) as $container) { 
  54. $container->append(self::generate_script(self::$_entities[$id])); 
  55. return $root_element; 
  56. /** 
  57. * Generate the JS that will be inserted into the template. This method static for the basic album controller to access. 
  58. * @param array $galleries 
  59. * @return string 
  60. */ 
  61. static function generate_script($galleries) 
  62. $retval = '<script type="text/javascript">window.addEventListener("load", function() {'; 
  63. foreach ($galleries as $gallery) { 
  64. $dg = $gallery->displayed_gallery; 
  65. $id = $dg->id(); 
  66. $retval .= 'galleries.gallery_' . $id . ' = ' . json_encode($dg->get_entity()) . ';'; 
  67. $retval .= 'galleries.gallery_' . $id . '.wordpress_page_root = "' . get_permalink() . '";'; 
  68. $retval .= '}, false);</script>'; 
  69. return $retval;