Guest User

Untitled

a guest
Jun 2nd, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. <?php
  2. // +---------------------------------------------------------------------------+
  3. // | This file is part of the Riobel Website project. |
  4. // | Copyright (C) Lemieux Bedard Communications |
  5. // | |
  6. // | For the full copyright and license information, please view the LICENSE |
  7. // | file that was distributed with this source code. |
  8. // +---------------------------------------------------------------------------+
  9.  
  10. /**
  11. * RiobelProductsModel model is the business logic around a product.
  12. * @package model
  13. * @subpackage models
  14. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  15. * @copyright Lemieux Bedard Communication (jean-philippe.dery@lemieuxbedard.com)
  16. * @since 1.0.0
  17. * @version 1.0.0
  18. */
  19. class RiobelProductModel extends RiobelBasePropelModel
  20. {
  21. const DEFAULT_HANDLE = 'L';
  22. const DEFAULT_COLOR = 'BN';
  23.  
  24. /**
  25. * Initialize this model.
  26. * @param array [om] To specify the default record.
  27. * @return void
  28. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  29. * @since 1.0.0
  30. */
  31. public function initialize(AgaviContext $context, array $parameters = array())
  32. {
  33. parent::initialize($context, $parameters, new RiobelProduct());
  34. }
  35.  
  36. /**
  37. * Transfer a general field name associated to this model to a propel
  38. * constant field name the database transparent from the outside.
  39. * @param string The field name.
  40. * @return string The translated field name.
  41. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  42. * @since 1.0.0
  43. */
  44. protected function translateFieldName($name)
  45. {
  46. try {
  47. $translation = RiobelProductPeer::translateFieldName($name, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
  48. } catch (PropelException $e) {
  49. return null;
  50. }
  51. return $translation;
  52. }
  53.  
  54. /**
  55. * Return the default instance of this product.
  56. * @return object The default instance.
  57. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  58. * @since 1.0.0
  59. */
  60. public function getDefaultInstance()
  61. {
  62. return $this->context->getModel('RiobelProductInstance', null, array('om' => $this->om->getProductInstance()));
  63. }
  64.  
  65. /**
  66. * Return the translated collection description.
  67. * @return string The translated field name.
  68. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  69. * @since 1.0.0
  70. */
  71. public function getName()
  72. {
  73. switch ($this->context->getTranslationManager()->getCurrentLocale()->getLocaleLanguage()) {
  74. case 'en' : return $this->om->getNameEn();
  75. case 'fr' : return $this->om->getNameFr();
  76. }
  77. }
  78.  
  79. /**
  80. * Return the translated collection description.
  81. * @return string The translated field name.
  82. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  83. * @since 1.0.0
  84. */
  85. public function getDesc()
  86. {
  87. switch ($this->context->getTranslationManager()->getCurrentLocale()->getLocaleLanguage()) {
  88. case 'en' : return $this->om->getDescEn();
  89. case 'fr' : return $this->om->getDescFr();
  90. }
  91. }
  92.  
  93. /**
  94. * Return the translated collection specification.
  95. * @return string The translated field name.
  96. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  97. * @since 1.0.0
  98. */
  99. public function getSpec()
  100. {
  101. switch ($this->context->getTranslationManager()->getCurrentLocale()->getLocaleLanguage()) {
  102. case 'en' : return $this->om->getSpecEn();
  103. case 'fr' : return $this->om->getSpecFr();
  104. }
  105. }
  106.  
  107. /**
  108. * Return the code of this product.
  109. * @param string The handle.
  110. * @param string The color
  111. * @return string The code.
  112. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  113. * @since 1.0.0
  114. */
  115. public function getCode($handle = null, $color = null)
  116. {
  117. if ($handle != null && $color != null) {
  118. $h = $this->formatHandle($handle);
  119. $h = $this->hasHandle($h) ? $h : self::DEFAULT_HANDLE;
  120. $c = $this->hasColor($color) ? $color : self::DEFAULT_COLOR;
  121. // load the instance from the data that was supplied. If however there is no
  122. // instance that match the demand, the default instance will be returned
  123. $instance = $this->context->getModel('RiobelProductInstances')->getProductInstanceByHandleAndColor($this->om->getId(), $h, $c);
  124. if ($instance == null) {
  125. $instance = $this->getDefaultInstance();
  126. }
  127. return $instance->getCode();
  128. }
  129. return $this->om->getCode();
  130. }
  131.  
  132. /**
  133. * Return the image of this product.
  134. * @param string The handle.
  135. * @param string The color
  136. * @return string The image.
  137. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  138. * @since 1.0.0
  139. */
  140. public function getImage($handle = null, $color = null)
  141. {
  142. $h = $this->formatHandle($handle);
  143. $h = $this->hasHandle($h) ? $h : self::DEFAULT_HANDLE;
  144. $c = $this->hasColor($color) ? $color : self::DEFAULT_COLOR;
  145. // load the instance from the data that was supplied. If however there is no
  146. // instance that match the demand, the default instance will be returned
  147. $instance = $this->context->getModel('RiobelProductInstances')->getProductInstanceByHandleAndColor($this->om->getId(), $h, $c);
  148. if ($instance == null) {
  149. $instance = $this->getDefaultInstance();
  150. }
  151. return $instance->getImage();
  152. }
  153.  
  154. /**
  155. * Return the viewable image of this product.
  156. * @param string The handle.
  157. * @param string The color
  158. * @return string The image.
  159. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  160. * @since 1.0.0
  161. */
  162. public function getViewableImage($handle = null, $color = null)
  163. {
  164. $file = $this->getImage($handle, $color);
  165. $path = 'images/products/%s';
  166. $path = sprintf($path, $file);
  167. return is_readable($path) && is_file($path) ? $file : 'empty_product.gif';
  168. }
  169.  
  170. /**
  171. * Return the colors of this product.
  172. * @param object The criteria.
  173. * @param string The order.
  174. * @param int The limit.
  175. * @param int The offset.
  176. * @return array The results.
  177. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  178. * @since 1.0.0
  179. */
  180. public function getColors($order = null, $limit = null, $offset = null)
  181. {
  182. return $this->context->getModel('RiobelProductColors')->getProductColors($this->om->getId(), null, $order, $limit, $offset);
  183. }
  184.  
  185. /**
  186. * Return the available codes.
  187. * @return array The available codes.
  188. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  189. * @since 1.0.0
  190. */
  191. public function getAvailableCodes()
  192. {
  193. return explode(',', $this->om->getAvailableCodes());
  194. }
  195.  
  196. /**
  197. * Return the available colors.
  198. * @return array The available colors.
  199. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  200. * @since 1.0.0
  201. */
  202. public function getAvailableColors()
  203. {
  204. return explode(',', $this->om->getAvailableColors());
  205. }
  206.  
  207. /**
  208. * Return the available colors objects.
  209. * @return array The available colors objects.
  210. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  211. * @since 1.0.0
  212. */
  213. public function getAvailableColorObjects()
  214. {
  215. switch ($this->context->getTranslationManager()->getCurrentLocale()->getLocaleLanguage()) {
  216. case 'en' : return $this->context->getModel('RiobelColors')->getColorsByCodes($this->getAvailableColors(), 'name_en');
  217. case 'fr' : return $this->context->getModel('RiobelColors')->getColorsByCodes($this->getAvailableColors(), 'name_fr');
  218. }
  219. }
  220.  
  221. /**
  222. * Return the available handles.
  223. * @return array The available handles.
  224. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  225. * @since 1.0.0
  226. */
  227. public function getAvailableHandles()
  228. {
  229. return explode(',', $this->om->getAvailableHandles());
  230. }
  231.  
  232. /**
  233. * Indicate whether or not this product have this code.
  234. * @param string The code.
  235. * @return bool True if the product have the code.
  236. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  237. * @since 1.0.0
  238. */
  239. public function hasCode($code)
  240. {
  241. return in_array($code, $this->getAvailableCodes());
  242. }
  243.  
  244. /**
  245. * Indicate whether or not this product have this color.
  246. * @param string The color.
  247. * @return bool True if the product have the color.
  248. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  249. * @since 1.0.0
  250. */
  251. public function hasColor($color)
  252. {
  253. return in_array($color, $this->getAvailableColors());
  254. }
  255.  
  256. /**
  257. * Indicate whether or not this product have this handle.
  258. * @param string The handle.
  259. * @return bool True if the product have the handle.
  260. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  261. * @since 1.0.0
  262. */
  263. public function hasHandle($handle)
  264. {
  265. return in_array($this->formatHandle($handle), $this->getAvailableHandles());
  266. }
  267.  
  268. /**
  269. * Indicate whether or not the product provides a lever handle.
  270. * @return bool True if the product have the handle.
  271. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  272. * @since 1.0.0
  273. */
  274. public function providesLeverHandle()
  275. {
  276. return $this->hasHandle('L');
  277. }
  278.  
  279. /**
  280. * Indicate whether or not the product provides a cross handle.
  281. * @return bool True if the product have the handle.
  282. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  283. * @since 1.0.0
  284. */
  285. public function providesCrossHandle()
  286. {
  287. return $this->hasHandle('+');
  288. }
  289.  
  290. /**
  291. * Change the cross handle C sign to a + sign.
  292. * @param string The cross handle sign.
  293. * @return string The cross handle sign.
  294. * @author Jean-Philippe Dery (jean-philippe.dery@lemieuxbedard.com)
  295. * @since 1.0.0
  296. */
  297. protected function formatHandle($handle)
  298. {
  299. return str_replace('C', '+', $handle);
  300. }
  301. }
  302. ?>
Add Comment
Please, Sign In to add comment