123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Block\Widget;
- use Magento\Customer\Api\CustomerMetadataInterface;
- use Magento\Framework\Api\ArrayObjectSearch;
- /**
- * Class Dob
- *
- * @SuppressWarnings(PHPMD.DepthOfInheritance)
- */
- class Dob extends AbstractWidget
- {
- /**
- * Constants for borders of date-type customer attributes
- */
- const MIN_DATE_RANGE_KEY = 'date_range_min';
- const MAX_DATE_RANGE_KEY = 'date_range_max';
- /**
- * Date inputs
- *
- * @var array
- */
- protected $_dateInputs = [];
- /**
- * @var \Magento\Framework\View\Element\Html\Date
- */
- protected $dateElement;
- /**
- * @var \Magento\Framework\Data\Form\FilterFactory
- */
- protected $filterFactory;
- /**
- * @param \Magento\Framework\View\Element\Template\Context $context
- * @param \Magento\Customer\Helper\Address $addressHelper
- * @param CustomerMetadataInterface $customerMetadata
- * @param \Magento\Framework\View\Element\Html\Date $dateElement
- * @param \Magento\Framework\Data\Form\FilterFactory $filterFactory
- * @param array $data
- */
- public function __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 = []
- ) {
- $this->dateElement = $dateElement;
- $this->filterFactory = $filterFactory;
- parent::__construct($context, $addressHelper, $customerMetadata, $data);
- }
- /**
- * @inheritdoc
- */
- public function _construct()
- {
- parent::_construct();
- $this->setTemplate('Magento_Customer::widget/dob.phtml');
- }
- /**
- * Check if dob attribute enabled in system
- *
- * @return bool
- */
- public function isEnabled()
- {
- $attributeMetadata = $this->_getAttribute('dob');
- return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
- }
- /**
- * Check if dob attribute marked as required
- *
- * @return bool
- */
- public function isRequired()
- {
- $attributeMetadata = $this->_getAttribute('dob');
- return $attributeMetadata ? (bool)$attributeMetadata->isRequired() : false;
- }
- /**
- * Set date
- *
- * @param string $date
- * @return $this
- */
- public function setDate($date)
- {
- $this->setTime($date ? strtotime($date) : false);
- $this->setValue($this->applyOutputFilter($date));
- return $this;
- }
- /**
- * Return Data Form Filter or false
- *
- * @return \Magento\Framework\Data\Form\Filter\FilterInterface|false
- */
- protected function getFormFilter()
- {
- $attributeMetadata = $this->_getAttribute('dob');
- $filterCode = $attributeMetadata->getInputFilter();
- if ($filterCode) {
- $data = [];
- if ($filterCode == 'date') {
- $data['format'] = $this->getDateFormat();
- }
- $filter = $this->filterFactory->create($filterCode, $data);
- return $filter;
- }
- return false;
- }
- /**
- * Apply output filter to value
- *
- * @param string $value
- * @return string
- */
- protected function applyOutputFilter($value)
- {
- $filter = $this->getFormFilter();
- if ($filter && $value) {
- $value = date('Y-m-d', $this->getTime());
- $value = $filter->outputFilter($value);
- }
- return $value;
- }
- /**
- * Get day
- *
- * @return string|bool
- */
- public function getDay()
- {
- return $this->getTime() ? date('d', $this->getTime()) : '';
- }
- /**
- * Get month
- *
- * @return string|bool
- */
- public function getMonth()
- {
- return $this->getTime() ? date('m', $this->getTime()) : '';
- }
- /**
- * Get year
- *
- * @return string|bool
- */
- public function getYear()
- {
- return $this->getTime() ? date('Y', $this->getTime()) : '';
- }
- /**
- * Return label
- *
- * @return \Magento\Framework\Phrase
- */
- public function getLabel()
- {
- return __('Date of Birth');
- }
- /**
- * Retrieve store attribute label
- *
- * @param string $attributeCode
- *
- * @return string
- */
- public function getStoreLabel($attributeCode)
- {
- $attribute = $this->_getAttribute($attributeCode);
- return $attribute ? __($attribute->getStoreLabel()) : '';
- }
- /**
- * Create correct date field
- *
- * @return string
- */
- public function getFieldHtml()
- {
- $this->dateElement->setData([
- 'extra_params' => $this->getHtmlExtraParams(),
- 'name' => $this->getHtmlId(),
- 'id' => $this->getHtmlId(),
- 'class' => $this->getHtmlClass(),
- 'value' => $this->getValue(),
- 'date_format' => $this->getDateFormat(),
- 'image' => $this->getViewFileUrl('Magento_Theme::calendar.png'),
- 'years_range' => '-120y:c+nn',
- 'max_date' => '-1d',
- 'change_month' => 'true',
- 'change_year' => 'true',
- 'show_on' => 'both',
- 'first_day' => $this->getFirstDay()
- ]);
- return $this->dateElement->getHtml();
- }
- /**
- * Return id
- *
- * @return string
- */
- public function getHtmlId()
- {
- return 'dob';
- }
- /**
- * Return data-validate rules
- *
- * @return string
- */
- public function getHtmlExtraParams()
- {
- $validators = [];
- if ($this->isRequired()) {
- $validators['required'] = true;
- }
- $validators['validate-date'] = [
- 'dateFormat' => $this->getDateFormat()
- ];
- return 'data-validate="' . $this->_escaper->escapeHtml(json_encode($validators)) . '"';
- }
- /**
- * Returns format which will be applied for DOB in javascript
- *
- * @return string
- */
- public function getDateFormat()
- {
- return $this->_localeDate->getDateFormatWithLongYear();
- }
- /**
- * Add date input html
- *
- * @param string $code
- * @param string $html
- * @return void
- */
- public function setDateInput($code, $html)
- {
- $this->_dateInputs[$code] = $html;
- }
- /**
- * Sort date inputs by dateformat order of current locale
- *
- * @param bool $stripNonInputChars
- *
- * @return string
- */
- public function getSortedDateInputs($stripNonInputChars = true)
- {
- $mapping = [];
- if ($stripNonInputChars) {
- $mapping['/[^medy]/i'] = '\\1';
- }
- $mapping['/m{1,5}/i'] = '%1$s';
- $mapping['/e{1,5}/i'] = '%2$s';
- $mapping['/d{1,5}/i'] = '%2$s';
- $mapping['/y{1,5}/i'] = '%3$s';
- $dateFormat = preg_replace(array_keys($mapping), array_values($mapping), $this->getDateFormat());
- return sprintf($dateFormat, $this->_dateInputs['m'], $this->_dateInputs['d'], $this->_dateInputs['y']);
- }
- /**
- * Return minimal date range value
- *
- * @return string|null
- */
- public function getMinDateRange()
- {
- $dob = $this->_getAttribute('dob');
- if ($dob !== null) {
- $rules = $this->_getAttribute('dob')->getValidationRules();
- $minDateValue = ArrayObjectSearch::getArrayElementByName(
- $rules,
- self::MIN_DATE_RANGE_KEY
- );
- if ($minDateValue !== null) {
- return date("Y/m/d", $minDateValue);
- }
- }
- return null;
- }
- /**
- * Return maximal date range value
- *
- * @return string|null
- */
- public function getMaxDateRange()
- {
- $dob = $this->_getAttribute('dob');
- if ($dob !== null) {
- $rules = $this->_getAttribute('dob')->getValidationRules();
- $maxDateValue = ArrayObjectSearch::getArrayElementByName(
- $rules,
- self::MAX_DATE_RANGE_KEY
- );
- if ($maxDateValue !== null) {
- return date("Y/m/d", $maxDateValue);
- }
- }
- return null;
- }
- /**
- * Return first day of the week
- *
- * @return int
- */
- public function getFirstDay()
- {
- return (int)$this->_scopeConfig->getValue(
- 'general/locale/firstday',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- }
|