Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Dob.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class Dob extends AbstractWidget
17 {
21  const MIN_DATE_RANGE_KEY = 'date_range_min';
22 
23  const MAX_DATE_RANGE_KEY = 'date_range_max';
24 
30  protected $_dateInputs = [];
31 
35  protected $dateElement;
36 
40  protected $filterFactory;
41 
50  public function __construct(
51  \Magento\Framework\View\Element\Template\Context $context,
52  \Magento\Customer\Helper\Address $addressHelper,
54  \Magento\Framework\View\Element\Html\Date $dateElement,
55  \Magento\Framework\Data\Form\FilterFactory $filterFactory,
56  array $data = []
57  ) {
58  $this->dateElement = $dateElement;
59  $this->filterFactory = $filterFactory;
60  parent::__construct($context, $addressHelper, $customerMetadata, $data);
61  }
62 
66  public function _construct()
67  {
68  parent::_construct();
69  $this->setTemplate('Magento_Customer::widget/dob.phtml');
70  }
71 
75  public function isEnabled()
76  {
77  $attributeMetadata = $this->_getAttribute('dob');
78  return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
79  }
80 
84  public function isRequired()
85  {
86  $attributeMetadata = $this->_getAttribute('dob');
87  return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
88  }
89 
94  public function setDate($date)
95  {
96  $this->setTime($date ? strtotime($date) : false);
97  $this->setValue($this->applyOutputFilter($date));
98  return $this;
99  }
100 
106  protected function getFormFilter()
107  {
108  $attributeMetadata = $this->_getAttribute('dob');
109  $filterCode = $attributeMetadata->getInputFilter();
110  if ($filterCode) {
111  $data = [];
112  if ($filterCode == 'date') {
113  $data['format'] = $this->getDateFormat();
114  }
115  $filter = $this->filterFactory->create($filterCode, $data);
116  return $filter;
117  }
118  return false;
119  }
120 
127  protected function applyOutputFilter($value)
128  {
129  $filter = $this->getFormFilter();
130  if ($filter && $value) {
131  $value = date('Y-m-d', $this->getTime());
132  $value = $filter->outputFilter($value);
133  }
134  return $value;
135  }
136 
140  public function getDay()
141  {
142  return $this->getTime() ? date('d', $this->getTime()) : '';
143  }
144 
148  public function getMonth()
149  {
150  return $this->getTime() ? date('m', $this->getTime()) : '';
151  }
152 
156  public function getYear()
157  {
158  return $this->getTime() ? date('Y', $this->getTime()) : '';
159  }
160 
166  public function getLabel()
167  {
168  return __('Date of Birth');
169  }
170 
176  public function getFieldHtml()
177  {
178  $this->dateElement->setData([
179  'extra_params' => $this->getHtmlExtraParams(),
180  'name' => $this->getHtmlId(),
181  'id' => $this->getHtmlId(),
182  'class' => $this->getHtmlClass(),
183  'value' => $this->getValue(),
184  'date_format' => $this->getDateFormat(),
185  'image' => $this->getViewFileUrl('Magento_Theme::calendar.png'),
186  'years_range' => '-120y:c+nn',
187  'max_date' => '-1d',
188  'change_month' => 'true',
189  'change_year' => 'true',
190  'show_on' => 'both',
191  'first_day' => $this->getFirstDay()
192  ]);
193  return $this->dateElement->getHtml();
194  }
195 
201  public function getHtmlId()
202  {
203  return 'dob';
204  }
205 
211  public function getHtmlExtraParams()
212  {
213  $validators = [];
214  if ($this->isRequired()) {
215  $validators['required'] = true;
216  }
217  $validators['validate-date'] = [
218  'dateFormat' => $this->getDateFormat()
219  ];
220  return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
221  }
222 
228  public function getDateFormat()
229  {
230  return $this->_localeDate->getDateFormatWithLongYear();
231  }
232 
240  public function setDateInput($code, $html)
241  {
242  $this->_dateInputs[$code] = $html;
243  }
244 
252  public function getSortedDateInputs($stripNonInputChars = true)
253  {
254  $mapping = [];
255  if ($stripNonInputChars) {
256  $mapping['/[^medy]/i'] = '\\1';
257  }
258  $mapping['/m{1,5}/i'] = '%1$s';
259  $mapping['/e{1,5}/i'] = '%2$s';
260  $mapping['/d{1,5}/i'] = '%2$s';
261  $mapping['/y{1,5}/i'] = '%3$s';
262 
263  $dateFormat = preg_replace(array_keys($mapping), array_values($mapping), $this->getDateFormat());
264 
265  return sprintf($dateFormat, $this->_dateInputs['m'], $this->_dateInputs['d'], $this->_dateInputs['y']);
266  }
267 
273  public function getMinDateRange()
274  {
275  $dob = $this->_getAttribute('dob');
276  if ($dob !== null) {
277  $rules = $this->_getAttribute('dob')->getValidationRules();
279  $rules,
280  self::MIN_DATE_RANGE_KEY
281  );
282  if ($minDateValue !== null) {
283  return date("Y/m/d", $minDateValue);
284  }
285  }
286  return null;
287  }
288 
294  public function getMaxDateRange()
295  {
296  $dob = $this->_getAttribute('dob');
297  if ($dob !== null) {
298  $rules = $this->_getAttribute('dob')->getValidationRules();
300  $rules,
301  self::MAX_DATE_RANGE_KEY
302  );
303  if ($maxDateValue !== null) {
304  return date("Y/m/d", $maxDateValue);
305  }
306  }
307  return null;
308  }
309 
315  public function getFirstDay()
316  {
317  return (int)$this->_scopeConfig->getValue(
318  'general/locale/firstday',
319  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
320  );
321  }
322 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Customer\Helper\Address $addressHelper, CustomerMetadataInterface $customerMetadata, \Magento\Framework\View\Element\Html\Date $dateElement, \Magento\Framework\Data\Form\FilterFactory $filterFactory, array $data=[])
Definition: Dob.php:50
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
setDateInput($code, $html)
Definition: Dob.php:240
static getArrayElementByName($data, $keyValue, $keyName='name', $valueName='value')
getSortedDateInputs($stripNonInputChars=true)
Definition: Dob.php:252
$code
Definition: info.phtml:12