1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
iblockproperty.php
См. документацию.
1<?php
2
5
7{
8 function _Update($ID, $arFields, $bCheckDescription = false)
9 {
10 global $DB;
11 $ID=intval($ID);
12 $rsProperty = $this->GetByID($ID);
13 $arProperty = $rsProperty->Fetch();
14 if(!$arProperty)
15 {
16 $this->LAST_ERROR = $this->FormatNotFoundError($ID);
17 return false;
18 }
19 \Bitrix\Iblock\PropertyIndex\Manager::onPropertyUpdate($arProperty["IBLOCK_ID"], $arProperty, $arFields);
20 if($arProperty["VERSION"]!=2)
21 {
22 return true;
23 }
24 $tableFields = $DB->GetTableFields("b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]);
25 if(is_set($arFields, "MULTIPLE") && $arFields["MULTIPLE"]!=$arProperty["MULTIPLE"])
26 {//MULTIPLE changed
27 if($arFields["MULTIPLE"]=="Y")
28 {//MULTIPLE=Y
29 $strSql = "
30 INSERT INTO b_iblock_element_prop_m".$arProperty["IBLOCK_ID"]."
31 (IBLOCK_ELEMENT_ID, IBLOCK_PROPERTY_ID, VALUE, VALUE_ENUM, VALUE_NUM
32 ".(isset($tableFields["DESCRIPTION_".$arProperty["ID"]])? ", DESCRIPTION": "")."
33 )
34 SELECT
35 IBLOCK_ELEMENT_ID
36 ,".$arProperty["ID"]."
37 ,PROPERTY_".$arProperty["ID"]."
38 ,".($arProperty["PROPERTY_TYPE"]=="S" || $arProperty["PROPERTY_TYPE"]=="s"?
39 "0":
40 "PROPERTY_".$arProperty["ID"])."
41 ,".($arProperty["PROPERTY_TYPE"]=="S" || $arProperty["PROPERTY_TYPE"]=="s"?
42 "0":
43 "PROPERTY_".$arProperty["ID"])."
44 ".(isset($tableFields["DESCRIPTION_".$arProperty["ID"]])? ",DESCRIPTION_".$arProperty["ID"]: "")."
45 FROM
46 b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
47 WHERE
48 PROPERTY_".$arProperty["ID"]." is not null
49 ";
50 if(!$DB->Query($strSql))
51 {
52 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY01");
53 return false;
54 }
55 $strSql = "
56 UPDATE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
57 SET PROPERTY_".$arProperty["ID"]."=null
58 ".(isset($tableFields["DESCRIPTION_".$arProperty["ID"]])? ", DESCRIPTION_".$arProperty["ID"]."=null": "")."
59 ";
60 if(!$DB->Query($strSql))
61 {
62 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY02");
63 return false;
64 }
65 if ($DB->type === 'MYSQL')
66 {
67 $strSql = '
68 ALTER TABLE b_iblock_element_prop_s' . $arProperty['IBLOCK_ID'] . '
69 CHANGE PROPERTY_' . $arProperty['ID'] . ' PROPERTY_' . $arProperty['ID'] . ' ' . self::getLongTextType() . '
70 ';
71 }
72 else
73 {
74 $strSql = '
75 ALTER TABLE b_iblock_element_prop_s' . $arProperty['IBLOCK_ID'] . '
76 ALTER COLUMN PROPERTY_' . $arProperty['ID'] . ' TYPE ' . self::getLongTextType() . '
77 ';
78 }
79 if(!$DB->DDL($strSql))
80 {
81 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY03");
82 return false;
83 }
84 }
85 else
86 {//MULTIPLE=N
87 switch($arFields["PROPERTY_TYPE"])
88 {
89 case "S":
90 $strType = "text";
91 break;
92 case "N":
93 $strType = "numeric(18,4)";
94 break;
95 case "L":
96 case "F":
97 case "G":
98 case "E":
99 $strType = self::getIntegerType();
100 break;
101 default://s - small string
102 $strType = "varchar(255)";
103 }
104 $strSql = "
105 UPDATE b_iblock_element_prop_s" . $arProperty["IBLOCK_ID"] . "
106 SET PROPERTY_" . $arProperty["ID"] . "=null
107 " . (isset($tableFields["DESCRIPTION_".$arProperty["ID"]]) ? ", DESCRIPTION_" . $arProperty["ID"] . "=null": "") . "
108 ";
109 if(!$DB->Query($strSql))
110 {
111 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY05");
112
113 return false;
114 }
115 if ($DB->type === 'MYSQL')
116 {
117 $strSql = "
118 ALTER TABLE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
119 CHANGE PROPERTY_".$arProperty["ID"]." PROPERTY_".$arProperty["ID"]." ".$strType."
120 ";
121 }
122 else
123 {
124 $strSql = "
125 ALTER TABLE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
126 ALTER COLUMN PROPERTY_".$arProperty["ID"]." TYPE ".$strType." USING PROPERTY_".$arProperty["ID"]."::" .$strType. "
127 ";
128 }
129 if(!$DB->DDL($strSql))
130 {
131 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY04");
132 return false;
133 }
134 switch($arFields["PROPERTY_TYPE"])
135 {
136 case "N":
137 case "F":
138 case "G":
139 case "E":
140 $strTrans = "VALUE_NUM";
141 break;
142 case "L":
143 $strTrans = "VALUE_ENUM";
144 break;
145 case "s":
146 $strTrans = "SUBSTRING(VALUE, 0 ,255)";
147 break;
148 default:
149 $strTrans = "VALUE";
150 }
151 $helper = Main\Application::getConnection()->getSqlHelper();
152 $strSql = $helper->prepareCorrelatedUpdate(
153 'b_iblock_element_prop_s' . $arProperty["IBLOCK_ID"],
154 'EL',
155 [
156 'PROPERTY_' . $ID => $strTrans . (isset($tableFields['DESCRIPTION_' . $ID]) ? ',DESCRIPTION_'.$ID.' = DESCRIPTION' : '')
157 ],
158 'b_iblock_element_prop_m' . $arProperty["IBLOCK_ID"] . ' AS EN',
159 'EN.IBLOCK_ELEMENT_ID = EL.IBLOCK_ELEMENT_ID AND EN.IBLOCK_PROPERTY_ID = ' . $ID
160 );
161 if(!$DB->Query($strSql))
162 {
163 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY05");
164 return false;
165 }
166 $strSql = "
167 DELETE FROM
168 b_iblock_element_prop_m".$arProperty["IBLOCK_ID"]."
169 WHERE
170 IBLOCK_PROPERTY_ID = ".$ID."
171 ";
172 if(!$DB->Query($strSql))
173 {
174 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY06");
175 return false;
176 }
177 }
178 }
179 else
180 {//MULTIPLE not changed
181 if(is_set($arFields ,"PROPERTY_TYPE")
182 && $arFields["PROPERTY_TYPE"]!=$arProperty["PROPERTY_TYPE"]
183 && $arProperty["MULTIPLE"]=="N")
184 {
185 switch($arFields["PROPERTY_TYPE"])
186 {
187 case "S":
188 $strType = "text";
189 break;
190 case "N":
191 $strType = "numeric(18,4)";
192 break;
193 case "L":
194 case "F":
195 case "G":
196 case "E":
197 $strType = self::getIntegerType();
198 break;
199 default://s - small string
200 $strType = "varchar(255)";
201 }
202 $strSql = "
203 ALTER TABLE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
204 CHANGE PROPERTY_".$arProperty["ID"]." PROPERTY_".$arProperty["ID"]." ".$strType."
205 ";
206 if(!$DB->DDL($strSql))
207 {
208 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY07");
209 return false;
210 }
211 }
212 }
213
214 if(
215 $bCheckDescription
216 || (
217 array_key_exists("WITH_DESCRIPTION", $arFields)
218 && $arFields["WITH_DESCRIPTION"] != $arProperty["WITH_DESCRIPTION"]
219 )
220 )
221 {//WITH_DESCRIPTION changed
222 if ($arFields["WITH_DESCRIPTION"] == "Y")
223 {//WITH_DESCRIPTION=Y
224 if (!isset($tableFields["DESCRIPTION_".$ID]))
225 {
226 $strSql = "
227 ALTER TABLE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
228 ADD DESCRIPTION_".$ID." varchar(255)
229 ";
230 if (!$DB->DDL($strSql))
231 {
232 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY08");
233 return false;
234 }
235 }
236 }
237 else
238 {//WITH_DESCRIPTION=N
239 if (isset($tableFields["DESCRIPTION_".$ID]))
240 {
241 $rs = $DB->Query("
242 SELECT COUNT(1) CNT
243 FROM b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
244 WHERE DESCRIPTION_".$ID." IS NOT NULL AND DESCRIPTION_".$ID." <> ''
245 ");
246 $ar = $rs->Fetch();
247 if ($ar["CNT"] <= 0)
248 {
249 $strSql = "
250 ALTER TABLE b_iblock_element_prop_s".$arProperty["IBLOCK_ID"]."
251 DROP DESCRIPTION_".$ID."
252 ";
253 if (!$DB->DDL($strSql))
254 {
255 $this->LAST_ERROR = $this->FormatUpdateError($ID, "MY09");
256 return false;
257 }
258 }
259 }
260 }
261 }
262
263 return true;
264 }
265
266 public static function DropColumnSQL($strTable, $arColumns)
267 {
268 global $DB;
269 $tableFields = $DB->GetTableFields($strTable);
270 foreach ($arColumns as $i => $columnName)
271 {
272 if (!isset($tableFields[$columnName]))
273 unset($arColumns[$i]);
274 }
275 if ($arColumns)
276 return array("ALTER TABLE ".$strTable." DROP ".implode(", DROP ", $arColumns));
277 else
278 return array();
279 }
280
281 function _Add($ID, $arFields)
282 {
283 global $DB;
284
285 $ID = (int)$ID;
286 if ($ID <= 0)
287 {
288 return false;
289 }
290
291 $arFields['IBLOCK_ID'] = (int)($arFields['IBLOCK_ID'] ?? 0);
292 if ($arFields['IBLOCK_ID'] <= 0)
293 {
294 return false;
295 }
296
297 $arFields['PROPERTY_TYPE'] ??= PropertyTable::TYPE_STRING;
298 $arFields['MULTIPLE'] ??= 'N';
299 $arFields['WITH_DESCRIPTION'] ??= 'N';
300
301 if ($arFields["MULTIPLE"] === "Y")
302 {
303 $strType = self::getLongTextType();
304 }
305 else
306 {
307 switch($arFields["PROPERTY_TYPE"])
308 {
309 case PropertyTable::TYPE_STRING:
310 $strType = "text";
311 break;
312 case PropertyTable::TYPE_NUMBER:
313 $strType = "numeric(18,4)";
314 break;
315 case PropertyTable::TYPE_LIST:
316 case PropertyTable::TYPE_FILE:
317 case PropertyTable::TYPE_SECTION:
318 case PropertyTable::TYPE_ELEMENT:
319 $strType = self::getIntegerType();
320 break;
321 default://s - small string
322 $strType = "varchar(255)";
323 }
324 }
325 $strSql = "
326 ALTER TABLE b_iblock_element_prop_s".$arFields["IBLOCK_ID"]."
327 ADD PROPERTY_".$ID." ".$strType."
328 ".($arFields["WITH_DESCRIPTION"] == "Y"? ", ADD DESCRIPTION_".$ID." varchar(255)": "")."
329 ";
330
331 return $DB->DDL($strSql, true);
332 }
333
334 private static function getLongTextType(): string
335 {
337 $helper = $connection->getSqlHelper();
338 unset($connection);
339
340 $field = (new Main\ORM\Fields\TextField('TMP'))
341 ->configureLong(true)
342 ;
343
344 return $helper->getColumnTypeByField($field);
345 }
346
347 private static function getIntegerType(?int $size = null): string
348 {
349 $connection = Main\Application::getConnection();
350 $helper = $connection->getSqlHelper();
351 unset($connection);
352
353 $field = (new Main\ORM\Fields\IntegerField('TMP'));
354 if ($size !== null)
355 {
356 $field->configureSize($size);
357 }
358
359 return $helper->getColumnTypeByField($field);
360 }
361}
$connection
Определения actionsdefinitions.php:38
static onPropertyUpdate($iblockId, $propertyOld, $propertyNew)
Определения manager.php:151
static getConnection($name="")
Определения application.php:638
FormatUpdateError($ID, $CODE)
Определения iblockproperty.php:1344
FormatNotFoundError($ID)
Определения iblockproperty.php:1349
static GetByID($ID, $IBLOCK_ID=false, $IBLOCK_CODE=false)
Определения iblockproperty.php:727
_Update($ID, $arFields, $bCheckDescription=false)
Определения iblockproperty.php:8
static DropColumnSQL($strTable, $arColumns)
Определения iblockproperty.php:266
_Add($ID, $arFields)
Определения iblockproperty.php:281
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
if($ajaxMode) $ID
Определения get_user.php:27
global $DB
Определения cron_frame.php:29
is_set($a, $k=false)
Определения tools.php:2133
$ar
Определения options.php:199
$i
Определения factura.php:643
$rs
Определения action.php:82