Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
nameentity.php
1<?php
9
10use Bitrix\Main;
13
15
16Loc::loadMessages(__FILE__);
17
18abstract class NameEntity extends Entity\DataManager
19{
20 public static function getLanguageFieldName()
21 {
22 return 'LANGUAGE_ID';
23 }
24
25 abstract public static function getReferenceFieldName();
26
34 public static function addMultipleForOwner($primaryOwner, $names = array())
35 {
36 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
37
38 // nothing to connect to, simply exit
39 if(!is_array($names) || empty($names))
40 return;
41
42 $langField = static::getLanguageFieldName();
43 $refField = static::getReferenceFieldName();
44
45 foreach($names as $lid => $name)
46 {
47 $lid = Assert::castTrimLC($lid);
48
49 $empty = true;
50 foreach($name as $arg)
51 {
52 if($arg <> '')
53 {
54 $empty = false;
55 break;
56 }
57 }
58
59 if(!$empty)
60 {
61 $res = static::add(array_merge(
62 array(
63 $langField => $lid,
64 $refField => $primaryOwner
65 ),
66 $name
67 ));
68
69 if(!$res->isSuccess())
70 {
71 throw new Main\SystemException(
72 Loc::getMessage('SALE_LOCATION_NAME_NAME_ENTITY_CANNOT_ADD_NAMES_EXCEPTION').
73 ' ('.
74 implode(
75 ',',
76 $res->getErrorMessages()).
77 ')'
78 );
79 }
80 }
81 }
82
83 $GLOBALS['CACHE_MANAGER']->ClearByTag('sale-location-data');
84 }
85
93 public static function updateMultipleForOwner($primaryOwner, $names)
94 {
95 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
96
97 if(!is_array($names))
98 $names = array();
99
100 $langField = static::getLanguageFieldName();
101 $refField = static::getReferenceFieldName();
102
103 // get already existed name records
104 $res = static::getList(array(
105 'filter' => array($refField => $primaryOwner),
106 'select' => array('ID', $langField)
107 ));
108 $existed = array();
109 while($item = $res->Fetch())
110 $existed[$item[$langField]] = $item['ID'];
111
112 foreach($names as $lid => $name)
113 {
114 $lid = Assert::castTrimLC($lid);
115
116 $empty = true;
117 foreach($name as $arg)
118 {
119 if($arg <> '')
120 {
121 $empty = false;
122 break;
123 }
124 }
125
126 if(!isset($existed[$lid]))
127 {
128 if(!$empty)
129 {
130 $res = static::add(array_merge(
131 array(
132 $langField => $lid,
133 $refField => $primaryOwner
134 ),
135 $name
136 ));
137 if(!$res->isSuccess())
138 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_NAME_NAME_ENTITY_CANNOT_ADD_NAMES_EXCEPTION'));
139 }
140 }
141 else
142 {
143 if($empty)
144 {
145 $res = static::delete($existed[$lid]);
146 if(!$res->isSuccess())
147 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_NAME_NAME_ENTITY_CANNOT_DELETE_NAMES_EXCEPTION'));
148 }
149 else
150 {
151 $res = static::update($existed[$lid], $name);
152 if(!$res->isSuccess())
153 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_NAME_NAME_ENTITY_CANNOT_UPDATE_NAMES_EXCEPTION'));
154 }
155 }
156 }
157 }
158
165 public static function deleteMultipleForOwner($primaryOwner)
166 {
167 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
168
169 // hunt existed
170 $listRes = static::getList(array(
171 'filter' => array(static::getReferenceFieldName() => $primaryOwner),
172 'select' => array('ID')
173 ));
174
175 // kill existed
176 while($item = $listRes->fetch())
177 {
178 $res = static::delete($item['ID']);
179 if(!$res->isSuccess())
180 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_NAME_NAME_ENTITY_CANNOT_DELETE_NAMES_EXCEPTION'));
181 }
182 }
183
191 public static function addAbsentForOwner($primaryOwner, $names, $behaviour = array('TREAT_EMPTY_AS_ABSENT' => true))
192 {
193 $primaryOwner = Assert::expectIntegerPositive($primaryOwner, '$primaryOwner');
194
195 if(!is_array($names))
196 $names = array();
197
198 if(!is_array($behaviour))
199 $behaviour = array();
200 if(!isset($behaviour['TREAT_EMPTY_AS_ABSENT']))
201 $behaviour['TREAT_EMPTY_AS_ABSENT'] = true;
202
203 if(empty($names))
204 return;
205
206 $namesLC = array();
207 foreach($names as $lid => $data)
208 {
209 $namesLC[Assert::castTrimLC($lid)] = $data;
210 }
211 $names = $namesLC;
212
213 $langField = static::getLanguageFieldName();
214 $refField = static::getReferenceFieldName();
215
216 $names2Update = array();
217 $res = static::getList(array('filter' => array('='.$refField => $primaryOwner)));
218 while($item = $res->fetch())
219 {
220 $isEmpty = static::checkEmpty($item);
221
222 if($isEmpty && $behaviour['TREAT_EMPTY_AS_ABSENT'])
223 {
224 $names2Update[$item['ID']] = $names[$item[$langField]];
225 }
226
227 unset($names[$item[$langField]]);
228 }
229
230 foreach($names as $lid => $data)
231 {
232 $data[$langField] = $lid;
233 $data[$refField] = $primaryOwner;
234
235 static::add($data);
236 }
237
238 foreach($names2Update as $id => $data)
239 {
240 static::update($id, $data);
241 }
242 }
243
249 public static function deleteMultipleByParentRangeSql($sql)
250 {
251 if($sql == '')
252 throw new Main\SystemException('Range sql is empty');
253
254 $dbConnection = Main\HttpApplication::getConnection();
255
256 $dbConnection->query('delete from '.static::getTableName().' where '.static::getReferenceFieldName().' in ('.$sql.')');
257 }
258
259 protected static function checkEmpty($item)
260 {
261 return !is_array($item) || (string) $item['NAME'] == '';
262 }
263}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static deleteMultipleForOwner($primaryOwner)
static addMultipleForOwner($primaryOwner, $names=array())
static updateMultipleForOwner($primaryOwner, $names)
static addAbsentForOwner($primaryOwner, $names, $behaviour=array('TREAT_EMPTY_AS_ABSENT'=> true))
$GLOBALS['____1444769544']
Definition license.php:1