Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
defaultsite.php
1<?php
9
10use Bitrix\Main;
13
15
16Loc::loadMessages(__FILE__);
17
34class DefaultSiteTable extends Entity\DataManager
35{
36 public static function getFilePath()
37 {
38 return __FILE__;
39 }
40
41 public static function getTableName()
42 {
43 return 'b_sale_loc_def2site';
44 }
45
49 public static function addMultipleForOwner($siteId, $locationCodeList = array())
50 {
51 $siteId = static::checkSiteId($siteId);
52
53 $existed = array();
54 $res = self::getList(array('filter' => array('SITE_ID' => $siteId)));
55 while($item = $res->fetch())
56 $existed[$item['LOCATION_CODE']] = true;
57
58 if(is_array($locationCodeList))
59 {
60 foreach($locationCodeList as $location)
61 {
62 if(!isset($existed[$location]))
63 {
64 $opRes = self::add(array(
65 'SITE_ID' => $siteId,
66 'LOCATION_CODE' => $location['LOCATION_CODE'],
67 'SORT' => $location['SORT'],
68 ));
69 if(!$opRes->isSuccess())
70 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_CANNOT_ADD_EXCEPTION'));
71 }
72 }
73 }
74 else
75 throw new Main\SystemException('Code list is not a valid array');
76
77 $GLOBALS['CACHE_MANAGER']->ClearByTag('sale-location-data');
78 }
79
83 public static function updateMultipleForOwner($siteId, $locationCodeList = array(), $behaviour = array('REMOVE_ABSENT' => true))
84 {
85 $siteId = static::checkSiteId($siteId);
86
87 if(!is_array($locationCodeList))
88 throw new Main\SystemException('Code list is not a valid array');
89
90 // throw away duplicates and make index array
91 $index = array();
92 $locationCodeListTemp = array();
93 foreach($locationCodeList as $location)
94 {
95 $index[$location['LOCATION_CODE']] = true;
96 $locationCodeListTemp[$location['LOCATION_CODE']] = $location;
97 }
98 $locationCodeList = $locationCodeListTemp;
99
100 $res = self::getList(array('filter' => array('SITE_ID' => $siteId)));
101 $update = array();
102 $delete = array();
103 while($item = $res->Fetch())
104 {
105 if(!isset($index[$item['LOCATION_CODE']]))
106 $delete[$item['LOCATION_CODE']] = true;
107 else
108 {
109 unset($index[$item['LOCATION_CODE']]);
110 $update[$item['LOCATION_CODE']] = true;
111 }
112 }
113
114 if($behaviour['REMOVE_ABSENT'])
115 {
116 foreach($delete as $code => $void)
117 {
118 $res = self::delete(array('SITE_ID' => $siteId, 'LOCATION_CODE' => $code));
119 if(!$res->isSuccess())
120 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_CANNOT_DELETE_EXCEPTION'));
121 }
122 }
123
124 foreach($update as $code => $void)
125 {
126 $res = self::update(array('SITE_ID' => $siteId, 'LOCATION_CODE' => $code), array('SORT' => $locationCodeList[$code]['SORT']));
127 if(!$res->isSuccess())
128 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_CANNOT_UPDATE_EXCEPTION'));
129 }
130
131 foreach($index as $code => $void)
132 {
133 $res = self::add(array(
134 'SORT' => $locationCodeList[$code]['SORT'],
135 'SITE_ID' => $siteId,
136 'LOCATION_CODE' => $code
137 ));
138 if(!$res->isSuccess())
139 throw new Main\SystemException(Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_CANNOT_ADD_EXCEPTION'));
140 }
141
142 $GLOBALS['CACHE_MANAGER']->ClearByTag('sale-location-data');
143 }
144
145 private static function checkSiteId($siteId)
146 {
147 $siteId = Assert::expectStringNotNull($siteId, '$siteId');
148
149 $res = Main\SiteTable::getList(array('filter' => array('LID' => $siteId)))->fetch();
150 if(!$res)
151 throw new Main\ArgumentOutOfRangeException(Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_SITE_ID_UNKNOWN_EXCEPTION'));
152
153 return $siteId;
154 }
155
156 public static function getMap()
157 {
158 return array(
159
160 'SORT' => array(
161 'data_type' => 'integer',
162 'title' => Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_SORT_FIELD'),
163 'default_value' => '100'
164 ),
165 'LOCATION_CODE' => array(
166 'data_type' => 'string',
167 'required' => true,
168 'primary' => true,
169 'title' => Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_LOCATION_ID_FIELD')
170 ),
171 'SITE_ID' => array(
172 'data_type' => 'string',
173 'required' => true,
174 'primary' => true,
175 'title' => Loc::getMessage('SALE_LOCATION_DEFAULTSITE_ENTITY_SITE_ID_FIELD')
176 ),
177
178 // virtual
179 'LOCATION' => array(
180 'data_type' => '\Bitrix\Sale\Location\Location',
181 'reference' => array(
182 '=this.LOCATION_CODE' => 'ref.CODE'
183 )
184 ),
185
186 'SITE' => array(
187 'data_type' => '\Bitrix\Main\Site',
188 'reference' => array(
189 '=this.SITE_ID' => 'ref.LID'
190 )
191 )
192 );
193 }
194}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static addMultipleForOwner($siteId, $locationCodeList=array())
static updateMultipleForOwner($siteId, $locationCodeList=array(), $behaviour=array('REMOVE_ABSENT'=> true))
$GLOBALS['____1444769544']
Definition license.php:1