PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/XLite/View/Image.php

https://github.com/ckdimka/core
PHP | 378 lines | 168 code | 51 blank | 159 comment | 20 complexity | e0ed9e4c077aac7dd7ddd9f28ea5f644 MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\View;
  27. /**
  28. * Image
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. class Image extends \XLite\View\AView
  34. {
  35. /**
  36. * Widget arguments names
  37. */
  38. const PARAM_IMAGE = 'image';
  39. const PARAM_ALT = 'alt';
  40. const PARAM_MAX_WIDTH = 'maxWidth';
  41. const PARAM_MAX_HEIGHT = 'maxHeight';
  42. const PARAM_CENTER_IMAGE = 'centerImage';
  43. const PARAM_VERTICAL_ALIGN = 'verticalAlign';
  44. const PARAM_USE_CACHE = 'useCache';
  45. const PARAM_USE_DEFAULT_IMAGE = 'useDefaultImage';
  46. /**
  47. * Vertical align types
  48. */
  49. const VERTICAL_ALIGN_TOP = 'top';
  50. const VERTICAL_ALIGN_MIDDLE = 'middle';
  51. const VERTICAL_ALIGN_BOTTOM = 'bottom';
  52. /**
  53. * Allowed properties names
  54. *
  55. * @var array
  56. * @see ____var_see____
  57. * @since 1.0.0
  58. */
  59. protected $allowedProperties = array(
  60. 'className' => 'class',
  61. 'id' => 'id',
  62. 'onclick' => 'onclick',
  63. 'style' => 'style',
  64. 'onmousemove' => 'onmousemove',
  65. 'onmouseup' => 'onmouseup',
  66. 'onmousedown' => 'onmousedown',
  67. 'onmouseover' => 'onmouseover',
  68. 'onmouseout' => 'onmouseout',
  69. );
  70. /**
  71. * Additioanl properties
  72. *
  73. * @var array
  74. * @see ____var_see____
  75. * @since 1.0.0
  76. */
  77. protected $properties = array();
  78. /**
  79. * Resized thumbnail URL
  80. *
  81. * @var string
  82. * @see ____var_see____
  83. * @since 1.0.0
  84. */
  85. protected $resizedURL = null;
  86. /**
  87. * Set widget parameters
  88. *
  89. * @param array $params Widget parameters
  90. *
  91. * @return void
  92. * @see ____func_see____
  93. * @since 1.0.0
  94. */
  95. public function setWidgetParams(array $params)
  96. {
  97. parent::setWidgetParams($params);
  98. // Save additional parameters
  99. foreach ($params as $name => $value) {
  100. if (isset($this->allowedProperties[$name])) {
  101. $this->properties[$this->allowedProperties[$name]] = $value;
  102. }
  103. }
  104. }
  105. /**
  106. * Get image URL
  107. *
  108. * @return string
  109. * @see ____func_see____
  110. * @since 1.0.0
  111. */
  112. public function getURL()
  113. {
  114. $url = null;
  115. if ($this->getParam(self::PARAM_IMAGE) && $this->getParam(self::PARAM_IMAGE)->isExists()) {
  116. // Specified image
  117. $url = $this->getParam(self::PARAM_USE_CACHE)
  118. ? $this->resizedURL
  119. : $this->getParam(self::PARAM_IMAGE)->getFrontURL();
  120. }
  121. if (!$url && $this->getParam(self::PARAM_USE_DEFAULT_IMAGE)) {
  122. // Default image
  123. $url = \XLite::getInstance()->getOptions(array('images', 'default_image'));
  124. if (!\XLite\Core\Converter::isURL($url)) {
  125. $url = \XLite\Core\Layout::getInstance()->getResourceWebPath(
  126. $url,
  127. \XLite\Core\Layout::WEB_PATH_OUTPUT_URL
  128. );
  129. }
  130. }
  131. return $url;
  132. }
  133. /**
  134. * Get image alternative text
  135. *
  136. * @return void
  137. * @see ____func_see____
  138. * @since 1.0.0
  139. */
  140. public function getAlt()
  141. {
  142. return $this->getParam(self::PARAM_ALT);
  143. }
  144. /**
  145. * Get properties
  146. *
  147. * @return void
  148. * @see ____func_see____
  149. * @since 1.0.0
  150. */
  151. public function getProperties()
  152. {
  153. return $this->properties;
  154. }
  155. /**
  156. * Return widget default template
  157. *
  158. * @return string
  159. * @see ____func_see____
  160. * @since 1.0.0
  161. */
  162. protected function getDefaultTemplate()
  163. {
  164. return 'common/image.tpl';
  165. }
  166. /**
  167. * Define widget parameters
  168. *
  169. * @return void
  170. * @see ____func_see____
  171. * @since 1.0.0
  172. */
  173. protected function defineWidgetParams()
  174. {
  175. parent::defineWidgetParams();
  176. $this->widgetParams += array(
  177. self::PARAM_IMAGE => new \XLite\Model\WidgetParam\Object('Image', null, false, '\XLite\Model\Base\Image'),
  178. self::PARAM_ALT => new \XLite\Model\WidgetParam\String('Alt. text', '', false),
  179. self::PARAM_MAX_WIDTH => new \XLite\Model\WidgetParam\Int('Max. width', 0),
  180. self::PARAM_MAX_HEIGHT => new \XLite\Model\WidgetParam\Int('Max. height', 0),
  181. self::PARAM_CENTER_IMAGE => new \XLite\Model\WidgetParam\Checkbox('Center the image after resizing', true),
  182. self::PARAM_VERTICAL_ALIGN => new \XLite\Model\WidgetParam\String('Vertical align', self::VERTICAL_ALIGN_MIDDLE),
  183. self::PARAM_USE_CACHE => new \XLite\Model\WidgetParam\Bool('Use cache', 1),
  184. self::PARAM_USE_DEFAULT_IMAGE => new \XLite\Model\WidgetParam\Bool('Use default image', 1),
  185. );
  186. }
  187. /**
  188. * checkImage
  189. *
  190. * @return boolean
  191. * @see ____func_see____
  192. * @since 1.0.0
  193. */
  194. protected function checkImage()
  195. {
  196. return $this->getParam(self::PARAM_IMAGE)
  197. && $this->getParam(self::PARAM_IMAGE)->isExists();
  198. }
  199. /**
  200. * checkDefaultImage
  201. *
  202. * @return boolean
  203. * @see ____func_see____
  204. * @since 1.0.0
  205. */
  206. protected function checkDefaultImage()
  207. {
  208. return $this->getParam(self::PARAM_USE_DEFAULT_IMAGE)
  209. && \XLite::getInstance()->getOptions(array('images', 'default_image'));
  210. }
  211. /**
  212. * Check widget visibility
  213. *
  214. * @return boolean
  215. * @see ____func_see____
  216. * @since 1.0.0
  217. */
  218. protected function isVisible()
  219. {
  220. $result = parent::isVisible();
  221. if ($result) {
  222. if ($this->checkImage()) {
  223. $this->processImage();
  224. } elseif ($this->checkDefaultImage()) {
  225. $this->processDefaultImage();
  226. } else {
  227. $result = false;
  228. }
  229. }
  230. return $result;
  231. }
  232. /**
  233. * Return a CSS style centering the image vertically and horizontally
  234. *
  235. * @return string
  236. * @see ____func_see____
  237. * @since 1.0.0
  238. */
  239. protected function setImagePaddings()
  240. {
  241. $vertical = ($this->getParam(self::PARAM_MAX_HEIGHT) - $this->properties['height']) / 2;
  242. $horizontal = ($this->getParam(self::PARAM_MAX_WIDTH) - $this->properties['width']) / 2;
  243. $left = max(0, ceil($horizontal));
  244. $right = max(0, floor($horizontal));
  245. switch ($this->getParam(self::PARAM_VERTICAL_ALIGN)) {
  246. case self::VERTICAL_ALIGN_TOP:
  247. $top = 0;
  248. $bottom = 0;
  249. break;
  250. case self::VERTICAL_ALIGN_BOTTOM:
  251. $top = $this->getParam(self::PARAM_MAX_HEIGHT) - $this->properties['height'];
  252. $bottom = 0;
  253. break;
  254. default:
  255. $top = max(0, ceil($vertical));
  256. $bottom = max(0, floor($vertical));
  257. }
  258. if (0 < $top || 0 < $bottom || 0 < $left || 0 < $right) {
  259. $this->addInlineStyle(
  260. 'padding: ' . $top . 'px ' . $right . 'px ' . $bottom . 'px ' . $left . 'px;'
  261. );
  262. }
  263. }
  264. /**
  265. * Add CSS styles to the value of "style" attribute of the image tag
  266. *
  267. * @param string $style CSS styles to be added to the end of "style" attribute
  268. *
  269. * @return void
  270. * @see ____func_see____
  271. * @since 1.0.0
  272. */
  273. protected function addInlineStyle($style)
  274. {
  275. if (!isset($this->properties['style'])) {
  276. $this->properties['style'] = $style;
  277. } else {
  278. $this->properties['style'] .= ' ' . $style;
  279. }
  280. }
  281. /**
  282. * Preprocess image
  283. * TODO: replace getResizedThumbnailURL to getResizedURL
  284. *
  285. * @return void
  286. * @see ____func_see____
  287. * @since 1.0.0
  288. */
  289. protected function processImage()
  290. {
  291. $maxw = max(0, $this->getParam(self::PARAM_MAX_WIDTH));
  292. $maxh = max(0, $this->getParam(self::PARAM_MAX_HEIGHT));
  293. $funcName = method_exists($this->getParam(self::PARAM_IMAGE), 'getResizedURL')
  294. ? 'getResizedURL'
  295. : 'getResizedThumbnailURL';
  296. // $funcName - getResizedURL or getResizedThumbnailURL
  297. list(
  298. $this->properties['width'],
  299. $this->properties['height'],
  300. $this->resizedURL
  301. ) = $this->getParam(self::PARAM_IMAGE)->$funcName($maxw, $maxh);
  302. // Center the image vertically and horizontally
  303. if ($this->getParam(self::PARAM_CENTER_IMAGE)) {
  304. $this->setImagePaddings();
  305. }
  306. }
  307. /**
  308. * Preprocess default image
  309. *
  310. * @return void
  311. * @see ____func_see____
  312. * @since 1.0.0
  313. */
  314. protected function processDefaultImage()
  315. {
  316. list($this->properties['width'], $this->properties['height']) = \XLite\Core\ImageOperator::getCroppedDimensions(
  317. \XLite::getInstance()->getOptions(array('images', 'default_image_width')),
  318. \XLite::getInstance()->getOptions(array('images', 'default_image_height')),
  319. max(0, $this->getParam(self::PARAM_MAX_WIDTH)),
  320. max(0, $this->getParam(self::PARAM_MAX_HEIGHT))
  321. );
  322. // Center the image vertically and horizontally
  323. if ($this->getParam(self::PARAM_CENTER_IMAGE)) {
  324. $this->setImagePaddings();
  325. }
  326. }
  327. }