PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/nextgen_data/class.image_wrapper.php

https://github.com/Fishgate/privatecollectionswp
PHP | 558 lines | 403 code | 68 blank | 87 comment | 49 complexity | b06d4f80c2c0e83fef52cc56320026fb MD5 | raw file
  1. <?php
  2. /**
  3. * This class provides a lazy-loading wrapper to the NextGen-Legacy "nggImage" class for use in legacy style templates
  4. */
  5. class C_Image_Wrapper
  6. {
  7. public $_cache; // cache of retrieved values
  8. public $_settings; // I_Settings_Manager cache
  9. public $_storage; // I_Gallery_Storage cache
  10. public $_galleries; // cache of I_Gallery_Mapper (plural)
  11. public $_orig_image; // original provided image
  12. public $_orig_image_id; // original image ID
  13. public $_cache_overrides; // allow for forcing variable values
  14. public $_legacy = FALSE;
  15. public $_displayed_gallery; // cached object
  16. /**
  17. * Constructor. Converts the image class into an array and fills from defaults any missing values
  18. *
  19. * @param object $gallery Individual result from displayed_gallery->get_entities()
  20. * @param object $displayed_gallery Displayed gallery -- MAY BE NULL
  21. * @param bool $legacy Whether the image source is from NextGen Legacy or NextGen
  22. * @return void
  23. */
  24. public function __construct($image, $displayed_gallery=NULL, $legacy = FALSE)
  25. {
  26. // for clarity
  27. if ($displayed_gallery && isset($displayed_gallery->display_settings['number_of_columns']))
  28. {
  29. $columns = $displayed_gallery->display_settings['number_of_columns'];
  30. }
  31. else {
  32. $columns = 0;
  33. }
  34. // Public variables
  35. $defaults = array(
  36. 'errmsg' => '', // Error message to display, if any
  37. 'error' => FALSE, // Error state
  38. 'imageURL' => '', // URL Path to the image
  39. 'thumbURL' => '', // URL Path to the thumbnail
  40. 'imagePath' => '', // Server Path to the image
  41. 'thumbPath' => '', // Server Path to the thumbnail
  42. 'href' => '', // A href link code
  43. // Mostly constant
  44. 'thumbPrefix' => 'thumbs_', // FolderPrefix to the thumbnail
  45. 'thumbFolder' => '/thumbs/', // Foldername to the thumbnail
  46. // Image Data
  47. 'galleryid' => 0, // Gallery ID
  48. 'pid' => 0, // Image ID
  49. 'filename' => '', // Image filename
  50. 'description' => '', // Image description
  51. 'alttext' => '', // Image alttext
  52. 'imagedate' => '', // Image date/time
  53. 'exclude' => '', // Image exclude
  54. 'thumbcode' => '', // Image effect code
  55. // Gallery Data
  56. 'name' => '', // Gallery name
  57. 'path' => '', // Gallery path
  58. 'title' => '', // Gallery title
  59. 'pageid' => 0, // Gallery page ID
  60. 'previewpic' => 0, // Gallery preview pic
  61. 'style' => ($columns > 0) ? 'style="width:' . floor(100 / $columns) . '%;"' : '',
  62. 'hidden' => FALSE,
  63. 'permalink' => '',
  64. 'tags' => '',
  65. );
  66. // convert the image to an array and apply the defaults
  67. $this->_orig_image = $image;
  68. $image = (array)$image;
  69. foreach ($defaults as $key => $val) {
  70. if (!isset($image[$key]))
  71. {
  72. $image[$key] = $val;
  73. }
  74. }
  75. // cache the results
  76. ksort($image);
  77. $id_field = (!empty($image['id_field']) ? $image['id_field'] : 'pid');
  78. $this->_cache = (array) apply_filters('ngg_image_object', (object) $image, $image[$id_field]);
  79. $this->_orig_image_id = $image[$id_field];
  80. $this->_legacy = $legacy;
  81. $this->_displayed_gallery = $displayed_gallery;
  82. }
  83. public function __set($name, $value)
  84. {
  85. $this->_cache[$name] = $value;
  86. }
  87. public function __isset($name)
  88. {
  89. return isset($this->_cache[$name]);
  90. }
  91. public function __unset($name)
  92. {
  93. unset($this->_cache[$name]);
  94. }
  95. /**
  96. * Lazy-loader for image variables.
  97. *
  98. * @param string $name Parameter name
  99. * @return mixed
  100. */
  101. public function __get($name)
  102. {
  103. if (isset($this->_cache_overrides[$name]))
  104. {
  105. return $this->_cache_overrides[$name];
  106. }
  107. // at the bottom we default to returning $this->_cache[$name].
  108. switch ($name)
  109. {
  110. case 'alttext':
  111. $this->_cache['alttext'] = (empty($this->_cache['alttext'])) ? ' ' : html_entity_decode(stripslashes($this->_cache['alttext']));
  112. return $this->_cache['alttext'];
  113. case 'author':
  114. if ($this->_legacy)
  115. {
  116. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  117. }
  118. else {
  119. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  120. $gallery = $gallery_map->find($this->__get('galleryid'));
  121. }
  122. $this->_cache['author'] = $gallery->name;
  123. return $this->_cache['author'];
  124. case 'caption':
  125. $caption = html_entity_decode(stripslashes($this->__get('description')));
  126. if (empty($caption))
  127. {
  128. $caption = '&nbsp;';
  129. }
  130. $this->_cache['caption'] = $caption;
  131. return $this->_cache['caption'];
  132. case 'description':
  133. $this->_cache['description'] = (empty($this->_cache['description'])) ? ' ' : html_entity_decode(stripslashes($this->_cache['description']));
  134. return $this->_cache['description'];
  135. case 'galdesc':
  136. if ($this->_legacy)
  137. {
  138. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  139. }
  140. else {
  141. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  142. $gallery = $gallery_map->find($this->__get('galleryid'));
  143. }
  144. $this->_cache['galdesc'] = $gallery->name;
  145. return $this->_cache['galdesc'];
  146. case 'gid':
  147. if ($this->_legacy)
  148. {
  149. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  150. }
  151. else {
  152. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  153. $gallery = $gallery_map->find($this->__get('galleryid'));
  154. }
  155. $this->_cache['gid'] = $gallery->name;
  156. return $this->_cache['gid'];
  157. case 'href':
  158. return $this->__get('imageHTML');
  159. case 'id':
  160. return $this->_orig_image_id;
  161. case 'imageHTML':
  162. $tmp = '<a href="' . $this->__get('imageURL') . '" title="'
  163. . htmlspecialchars(stripslashes($this->__get('description')))
  164. . '" ' . $this->get_thumbcode($this->__get('name')) . '>' . '<img alt="' . $this->__get('alttext')
  165. . '" src="' . $this->__get('imageURL') . '"/>' . '</a>';
  166. $this->_cache['href'] = $tmp;
  167. $this->_cache['imageHTML'] = $tmp;
  168. return $this->_cache['imageHTML'];
  169. case 'imagePath':
  170. $storage = $this->get_storage();
  171. $this->_cache['imagePath'] = $storage->get_image_abspath($this->_orig_image, 'full');
  172. return $this->_cache['imagePath'];
  173. case 'imageURL':
  174. $storage = $this->get_storage();
  175. $this->_cache['imageURL'] = $storage->get_image_url($this->_orig_image, 'full');
  176. return $this->_cache['imageURL'];
  177. case 'linktitle':
  178. $this->_cache['linktitle'] = htmlspecialchars(stripslashes($this->__get('description')));
  179. return $this->_cache['linktitle'];
  180. case 'name':
  181. if ($this->_legacy)
  182. {
  183. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  184. }
  185. else {
  186. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  187. $gallery = $gallery_map->find($this->__get('galleryid'));
  188. }
  189. $this->_cache['name'] = $gallery->name;
  190. return $this->_cache['name'];
  191. case 'pageid':
  192. if ($this->_legacy)
  193. {
  194. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  195. }
  196. else {
  197. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  198. $gallery = $gallery_map->find($this->__get('galleryid'));
  199. }
  200. $this->_cache['pageid'] = $gallery->name;
  201. return $this->_cache['pageid'];
  202. case 'path':
  203. if ($this->_legacy)
  204. {
  205. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  206. }
  207. else {
  208. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  209. $gallery = $gallery_map->find($this->__get('galleryid'));
  210. }
  211. $this->_cache['path'] = $gallery->name;
  212. return $this->_cache['path'];
  213. case 'permalink':
  214. $this->_cache['permalink'] = $this->__get('imageURL');
  215. return $this->_cache['permalink'];
  216. case 'pid':
  217. return $this->_orig_image_id;
  218. case 'pidlink':
  219. $application = C_Router::get_instance()->get_routed_app();
  220. $controller = C_Display_Type_Controller::get_instance();
  221. $this->_cache['pidlink'] = $controller->set_param_for(
  222. $application->get_routed_url(TRUE),
  223. 'pid',
  224. $this->__get('image_slug')
  225. );
  226. return $this->_cache['pidlink'];
  227. case 'previewpic':
  228. if ($this->_legacy)
  229. {
  230. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  231. }
  232. else {
  233. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  234. $gallery = $gallery_map->find($this->__get('galleryid'));
  235. }
  236. $this->_cache['previewpic'] = $gallery->name;
  237. return $this->_cache['previewpic'];
  238. case 'size':
  239. $w = 0;
  240. $h = 0;
  241. if ($this->_displayed_gallery && isset($this->_displayed_gallery->display_settings)) {
  242. $ds = $this->_displayed_gallery->display_settings;
  243. if (isset($ds['override_thumbnail_settings']) && $ds['override_thumbnail_settings'])
  244. {
  245. $w = $ds['thumbnail_width'];
  246. $h = $ds['thumbnail_height'];
  247. }
  248. }
  249. if (!$w || !$h) {
  250. if (is_string($this->_orig_image->meta_data)) {
  251. $this->_orig_image = C_Image_Mapper::get_instance()->unserialize(
  252. $this->_orig_image->meta_data
  253. );
  254. }
  255. if (!isset($this->_orig_image->meta_data['thumbnail'])) {
  256. $storage = $this->get_storage();
  257. $storage->generate_thumbnail($this->_orig_image);
  258. }
  259. $w = $this->_orig_image->meta_data['thumbnail']['width'];
  260. $h = $this->_orig_image->meta_data['thumbnail']['height'];
  261. }
  262. return "width='{$w}' height='{$h}'";
  263. case 'slug':
  264. if ($this->_legacy)
  265. {
  266. $gallery = $this->get_legacy_gallery($this->__get('galleryid'));
  267. }
  268. else {
  269. $gallery_map = $this->get_gallery($this->__get('galleryid'));
  270. $gallery = $gallery_map->find($this->__get('galleryid'));
  271. }
  272. $this->_cache['slug'] = $gallery->name;
  273. return $this->_cache['slug'];
  274. case 'tags':
  275. $this->_cache['tags'] = wp_get_object_terms($this->__get('id'), 'ngg_tag', 'fields=all');
  276. return $this->_cache['tags'];
  277. case 'thumbHTML':
  278. $tmp = '<a href="' . $this->__get('imageURL') . '" title="'
  279. . htmlspecialchars(stripslashes($this->__get('description')))
  280. . '" ' . $this->get_thumbcode($this->__get('name')) . '>' . '<img alt="' . $this->__get('alttext')
  281. . '" src="' . $this->thumbURL . '"/>' . '</a>';
  282. $this->_cache['href'] = $tmp;
  283. $this->_cache['thumbHTML'] = $tmp;
  284. return $this->_cache['thumbHTML'];
  285. case 'thumbPath':
  286. $storage = $this->get_storage();
  287. $this->_cache['thumbPath'] = $storage->get_image_abspath($this->_orig_image, 'thumbnail');
  288. return $this->_cache['thumbPath'];
  289. case 'thumbnailURL':
  290. $storage = $this->get_storage();
  291. $thumbnail_size_name = 'thumbnail';
  292. if ($this->_displayed_gallery && isset($this->_displayed_gallery->display_settings)) {
  293. $ds = $this->_displayed_gallery->display_settings;
  294. if (isset($ds['override_thumbnail_settings']) && $ds['override_thumbnail_settings']) {
  295. $dynthumbs = C_Component_Registry::get_instance()
  296. ->get_utility('I_Dynamic_Thumbnails_Manager');
  297. $dyn_params = array(
  298. 'width' => $ds['thumbnail_width'],
  299. 'height' => $ds['thumbnail_height']
  300. );
  301. if ($ds['thumbnail_quality'])
  302. $dyn_params['quality'] = $ds['thumbnail_quality'];
  303. if ($ds['thumbnail_crop'])
  304. $dyn_params['crop'] = TRUE;
  305. if ($ds['thumbnail_watermark'])
  306. $dyn_params['watermark'] = TRUE;
  307. $thumbnail_size_name = $dynthumbs->get_size_name($dyn_params);
  308. }
  309. }
  310. $this->_cache['thumbnailURL'] = $storage->get_image_url($this->_orig_image, $thumbnail_size_name);
  311. return $this->_cache['thumbnailURL'];
  312. case 'thumbcode':
  313. $this->_cache['thumbcode'] = $this->get_thumbcode($this->__get('name'));
  314. return $this->_cache['thumbcode'];
  315. case 'thumbURL':
  316. return $this->__get('thumbnailURL');
  317. case 'title':
  318. $this->_cache['title'] = stripslashes($this->__get('name'));
  319. return $this->_cache['title'];
  320. case 'url':
  321. $storage = $this->get_storage();
  322. $this->_cache['url'] = $storage->get_image_url($this->_orig_image, 'full');
  323. return $this->_cache['url'];
  324. default:
  325. return $this->_cache[$name];
  326. }
  327. }
  328. // called on initial nggLegacy image at construction. not sure what to do with it now.
  329. function construct_ngg_Image($gallery)
  330. {
  331. do_action_ref_array('ngg_get_image', array(&$this));
  332. unset($this->tags);
  333. }
  334. /**
  335. * Retrieves and caches an I_Settings_Manager instance
  336. *
  337. * @return mixed
  338. */
  339. function get_settings()
  340. {
  341. if (is_null($this->_settings))
  342. {
  343. $this->_settings = C_NextGen_Settings::get_instance();
  344. }
  345. return $this->_settings;
  346. }
  347. /**
  348. * Retrieves and caches an I_Gallery_Storage instance
  349. *
  350. * @return mixed
  351. */
  352. function get_storage()
  353. {
  354. if (is_null($this->_storage))
  355. {
  356. $this->_storage = C_Gallery_Storage::get_instance();
  357. }
  358. return $this->_storage;
  359. }
  360. /**
  361. * Retrieves I_Gallery_Mapper instance.
  362. *
  363. * @param int $gallery_id Gallery ID
  364. * @return mixed
  365. */
  366. function get_gallery($gallery_id)
  367. {
  368. if (isset($this->container) && method_exists($this->container, 'get_gallery'))
  369. {
  370. return $this->container->get_gallery($gallery_id);
  371. }
  372. $gallery_map = C_Gallery_Mapper::get_instance();
  373. return $gallery_map->find($gallery_id);
  374. }
  375. /**
  376. * Retrieves I_Gallery_Mapper instance.
  377. *
  378. * @param int $gallery_id Gallery ID
  379. * @return mixed
  380. */
  381. function get_legacy_gallery($gallery_id)
  382. {
  383. return C_Gallery_Mapper::get_instance()->find($gallery_id);
  384. }
  385. /**
  386. * Get the thumbnail code (to add effects on thumbnail click)
  387. *
  388. * Applies the filter 'ngg_get_thumbcode'
  389. */
  390. function get_thumbcode($gallery_name = '')
  391. {
  392. if (empty($this->_displayed_gallery))
  393. {
  394. $effect_code = C_NextGen_Settings::get_instance()->thumbCode;
  395. $effect_code = str_replace('%GALLERY_ID%', $gallery_name, $effect_code);
  396. $effect_code = str_replace('%GALLERY_NAME%', $gallery_name, $effect_code);
  397. $retval = $effect_code;
  398. }
  399. else {
  400. $controller = C_Display_Type_Controller::get_instance();
  401. $retval = $controller->get_effect_code($this->_displayed_gallery);
  402. // This setting requires that we disable the effect code
  403. $ds = $this->_displayed_gallery->display_settings;
  404. if (isset($ds['use_imagebrowser_effect']) && $ds['use_imagebrowser_effect'])
  405. $retval = '';
  406. }
  407. $retval = apply_filters('ngg_get_thumbcode', $retval, $this);
  408. // ensure some additional data- fields are added; provides Pro-Lightbox compatibility
  409. $retval .= ' data-image-id="' . $this->__get('id') . '"';
  410. $retval .= ' data-src="' . $this->__get('imageURL') . '"';
  411. $retval .= ' data-thumbnail="' . $this->__get('thumbnailURL') . '"';
  412. $retval .= ' data-image-id="' . $this->__get('pid') . '"';
  413. $retval .= ' data-title="' . esc_attr($this->__get('alttext')) . '"';
  414. $retval .= ' data-description="' . esc_attr($this->__get('description')) . '"';
  415. $this->_cache['thumbcode'] = $retval;
  416. return $retval;
  417. }
  418. /**
  419. * For compatibility support
  420. *
  421. * @return mixed
  422. */
  423. function get_href_link()
  424. {
  425. return $this->__get('imageHTML');
  426. }
  427. /**
  428. * For compatibility support
  429. *
  430. * @return mixed
  431. */
  432. function get_href_thumb_link()
  433. {
  434. return $this->__get('thumbHTML');
  435. }
  436. /**
  437. * Function exists for legacy support but has been gutted to not do anything
  438. *
  439. * @param int $width
  440. * @param int $height
  441. * @param string $mode could be watermark | web20 | crop
  442. * @return the url for the image or false if failed
  443. */
  444. function cached_singlepic_file($width = '', $height = '', $mode = '' )
  445. {
  446. $dynthumbs = C_Dynamic_Thumbnails_Manager::get_instance();
  447. $storage = $this->get_storage();
  448. // determine what to do with 'mode'
  449. $display_reflection = FALSE;
  450. $display_watermark = FALSE;
  451. if (!is_array($mode))
  452. $mode = explode(',', $mode);
  453. if (in_array('web20', $mode))
  454. $display_reflection = TRUE;
  455. if (in_array('watermark', $mode))
  456. $display_watermark = TRUE;
  457. // and go for it
  458. $params = array(
  459. 'width' => $width,
  460. 'height' => $height,
  461. 'watermark' => $display_watermark,
  462. 'reflection' => $display_reflection
  463. );
  464. return $storage->get_image_url((object)$this->_cache, $dynthumbs->get_size_name($params));
  465. }
  466. /**
  467. * Get the tags associated to this image
  468. */
  469. function get_tags()
  470. {
  471. return $this->__get('tags');
  472. }
  473. /**
  474. * Get the permalink to the image
  475. *
  476. * TODO: Get a permalink to a page presenting the image
  477. */
  478. function get_permalink()
  479. {
  480. return $this->__get('permalink');
  481. }
  482. /**
  483. * Returns the _cache array; used by nggImage
  484. * @return array
  485. */
  486. function _get_image()
  487. {
  488. return $this->_cache;
  489. }
  490. }