Region.php 5.41 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Customer\Model\Renderer;

use Magento\Framework\Data\Form\Element\AbstractElement;

/**
 * Region field renderer
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Region implements \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
{
    /**
     * Country region collections
     *
     * Structure:
     * array(
     *      [$countryId] => \Magento\Framework\Data\Collection\AbstractDb
     * )
     *
     * @var array
     */
    protected static $_regionCollections;

    /**
     * Adminhtml data
     *
     * @var \Magento\Framework\Escaper
     */
    protected $_escaper = null;

    /**
     * @var \Magento\Directory\Model\CountryFactory
     */
    protected $_countryFactory;

    /**
     * @param \Magento\Directory\Model\CountryFactory $countryFactory
     * @param \Magento\Directory\Helper\Data $directoryHelper
     * @param \Magento\Framework\Escaper $escaper
     */
    public function __construct(
        \Magento\Directory\Model\CountryFactory $countryFactory,
        \Magento\Directory\Helper\Data $directoryHelper,
        \Magento\Framework\Escaper $escaper
    ) {
        $this->_countryFactory = $countryFactory;
        $this->_directoryHelper = $directoryHelper;
        $this->_escaper = $escaper;
    }

    /**
     * Render element
     *
     * @param AbstractElement $element
     * @return string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function render(AbstractElement $element)
    {
        $countryId = false;
        $isRegionRequired = false;
        if ($country = $element->getForm()->getElement('country_id')) {
            $countryId = $country->getValue();
            $isRegionRequired = $this->_directoryHelper->isRegionRequired($countryId);
        }

        $html = '<div class="field field-region ' . ($isRegionRequired ? 'required' : '') . '">' . "\n";

        $regionCollection = false;
        if ($countryId) {
            if (!isset(self::$_regionCollections[$countryId])) {
                self::$_regionCollections[$countryId] = $this->_countryFactory->create()->setId(
                    $countryId
                )->getLoadedRegionCollection()->toOptionArray();
            }
            $regionCollection = self::$_regionCollections[$countryId];
        }

        $regionId = (int)$element->getForm()->getElement('region_id')->getValue();

        $htmlAttributes = $element->getHtmlAttributes();
        foreach ($htmlAttributes as $key => $attribute) {
            if ('type' === $attribute) {
                unset($htmlAttributes[$key]);
                break;
            }
        }

        // Output two elements - for 'region' and for 'region_id'.
        // Two elements are needed later upon form post - to properly set data to address model,
        // otherwise old value can be left in region_id attribute and saved to DB.
        // Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user
        $regionHtmlName = $element->getName();
        $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName);
        $regionHtmlId = $element->getHtmlId();
        $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId);

        if ($isRegionRequired) {
            $element->addClass('required-entry');
        }

        if ($regionCollection && count($regionCollection) > 0) {
            $elementClass = $element->getClass();
            $html .= '<label class="label" for="' .
                $regionIdHtmlId .
                '"><span>' .
                $element->getLabel() .
                '</span>' .
                '</label>';
            $html .= '<div class="control">';

            $html .= '<select id="' . $regionIdHtmlId . '" name="' . $regionIdHtmlName . '" ' . $element->serialize(
                $htmlAttributes
            ) . '>' . "\n";
            foreach ($regionCollection as $region) {
                $selected = $regionId == $region['value'] ? ' selected="selected"' : '';
                $regionVal = 0 == $region['value'] ? '' : (int)$region['value'];
                $html .= '<option value="' . $regionVal . '"' . $selected . '>' . $this->_escaper->escapeHtml(
                    __($region['label'])
                ) . '</option>';
            }
            $html .= '</select>' . "\n";

            $html .= '<input type="hidden" name="' . $regionHtmlName . '" id="' . $regionHtmlId . '" value=""/>';

            $html .= '</div>';
            $element->setClass($elementClass);
        } else {
            $html .= '<label class="label" for="' .
                $regionHtmlId .
                '"><span>' .
                $element->getLabel() .
                '</span></label>';
            $html .= '<div class="control">';
            $html .= '<input id="' .
                $regionHtmlId .
                '" name="' .
                $regionHtmlName .
                '" value="' .
                $element->getEscapedValue() .
                '" ' .
                $element->serialize(
                    $htmlAttributes
                ) . "/>" . "\n";
            $html .= '<input type="hidden" name="' . $regionIdHtmlName . '" id="' . $regionIdHtmlId . '" value=""/>';
            $html .= '</div>' . "\n";
        }
        $html .= '</div>' . "\n";
        return $html;
    }
}