Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
9
10use Bitrix\Main\Entity\Base as MainEntityBase;
11use Bitrix\Main\Entity\DataManager as MainDataManager;
18
19Loc::loadMessages(__FILE__);
20
25abstract class Base
26{
27 const SEARCH_FIELD_NAME = 'SEARCH_CONTENT';
28
30 protected $errors;
31
33 protected $id;
34
36 protected $data = array();
37
39 protected $user;
40
42 protected $searchBuilder;
43
50 public static function create($id = null)
51 {
52 return new static($id);
53 }
54
60 public function __construct($id = null)
61 {
62 $this->errors = new ErrorCollection();
63 $this->setData($this->getDefaultData());
64
65 if ($id)
66 {
67 $this->load($id);
68 }
69 }
70
76 protected function getDefaultData()
77 {
78 return array();
79 }
80
81
82 protected function filterDataByEntityFields(MainEntityBase $entity, array &$data)
83 {
84 foreach ($data as $key => $value)
85 {
86 if (!$entity->hasField($key))
87 {
88 unset($data[$key]);
89 }
90 }
91 }
92
93
94 protected function filterDataByChanging(array &$data, array $previousData)
95 {
96 foreach ($data as $key => $value)
97 {
98 if (!isset($previousData[$key]))
99 {
100 continue;
101 }
102
103 if ($previousData[$key] !== $data[$key])
104 {
105 continue;
106 }
107
108 unset($data[$key]);
109 }
110
111 return count($data) > 0;
112 }
113
124 protected function saveByEntity(MainEntityBase $entity, $id, array $data, $primary = null)
125 {
127 $className = $entity->getDataClass();
128
129 $primary = $primary ?: $id;
130
131 if($id)
132 {
133 if (array_key_exists('ID', $data))
134 {
135 unset($data['ID']);
136 }
137 $resultDb = $className::update($primary, $data);
138 }
139 else
140 {
141 $resultDb = $className::add($data);
142 $id = $resultDb->getId();
143 }
144
145 if(!$resultDb->isSuccess())
146 {
147 $this->errors->add($resultDb->getErrors());
148 }
149
150 return $id;
151 }
152
161 protected function removeByEntity(MainEntityBase $entity, $primary)
162 {
164 $className = $entity->getDataClass();
165 $result = $className::delete($primary);
166
167 if(!$result->isSuccess())
168 {
169 $this->errors->add($result->getErrors());
170 }
171
172 return !$this->hasErrors();
173 }
174
181 abstract protected function loadData($id);
182
190 abstract protected function saveData($id, array $data);
191
199 protected function copyData($id, array $data = array())
200 {
201 $loadedData = $this->loadData($id);
202 if (!$loadedData)
203 {
204 return false;
205 }
206 unset($loadedData['ID']);
207 $data = $data + $loadedData;
208
209 if (isset($data['FIELDS']))
210 {
211 foreach ($data['FIELDS'] as $index => $field)
212 {
213 if ($field['TYPE'] !== 'file')
214 {
215 continue;
216 }
217
218 if (empty($field['VALUE']))
219 {
220 continue;
221 }
222
223 $values = is_array($field['VALUE']) ? $field['VALUE'] : explode(',', $field['VALUE']);
224 $field['VALUE'] = array();
225 foreach ($values as $fileId)
226 {
227 $copiedFileId = \CFile::copyFile($fileId);
228 if (!$copiedFileId)
229 {
230 continue;
231 }
232
233 $field['VALUE'][] = $copiedFileId;
234 }
235 $field['VALUE'] = implode(',', $field['VALUE']);
236 $data['FIELDS'][$index] = $field;
237 }
238 }
239
240 return $this->saveData(null, $data);
241 }
242
249 public function loadByArray(array $data)
250 {
251 $this->clearErrors();
252 $this->setId((isset($data['ID']) && $data['ID']) ? $data['ID'] : null);
253 $this->setData($this->getDefaultData())->mergeData($data);
254
255 return !$this->hasErrors();
256 }
257
264 public function load($id)
265 {
266 $this->clearErrors();
267 $this->setData($this->getDefaultData());
268
269 if (!$id)
270 {
271 return false;
272 }
273
274 $data = $this->loadData($id);
275 if (!is_array($data))
276 {
277 $data = array();
278 }
279 $this->mergeData($data);
280
281 if (!$this->hasErrors())
282 {
283 $this->id = $id;
284 }
285
286 return !$this->hasErrors();
287 }
288
292 public function save()
293 {
294 $this->clearErrors();
295
296 $id = $this->saveData($this->getId(), $this->getData());
297 if ($id)
298 {
299 $this->setId($id);
300 }
301
302 if (!$this->hasErrors())
303 {
304 $this->saveSearchIndex();
305 }
306
307 return !$this->hasErrors();
308 }
309
315 public function getId()
316 {
317 return $this->id;
318 }
319
326 public function setId($id)
327 {
328 if ($id)
329 {
330 $this->id = $id;
331 }
332
333 return $this;
334 }
335
343 public function set($key, $value)
344 {
345 $this->data[$key] = $value;
346 return $this;
347 }
348
355 public function unsetByKey($key)
356 {
357 unset($this->data[$key]);
358 return $this;
359 }
360
368 public function get($key, $defaultValue = null)
369 {
370 return (isset($this->data[$key]) ? $this->data[$key] : $defaultValue);
371 }
372
379 public function mergeData(array $data)
380 {
381 $this->setData($data + $this->getData());
382 return $this;
383 }
384
391 public function setData(array $data)
392 {
393 $this->data = $data;
394 return $this;
395 }
396
400 public function getData()
401 {
402 return $this->data;
403 }
404
410 public function hasErrors()
411 {
412 return !$this->errors->isEmpty();
413 }
414
418 public function clearErrors()
419 {
420 $this->errors->clear();
421 }
422
429 public function addError($message, $code = null)
430 {
431 $this->errors->setError(new Error($message, $code));
432 }
433
439 public function getErrorCollection()
440 {
441 return $this->errors;
442 }
443
449 public function getErrors()
450 {
451 return $this->errors->toArray();
452 }
453
459 public function getErrorMessages()
460 {
461 $list = array();
462 foreach ($this->errors as $error)
463 {
465 $list[] = $error->getMessage();
466 }
467
468 return $list;
469 }
470
477 public function setUser(Security\User $user = null)
478 {
479 $this->user = $user;
480 return $this;
481 }
482
488 public function getUser()
489 {
490 if (!$this->user)
491 {
492 $this->user = Security\User::current();
493 }
494
495 return $this->user;
496 }
497
503 public static function getDataClass()
504 {
505 return null;
506 }
507
513 public static function getSearchBuilder()
514 {
515 static $builder = null;
516 if ($builder === null && static::getDataClass())
517 {
518 $dataClass = static::getDataClass();
519 $builder = new Search\Builder(
520 $dataClass::getEntity(),
521 static::SEARCH_FIELD_NAME
522 );
523 }
524
525 return $builder;
526 }
527
528
529
535 protected function prepareSearchContent()
536 {
537 return $this;
538 }
539
545 public function saveSearchIndex()
546 {
547 if (!$this->getId())
548 {
549 return false;
550 }
551
552 if (!$this->getSearchBuilder())
553 {
554 return false;
555 }
556
557 if (!$this->getSearchBuilder()->hasField())
558 {
559 return false;
560 }
561
562 $this->getSearchBuilder()->getContent()->clear();
563 $this->prepareSearchContent();
564 return $this->getSearchBuilder()->save($this->getId());
565 }
566}
static loadMessages($file)
Definition loc.php:64
filterDataByChanging(array &$data, array $previousData)
Definition base.php:94
mergeData(array $data)
Definition base.php:379
addError($message, $code=null)
Definition base.php:429
setData(array $data)
Definition base.php:391
loadByArray(array $data)
Definition base.php:249
copyData($id, array $data=array())
Definition base.php:199
setUser(Security\User $user=null)
Definition base.php:477
filterDataByEntityFields(MainEntityBase $entity, array &$data)
Definition base.php:82
__construct($id=null)
Definition base.php:60
static getSearchBuilder()
Definition base.php:513
static create($id=null)
Definition base.php:50
saveData($id, array $data)