Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usergrouprestriction.php
1<?php
3
10
11Loc::loadMessages(__FILE__);
12
19{
20 public static $easeSort = 200;
21
25 public static function getClassTitle()
26 {
27 return Loc::getMessage("SALE_SRV_RSTR_BY_UG_NAME");
28 }
29
33 public static function getClassDescription()
34 {
35 return Loc::getMessage("SALE_SRV_RSTR_BY_UG_DESC");
36 }
37
38 public static function getOnApplyErrorMessage(): string
39 {
40 return Loc::getMessage('SALE_SRV_RSTR_BY_UG_ON_APPLY_ERROR_MSG');
41 }
42
49 public static function check($groups, array $restrictionParams, $entityId = 0)
50 {
51 if(intval($entityId) <= 0)
52 return true;
53
54 if(empty($groups) <= 0)
55 return false;
56
57 $commonGroups = array_intersect($groups, $restrictionParams['GROUP_IDS']);
58 return !empty($commonGroups);
59 }
60
66 protected static function getOrder(Entity $entity)
67 {
68 throw new NotImplementedException('Method '.__METHOD__.' must be overload');
69 }
70
75 protected static function getEntityTypeId()
76 {
77 throw new NotImplementedException('Method '.__METHOD__.' must be overload');
78 }
79
84 protected static function extractParams(Entity $entity)
85 {
86 $result = [];
87
88 if($order = static::getOrder($entity))
89 {
90 $result = \Bitrix\Main\UserTable::getUserGroupIds($order->getUserId());
91 }
92
93 return $result;
94 }
95
99 protected static function prepareParamsForSaving(array $params = array(), $entityId = 0)
100 {
101 $entityId = (int)$entityId;
102 $entityTypeId = (int)static::getEntityTypeId();
103 UserGroupRestrictionTable::deleteByEntity($entityTypeId, $entityId);
104
105 if(is_array($params['GROUP_IDS']) && !empty($params['GROUP_IDS']))
106 {
107 foreach($params['GROUP_IDS'] as $groupId)
108 {
110 'ENTITY_TYPE_ID' => $entityTypeId,
111 'ENTITY_ID' => $entityId,
112 'GROUP_ID' => (int)$groupId
113 ]);
114 }
115 }
116
117 return [];
118 }
119
120 protected static function getUserGroups()
121 {
122 $result = [];
123 $res = GroupTable::getList([
124 'filter' => ['ACTIVE' => 'Y'],
125 'order' => ['NAME' => 'ASC']
126 ]);
127
128 while($group = $res->fetch())
129 {
130 $result[$group['ID']] = $group['NAME'];
131 }
132
133 return $result;
134 }
135
136 public static function getParamsStructure($entityId = 0)
137 {
138 return array(
139 "GROUP_IDS" => array(
140 "TYPE" => "ENUM",
141 'MULTIPLE' => 'Y',
142 "LABEL" => Loc::getMessage("SALE_SRV_RSTR_BY_UG_LIST"),
143 "OPTIONS" => static::getUserGroups()
144 )
145 );
146 }
147
148 public static function prepareParamsValues(array $paramsValues, $entityId = 0)
149 {
150 $result = [];
151
152 $res = UserGroupRestrictionTable::getList(['filter' => [
153 '=ENTITY_TYPE_ID' => static::getEntityTypeId(),
154 '=ENTITY_ID' => $entityId
155 ]]);
156
157 while($row = $res->fetch())
158 {
159 $result[] = $row['GROUP_ID'];
160 }
161
162 return array("GROUP_IDS" => $result);
163 }
164
165 public static function save(array $fields, $restrictionId = 0)
166 {
167 $fields["PARAMS"] = static::prepareParamsForSaving($fields["PARAMS"], $fields["SERVICE_ID"]);
168 return parent::save($fields, $restrictionId);
169 }
170
171 public static function delete($restrictionId, $entityId = 0)
172 {
173 UserGroupRestrictionTable::deleteByEntity(static::getEntityTypeId(), $entityId);
174 return parent::delete($restrictionId);
175 }
176
182 public static function filterServicesArray(Entity $entity, array $restrictionFields)
183 {
184 if(empty($restrictionFields))
185 return [];
186
187 $groups = static::extractParams($entity);
188
189 if(empty($groups))
190 {
191 return [];
192 }
193
194 $entityIds = array_keys($restrictionFields);
195
197 'filter' => array(
198 '=ENTITY_TYPE_ID' => static::getEntityTypeId(),
199 '=ENTITY_ID' => $entityIds,
200 '=GROUP_ID' => $groups
201 )
202 ));
203
204 $result = [];
205
206 while($row = $res->fetch())
207 {
208 if(!isset($result[$row['ENTITY_ID']]))
209 {
210 $result[$row['ENTITY_ID']] = true;
211 }
212 }
213
214 return array_keys($result);
215 }
216
217 public static function isAvailable()
218 {
219 return true;
220 }
221}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static prepareParamsValues(array $paramsValues, $entityId=0)
static prepareParamsForSaving(array $params=array(), $entityId=0)
static save(array $fields, $restrictionId=0)
static filterServicesArray(Entity $entity, array $restrictionFields)
static check($groups, array $restrictionParams, $entityId=0)