1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
measure.php
См. документацию.
1<?php
2
6
11{
12 public const DEFAULT_MEASURE_CODE = 796;
13
14 protected static array $defaultMeasure = [];
15
22 protected static function checkFields($action, &$arFields, $id = 0)
23 {
24 global $APPLICATION;
25
26 $action = mb_strtoupper($action);
27 if ($action != 'ADD' && $action != 'UPDATE')
28 return false;
29 $id = (int)$id;
30 if ($action == 'UPDATE' && $id <= 0)
31 return false;
32
33 if (!isset($arFields['SYMBOL']) && isset($arFields['SYMBOL_RUS']))
34 {
35 $arFields['SYMBOL'] = $arFields['SYMBOL_RUS'];
36 unset($arFields['SYMBOL_RUS']);
37 }
38 $whiteList = array(
39 'CODE' => true,
40 'MEASURE_TITLE' => true,
41 'SYMBOL' => true,
42 'SYMBOL_INTL' => true,
43 'SYMBOL_LETTER_INTL' => true,
44 'IS_DEFAULT' => true
45 );
46
47 $arFields = array_intersect_key($arFields, $whiteList);
48
49 if (array_key_exists('CODE', $arFields))
50 {
51 $code = trim($arFields['CODE']);
52 if ($code === '')
53 {
54 $APPLICATION->ThrowException(Loc::getMessage('CAT_MEASURE_ERR_CODE_IS_ABSENT'));
55 return false;
56 }
57 elseif(preg_match('/^[0-9]+$/', $code) !== 1)
58 {
59 $APPLICATION->ThrowException(Loc::getMessage('CAT_MEASURE_ERR_CODE_IS_BAD'));
60 return false;
61 }
62 else
63 {
64 $arFields['CODE'] = (int)$code;
65 }
66 }
67
68 $cnt = 0;
69 switch ($action)
70 {
71 case 'ADD':
72 if (!isset($arFields['CODE']))
73 return false;
74 $cnt = CCatalogMeasure::getList(array(), array("CODE" => $arFields['CODE']), array());
75 break;
76 case 'UPDATE':
77 if (isset($arFields['CODE']))
78 $cnt = CCatalogMeasure::getList(array(), array("CODE" => $arFields['CODE'], '!ID' => $id), array(), false, array('ID'));
79 break;
80 }
81 if ($cnt > 0)
82 {
83 $APPLICATION->ThrowException(Loc::getMessage('CAT_MEASURE_ERR_CODE_ALREADY_EXISTS'));
84 return false;
85 }
86
87 if (isset($arFields["IS_DEFAULT"]) && $arFields["IS_DEFAULT"] == 'Y')
88 {
89 $filter = array('=IS_DEFAULT' => 'Y');
90 if ($action == 'UPDATE')
91 $filter['!=ID'] = $id;
92 $iterator = Catalog\MeasureTable::getList(array(
93 'select' => array('ID'),
94 'filter' => $filter
95 ));
96 while ($row = $iterator->fetch())
97 {
98 $result = Catalog\MeasureTable::update((int)$row['ID'], array('IS_DEFAULT' => 'N'));
99 if (!$result->isSuccess())
100 return false;
101 }
102 unset($result, $row, $iterator);
103 }
104
105 return true;
106 }
107
115 public static function add($arFields)
116 {
117 if (!static::checkFields('ADD', $arFields))
118 return false;
119
120 if (empty($arFields))
121 return false;
122
123 $id = false;
124 $result = Catalog\MeasureTable::add($arFields);
125 $success = $result->isSuccess();
126 if (!$success)
127 self::convertErrors($result);
128 else
129 $id = (int)$result->getId();
130 unset($success, $result);
131
132 return $id;
133 }
134
143 public static function update($id, $arFields)
144 {
145 $id = (int)$id;
146 if ($id <= 0)
147 return false;
148 if (!static::checkFields('UPDATE', $arFields, $id))
149 return false;
150
151 if (empty($arFields))
152 return $id;
153
154 $result = Catalog\MeasureTable::update($id, $arFields);
155 $success = $result->isSuccess();
156 if (!$success)
157 self::convertErrors($result);
158 unset($result);
159
160 return ($success ? $id : false);
161 }
162
170 public static function delete($id)
171 {
172 $id = (int)$id;
173 if ($id <= 0)
174 return false;
175
176 $result = Catalog\MeasureTable::delete($id);
177 $success = $result->isSuccess();
178 if (!$success)
179 self::convertErrors($result);
180 unset($result);
181
182 return $success;
183 }
184
185 public static function getDefaultMeasure($getStub = false, $getExt = false)
186 {
187 $getStub = ($getStub === true);
188 $getExt = ($getExt === true);
189
190 $cacheId = (string)$getStub . '-' . (string)$getExt;
191
192 if (!isset(self::$defaultMeasure[$cacheId]))
193 {
194 $measureRes = CCatalogMeasure::getList(
195 array(),
196 array('IS_DEFAULT' => 'Y'),
197 false,
198 false,
199 array()
200 );
201 $measure = $measureRes->GetNext(true, $getExt);
202 if ($measure)
203 {
204 $measure['ID'] = (int)$measure['ID'];
205 $measure['CODE'] = (int)$measure['CODE'];
206 self::$defaultMeasure[$cacheId] = $measure;
207 }
208 }
209 if (!isset(self::$defaultMeasure[$cacheId]))
210 {
211 $measureRes = CCatalogMeasure::getList(
212 array(),
213 array('CODE' => self::DEFAULT_MEASURE_CODE),
214 false,
215 false,
216 array()
217 );
218 $measure = $measureRes->GetNext(true, $getExt);
219 if ($measure)
220 {
221 $measure['ID'] = (int)$measure['ID'];
222 $measure['CODE'] = (int)$measure['CODE'];
223 self::$defaultMeasure[$cacheId] = $measure;
224 }
225 }
226 if (!isset(self::$defaultMeasure[$cacheId]))
227 {
228 if ($getStub)
229 {
230 $defaultMeasureDescription = CCatalogMeasureClassifier::getMeasureInfoByCode(
231 self::DEFAULT_MEASURE_CODE
232 );
233 if ($defaultMeasureDescription !== null)
234 {
235 self::$defaultMeasure[$cacheId] = array(
236 'ID' => 0,
237 'CODE' => self::DEFAULT_MEASURE_CODE,
238 'MEASURE_TITLE' => htmlspecialcharsEx($defaultMeasureDescription['MEASURE_TITLE']),
239 'SYMBOL_RUS' => htmlspecialcharsEx($defaultMeasureDescription['SYMBOL_RUS']),
240 'SYMBOL' => htmlspecialcharsEx($defaultMeasureDescription['SYMBOL_RUS']),
241 'SYMBOL_INTL' => htmlspecialcharsEx($defaultMeasureDescription['SYMBOL_INTL']),
242 'SYMBOL_LETTER_INTL' => htmlspecialcharsEx($defaultMeasureDescription['SYMBOL_LETTER_INTL']),
243 'IS_DEFAULT' => 'Y'
244 );
245 if ($getExt)
246 {
247 self::$defaultMeasure[$cacheId]['~ID'] = '0';
248 self::$defaultMeasure[$cacheId]['~CODE'] = (string)self::DEFAULT_MEASURE_CODE;
249 self::$defaultMeasure[$cacheId]['~MEASURE_TITLE'] = $defaultMeasureDescription['MEASURE_TITLE'];
250 self::$defaultMeasure[$cacheId]['~SYMBOL_RUS'] = $defaultMeasureDescription['SYMBOL_RUS'];
251 self::$defaultMeasure[$cacheId]['~SYMBOL'] = $defaultMeasureDescription['SYMBOL_RUS'];
252 self::$defaultMeasure[$cacheId]['~SYMBOL_INTL'] = $defaultMeasureDescription['SYMBOL_INTL'];
253 self::$defaultMeasure[$cacheId]['~SYMBOL_LETTER_INTL'] = $defaultMeasureDescription['SYMBOL_LETTER_INTL'];
254 self::$defaultMeasure[$cacheId]['~IS_DEFAULT'] = 'Y';
255 }
256 }
257 }
258 }
259
260 return self::$defaultMeasure[$cacheId];
261 }
262
263 private static function convertErrors(Main\Entity\Result $result)
264 {
265 global $APPLICATION;
266
267 $oldMessages = array();
268 foreach ($result->getErrorMessages() as $errorText)
269 $oldMessages[] = array('text' => $errorText);
270 unset($errorText);
271
272 if (!empty($oldMessages))
273 {
274 $error = new CAdminException($oldMessages);
275 $APPLICATION->ThrowException($error);
276 unset($error);
277 }
278 unset($oldMessages);
279 }
280}
281
286{
290 public function __construct($res)
291 {
292 parent::__construct($res);
293 }
294
298 function Fetch()
299 {
300 $res = parent::Fetch();
301 if (!empty($res) && isset($res['CODE']))
302 {
303 if (array_key_exists('MEASURE_TITLE', $res) && $res["MEASURE_TITLE"] == '')
304 {
305 $tmpTitle = CCatalogMeasureClassifier::getMeasureTitle($res["CODE"], 'MEASURE_TITLE');
306 $res["MEASURE_TITLE"] = ($tmpTitle == '') ? $res["SYMBOL_INTL"] : $tmpTitle;
307 }
308 if (array_key_exists('SYMBOL_RUS', $res) && $res["SYMBOL_RUS"] == '')
309 {
310 $tmpSymbol = CCatalogMeasureClassifier::getMeasureTitle($res["CODE"], 'SYMBOL_RUS');
311 $res["SYMBOL_RUS"] = ($tmpSymbol == '') ? $res["SYMBOL_INTL"] : $tmpSymbol;
312 }
313 if (array_key_exists('SYMBOL', $res) && $res['SYMBOL'] == '')
314 {
315 $tmpSymbol = CCatalogMeasureClassifier::getMeasureTitle($res["CODE"], 'SYMBOL_RUS');
316 $res["SYMBOL"] = ($tmpSymbol == '') ? $res["SYMBOL_INTL"] : $tmpSymbol;
317 }
318 }
319 return $res;
320 }
321}
global $APPLICATION
Определения include.php:80
static getMessage($code, $replace=null, $language=null)
Определения loc.php:30
Определения measure.php:11
const DEFAULT_MEASURE_CODE
Определения measure.php:12
static array $defaultMeasure
Определения measure.php:14
static update($id, $arFields)
Определения measure.php:143
static checkFields($action, &$arFields, $id=0)
Определения measure.php:22
static getDefaultMeasure($getStub=false, $getExt=false)
Определения measure.php:185
static add($arFields)
Определения measure.php:115
Fetch()
Определения measure.php:298
__construct($res)
Определения measure.php:290
$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
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$success
Определения mail_entry.php:69
htmlspecialcharsEx($str)
Определения tools.php:2685
Определения ufield.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$error
Определения subscription_card_product.php:20
$action
Определения file_dialog.php:21
$iterator
Определения yandex_run.php:610