PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php

https://bitbucket.org/jokusafet/magento2
PHP | 312 lines | 174 code | 30 blank | 108 comment | 29 complexity | 0b1156efd4a190f0a5fd28683c25c5ba MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_CatalogSearch
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Advanced search form
  28. *
  29. * @category Mage
  30. * @package Mage_CatalogSearch
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_CatalogSearch_Block_Advanced_Form extends Mage_Core_Block_Template
  34. {
  35. public function _prepareLayout()
  36. {
  37. // add Home breadcrumb
  38. if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
  39. $breadcrumbs->addCrumb('home', array(
  40. 'label'=>Mage::helper('Mage_CatalogSearch_Helper_Data')->__('Home'),
  41. 'title'=>Mage::helper('Mage_CatalogSearch_Helper_Data')->__('Go to Home Page'),
  42. 'link'=>Mage::getBaseUrl()
  43. ))->addCrumb('search', array(
  44. 'label'=>Mage::helper('Mage_CatalogSearch_Helper_Data')->__('Catalog Advanced Search')
  45. ));
  46. }
  47. return parent::_prepareLayout();
  48. }
  49. /**
  50. * Retrieve collection of product searchable attributes
  51. *
  52. * @return Varien_Data_Collection_Db
  53. */
  54. public function getSearchableAttributes()
  55. {
  56. $attributes = $this->getModel()->getAttributes();
  57. return $attributes;
  58. }
  59. /**
  60. * Retrieve attribute label
  61. *
  62. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  63. * @return string
  64. */
  65. public function getAttributeLabel($attribute)
  66. {
  67. return $attribute->getStoreLabel();
  68. }
  69. /**
  70. * Retrieve attribute input validation class
  71. *
  72. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  73. * @return string
  74. */
  75. public function getAttributeValidationClass($attribute)
  76. {
  77. return $attribute->getFrontendClass();
  78. }
  79. /**
  80. * Retrieve search string for given field from request
  81. *
  82. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  83. * @param string|null $part
  84. * @return mixed|string
  85. */
  86. public function getAttributeValue($attribute, $part = null)
  87. {
  88. $value = $this->getRequest()->getQuery($attribute->getAttributeCode());
  89. if ($part && $value) {
  90. if (isset($value[$part])) {
  91. $value = $value[$part];
  92. } else {
  93. $value = '';
  94. }
  95. }
  96. return $value;
  97. }
  98. /**
  99. * Retrieve the list of available currencies
  100. *
  101. * @return array
  102. */
  103. public function getAvailableCurrencies()
  104. {
  105. $currencies = $this->getData('_currencies');
  106. if (is_null($currencies)) {
  107. $currencies = array();
  108. $codes = Mage::app()->getStore()->getAvailableCurrencyCodes(true);
  109. if (is_array($codes) && count($codes)) {
  110. $rates = Mage::getModel('Mage_Directory_Model_Currency')->getCurrencyRates(
  111. Mage::app()->getStore()->getBaseCurrency(),
  112. $codes
  113. );
  114. foreach ($codes as $code) {
  115. if (isset($rates[$code])) {
  116. $currencies[$code] = $code;
  117. }
  118. }
  119. }
  120. $this->setData('currencies', $currencies);
  121. }
  122. return $currencies;
  123. }
  124. /**
  125. * Count available currencies
  126. *
  127. * @return int
  128. */
  129. public function getCurrencyCount()
  130. {
  131. return count($this->getAvailableCurrencies());
  132. }
  133. /**
  134. * Retrieve currency code for attribute
  135. *
  136. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  137. * @return string
  138. */
  139. public function getCurrency($attribute)
  140. {
  141. return Mage::app()->getStore()->getCurrentCurrencyCode();
  142. $baseCurrency = Mage::app()->getStore()->getBaseCurrency()->getCurrencyCode();
  143. return $this->getAttributeValue($attribute, 'currency') ?
  144. $this->getAttributeValue($attribute, 'currency') : $baseCurrency;
  145. }
  146. /**
  147. * Retrieve attribute input type
  148. *
  149. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  150. * @return string
  151. */
  152. public function getAttributeInputType($attribute)
  153. {
  154. $dataType = $attribute->getBackend()->getType();
  155. $imputType = $attribute->getFrontend()->getInputType();
  156. if ($imputType == 'select' || $imputType == 'multiselect') {
  157. return 'select';
  158. }
  159. if ($imputType == 'boolean') {
  160. return 'yesno';
  161. }
  162. if ($imputType == 'price') {
  163. return 'price';
  164. }
  165. if ($dataType == 'int' || $dataType == 'decimal') {
  166. return 'number';
  167. }
  168. if ($dataType == 'datetime') {
  169. return 'date';
  170. }
  171. return 'string';
  172. }
  173. /**
  174. * Build attribute select element html string
  175. *
  176. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  177. * @return string
  178. */
  179. public function getAttributeSelectElement($attribute)
  180. {
  181. $extra = '';
  182. $options = $attribute->getSource()->getAllOptions(false);
  183. $name = $attribute->getAttributeCode();
  184. // 2 - avoid yes/no selects to be multiselects
  185. if (is_array($options) && count($options)>2) {
  186. $extra = 'multiple="multiple" size="4"';
  187. $name.= '[]';
  188. }
  189. else {
  190. array_unshift($options, array('value'=>'', 'label'=>Mage::helper('Mage_CatalogSearch_Helper_Data')->__('All')));
  191. }
  192. return $this->_getSelectBlock()
  193. ->setName($name)
  194. ->setId($attribute->getAttributeCode())
  195. ->setTitle($this->getAttributeLabel($attribute))
  196. ->setExtraParams($extra)
  197. ->setValue($this->getAttributeValue($attribute))
  198. ->setOptions($options)
  199. ->setClass('multiselect')
  200. ->getHtml();
  201. }
  202. /**
  203. * Retrieve yes/no element html for provided attribute
  204. *
  205. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  206. * @return string
  207. */
  208. public function getAttributeYesNoElement($attribute)
  209. {
  210. $options = array(
  211. array('value' => '', 'label' => Mage::helper('Mage_CatalogSearch_Helper_Data')->__('All')),
  212. array('value' => '1', 'label' => Mage::helper('Mage_CatalogSearch_Helper_Data')->__('Yes')),
  213. array('value' => '0', 'label' => Mage::helper('Mage_CatalogSearch_Helper_Data')->__('No'))
  214. );
  215. $name = $attribute->getAttributeCode();
  216. return $this->_getSelectBlock()
  217. ->setName($name)
  218. ->setId($attribute->getAttributeCode())
  219. ->setTitle($this->getAttributeLabel($attribute))
  220. ->setExtraParams("")
  221. ->setValue($this->getAttributeValue($attribute))
  222. ->setOptions($options)
  223. ->getHtml();
  224. }
  225. protected function _getSelectBlock()
  226. {
  227. $block = $this->getData('_select_block');
  228. if (is_null($block)) {
  229. $block = $this->getLayout()->createBlock('Mage_Core_Block_Html_Select');
  230. $this->setData('_select_block', $block);
  231. }
  232. return $block;
  233. }
  234. protected function _getDateBlock()
  235. {
  236. $block = $this->getData('_date_block');
  237. if (is_null($block)) {
  238. $block = $this->getLayout()->createBlock('Mage_Core_Block_Html_Date');
  239. $this->setData('_date_block', $block);
  240. }
  241. return $block;
  242. }
  243. /**
  244. * Retrieve advanced search model object
  245. *
  246. * @return Mage_CatalogSearch_Model_Advanced
  247. */
  248. public function getModel()
  249. {
  250. return Mage::getSingleton('Mage_CatalogSearch_Model_Advanced');
  251. }
  252. /**
  253. * Retrieve search form action url
  254. *
  255. * @return string
  256. */
  257. public function getSearchPostUrl()
  258. {
  259. return $this->getUrl('*/*/result');
  260. }
  261. /**
  262. * Build date element html string for attribute
  263. *
  264. * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
  265. * @param string $part
  266. * @return string
  267. */
  268. public function getDateInput($attribute, $part = 'from')
  269. {
  270. $name = $attribute->getAttributeCode() . '[' . $part . ']';
  271. $value = $this->getAttributeValue($attribute, $part);
  272. return $this->_getDateBlock()
  273. ->setName($name)
  274. ->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))
  275. ->setTitle($this->getAttributeLabel($attribute))
  276. ->setValue($value)
  277. ->setImage($this->getViewFileUrl('Mage_Core::calendar.gif'))
  278. ->setDateFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT))
  279. ->setClass('input-text')
  280. ->getHtml();
  281. }
  282. }