Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3.  
  4. /**
  5. * X-Cart
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * This source file is subject to the software license agreement
  10. * that is bundled with this package in the file LICENSE.txt.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://www.x-cart.com/license-agreement.html
  13. * If you did not receive a copy of the license and are unable to
  14. * obtain it through the world-wide-web, please send an email
  15. * to licensing@x-cart.com so we can send you a copy immediately.
  16. *
  17. * DISCLAIMER
  18. *
  19. * Do not modify this file if you wish to upgrade X-Cart to newer versions
  20. * in the future. If you wish to customize X-Cart for your needs please
  21. * refer to http://www.x-cart.com/ for more information.
  22. *
  23. * @category X-Cart 5
  24. * @author Qualiteam software Ltd <info@x-cart.com>
  25. * @copyright Copyright (c) 2011-2015 Qualiteam software Ltd <info@x-cart.com>. All rights reserved
  26. * @license http://www.x-cart.com/license-agreement.html X-Cart 5 License Agreement
  27. * @link http://www.x-cart.com/
  28. */
  29.  
  30. namespace XLite\View;
  31.  
  32. /**
  33. * Image
  34. */
  35. class Image extends \XLite\View\AView
  36. {
  37. /**
  38. * Widget arguments names
  39. */
  40. const PARAM_IMAGE = 'image';
  41. const PARAM_ALT = 'alt';
  42. const PARAM_MAX_WIDTH = 'maxWidth';
  43. const PARAM_MAX_HEIGHT = 'maxHeight';
  44. const PARAM_SIZE_ID = 'sizeId';
  45. const PARAM_CENTER_IMAGE = 'centerImage';
  46. const PARAM_VERTICAL_ALIGN = 'verticalAlign';
  47. const PARAM_USE_CACHE = 'useCache';
  48. const PARAM_USE_DEFAULT_IMAGE = 'useDefaultImage';
  49. const PARAM_IMAGE_SIZE_TYPE = 'imageSizeType';
  50.  
  51.  
  52. /**
  53. * Vertical align types
  54. */
  55. const VERTICAL_ALIGN_TOP = 'top';
  56. const VERTICAL_ALIGN_MIDDLE = 'middle';
  57. const VERTICAL_ALIGN_BOTTOM = 'bottom';
  58.  
  59. /**
  60. * Default image (no image) dimensions
  61. */
  62. const DEFAULT_IMAGE_WIDTH = 300;
  63. const DEFAULT_IMAGE_HEIGHT = 300;
  64.  
  65. /**
  66. * Allowed properties names
  67. *
  68. * @var array
  69. */
  70. protected $allowedProperties = array(
  71. 'className' => 'class',
  72. 'id' => 'id',
  73. 'onclick' => 'onclick',
  74. 'style' => 'style',
  75. 'onmousemove' => 'onmousemove',
  76. 'onmouseup' => 'onmouseup',
  77. 'onmousedown' => 'onmousedown',
  78. 'onmouseover' => 'onmouseover',
  79. 'onmouseout' => 'onmouseout',
  80. );
  81.  
  82. /**
  83. * Additioanl properties
  84. *
  85. * @var array
  86. */
  87. protected $properties = array();
  88.  
  89. /**
  90. * Resized thumbnail URL
  91. *
  92. * @var string
  93. */
  94. protected $resizedURL = null;
  95.  
  96. /**
  97. * Use default image
  98. *
  99. * @var boolean
  100. */
  101. protected $useDefaultImage = false;
  102.  
  103. /**
  104. * Set widget parameters
  105. *
  106. * @param array $params Widget parameters
  107. *
  108. * @return void
  109. */
  110. public function setWidgetParams(array $params)
  111. {
  112. parent::setWidgetParams($params);
  113.  
  114. // Save additional parameters
  115. foreach ($params as $name => $value) {
  116. if (isset($this->allowedProperties[$name])) {
  117. $this->properties[$this->allowedProperties[$name]] = $value;
  118. }
  119. }
  120. }
  121.  
  122. /**
  123. * Get image URL
  124. *
  125. * @return string
  126. */
  127. public function getURL()
  128. {
  129. $url = null;
  130.  
  131. if ($this->getParam(self::PARAM_IMAGE) && $this->getParam(self::PARAM_IMAGE)->isExists()) {
  132. // Specified image
  133. $url = $this->getParam(self::PARAM_USE_CACHE)
  134. ? $this->resizedURL
  135. : $this->getParam(self::PARAM_IMAGE)->getFrontURL();
  136. }
  137.  
  138. if (!$url && $this->getParam(self::PARAM_USE_DEFAULT_IMAGE)) {
  139. // Default image
  140. $url = \XLite::getInstance()->getOptions(array('images', 'default_image'));
  141.  
  142. if (!\XLite\Core\Converter::isURL($url)) {
  143. $url = \XLite\Core\Layout::getInstance()->getResourceWebPath(
  144. $url,
  145. \XLite\Core\Layout::WEB_PATH_OUTPUT_URL
  146. );
  147. $this->useDefaultImage = true;
  148. }
  149. }
  150.  
  151. return $this->prepareURL($url);
  152. }
  153.  
  154. /**
  155. * Get image alternative text
  156. *
  157. * @return void
  158. */
  159. public function getAlt()
  160. {
  161. return $this->getParam(self::PARAM_ALT);
  162. }
  163.  
  164. /**
  165. * Get properties
  166. *
  167. * @return void
  168. */
  169. public function getProperties()
  170. {
  171. $this->properties['data-max-width'] = max(0, $this->getParam(self::PARAM_MAX_WIDTH));
  172. $this->properties['data-max-height'] = max(0, $this->getParam(self::PARAM_MAX_HEIGHT));
  173. $this->properties['data-is-default-image'] = $this->useDefaultImage;
  174.  
  175. return $this->properties;
  176. }
  177.  
  178. /**
  179. * Remove the protocol from the url definition
  180. *
  181. * @param string $url
  182. *
  183. * @return string
  184. */
  185. protected function prepareURL($url)
  186. {
  187. return str_replace(array('http://', 'https://'), '//', $url);
  188. }
  189.  
  190. /**
  191. * Return widget default template
  192. *
  193. * @return string
  194. */
  195. protected function getDefaultTemplate()
  196. {
  197. return 'common/image.tpl';
  198. }
  199.  
  200. /**
  201. * Define widget parameters
  202. *
  203. * @return void
  204. */
  205. protected function defineWidgetParams()
  206. {
  207. parent::defineWidgetParams();
  208.  
  209. $this->widgetParams += array(
  210. self::PARAM_IMAGE => new \XLite\Model\WidgetParam\Object('Image', null, false, '\XLite\Model\Base\Image'),
  211. self::PARAM_ALT => new \XLite\Model\WidgetParam\String('Alt. text', '', false),
  212. self::PARAM_MAX_WIDTH => new \XLite\Model\WidgetParam\Int('Max. width', 0),
  213. self::PARAM_MAX_HEIGHT => new \XLite\Model\WidgetParam\Int('Max. height', 0),
  214. self::PARAM_CENTER_IMAGE => new \XLite\Model\WidgetParam\Checkbox('Center the image after resizing', true),
  215. self::PARAM_VERTICAL_ALIGN => new \XLite\Model\WidgetParam\String('Vertical align', self::VERTICAL_ALIGN_MIDDLE),
  216. self::PARAM_USE_CACHE => new \XLite\Model\WidgetParam\Bool('Use cache', 1),
  217. self::PARAM_USE_DEFAULT_IMAGE => new \XLite\Model\WidgetParam\Bool('Use default image', 1),
  218. self::PARAM_IMAGE_SIZE_TYPE => new \XLite\Model\WidgetParam\String('Imeage size type', 'normal'),
  219. );
  220. }
  221.  
  222. /**
  223. * checkImage
  224. *
  225. * @return boolean
  226. */
  227. protected function checkImage()
  228. {
  229. return $this->getParam(self::PARAM_IMAGE)
  230. && $this->getParam(self::PARAM_IMAGE)->isExists();
  231. }
  232.  
  233. /**
  234. * checkDefaultImage
  235. *
  236. * @return boolean
  237. */
  238. protected function checkDefaultImage()
  239. {
  240. return $this->getParam(self::PARAM_USE_DEFAULT_IMAGE)
  241. && \XLite::getInstance()->getOptions(array('images', 'default_image'));
  242. }
  243.  
  244. /**
  245. * Check widget visibility
  246. *
  247. * @return boolean
  248. */
  249. protected function isVisible()
  250. {
  251. $result = parent::isVisible();
  252.  
  253. if ($result) {
  254.  
  255. if ($this->checkImage()) {
  256. $this->processImage();
  257.  
  258. } elseif ($this->checkDefaultImage()) {
  259. $this->processDefaultImage();
  260.  
  261. } else {
  262. $result = false;
  263. }
  264. }
  265.  
  266. return $result;
  267. }
  268.  
  269. /**
  270. * Return a CSS style centering the image vertically and horizontally
  271. *
  272. * @return string
  273. */
  274. protected function setImageMargin()
  275. {
  276. $vertical = ($this->getParam(self::PARAM_MAX_HEIGHT) - $this->properties['height']) / 2;
  277.  
  278. switch ($this->getParam(self::PARAM_VERTICAL_ALIGN)) {
  279. case self::VERTICAL_ALIGN_TOP:
  280. $top = 0;
  281. $bottom = 0;
  282. break;
  283.  
  284. case self::VERTICAL_ALIGN_BOTTOM:
  285. $top = $this->getParam(self::PARAM_MAX_HEIGHT) - $this->properties['height'];
  286. $bottom = 0;
  287. break;
  288.  
  289. default:
  290. $top = max(0, ceil($vertical));
  291. $bottom = max(0, floor($vertical));
  292. }
  293.  
  294. if (0 < $top || 0 < $bottom) {
  295. $this->addInlineStyle('margin: 0 auto;margin-bottom:' . $bottom . 'px;' . 'margin-top:' . $top . 'px;');
  296. }
  297. }
  298.  
  299. /**
  300. * Add CSS styles to the value of "style" attribute of the image tag
  301. *
  302. * @param string $style CSS styles to be added to the end of "style" attribute
  303. *
  304. * @return void
  305. */
  306. protected function addInlineStyle($style)
  307. {
  308. if (!isset($this->properties['style'])) {
  309. $this->properties['style'] = $style;
  310.  
  311. } else {
  312. $this->properties['style'] .= ' ' . $style;
  313. }
  314. }
  315.  
  316. /**
  317. * Preprocess image
  318. * TODO: replace getResizedThumbnailURL to getResizedURL
  319. *
  320. * @return void
  321. */
  322. protected function processImage()
  323. {
  324. $maxw = max(0, $this->getParam(self::PARAM_MAX_WIDTH));
  325. $maxh = max(0, $this->getParam(self::PARAM_MAX_HEIGHT));
  326.  
  327. $funcName = method_exists($this->getParam(self::PARAM_IMAGE), 'getResizedURL')
  328. ? 'getResizedURL'
  329. : 'getResizedThumbnailURL';
  330.  
  331. // $funcName - getResizedURL or getResizedThumbnailURL
  332. list(
  333. $this->properties['width'],
  334. $this->properties['height'],
  335. $this->resizedURL
  336. ) = $this->getParam(self::PARAM_IMAGE)->$funcName($maxw, $maxh);
  337.  
  338. // Center the image vertically and horizontally
  339. if ($this->getParam(self::PARAM_CENTER_IMAGE)) {
  340. $this->setImageMargin();
  341. }
  342. }
  343.  
  344. /**
  345. * Preprocess default image
  346. *
  347. * @return void
  348. */
  349. protected function processDefaultImage()
  350. {
  351. list($this->properties['width'], $this->properties['height']) = \XLite\Core\ImageOperator::getCroppedDimensions(
  352. static::DEFAULT_IMAGE_WIDTH,
  353. static::DEFAULT_IMAGE_HEIGHT,
  354. max(0, $this->getParam(self::PARAM_MAX_WIDTH)),
  355. max(0, $this->getParam(self::PARAM_MAX_HEIGHT))
  356. );
  357.  
  358. // Center the image vertically and horizontally
  359. if ($this->getParam(self::PARAM_CENTER_IMAGE)) {
  360. $this->setImageMargin();
  361. }
  362. }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement