Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entitycompatibility.php
1<?php
2
3
5
18use Bitrix\Sale;
19
20abstract class EntityCompatibility
21{
23 protected $query = null;
24
26 protected $filter = array();
27
29 protected $select = array();
30
32 protected $group = null;
33
35 protected $sort = array();
36
38 protected $nav = null;
39
41 protected $queryAliasList = null;
42
44 protected $fields = null;
45
47 protected $rawFields = array();
48
49 const ENTITY_ORDER = 'ORDER';
50 const ENTITY_PAYMENT = 'PAYMENT';
51
52 const ENTITY_ORDER_TABLE = 'b_sale_order';
53 const ENTITY_PAYMENT_TABLE = 'b_sale_order_payment';
54 protected function __construct(array $fields = array())
55 {
56 throw new SystemException('not set construct');
57 }
58
59 protected static function getRegistryType()
60 {
61 throw new NotImplementedException();
62 }
63
64 protected static function getEntity()
65 {
66 throw new NotImplementedException();
67 }
68
73 public static function getAliasFields()
74 {
75 return array();
76 }
77
81 protected static function getSelectFields()
82 {
83 return array();
84 }
85
90 public static function getById($id)
91 {
93 $compatibility = new static();
94 return static::setGetListParameters($compatibility);
95 }
96
106 public static function getList($sort = array(), $filter = array(), $group = null, $nav = array(), $select = array(), $callback = false)
107 {
108 $compatibility = new static();
109 return static::setGetListParameters($compatibility, $sort, $filter, $group, $nav, $select, $callback);
110 }
111
123 protected static function setGetListParameters(EntityCompatibility $compatibility, $sort = array(), $filter = array(), $group = null, $nav = array(), $select = array(), $callback = false)
124 {
125 if (empty($select))
126 {
127 $select = array('*');
128 }
129
130
131 if (!empty($filter))
132 {
133 $compatibility->setFilter($filter);
134 }
135
136 if (!empty($select))
137 {
138 $compatibility->setSelect($select);
139 }
140
141 if ($group !== null)
142 {
143 $compatibility->setGroup($group);
144 }
145
146 if (!empty($sort))
147 {
148 $compatibility->setSort($sort);
149 }
150
151 if (!empty($nav))
152 {
153 $compatibility->setNav($nav);
154 }
155
156 if (!empty($callback))
157 {
158 $compatibility->setCallback($callback);
159 }
160
161 return $compatibility->execute();
162 }
163
168 public static function add(array $fields)
169 {
170 throw new SystemException('not set add');
171 }
172
176 public function setFilter(array $filter = array())
177 {
178
179 $aliasFields = static::getAliasFields();
180 foreach($filter as $fieldName => $fieldValue)
181 {
182 $fieldName = ToUpper($fieldName);
183 $filterMatch = $this->query->explodeFilterKey($fieldName);
184 $fieldClearName = $filterMatch['alias'];
185
186 if (!in_array($fieldClearName, $this->getQueryAliasList()))
187 {
188 if (isset($aliasFields[$fieldClearName]))
189 {
190 $this->addQueryAlias($fieldClearName, $aliasFields[$fieldClearName]);
191 }
192 }
193
194 if ($propKey = $this->parseField($fieldClearName))
195 {
196 $this->addFilter($filterMatch['modifier'].$filterMatch['operator'].$propKey, $fieldValue);
197 }
198 else
199 {
200 if (!$this->checkWhiteListFields($fieldClearName))
201 {
202 continue;
203 }
204
205 $aliasFieldsValue = $aliasFields[$fieldClearName] ?? null;
206 if (!is_array($aliasFieldsValue))
207 {
208 $this->addFilter($fieldName, $fieldValue);
209 }
210 else
211 {
212 $this->addFilterForAlias($aliasFieldsValue, $fieldName, $fieldValue);
213 $this->addSelectForAlias($aliasFieldsValue);
214 }
215 }
216 }
217 }
218
222 public function setSelect(array $select = array())
223 {
224 $aliasFields = static::getAliasFields();
225 if (empty($select))
226 {
227 $select = static::getSelectFields();
228 }
229
230 foreach($select as $key => $fieldName)
231 {
232 if ($fieldName == "*")
233 {
234 unset($select[$key]);
235
236 $select = array_merge($select, static::getSelectFields());
237
238 break;
239 }
240 }
241
242 foreach($select as $fieldName)
243 {
244 $fieldName = mb_strtoupper($fieldName);
245 if (!in_array($fieldName, $this->getQueryAliasList()))
246 {
247 if (isset($aliasFields[$fieldName]))
248 {
249 $this->addQueryAlias($fieldName, $aliasFields[$fieldName]);
250 }
251 }
252
253 if ($propKey = $this->parseField($fieldName))
254 {
255 $this->addSelect($propKey);
256 }
257 else
258 {
259 $aliasFieldsValue = $aliasFields[$fieldName] ?? null;
260 if (!is_array($aliasFieldsValue))
261 {
262 $this->addSelect($fieldName);
263 }
264 else
265 {
266 $this->addSelectForAlias($aliasFieldsValue);
267 }
268 }
269 }
270 }
271
277 public function parseField($key)
278 {
279 return null;
280 }
281
285 public function setGroup($group = null)
286 {
287 if (is_array($group))
288 {
289 if (empty($group))
290 {
291 $this->select = array();
292 $this->group = $group;
293 }
294 else
295 {
296 foreach($group as $fieldName => $fieldValue)
297 {
298 if ($propKey = $this->parseField($fieldName))
299 {
300 $this->group[$propKey] = $fieldValue;
301 }
302 else
303 {
304 $this->group[$fieldName] = $fieldValue;
305 }
306 }
307 }
308 }
309 }
310
314 public function setSort(array $sort = array())
315 {
316 foreach($sort as $fieldName => $fieldValue)
317 {
318 $fieldName = mb_strtoupper($fieldName);
319 if ($propKey = $this->parseField($fieldName))
320 {
321 $this->sort[$propKey] = $fieldValue;
322 }
323 else
324 {
325 $this->sort[$fieldName] = $fieldValue;
326 }
327 }
328 }
329
333 public function setNav(array $nav = array())
334 {
335 $this->nav = $nav;
336 }
337
341 public function setCallback(array $callback)
342 {
343
344 if (($sql = call_user_func_array($callback, array())) && strval(trim($sql)) != '')
345 {
346 $this->query->registerRuntimeField('',
347 new Entity\ExpressionField(
348 '__CALLBACK',
349 '(CASE WHEN ('.$sql.') THEN 1 ELSE 0 END)'
350 )
351 );
352 $this->query->addFilter('=__CALLBACK', 1);
353 }
354 }
355
361 protected function addFilter($name, $value)
362 {
363 if (isset($this->filter[$name]))
364 return false;
365
366 $this->filter[$name] = $value;
367
368 return true;
369 }
370
377 protected function addFilterForAlias(array $aliasList, $name, $value)
378 {
379 $match = $this->query->explodeFilterKey($name);
380 $rule = ($match['modifier']? $match['modifier']:"").($match['operator']? $match['operator']:"");
381
382 $logic = array();
383 foreach ($aliasList as $fieldName => $fieldValue)
384 {
385 $filterName = $rule.$fieldName;
386 $logic[] = array($filterName => $value);
387 }
388
389 if (!empty($logic))
390 {
391 $logic['LOGIC'] = 'OR';
392 $this->query->addFilter(null, $logic);
393 }
394
395 return true;
396 }
397
402 protected function addSelect($name)
403 {
404 if (in_array($name, $this->select))
405 return false;
406
407 $this->select[] = $name;
408
409 return true;
410 }
411
416 protected function addSelectForAlias(array $aliasList)
417 {
418 foreach ($aliasList as $fieldName => $fieldValue)
419 {
420 $this->addSelect($fieldName);
421 }
422
423 return true;
424 }
425
432 protected function addQueryAlias($name, $value = null)
433 {
434 $list = array();
435
436 if ($name == $value)
437 {
438 $value = null;
439 }
440
441 if (!empty($value) && is_array($value))
442 {
443 $list = $value;
444 }
445 else
446 {
447 $list[$name] = $value;
448 }
449
450 foreach ($list as $fieldName => $fieldValue)
451 {
452 if (in_array($fieldName, $this->queryAliasList))
453 {
454 return false;
455 }
456
457 $this->queryAliasList[] = $fieldName;
458 $this->query->addAlias($fieldName, $fieldValue);
459
460 }
461
462 return true;
463 }
464
468 protected function getQueryAliasList()
469 {
470 if ($this->queryAliasList === null)
471 {
472 $this->queryAliasList = array_keys($this->query->getAliases());
473 }
474
476 }
477
481 public function execute()
482 {
484 $result = new Compatible\CDBResult();
485 $this->query->prepare($this->sort, $this->filter, $this->group, $this->select);
486 if ($this->query->counted())
487 {
488 return $this->query->exec()->getSelectedRowsCount();
489 }
490
491 return $this->query->compatibleExec($result, $this->nav);
492 }
493
494
499 public function getField($name)
500 {
501 return $this->fields->get($name);
502 }
503
508 public function setField($name, $value)
509 {
510 $this->fields->set($name, $value);
511 }
512
516 public function getFieldValues()
517 {
518 return $this->fields->getValues();
519 }
520
524 public function setFields(array $values)
525 {
526 $this->fields->resetValues($values);
527 }
528
529
537 protected static function clearFields(array $fields, array $availableFields = array())
538 {
539 if (empty($availableFields))
540 $availableFields = static::getAvailableFields();
541
542 foreach ($fields as $fieldName => $fieldValue)
543 {
544 if (!in_array($fieldName, $availableFields))
545 {
546 unset($fields[$fieldName]);
547 }
548 }
549
550 return $fields;
551 }
552
553
562 public static function convertDateFields(array $fields, array $dateFields = array())
563 {
564 $resultList = array();
565 foreach ($fields as $k => $value)
566 {
567 $resultList[$k] = static::convertDateField($k, $value, $dateFields);
568 }
569 return $resultList;
570 }
571
581 protected static function convertDateField($name, $value, array $dateFields = array())
582 {
583 $key = $name;
584 if (mb_substr($key, 0, 1) == '=')
585 {
586 $key = mb_substr($key, 1);
587 }
588
589 if (!array_key_exists($key, $dateFields))
590 {
591 return $value;
592 }
593
594 $nowDate = Application::getConnection()->getSqlHelper()->getCurrentDateTimeFunction();
595
596 if (!($value instanceof DateTime)
597 && !($value instanceof Date))
598 {
599 if ($value === null)
600 return null;
601
602 if (strval($value) == '')
603 return false;
604
605
606 $setValue = null;
607
608 if (ToLower($value) != ToLower($nowDate))
609 {
610 $setValue = $value;
611 }
612
613 if (ToUpper($dateFields[$key]) == "DATE")
614 {
615 $value = new Date($setValue);
616 }
617 elseif (ToUpper($dateFields[$key]) == "DATETIME")
618 {
619 $value = new DateTime($setValue);
620 }
621 }
622
623 return $value;
624 }
625
632 public static function convertDateFieldsToOldFormat(array $fields)
633 {
634 $resultList = array();
635 foreach ($fields as $k => $value)
636 {
637 $valueString = static::convertDateFieldToOldFormat($value);
638 $resultList[$k] = $valueString;
639 }
640
641 return $resultList;
642 }
643
650 protected static function convertDateFieldToOldFormat($value)
651 {
652 $setValue = $value;
653
654 if (($value instanceof DateTime)
655 || ($value instanceof Date))
656 {
657 $setValue = $value->toString();
658 }
659
660 return $setValue;
661 }
662
671 public static function convertDateFieldToFormat($value, $format)
672 {
673 $setValue = $value;
674
675 if (($value instanceof DateTime)
676 || ($value instanceof Date))
677 {
678 $phpFormat = $value->convertFormatToPhp($format);
679 $setValue = $value->format($phpFormat);
680 $setValue = str_replace(" 00:00:00", "", $setValue);
681 }
682
683 return $setValue;
684 }
685
686
693 protected static function replaceFields(array $fields, array $replace = array())
694 {
695 if (empty($replace))
696 return $fields;
697
698 $replacedFields = $fields;
699
700 foreach ($fields as $name => $value)
701 {
702 if (array_key_exists($name, $replace))
703 {
704 $replacedFields[$replace[$name]] = $replacedFields[$name];
705 unset($replacedFields[$name]);
706 }
707 }
708
709 return $replacedFields;
710 }
711
712
718 public static function getAvailableFields()
719 {
720 return array();
721 }
722
728 protected static function checkEntityName($entityName)
729 {
730 return ($entityName == static::ENTITY_ORDER
731 || $entityName == static::ENTITY_PAYMENT
732 );
733 }
734
743 protected function parseRawFields($entityName, array $fields, array $availableFields = array())
744 {
745 if (!static::checkEntityName($entityName))
746 {
747 throw new ArgumentOutOfRangeException('entityName');
748 }
749
750 if (empty($availableFields))
751 $availableFields = static::getAvailableFields();
752
753 foreach ($fields as $name => $value)
754 {
755 $firstLetter = mb_substr($name, 0, 1);
756 if ($firstLetter == "~" || $firstLetter == "=")
757 {
758 $fieldName = ltrim($name, '=');
759 $fieldName = ltrim($fieldName, '~');
760
761 if (!in_array($fieldName, $availableFields))
762 continue;
763
764 $this->rawFields[$entityName][$firstLetter.$fieldName] = $value;
765 unset($fields[$name]);
766 }
767 }
768
769 return $fields;
770 }
771
772
782 public function saveRawFields($entity, $entityName)
783 {
784 global $DB;
785
786 if (!static::checkEntityName($entityName))
787 {
788 throw new ArgumentOutOfRangeException('entityName');
789 }
790
791 if (empty($this->rawFields[$entityName]) || !is_array($this->rawFields[$entityName]))
792 return new Sale\Result();
793
794 $tableName = null;
795 if ($entityName == static::ENTITY_ORDER)
796 {
798 if (!$entity instanceof Sale\OrderBase)
799 {
800 throw new ObjectNotFoundException('Entity "Order" not found');
801 }
802
803 $tableName = static::ENTITY_ORDER_TABLE;
804 }
805 elseif ($entityName == static::ENTITY_PAYMENT)
806 {
808 if (!$entity instanceof Sale\Payment)
809 {
810 throw new ObjectNotFoundException('Entity "Payment" not found');
811 }
812 $tableName = static::ENTITY_PAYMENT_TABLE;
813 }
814
815 if ($entity->getId() <= 0)
816 {
817 throw new ArgumentNullException('id');
818 }
819
820 if (strval(trim($tableName)) == '')
821 {
822 throw new ArgumentNullException('tableName');
823 }
824
825 $result = new Sale\Result();
826
827 $queryValue = $DB->PrepareUpdate($tableName, $this->rawFields[$entityName]);
828
829 foreach ($this->rawFields[$entityName] as $key => $value)
830 {
831 if (mb_substr($key, 0, 1) != "=")
832 continue;
833
834 if (strval($queryValue) != '')
835 $queryValue .= ", ";
836
837 $queryValue .= mb_substr($key, 1)."=".$value." ";
838 }
839
840 $sql =
841 "UPDATE ".$tableName." SET ".
842 " ".$queryValue." WHERE ID = ".$entity->getId()." ";
843
844 if (!($DB->Query($sql, true, "File: ".__FILE__."<br>Line: ".__LINE__)))
845 {
846 $result->addError(new Sale\ResultError(Loc::getMessage('SALE_COMPATIBLE_'.$entityName.'_RAW_FIELD_UPDATE_ERROR'), 'SALE_'.$entityName.'_COMPATIBLE_RAW_FIELD_UPDATE_ERROR'));
847 return $result;
848 }
849
850 return $result;
851 }
852
861 public static function backRawField($entityName, array $fields, $separator = '=')
862 {
863 if (!static::checkEntityName($entityName))
864 {
865 throw new ArgumentOutOfRangeException('entityName');
866 }
867
868 if (empty($fields))
869 return array();
870
871 foreach($fields as $name => $value)
872 {
873 $fields[$separator.$name] = $value;
874 unset($fields[$name]);
875 }
876
877 return $fields;
878 }
879
880
881 protected function getWhiteListFields()
882 {
883 return array_merge($this->getAvailableFields(), $this->getSelectFields());
884 }
885
891 protected function checkWhiteListFields($fieldName)
892 {
893 $fields = $this->getWhiteListFields();
894 if (!empty($fields))
895 {
896 if (in_array($fieldName, $fields))
897 return true;
898
899 if (mb_strpos($fieldName, 'UF_') === 0)
900 return true;
901 }
902
903 return false;
904 }
905
906
907}
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static convertDateField($name, $value, array $dateFields=array())
static backRawField($entityName, array $fields, $separator='=')
static setGetListParameters(EntityCompatibility $compatibility, $sort=array(), $filter=array(), $group=null, $nav=array(), $select=array(), $callback=false)
static clearFields(array $fields, array $availableFields=array())
static replaceFields(array $fields, array $replace=array())
static convertDateFields(array $fields, array $dateFields=array())
parseRawFields($entityName, array $fields, array $availableFields=array())
static getList($sort=array(), $filter=array(), $group=null, $nav=array(), $select=array(), $callback=false)