Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productform.php
1<?php
2
4
12
13class ProductForm extends Engine\Controller
14{
15 protected function getDefaultPreFilters()
16 {
17 return array_merge(
18 parent::getDefaultPreFilters(),
19 [
20 new ActionFilter\HttpMethod([ActionFilter\HttpMethod::METHOD_POST]),
21 new ActionFilter\Scope(ActionFilter\Scope::AJAX),
22 ]
23 );
24 }
25
26 public function setConfigAction($configName, $value): void
27 {
28 $formConfigs = [
29 'showTaxBlock', 'showDiscountBlock'
30 ];
31 if (in_array($configName, $formConfigs, true))
32 {
33 $value = ($value === 'N') ? 'N' : 'Y';
34 \CUserOptions::SetOption("catalog.product-form", $configName, $value);
35 }
36 }
37
38 public function createBrandAction(array $fields): ?array
39 {
40 $iblockId = (int)$fields['iblockId'];
41 $name = $fields['name'];
42
43 if (empty($name))
44 {
45 $this->addError(new Error("Empty name"));
46
47 return null;
48 }
49
50 if (!Loader::includeModule('highloadblock') || !Loader::includeModule('iblock'))
51 {
52 $this->addError(new Error("Modules is not included"));
53
54 return null;
55 }
56
57 if (!\CIBlockSectionRights::UserHasRightTo($iblockId, 0, 'section_element_bind'))
58 {
59 $this->addError(new Error("User has no permissions to create product"));
60
61 return null;
62 }
63
64 $propertySettings = PropertyTable::getList([
65 'select' => ['ID', 'USER_TYPE_SETTINGS'],
66 'filter' => [
67 '=IBLOCK_ID' => $iblockId,
68 '=ACTIVE' => 'Y',
69 '=CODE' => 'BRAND_FOR_FACEBOOK',
70 ],
71 'limit' => 1,
72 ])
73 ->fetch()
74 ;
75
76 if (!$propertySettings)
77 {
78 return null;
79 }
80
81 $propertySettings['USER_TYPE_SETTINGS'] = (
82 $userTypeSettings = CheckSerializedData($propertySettings['USER_TYPE_SETTINGS'])
83 ? unserialize($propertySettings['USER_TYPE_SETTINGS'], ['allowed_classes' => false])
84 : array()
85 );
86
87 if (empty($userTypeSettings['TABLE_NAME']))
88 {
89 return null;
90 }
91
92 $table = HL\HighloadBlockTable::getList(
93 array(
94 'select' => array('TABLE_NAME', 'NAME', 'ID'),
95 'filter' => array('=TABLE_NAME' => $userTypeSettings['TABLE_NAME'])
96 )
97 )->fetch();
98
99 $xmlId = Random::getString(16);
100 $brandEntity = HL\HighloadBlockTable::compileEntity($table);
101 $brandEntityClass = $brandEntity->getDataClass();
102 $resultAdd = $brandEntityClass::add([
103 'UF_NAME' => $name,
104 'UF_XML_ID' => $xmlId,
105 ]);
106
107 if (!$resultAdd->isSuccess())
108 {
109 $this->addErrors($resultAdd->getErrors());
110
111 return null;
112 }
113
114 return [
115 'id' => $xmlId,
116 ];
117 }
118}