| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Copyright © O2TI. All rights reserved. |
||||
| 4 | * |
||||
| 5 | * @author Bruno Elisei <[email protected]> |
||||
| 6 | * See COPYING.txt for license details. |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | declare(strict_types=1); |
||||
| 10 | |||||
| 11 | namespace O2TI\AdvancedFieldsCheckout\Block\Adminhtml\Form\Field\Column; |
||||
| 12 | |||||
| 13 | use Magento\Customer\Model\Address; |
||||
| 14 | use Magento\Customer\Model\Customer; |
||||
| 15 | use Magento\Eav\Api\Data\AttributeInterface; |
||||
| 16 | use Magento\Framework\View\Element\Context; |
||||
| 17 | use Magento\Framework\View\Element\Html\Select; |
||||
| 18 | use Magento\Ui\Component\Form\AttributeMapper; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Class FieldColumn - Create Field to Column. |
||||
| 22 | */ |
||||
| 23 | class FieldColumn extends Select |
||||
| 24 | { |
||||
| 25 | /** |
||||
| 26 | * @var Address |
||||
| 27 | */ |
||||
| 28 | private $address; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @var Customer |
||||
| 32 | */ |
||||
| 33 | private $customer; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * @var AttributeMapper |
||||
| 37 | */ |
||||
| 38 | protected $attributeMapper; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Constructor. |
||||
| 42 | * |
||||
| 43 | * @param Context $context |
||||
| 44 | * @param Address $address |
||||
| 45 | * @param Customer $customer |
||||
| 46 | * @param AttributeMapper $attributeMapper |
||||
| 47 | * @param array $data |
||||
| 48 | */ |
||||
| 49 | public function __construct( |
||||
| 50 | Context $context, |
||||
| 51 | Address $address, |
||||
| 52 | Customer $customer, |
||||
| 53 | AttributeMapper $attributeMapper, |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | array $data = [] |
||||
| 55 | ) { |
||||
| 56 | parent::__construct($context, $data); |
||||
| 57 | $this->address = $address; |
||||
| 58 | $this->customer = $customer; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Set "name" for <select> element. |
||||
| 63 | * |
||||
| 64 | * @param string $value |
||||
| 65 | * |
||||
| 66 | * @return $this |
||||
| 67 | */ |
||||
| 68 | public function setInputName($value) |
||||
| 69 | { |
||||
| 70 | return $this->setName($value); |
||||
|
0 ignored issues
–
show
The method
setName() does not exist on O2TI\AdvancedFieldsCheck...ield\Column\FieldColumn. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * Set "id" for <select> element. |
||||
| 75 | * |
||||
| 76 | * @param string $value |
||||
| 77 | * |
||||
| 78 | * @return $this |
||||
| 79 | */ |
||||
| 80 | public function setInputId($value) |
||||
| 81 | { |
||||
| 82 | return $this->setId($value); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * Render block HTML. |
||||
| 87 | * |
||||
| 88 | * @return string |
||||
| 89 | */ |
||||
| 90 | public function _toHtml(): string |
||||
| 91 | { |
||||
| 92 | if (!$this->getOptions()) { |
||||
| 93 | $this->setOptions($this->getSourceOptions()); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | return parent::_toHtml(); |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * Render Options. |
||||
| 101 | * |
||||
| 102 | * @return array |
||||
| 103 | */ |
||||
| 104 | private function getSourceOptions(): array |
||||
| 105 | { |
||||
| 106 | $customerAttributes = $this->getCustomerAttributes(); |
||||
| 107 | |||||
| 108 | foreach ($customerAttributes as $atribute => $val) { |
||||
| 109 | $attributesArrays[] = [ |
||||
| 110 | 'label' => __($val['label']).' - '.$atribute, |
||||
| 111 | 'value' => $atribute, |
||||
| 112 | ]; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | return $attributesArrays; |
||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
| 116 | } |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * Get customer attributes. |
||||
| 120 | * |
||||
| 121 | * @return array |
||||
| 122 | */ |
||||
| 123 | private function getCustomerAttributes(): array |
||||
| 124 | { |
||||
| 125 | $elements = []; |
||||
| 126 | |||||
| 127 | /** @var AttributeInterface[] $attributes */ |
||||
| 128 | $attributesCustomer = $this->customer->getAttributes(); |
||||
| 129 | |||||
| 130 | foreach ($attributesCustomer as $attribute) { |
||||
| 131 | $code = $attribute->getAttributeCode(); |
||||
| 132 | $inForms = $attribute->getUsedInForms(); |
||||
| 133 | $forms = []; |
||||
| 134 | foreach ($inForms as $value) { |
||||
| 135 | $forms[] = $value; |
||||
| 136 | } |
||||
| 137 | if (!in_array('customer_account_create', $forms)) { |
||||
| 138 | continue; |
||||
| 139 | } |
||||
| 140 | if ((int) $attribute->getData('is_visible') === 0) { |
||||
| 141 | continue; |
||||
| 142 | } |
||||
| 143 | $elements[$code]['label'] = $attribute->getData('frontend_label'); |
||||
| 144 | } |
||||
| 145 | |||||
| 146 | $attributesAddress = $this->address->getAttributes(); |
||||
| 147 | |||||
| 148 | foreach ($attributesAddress as $attribute) { |
||||
| 149 | $code = $attribute->getAttributeCode(); |
||||
| 150 | $inForms = $attribute->getUsedInForms(); |
||||
| 151 | $forms = []; |
||||
| 152 | foreach ($inForms as $value) { |
||||
| 153 | $forms[] = $value; |
||||
| 154 | } |
||||
| 155 | if (!in_array('customer_register_address', $forms)) { |
||||
| 156 | continue; |
||||
| 157 | } |
||||
| 158 | if ((int) $attribute->getData('is_visible') === 0) { |
||||
| 159 | continue; |
||||
| 160 | } |
||||
| 161 | $elements[$code]['label'] = $attribute->getData('frontend_label'); |
||||
| 162 | } |
||||
| 163 | $elements['password']['label'] = 'Password'; |
||||
| 164 | $elements['street_0']['label'] = 'Street Line 1'; |
||||
| 165 | $elements['street_1']['label'] = 'Street Line 2'; |
||||
| 166 | $elements['street_2']['label'] = 'Street Line 3'; |
||||
| 167 | $elements['street_3']['label'] = 'Street Line 4'; |
||||
| 168 | |||||
| 169 | return $elements; |
||||
| 170 | } |
||||
| 171 | } |
||||
| 172 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.