1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ProductInternalService.php
См. документацию.
1<?php
2
4
12
14{
15 private Result $result;
16
17 public function __construct(
18 private readonly bool $shouldRegisterEvents = false,
19 )
20 {
21 }
22
23 public function isShouldRegisterEvents(): bool
24 {
25 return $this->shouldRegisterEvents;
26 }
27
28 public function update(int $id, array $fields): Result
29 {
30 $this->result = new Result();
31
32 $propertyFields = $fields['PROPERTY_VALUES'] ?? [];
33
35
36 $elementFields = !empty($propertyFields)
37 ? array_merge($elementFields, ['PROPERTY_VALUES' => $propertyFields])
38 : $elementFields
39 ;
40
41 if (!empty($elementFields))
42 {
43 $element = new \CIBlockElement();
44 $res = $element->update($id, $elementFields);
45
46 if (!$res)
47 {
48 $this->result->addError(new Error($element->getLastError()));
49 }
50 }
51
52 if ($this->result->isSuccess())
53 {
55
56 if (!empty($productFields))
57 {
59
60 if (!$res->isSuccess())
61 {
62 $this->result->addErrors($res->getErrors());
63 }
64 }
65 }
66
67 $this->processAfterUpdate($id, $fields);
68
69 return $this->result;
70 }
71
72 private function processAfterUpdate(int $id, array $fields): void
73 {
74 if ($this->result->isSuccess())
75 {
76 $ipropValues = new ElementValues(
77 \CIBlockElement::GetIBlockByID($id),
78 $id
79 );
80
81 $ipropValues->clearValues();
82
83 unset($ipropValues);
84 }
85
86 if ($this->isShouldRegisterEvents())
87 {
88 Event::send(
89 Event::ENTITY_PRODUCT,
90 Event::METHOD_UPDATE,
91 Event::STAGE_AFTER,
92 [
93 'id' => $id,
94 ],
95 );
96 }
97 }
98
99 public function add(array $fields): Result
100 {
101 $this->result = new Result();
102
103 $propertyFields = $fields['PROPERTY_VALUES'] ?? [];
104
106
107 $elementFields = !empty($propertyFields)
108 ? array_merge($elementFields, ['PROPERTY_VALUES' => $propertyFields])
109 : $elementFields
110 ;
111
112 if (!empty($elementFields))
113 {
114 $element = new \CIBlockElement();
115 $id = $element->add($elementFields);
116
117 if ($id)
118 {
119 $this->result->setData(['ID' => $id]);
120 }
121 else
122 {
123 $this->result->addError(new Error($element->getLastError()));
124 }
125 }
126
127 if ($this->result->isSuccess())
128 {
130
131 $productFields['ID'] = $this->result->getData()['ID'];
133 'fields' => $productFields,
134 'external_fields' => [
135 'IBLOCK_ID' => $elementFields['IBLOCK_ID'],
136 ],
137 ]);
138
139 if (!$res->isSuccess())
140 {
141 $this->result->addErrors($res->getErrors());
142 }
143
144 }
145
146 return $this->result;
147 }
148
149 public static function prepareElementFields(array $fields): array
150 {
151 if (array_key_exists('ACTIVE_FROM', $fields) && $fields['ACTIVE_FROM'] === null)
152 {
153 $fields['ACTIVE_FROM'] = false;
154 }
155
156 if (array_key_exists('ACTIVE_TO', $fields) && $fields['ACTIVE_TO'] === null)
157 {
158 $fields['ACTIVE_TO'] = false;
159 }
160
161 if (!array_key_exists('MODIFIED_BY', $fields))
162 {
163 global $USER;
164 if (isset($USER) && $USER instanceof \CUser)
165 {
166 $fields['MODIFIED_BY'] = $USER->getID();
167 }
168 }
169
170 return array_intersect_key($fields, ElementTable::getMap());
171 }
172
173 public static function prepareProductFields(array $fields): array
174 {
175 $catalogFields = array_intersect_key(
176 $fields,
177 array_fill_keys(
179 true
180 )
181 );
182
183 if (isset($catalogFields['TIMESTAMP_X']))
184 {
185 $catalogFields['TIMESTAMP_X'] = new DateTime($catalogFields['TIMESTAMP_X']);
186 }
187
188 if (isset($catalogFields['TYPE']))
189 {
190 $catalogFields['TYPE'] = (int)$catalogFields['TYPE'];
191 }
192
193 return $catalogFields;
194 }
195}
if($strVal !='') $productFields
Определения options.php:1791
const FIELDS_ALL
Определения entity.php:19
static getTabletFieldNames(int $fields=self::FIELDS_MAIN)
Определения entity.php:435
__construct(private readonly bool $shouldRegisterEvents=false,)
Определения ProductInternalService.php:17
static getMap()
Определения elementtable.php:93
Определения error.php:15
Определения user.php:6037
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
global $USER
Определения csv_new_run.php:40
Определения basket.php:2
$propertyFields
Определения yandex_run.php:558
$fields
Определения yandex_run.php:501