| 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 | namespace O2TI\AdvancedFieldsCheckout\Helper; |
||
| 10 | |||
| 11 | use Magento\Framework\App\Config\ScopeConfigInterface; |
||
| 12 | use Magento\Framework\App\Helper\AbstractHelper; |
||
| 13 | use Magento\Framework\Serialize\Serializer\Json; |
||
| 14 | use Magento\Store\Model\ScopeInterface; |
||
| 15 | use Magento\Store\Model\StoreManagerInterface; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class Config - Helper configuration. |
||
| 19 | */ |
||
| 20 | class Config extends AbstractHelper |
||
| 21 | { |
||
| 22 | public const CONFIG_PATH_GENERAL = 'advanced_fields_checkout/general/%s'; |
||
| 23 | |||
| 24 | public const CONFIG_PATH_CLASSES = 'advanced_fields_checkout/general/columns/classes'; |
||
| 25 | |||
| 26 | public const CONFIG_PATH_AUTOCOMPLETE = 'advanced_fields_checkout/general/autocomplete/autocomplete'; |
||
| 27 | |||
| 28 | public const CONFIG_PATH_PLACEHOLDER = 'advanced_fields_checkout/general/placeholder/placeholder'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Json |
||
| 32 | */ |
||
| 33 | protected $json; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var ScopeConfigInterface |
||
| 37 | */ |
||
| 38 | protected $scopeConfig; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var StoreManagerInterface |
||
| 42 | */ |
||
| 43 | protected $storeManagerInterface; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param ScopeConfigInterface $scopeConfig |
||
| 47 | * @param StoreManagerInterface $storeManagerInterface |
||
| 48 | * @param Json $json |
||
| 49 | */ |
||
| 50 | public function __construct( |
||
| 51 | ScopeConfigInterface $scopeConfig, |
||
| 52 | StoreManagerInterface $storeManagerInterface, |
||
| 53 | Json $json |
||
| 54 | ) { |
||
| 55 | $this->scopeConfig = $scopeConfig; |
||
| 56 | $this->storeManagerInterface = $storeManagerInterface; |
||
| 57 | $this->json = $json; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get Configs Module. |
||
| 62 | * |
||
| 63 | * @param string $field |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getConfigModule(string $field): ?string |
||
| 68 | { |
||
| 69 | $storeId = $this->storeManagerInterface->getStore()->getId(); |
||
| 70 | $configPath = sprintf(self::CONFIG_PATH_GENERAL, $field); |
||
| 71 | |||
| 72 | return $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORE, $storeId); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get Config Field to Classes. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public function getConfigFieldForToClasses(): ?array |
||
| 81 | { |
||
| 82 | $storeId = $this->storeManagerInterface->getStore()->getId(); |
||
| 83 | $fields = $this->scopeConfig->getValue(self::CONFIG_PATH_CLASSES, ScopeInterface::SCOPE_STORE, $storeId); |
||
| 84 | if (is_array($fields)) { |
||
| 85 | return $fields; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $this->json->unserialize($fields); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get Addtional Classes for Field. |
||
| 93 | * |
||
| 94 | * @param string $fieldName |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getAddtionalClassesForField(string $fieldName): ?string |
||
| 99 | { |
||
| 100 | $fieldsToSort = $this->getConfigFieldForToClasses(); |
||
| 101 | foreach ($fieldsToSort as $key => $fields) { |
||
| 102 | if (is_array($fields) && $fields['field'] == $fieldName) { |
||
| 103 | $classSize = $fieldsToSort[$key]['size']; |
||
| 104 | $classBreakLine = $fieldsToSort[$key]['is_break_line'] ? 'break-line' : 'parent-field'; |
||
| 105 | |||
| 106 | return $classSize.' '.$classBreakLine; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | return ''; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get Config Field to Autocomplete. |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function getConfigFieldForToAutocomplete(): ?array |
||
| 119 | { |
||
| 120 | $storeId = $this->storeManagerInterface->getStore()->getId(); |
||
| 121 | $fields = $this->scopeConfig->getValue(self::CONFIG_PATH_AUTOCOMPLETE, ScopeInterface::SCOPE_STORE, $storeId); |
||
| 122 | if (is_array($fields)) { |
||
| 123 | return $fields; |
||
| 124 | } |
||
| 125 | |||
| 126 | return $this->json->unserialize($fields); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get Autocomplete for Field. |
||
| 131 | * |
||
| 132 | * @param string $fieldName |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getAutocompleteForField(string $fieldName): ?string |
||
| 137 | { |
||
| 138 | $fieldsToSort = $this->getConfigFieldForToAutocomplete(); |
||
| 139 | foreach ($fieldsToSort as $key => $fields) { |
||
| 140 | if (is_array($fields) && $fields['field'] == $fieldName) { |
||
| 141 | return $fieldsToSort[$key]['autocomplete']; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return ''; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get Config Field to Placeholder. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getConfigFieldForToPlaceholder(): ?array |
||
| 154 | { |
||
| 155 | $storeId = $this->storeManagerInterface->getStore()->getId(); |
||
| 156 | $fields = $this->scopeConfig->getValue(self::CONFIG_PATH_PLACEHOLDER, ScopeInterface::SCOPE_STORE, $storeId); |
||
| 157 | if (is_array($fields)) { |
||
| 158 | return $fields; |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this->json->unserialize($fields); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get Placeholder for Field. |
||
| 166 | * |
||
| 167 | * @param string $fieldName |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getPlaceholderForField(string $fieldName): ?string |
||
| 172 | { |
||
| 173 | $fieldsToSort = $this->getConfigFieldForToPlaceholder(); |
||
| 174 | foreach ($fieldsToSort as $key => $fields) { |
||
| 175 | if (is_array($fields) && $fields['field'] == $fieldName) { |
||
| 176 | return $fieldsToSort[$key]['placeholder']; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return ''; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get If is Enabled Module. |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isEnabled(): ?bool |
||
| 189 | { |
||
| 190 | return $this->getConfigModule('enabled'); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get If is enabled Classes. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function isEnabledClasses(): ?bool |
||
| 199 | { |
||
| 200 | return $this->getConfigModule('columns/enabled'); |
||
|
0 ignored issues
–
show
|
|||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get If is enabled Autocomplete. |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function isEnabledAutocomplete(): ?bool |
||
| 209 | { |
||
| 210 | return $this->getConfigModule('autocomplete/enabled'); |
||
|
0 ignored issues
–
show
|
|||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get If is enabled Placeholder. |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function isEnabledPlaceholder(): ?bool |
||
| 219 | { |
||
| 220 | return $this->getConfigModule('placeholder/enabled'); |
||
|
0 ignored issues
–
show
|
|||
| 221 | } |
||
| 222 | } |
||
| 223 |