PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/sevenly/magento-ce
PHP | 307 lines | 175 code | 30 blank | 102 comment | 29 complexity | 769f38df28dc28680d329ee8d8ee0668 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 Magento 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('catalogsearch')->__('Home'),
  41. 'title'=>Mage::helper('catalogsearch')->__('Go to Home Page'),
  42. 'link'=>Mage::getBaseUrl()
  43. ))->addCrumb('search', array(
  44. 'label'=>Mage::helper('catalogsearch')->__('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 $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 $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 string $attribute
  83. * @param string $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. }
  93. else {
  94. $value = '';
  95. }
  96. }
  97. return $value;
  98. }
  99. /**
  100. * Retrieve the list of available currencies
  101. *
  102. * @return array
  103. */
  104. public function getAvailableCurrencies()
  105. {
  106. $currencies = $this->getData('_currencies');
  107. if (is_null($currencies)) {
  108. $currencies = array();
  109. $codes = Mage::app()->getStore()->getAvailableCurrencyCodes(true);
  110. if (is_array($codes) && count($codes)) {
  111. $rates = Mage::getModel('directory/currency')->getCurrencyRates(
  112. Mage::app()->getStore()->getBaseCurrency(),
  113. $codes
  114. );
  115. foreach ($codes as $code) {
  116. if (isset($rates[$code])) {
  117. $currencies[$code] = $code;
  118. }
  119. }
  120. }
  121. $this->setData('currencies', $currencies);
  122. }
  123. return $currencies;
  124. }
  125. /**
  126. * Count available currencies
  127. *
  128. * @return int
  129. */
  130. public function getCurrencyCount()
  131. {
  132. return count($this->getAvailableCurrencies());
  133. }
  134. /**
  135. * Retrieve currency code for attribute
  136. *
  137. * @param $attribute
  138. * @return string
  139. */
  140. public function getCurrency($attribute)
  141. {
  142. return Mage::app()->getStore()->getCurrentCurrencyCode();
  143. $baseCurrency = Mage::app()->getStore()->getBaseCurrency()->getCurrencyCode();
  144. return $this->getAttributeValue($attribute, 'currency') ?
  145. $this->getAttributeValue($attribute, 'currency') : $baseCurrency;
  146. }
  147. /**
  148. * Retrieve attribute input type
  149. *
  150. * @param $attribute
  151. * @return string
  152. */
  153. public function getAttributeInputType($attribute)
  154. {
  155. $dataType = $attribute->getBackend()->getType();
  156. $imputType = $attribute->getFrontend()->getInputType();
  157. if ($imputType == 'select' || $imputType == 'multiselect') {
  158. return 'select';
  159. }
  160. if ($imputType == 'boolean') {
  161. return 'yesno';
  162. }
  163. if ($imputType == 'price') {
  164. return 'price';
  165. }
  166. if ($dataType == 'int' || $dataType == 'decimal') {
  167. return 'number';
  168. }
  169. if ($dataType == 'datetime') {
  170. return 'date';
  171. }
  172. return 'string';
  173. }
  174. /**
  175. * Build attribute select element html string
  176. *
  177. * @param $attribute
  178. * @return string
  179. */
  180. public function getAttributeSelectElement($attribute)
  181. {
  182. $extra = '';
  183. $options = $attribute->getSource()->getAllOptions(false);
  184. $name = $attribute->getAttributeCode();
  185. // 2 - avoid yes/no selects to be multiselects
  186. if (is_array($options) && count($options)>2) {
  187. $extra = 'multiple="multiple" size="4"';
  188. $name.= '[]';
  189. }
  190. else {
  191. array_unshift($options, array('value'=>'', 'label'=>Mage::helper('catalogsearch')->__('All')));
  192. }
  193. return $this->_getSelectBlock()
  194. ->setName($name)
  195. ->setId($attribute->getAttributeCode())
  196. ->setTitle($this->getAttributeLabel($attribute))
  197. ->setExtraParams($extra)
  198. ->setValue($this->getAttributeValue($attribute))
  199. ->setOptions($options)
  200. ->setClass('multiselect')
  201. ->getHtml();
  202. }
  203. public function getAttributeYesNoElement($attribute)
  204. {
  205. $options = array(
  206. array('value' => '', 'label' => Mage::helper('catalogsearch')->__('All')),
  207. array('value' => '1', 'label' => Mage::helper('catalogsearch')->__('Yes')),
  208. array('value' => '0', 'label' => Mage::helper('catalogsearch')->__('No'))
  209. );
  210. $name = $attribute->getAttributeCode();
  211. return $this->_getSelectBlock()
  212. ->setName($name)
  213. ->setId($attribute->getAttributeCode())
  214. ->setTitle($this->getAttributeLabel($attribute))
  215. ->setExtraParams("")
  216. ->setValue($this->getAttributeValue($attribute))
  217. ->setOptions($options)
  218. ->getHtml();
  219. }
  220. protected function _getSelectBlock()
  221. {
  222. $block = $this->getData('_select_block');
  223. if (is_null($block)) {
  224. $block = $this->getLayout()->createBlock('core/html_select');
  225. $this->setData('_select_block', $block);
  226. }
  227. return $block;
  228. }
  229. protected function _getDateBlock()
  230. {
  231. $block = $this->getData('_date_block');
  232. if (is_null($block)) {
  233. $block = $this->getLayout()->createBlock('core/html_date');
  234. $this->setData('_date_block', $block);
  235. }
  236. return $block;
  237. }
  238. /**
  239. * Retrieve advanced search model object
  240. *
  241. * @return Mage_CatalogSearch_Model_Advanced
  242. */
  243. public function getModel()
  244. {
  245. return Mage::getSingleton('catalogsearch/advanced');
  246. }
  247. /**
  248. * Retrieve search form action string
  249. *
  250. * @return string
  251. */
  252. public function getSearchPostUrl()
  253. {
  254. return $this->getUrl('*/*/result');
  255. }
  256. /**
  257. * Build date element html string for attribute
  258. *
  259. * @param $attribute
  260. * @param string $part
  261. * @return string
  262. */
  263. public function getDateInput($attribute, $part = 'from')
  264. {
  265. $name = $attribute->getAttributeCode() . '[' . $part . ']';
  266. $value = $this->getAttributeValue($attribute, $part);
  267. return $this->_getDateBlock()
  268. ->setName($name)
  269. ->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))
  270. ->setTitle($this->getAttributeLabel($attribute))
  271. ->setValue($value)
  272. ->setImage($this->getSkinUrl('images/calendar.gif'))
  273. ->setFormat('%m/%d/%y')
  274. ->setClass('input-text')
  275. ->getHtml();
  276. }
  277. }