/products/photocrati_nextgen/modules/nextgen_basic_templates/package.module.nextgen_basic_templates.php

  1. <?php 
  2. /** 
  3. * Class A_NextGen_Basic_Template_Form 
  4. * @mixin C_Form 
  5. * @adapts I_Form 
  6. */ 
  7. class A_NextGen_Basic_Template_Form extends Mixin 
  8. /** 
  9. * Renders 'template' settings field 
  10. * 
  11. * @param $display_type 
  12. * @return mixed 
  13. */ 
  14. function _render_nextgen_basic_templates_template_field($display_type) 
  15. switch ($display_type->name) { 
  16. case 'photocrati-nextgen_basic_singlepic': 
  17. $prefix = 'singlepic'; 
  18. break; 
  19. case 'photocrati-nextgen_basic_thumbnails': 
  20. $prefix = 'gallery'; 
  21. break; 
  22. case 'photocrati-nextgen_basic_slideshow': 
  23. $prefix = 'gallery'; 
  24. break; 
  25. case 'photocrati-nextgen_basic_imagebrowser': 
  26. $prefix = 'imagebrowser'; 
  27. break; 
  28. case NGG_BASIC_COMPACT_ALBUM: 
  29. $prefix = 'album'; 
  30. break; 
  31. case NGG_BASIC_EXTENDED_ALBUM: 
  32. $prefix = 'album'; 
  33. break; 
  34. default: 
  35. $prefix = FALSE; 
  36. break; 
  37. // ensure the current file is in the list 
  38. $templates = $this->object->_get_available_templates($prefix); 
  39. if (!isset($templates[$display_type->settings['template']])) { 
  40. $templates[$display_type->settings['template']] = $display_type->settings['template']; 
  41. // add <default> template that acts the same way as having no template specified 
  42. $templates['default'] = __('Default', 'nggallery'); 
  43. return $this->object->render_partial('photocrati-nextgen_basic_templates#nextgen_basic_templates_settings_template', array('display_type_name' => $display_type->name, 'template_label' => __('Template', 'nggallery'), 'template_text' => __('Use a legacy template when rendering (not recommended).', 'nggallery'), 'chosen_file' => $display_type->settings['template'], 'templates' => $templates), True); 
  44. /** 
  45. * Retrieves listing of available templates 
  46. * 
  47. * Override this function to modify or add to the available templates listing, array format 
  48. * is array(file_abspath => label) 
  49. * @return array 
  50. */ 
  51. function _get_available_templates($prefix = FALSE) 
  52. $templates = array(); 
  53. foreach (C_Legacy_Template_Locator::get_instance()->find_all($prefix) as $label => $files) { 
  54. foreach ($files as $file) { 
  55. $tmp = explode(DIRECTORY_SEPARATOR, $file); 
  56. $templates[$file] = "{$label}: " . end($tmp); 
  57. asort($templates); 
  58. return $templates; 
  59. /** 
  60. * Returns the parameter objects necessary for legacy template rendering (legacy_render()) 
  61. * 
  62. * @param $images 
  63. * @param $displayed_gallery 
  64. * @param array $params 
  65. * 
  66. * @return array 
  67. */ 
  68. function prepare_legacy_parameters($images, $displayed_gallery, $params = array()) 
  69. // setup 
  70. $image_map = C_Image_Mapper::get_instance(); 
  71. $gallery_map = C_Gallery_Mapper::get_instance(); 
  72. $image_key = $image_map->get_primary_key_column(); 
  73. $gallery_key = $gallery_map->get_primary_key_column(); 
  74. $gallery_id = $displayed_gallery->id(); 
  75. $pid = $this->object->param('pid'); 
  76. // because picture_list implements ArrayAccess any array-specific actions must be taken on 
  77. // $picture_list->container or they won't do anything 
  78. $picture_list = new C_Image_Wrapper_Collection(); 
  79. $current_pid = NULL; 
  80. // begin processing 
  81. $current_page = @get_the_ID() == FALSE ? 0 : @get_the_ID(); 
  82. // determine what the "current image" is; used mostly for carousel 
  83. if (!is_numeric($pid) && !empty($pid)) { 
  84. $picture = $image_map->find_first(array('image_slug = %s', $pid)); 
  85. $pid = $picture->{$image_key}; 
  86. // create our new wrappers 
  87. foreach ($images as &$image) { 
  88. if ($image && isset($params['effect_code'])) { 
  89. if (is_object($image)) { 
  90. $image->thumbcode = $params['effect_code']; 
  91. } elseif (is_array($image)) { 
  92. $image['thumbcode'] = $params['effect_code']; 
  93. $new_image = new C_Image_Wrapper($image, $displayed_gallery); 
  94. if ($pid == $new_image->{$image_key}) { 
  95. $current_pid = $new_image; 
  96. $picture_list[] = $new_image; 
  97. reset($picture_list->container); 
  98. // assign current_pid 
  99. $current_pid = is_null($current_pid) ? current($picture_list->container) : $current_pid; 
  100. foreach ($picture_list as &$image) { 
  101. if (isset($image->hidden) && $image->hidden) { 
  102. $tmp = $displayed_gallery->display_settings['number_of_columns']; 
  103. $image->style = $tmp > 0 ? 'style="width:' . floor(100 / $tmp) . '%;display: none;"' : 'style="display: none;"'; 
  104. // find our gallery to build the new one on 
  105. $orig_gallery = $gallery_map->find(current($picture_list->container)->galleryid); 
  106. // create the 'gallery' object 
  107. $gallery = new stdclass(); 
  108. $gallery->ID = $displayed_gallery->id(); 
  109. $gallery->name = stripslashes($orig_gallery->name); 
  110. $gallery->title = stripslashes($orig_gallery->title); 
  111. $gallery->description = html_entity_decode(stripslashes($orig_gallery->galdesc)); 
  112. $gallery->pageid = $orig_gallery->pageid; 
  113. $gallery->anchor = 'ngg-gallery-' . $gallery_id . '-' . $current_page; 
  114. $gallery->displayed_gallery =& $displayed_gallery; 
  115. $gallery->columns = @intval($displayed_gallery->display_settings['number_of_columns']); 
  116. $gallery->imagewidth = $gallery->columns > 0 ? 'style="width:' . floor(100 / $gallery->columns) . '%;"' : ''; 
  117. if (!empty($displayed_gallery->display_settings['show_slideshow_link'])) { 
  118. $gallery->show_slideshow = TRUE; 
  119. $gallery->slideshow_link = $params['slideshow_link']; 
  120. $gallery->slideshow_link_text = $displayed_gallery->display_settings['slideshow_link_text']; 
  121. } else { 
  122. $gallery->show_slideshow = FALSE; 
  123. $gallery = apply_filters('ngg_gallery_object', $gallery, 4); 
  124. // build our array of things to return 
  125. $return = array('registry' => C_Component_Registry::get_instance(), 'gallery' => $gallery); 
  126. // single_image is an internally added flag 
  127. if (!empty($params['single_image'])) { 
  128. $return['image'] = $picture_list[0]; 
  129. } else { 
  130. $return['current'] = $current_pid; 
  131. $return['images'] = $picture_list->container; 
  132. // this is expected to always exist 
  133. if (!empty($params['pagination'])) { 
  134. $return['pagination'] = $params['pagination']; 
  135. } else { 
  136. $return['pagination'] = NULL; 
  137. if (!empty($params['next'])) { 
  138. $return['next'] = $params['next']; 
  139. } else { 
  140. $return['next'] = FALSE; 
  141. if (!empty($params['prev'])) { 
  142. $return['prev'] = $params['prev']; 
  143. } else { 
  144. $return['prev'] = FALSE; 
  145. return $return; 
  146. function enqueue_static_resources() 
  147. wp_enqueue_style('ngg_template_settings', $this->get_static_url('photocrati-nextgen_basic_templates#ngg_template_settings.css'), FALSE, NGG_SCRIPT_VERSION); 
  148. wp_enqueue_script('ngg_template_settings', $this->get_static_url('photocrati-nextgen_basic_templates#ngg_template_settings.js'), array('ngg_select2'), NGG_SCRIPT_VERSION, TRUE); 
  149. wp_localize_script('ngg_template_settings', 'ngg_template_settings', array('placeholder_text' => __('No template selected'))); 
  150. $atp = C_Attach_Controller::get_instance(); 
  151. if ($atp != null) { 
  152. $atp->mark_script('ngg_template_settings'); 
  153. /** 
  154. * Provides a utility to locate legacy templates 
  155. * @mixin Mixin_Legacy_Template_Locator 
  156. * @implements I_Legacy_Template_Locator 
  157. */ 
  158. class C_Legacy_Template_Locator extends C_Component 
  159. static $_instances = array(); 
  160. function define($context = FALSE) 
  161. parent::define($context); 
  162. $this->add_mixin('Mixin_Legacy_Template_Locator'); 
  163. $this->implement('I_Legacy_Template_Locator'); 
  164. static function get_instance($context = FALSE) 
  165. if (!isset(self::$_instances[$context])) { 
  166. $klass = get_class(); 
  167. self::$_instances[$context] = new $klass($context); 
  168. return self::$_instances[$context]; 
  169. /** 
  170. * Provides instance methods for the legacy template locator 
  171. */ 
  172. class Mixin_Legacy_Template_Locator extends Mixin 
  173. /** 
  174. * Returns an array of template storing directories 
  175. * 
  176. * @return array Template storing directories 
  177. */ 
  178. function get_template_directories() 
  179. return apply_filters('ngg_legacy_template_directories', array('Child Theme' => get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'nggallery' . DIRECTORY_SEPARATOR, 'Parent Theme' => get_template_directory() . DIRECTORY_SEPARATOR . 'nggallery' . DIRECTORY_SEPARATOR, 'NextGEN Legacy' => NGGALLERY_ABSPATH . 'view' . DIRECTORY_SEPARATOR, 'NextGEN Overrides' => implode(DIRECTORY_SEPARATOR, array(WP_CONTENT_DIR, 'ngg', 'legacy', 'templates')))); 
  180. /** 
  181. * Returns an array of all available template files 
  182. * 
  183. * @return array All available template files 
  184. */ 
  185. function find_all($prefix = FALSE) 
  186. $files = array(); 
  187. foreach ($this->object->get_template_directories() as $label => $dir) { 
  188. $tmp = $this->object->get_templates_from_dir($dir, $prefix); 
  189. if (!$tmp) { 
  190. continue; 
  191. $files[$label] = $tmp; 
  192. return $files; 
  193. /** 
  194. * Recursively scans $dir for files ending in .php 
  195. * 
  196. * @param string $dir Directory 
  197. * @return array All php files in $dir 
  198. */ 
  199. function get_templates_from_dir($dir, $prefix = FALSE) 
  200. if (!is_dir($dir)) { 
  201. return; 
  202. $dir = new RecursiveDirectoryIterator($dir); 
  203. $iterator = new RecursiveIteratorIterator($dir); 
  204. // convert single-item arrays to string 
  205. if (is_array($prefix) && count($prefix) <= 1) { 
  206. $prefix = end($prefix); 
  207. // we can filter results by allowing a set of prefixes, one prefix, or by showing all available files 
  208. if (is_array($prefix)) { 
  209. $str = implode('|', $prefix); 
  210. $regex_iterator = new RegexIterator($iterator, "/({$str})-.+\\.php\$/i", RecursiveRegexIterator::GET_MATCH); 
  211. } elseif (is_string($prefix)) { 
  212. $regex_iterator = new RegexIterator($iterator, "#(.*)[/\\\\]{$prefix}\\-?.*\\.php\$#i", RecursiveRegexIterator::GET_MATCH); 
  213. } else { 
  214. $regex_iterator = new RegexIterator($iterator, '/^.+\\.php$/i', RecursiveRegexIterator::GET_MATCH); 
  215. $files = array(); 
  216. foreach ($regex_iterator as $filename) { 
  217. $files[] = reset($filename); 
  218. return $files; 
  219. /** 
  220. * Find a particular template by name 
  221. * @param $template 
  222. */ 
  223. function find($template_name) 
  224. $template_abspath = FALSE; 
  225. // hook into the render feature to allow other plugins to include templates 
  226. $custom_template = apply_filters('ngg_render_template', FALSE, $template_name); 
  227. if ($custom_template === FALSE) { 
  228. $custom_template = $template_name; 
  229. // Ensure we have a PHP extension 
  230. if (strpos($custom_template, '.php') === FALSE) { 
  231. $custom_template .= '.php'; 
  232. // Find the abspath of the template to render 
  233. if (!@file_exists($custom_template)) { 
  234. foreach ($this->object->get_template_directories() as $dir) { 
  235. if ($template_abspath) { 
  236. break; 
  237. $filename = implode(DIRECTORY_SEPARATOR, array(rtrim($dir, "/\\"), $custom_template)); 
  238. if (@file_exists($filename)) { 
  239. $template_abspath = $filename; 
  240. } elseif (strpos($custom_template, '-template') === FALSE) { 
  241. $filename = implode(DIRECTORY_SEPARATOR, array(rtrim($dir, "/\\"), str_replace('.php', '', $custom_template) . '-template.php')); 
  242. if (@file_exists($filename)) { 
  243. $template_abspath = $filename; 
  244. } else { 
  245. // An absolute path was already given 
  246. $template_abspath = $custom_template; 
  247. return $template_abspath; 
  248. /** 
  249. * Class Mixin_NextGen_Basic_Templates 
  250. * @mixin C_Display_Type_Controller 
  251. * 
  252. * Provides a mixin that other display type controllers can use to render the ngglegacy templates 
  253. */ 
  254. class Mixin_NextGen_Basic_Templates extends Mixin 
  255. /** 
  256. * Renders NextGen-Legacy style templates 
  257. * 
  258. * @param string $template_name File name 
  259. * @param array $vars Specially formatted array of parameters 
  260. * @param bool $callback 
  261. * @param bool $return 
  262. */ 
  263. function legacy_render($template_name, $vars = array(), $return = FALSE, $prefix = NULL) 
  264. $retval = "[Not a valid template]"; 
  265. $template_locator = C_Legacy_Template_Locator::get_instance(); 
  266. // search first for files with their prefix 
  267. $template_abspath = $template_locator->find($prefix . '-' . $template_name); 
  268. if (!$template_abspath) { 
  269. $template_abspath = $template_locator->find($template_name); 
  270. if ($template_abspath) { 
  271. // render the template 
  272. extract($vars); 
  273. if ($return) { 
  274. ob_start(); 
  275. include $template_abspath; 
  276. if ($return) { 
  277. $retval = ob_get_contents(); 
  278. ob_end_clean(); 
  279. return $retval; 
.