/modules/catalog/lib/update/uiformconfiguration.php

https://gitlab.com/alexprowars/bitrix · PHP · 374 lines · 309 code · 47 blank · 18 comment · 53 complexity · 44d8fbe8b8ad17f54cf09181314245d9 MD5 · raw file

  1. <?php
  2. namespace Bitrix\Catalog\Update;
  3. use Bitrix\UI\Form\EntityEditorConfiguration;
  4. use Bitrix\Ui\Form\EntityEditorConfigScope;
  5. class UiFormConfiguration
  6. {
  7. protected const SET_MODE_FIRST = 'first';
  8. protected const SET_MODE_LAST = 'last';
  9. protected const SET_MODE_BEFORE = 'before';
  10. protected const SET_MODE_AFTER = 'after';
  11. protected const FORM_CATEGORY = 'ui.form.editor';
  12. protected const PRODUCT_FORM_ID = 'CATALOG_PRODUCT_CARD';
  13. public const PARENT_SECTION_MAIN = 'main';
  14. /**
  15. * Returns column validation's result.
  16. *
  17. * @param mixed $config
  18. * @return bool
  19. */
  20. protected static function isValidColumnConfig($config): bool
  21. {
  22. if (empty($config) || !is_array($config))
  23. {
  24. return false;
  25. }
  26. if (empty($config['elements']) || !is_array($config['elements']))
  27. {
  28. return false;
  29. }
  30. return true;
  31. }
  32. /**
  33. * Returns elements validation's result.
  34. *
  35. * @param mixed $config
  36. * @return bool
  37. */
  38. protected static function isValidElementListConfig($config): bool
  39. {
  40. if (empty($config) || !is_array($config))
  41. {
  42. return false;
  43. }
  44. if (
  45. !isset($config['type'])
  46. || (
  47. empty($config['elements'])
  48. || !is_array($config['elements'])
  49. )
  50. )
  51. {
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * Returns row validation's result.
  58. *
  59. * @param mixed $config
  60. * @return bool
  61. */
  62. protected static function isValidRowConfig($config): bool
  63. {
  64. if (empty($config) || !is_array($config))
  65. {
  66. return false;
  67. }
  68. if (!isset($config['name']))
  69. {
  70. return false;
  71. }
  72. return true;
  73. }
  74. protected static function getFieldIndex(array $formSettings, string $fieldName): ?array
  75. {
  76. if (
  77. empty($formSettings)
  78. || $fieldName === ''
  79. )
  80. {
  81. return null;
  82. }
  83. foreach ($formSettings as $columnIndex => $column)
  84. {
  85. if (!static::isValidColumnConfig($column))
  86. {
  87. continue;
  88. }
  89. foreach ($column['elements'] as $listIndex => $list)
  90. {
  91. if (!static::isValidElementListConfig($list))
  92. {
  93. continue;
  94. }
  95. if ($list['type'] !== 'section')
  96. {
  97. continue;
  98. }
  99. foreach ($list['elements'] as $rowIndex => $row)
  100. {
  101. if (!static::isValidRowConfig($row))
  102. {
  103. continue;
  104. }
  105. if ($row['name'] === $fieldName)
  106. {
  107. return [
  108. 'COLUMN' => $columnIndex,
  109. 'LIST' => $listIndex,
  110. 'ROW' => $rowIndex,
  111. ];
  112. }
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. protected static function getListIndex(array $formSettings, string $listName): ?array
  119. {
  120. if (
  121. empty($formSettings)
  122. || $listName === ''
  123. )
  124. {
  125. return null;
  126. }
  127. foreach ($formSettings as $columnIndex => $column)
  128. {
  129. if (!static::isValidColumnConfig($column))
  130. {
  131. continue;
  132. }
  133. foreach ($column['elements'] as $listIndex => $list)
  134. {
  135. if (!static::isValidElementListConfig($list))
  136. {
  137. continue;
  138. }
  139. if ($list['type'] !== 'section')
  140. {
  141. continue;
  142. }
  143. if ($list['name'] === $listName)
  144. {
  145. return [
  146. 'COLUMN' => $columnIndex,
  147. 'LIST' => $listIndex,
  148. 'ROW' => 0,
  149. ];
  150. }
  151. }
  152. }
  153. return null;
  154. }
  155. protected static function checkRowIndex(array $index): bool
  156. {
  157. return (isset($index['COLUMN']) && isset($index['LIST']) && isset($index['ROW']));
  158. }
  159. protected static function isRowExists(array $formSettings, array $index): bool
  160. {
  161. if (empty($formSettings) || !static::checkRowIndex($index))
  162. {
  163. return false;
  164. }
  165. $column = $index['COLUMN'];
  166. $list = $index['LIST'];
  167. $row = $index['ROW'];
  168. if (!isset($formSettings[$column]))
  169. {
  170. return false;
  171. }
  172. if (!isset($formSettings[$column]['elements'][$list]))
  173. {
  174. return false;
  175. }
  176. if (!isset($formSettings[$column]['elements'][$list]['elements'][$row]))
  177. {
  178. return false;
  179. }
  180. return true;
  181. }
  182. protected static function unsetField(array $formSettings, array $index): array
  183. {
  184. if (!static::isRowExists($formSettings, $index))
  185. {
  186. return $formSettings;
  187. }
  188. $column = $index['COLUMN'];
  189. $list = $index['LIST'];
  190. $row = $index['ROW'];
  191. unset($formSettings[$column]['elements'][$list]['elements'][$row]);
  192. $formSettings[$column]['elements'][$list]['elements'] = array_values(
  193. $formSettings[$column]['elements'][$list]['elements']
  194. );
  195. return $formSettings;
  196. }
  197. protected static function replaceField(array $formSettings, array $index, array $field): array
  198. {
  199. if (!static::isRowExists($formSettings, $index))
  200. {
  201. return $formSettings;
  202. }
  203. $column = $index['COLUMN'];
  204. $list = $index['LIST'];
  205. $row = $index['ROW'];
  206. $formSettings[$column]['elements'][$list]['elements'][$row] = $field;
  207. return $formSettings;
  208. }
  209. protected static function setField(array $formSettings, array $index, array $field, string $mode): array
  210. {
  211. if (empty($formSettings) || !static::checkRowIndex($index))
  212. {
  213. return $formSettings;
  214. }
  215. $column = $index['COLUMN'];
  216. $list = $index['LIST'];
  217. $row = $index['ROW'];
  218. if (!isset($formSettings[$column]))
  219. {
  220. return $formSettings;
  221. }
  222. if (!isset($formSettings[$column]['elements'][$list]))
  223. {
  224. return $formSettings;
  225. }
  226. if (
  227. !isset($formSettings[$column]['elements'][$list]['elements'])
  228. || !is_array($formSettings[$column]['elements'][$list]['elements'])
  229. )
  230. {
  231. return $formSettings;
  232. }
  233. switch ($mode)
  234. {
  235. case self::SET_MODE_FIRST:
  236. array_unshift(
  237. $formSettings[$column]['elements'][$list]['elements'],
  238. $field
  239. );
  240. break;
  241. case self::SET_MODE_LAST:
  242. $formSettings[$column]['elements'][$list]['elements'][] = $field;
  243. break;
  244. case self::SET_MODE_BEFORE:
  245. if (static::isRowExists($formSettings, $index))
  246. {
  247. if ($row === 0)
  248. {
  249. array_unshift(
  250. $formSettings[$column]['elements'][$list]['elements'],
  251. $field
  252. );
  253. }
  254. else
  255. {
  256. $before = array_slice($formSettings[$column]['elements'][$list]['elements'], 0, $row);
  257. $before[] = $field;
  258. $after = array_slice($formSettings[$column]['elements'][$list]['elements'], $row);
  259. $formSettings[$column]['elements'][$list]['elements'] = array_merge(
  260. $before,
  261. $after
  262. );
  263. unset($after, $before);
  264. }
  265. }
  266. break;
  267. case self::SET_MODE_AFTER:
  268. if (static::isRowExists($formSettings, $index))
  269. {
  270. if ($row === count($formSettings[$column]['elements'][$list]['elements']))
  271. {
  272. $formSettings[$column]['elements'][$list]['elements'][] = $field;
  273. }
  274. else
  275. {
  276. $before = array_slice($formSettings[$column]['elements'][$list]['elements'], 0, $row + 1);
  277. $before[] = $field;
  278. $after = array_slice($formSettings[$column]['elements'][$list]['elements'], $row + 1);
  279. $formSettings[$column]['elements'][$list]['elements'] = array_merge(
  280. $before,
  281. $after
  282. );
  283. unset($after, $before);
  284. }
  285. }
  286. break;
  287. }
  288. return $formSettings;
  289. }
  290. protected static function getConfiguration(): EntityEditorConfiguration
  291. {
  292. return new EntityEditorConfiguration(self::FORM_CATEGORY);
  293. }
  294. public static function addFormField(array $field, string $parentId): void
  295. {
  296. if (empty($field) || !isset($field['name']) || !is_string($field['name']))
  297. {
  298. return;
  299. }
  300. if ($parentId === '')
  301. {
  302. return;
  303. }
  304. $config = static::getConfiguration();
  305. $formSettings = $config->get(self::PRODUCT_FORM_ID, EntityEditorConfigScope::COMMON);
  306. if (empty($formSettings) || !is_array($formSettings))
  307. {
  308. return;
  309. }
  310. if (static::getFieldIndex($formSettings, $field['name']) !== null)
  311. {
  312. return;
  313. }
  314. $listIndex = static::getListIndex($formSettings, $parentId);
  315. if ($listIndex === null)
  316. {
  317. return;
  318. }
  319. $formSettings = static::setField($formSettings, $listIndex, $field, self::SET_MODE_LAST);
  320. $config->set(
  321. self::PRODUCT_FORM_ID,
  322. $formSettings,
  323. ['scope' => EntityEditorConfigScope::COMMON]
  324. );
  325. unset($config);
  326. }
  327. }