Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Directory\Model\CurrencyFactory;
17 
24 class Form extends Template
25 {
31  protected $_currencyFactory;
32 
39 
46  public function __construct(
47  Context $context,
48  Advanced $catalogSearchAdvanced,
49  CurrencyFactory $currencyFactory,
50  array $data = []
51  ) {
52  $this->_catalogSearchAdvanced = $catalogSearchAdvanced;
53  $this->_currencyFactory = $currencyFactory;
54  parent::__construct($context, $data);
55  }
56 
60  public function _prepareLayout()
61  {
62  // add Home breadcrumb
63  if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
64  $breadcrumbs->addCrumb(
65  'home',
66  [
67  'label' => __('Home'),
68  'title' => __('Go to Home Page'),
69  'link' => $this->_storeManager->getStore()->getBaseUrl()
70  ]
71  )->addCrumb(
72  'search',
73  ['label' => __('Catalog Advanced Search')]
74  );
75  }
76  return parent::_prepareLayout();
77  }
78 
84  public function getSearchableAttributes()
85  {
86  $attributes = $this->_catalogSearchAdvanced->getAttributes();
87  return $attributes;
88  }
89 
96  public function getAttributeLabel($attribute)
97  {
98  return $attribute->getStoreLabel();
99  }
100 
108  {
109  return $attribute->getFrontendClass();
110  }
111 
119  public function getAttributeValue($attribute, $part = null)
120  {
121  $value = $this->getRequest()->getQuery($attribute->getAttributeCode());
122  if ($part && $value) {
123  if (isset($value[$part])) {
124  $value = $value[$part];
125  } else {
126  $value = '';
127  }
128  }
129 
130  return $value;
131  }
132 
138  public function getAvailableCurrencies()
139  {
140  $currencies = $this->getData('_currencies');
141  if ($currencies === null) {
142  $currencies = [];
143  $codes = $this->_storeManager->getStore()->getAvailableCurrencyCodes(true);
144  if (is_array($codes) && count($codes)) {
145  $rates = $this->_currencyFactory->create()->getCurrencyRates(
146  $this->_storeManager->getStore()->getBaseCurrency(),
147  $codes
148  );
149 
150  foreach ($codes as $code) {
151  if (isset($rates[$code])) {
152  $currencies[$code] = $code;
153  }
154  }
155  }
156 
157  $this->setData('currencies', $currencies);
158  }
159  return $currencies;
160  }
161 
167  public function getCurrencyCount()
168  {
169  return count($this->getAvailableCurrencies());
170  }
171 
179  public function getCurrency($attribute)
180  {
181  return $this->_storeManager->getStore()->getCurrentCurrencyCode();
182  }
183 
191  {
192  $dataType = $attribute->getBackend()->getType();
193  $inputType = $attribute->getFrontend()->getInputType();
194  if ($inputType == 'select' || $inputType == 'multiselect') {
195  return 'select';
196  }
197 
198  if ($inputType == 'boolean') {
199  return 'yesno';
200  }
201 
202  if ($inputType == 'price') {
203  return 'price';
204  }
205 
206  if ($dataType == 'int' || $dataType == 'decimal') {
207  return 'number';
208  }
209 
210  if ($dataType == 'datetime') {
211  return 'date';
212  }
213 
214  return 'string';
215  }
216 
224  {
225  $extra = '';
226  $options = $attribute->getSource()->getAllOptions(false);
227 
228  $name = $attribute->getAttributeCode();
229 
230  // 2 - avoid yes/no selects to be multiselects
231  if (is_array($options) && count($options) > 2) {
232  $extra = 'multiple="multiple" size="4"';
233  $name .= '[]';
234  } else {
235  array_unshift($options, ['value' => '', 'label' => __('All')]);
236  }
237 
238  return $this->_getSelectBlock()->setName(
239  $name
240  )->setId(
241  $attribute->getAttributeCode()
242  )->setTitle(
244  )->setExtraParams(
245  $extra
246  )->setValue(
248  )->setOptions(
249  $options
250  )->setClass(
251  'multiselect'
252  )->getHtml();
253  }
254 
262  {
263  $options = [
264  ['value' => '', 'label' => __('All')],
265  ['value' => '1', 'label' => __('Yes')],
266  ['value' => '0', 'label' => __('No')],
267  ];
268 
269  $name = $attribute->getAttributeCode();
270  return $this->_getSelectBlock()->setName(
271  $name
272  )->setId(
273  $attribute->getAttributeCode()
274  )->setTitle(
276  )->setExtraParams(
277  ""
278  )->setValue(
280  )->setOptions(
281  $options
282  )->getHtml();
283  }
284 
290  protected function _getSelectBlock()
291  {
292  $block = $this->getData('_select_block');
293  if ($block === null) {
294  $block = $this->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Select::class);
295  $this->setData('_select_block', $block);
296  }
297  return $block;
298  }
299 
305  protected function _getDateBlock()
306  {
307  $block = $this->getData('_date_block');
308  if ($block === null) {
309  $block = $this->getLayout()->createBlock(\Magento\Framework\View\Element\Html\Date::class);
310  $this->setData('_date_block', $block);
311  }
312  return $block;
313  }
314 
320  public function getSearchPostUrl()
321  {
322  return $this->getUrl('*/*/result');
323  }
324 
332  public function getDateInput($attribute, $part = 'from')
333  {
334  $name = $attribute->getAttributeCode() . '[' . $part . ']';
335  $value = $this->getAttributeValue($attribute, $part);
336 
337  return $this->_getDateBlock()->setName(
338  $name
339  )->setId(
340  $attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part)
341  )->setTitle(
343  )->setValue(
344  $value
345  )->setImage(
346  $this->getViewFileUrl('Magento_Theme::calendar.png')
347  )->setDateFormat(
348  $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT)
349  )->setClass(
350  'input-text'
351  )->getHtml();
352  }
353 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$rates
Definition: tax.phtml:35
$block
Definition: block.php:8
getAttributeValue($attribute, $part=null)
Definition: Form.php:119
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72
__construct(Context $context, Advanced $catalogSearchAdvanced, CurrencyFactory $currencyFactory, array $data=[])
Definition: Form.php:46
getDateInput($attribute, $part='from')
Definition: Form.php:332
$code
Definition: info.phtml:12
if( $_orders &&count( $_orders))( 'Orders') ?></caption >< thead >< tr >< th scopeforeach( $_orders as $_order)(__( 'Order #')) ?>" class $extra
Definition: history.phtml:32
if(!isset($_GET['name'])) $name
Definition: log.php:14