Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
5
7use Bitrix\Main\Entity\ExpressionField;
8use Bitrix\Main\Entity\Query;
18
19Loc::loadMessages(__FILE__);
20
21final class Manager
22{
23 const DEFAULT_PRESET_DIRECTORY = '/bitrix/modules/sale/handlers/discountpreset/';
24
28 const CATEGORY_OTHER = 7;
29
33 private static $instance;
35 private $presetList;
37 private $restrictedGroupsMode = false;
38
43 public static function getInstance()
44 {
45 if (!isset(self::$instance))
46 {
47 self::$instance = new self;
48 }
49
50 return self::$instance;
51 }
52
57 public function registerAutoLoader()
58 {
59 if (!$this->isAlreadyRegisteredAutoLoader())
60 {
61 \spl_autoload_register(array($this, 'autoLoad'), true);
62 }
63 }
64
65 private function isAlreadyRegisteredAutoLoader()
66 {
67 $autoLoaders = spl_autoload_functions();
68 if(!$autoLoaders)
69 {
70 return false;
71 }
72
73 foreach ($autoLoaders as $autoLoader)
74 {
75 if(!is_array($autoLoader))
76 {
77 continue;
78 }
79
80 list($object, $method) = $autoLoader;
81
82 if ($object instanceof $this)
83 {
84 return true;
85 }
86 }
87
88 return false;
89 }
90
91 private function __construct()
92 {
93 $this->errorCollection = new ErrorCollection;
94
95 $this->registerAutoLoader();
96 }
97
98 private function __clone()
99 {}
100
101 public function enableRestrictedGroupsMode($state)
102 {
103 $this->restrictedGroupsMode = $state === true;
104 }
105
107 {
108 return $this->restrictedGroupsMode;
109 }
110
111 public function autoLoad($className)
112 {
113 $file = ltrim($className, "\\"); // fix web env
114 $file = strtr($file, Loader::ALPHA_UPPER, Loader::ALPHA_LOWER);
115
116 $documentRoot = $documentRoot = rtrim($_SERVER["DOCUMENT_ROOT"], "/\\");
117
118 if(preg_match("#[^\\\\/a-zA-Z0-9_]#", $file))
119 {
120 return;
121 }
122
123 $file = str_replace('\\', '/', $file);
124 $fileParts = explode("/", $file);
125
126 if($fileParts[0] !== "sale" || $fileParts[1] !== "handlers" || $fileParts[2] !== 'discountpreset')
127 {
128 return;
129 }
130 array_shift($fileParts);
131
132 $filePath = $documentRoot . "/bitrix/modules/sale/" . implode("/", $fileParts) . ".php";
133
134 if(file_exists($filePath))
135 {
136 require_once($filePath);
137 }
138 }
139
140 private function buildPresets()
141 {
142 if($this->presetList === null)
143 {
144 $this->presetList = array_filter(
145 array_merge(
146 $this->buildDefaultPresets(),
147 $this->buildCustomPresets()
148 ),
149 function(BasePreset $preset)
150 {
151 return $preset->getPossible();
152 }
153 );
154 }
155
156 return $this;
157 }
158
159 private function buildCustomPresets()
160 {
161 $presetList = array();
162
163 $event = new Event('sale', 'OnSaleDiscountPresetBuildList');
164 $event->send();
165
166 foreach($event->getResults() as $evenResult)
167 {
168 if($evenResult->getType() != EventResult::SUCCESS)
169 {
170 continue;
171 }
172
173 $result = $evenResult->getParameters();
174 if(!is_array($result))
175 {
176 throw new SystemException('Wrong event result by building preset list. Must be array.');
177 }
178
179 foreach($result as $preset)
180 {
181 if(empty($preset['CLASS']))
182 {
183 throw new SystemException('Wrong event result by building preset list. Could not find CLASS.');
184 }
185
186 if(is_string($preset['CLASS']) && class_exists($preset['CLASS']))
187 {
188 $preset = $this->createPresetInstance($preset['CLASS']);
189 if($preset)
190 {
191 $presetList[] = $preset;
192 }
193 }
194 else
195 {
196 throw new SystemException("Wrong event result by building preset list. Could not find class by CLASS {$preset['CLASS']}");
197 }
198 }
199 }
200
201 return $presetList;
202 }
203
204 private function buildDefaultPresets()
205 {
206 $documentRoot = Application::getDocumentRoot();
207
208 if(!Directory::isDirectoryExists($documentRoot . self::DEFAULT_PRESET_DIRECTORY))
209 {
210 throw new SystemException('Could not find folder with default presets. ' . self::DEFAULT_PRESET_DIRECTORY);
211 }
212
213 $defaultList = array();
214 $directory = new Directory($documentRoot . self::DEFAULT_PRESET_DIRECTORY);
215 foreach($directory->getChildren() as $presetFile)
216 {
217 if(!$presetFile->isFile() || !$presetFile->getName())
218 {
219 continue;
220 }
221
222 $className = $this->getClassNameFromPath($presetFile->getPath());
223 if($className)
224 {
225 $preset = $this->createPresetInstance($className);
226 if($preset)
227 {
228 $defaultList[] = $preset;
229 }
230 }
231 }
232
233 return $defaultList;
234 }
235
240 private function createPresetInstance($className)
241 {
242 try
243 {
244 $class = new \ReflectionClass($className);
245
247 $instance = $class->newInstanceArgs([]);
248 $instance->enableRestrictedGroupsMode($this->isRestrictedGroupsModeEnabled());
249
250 return $instance;
251 }
252 catch (\ReflectionException $exception)
253 {
254 }
255
256 return null;
257 }
258
259 private function getClassNameFromPath($path)
260 {
261 return "Sale\\Handlers\\DiscountPreset\\" . getFileNameWithoutExtension($path);
262 }
263
269 public function getPresets()
270 {
271 return $this->buildPresets()->presetList;
272 }
273
280 public function getPresetById($id)
281 {
282 if(class_exists($id))
283 {
284 return $this->createPresetInstance($id);
285 }
286 else
287 {
288 foreach($this->buildPresets()->presetList as $preset)
289 {
290 if($preset::className() === $id)
291 {
292 return $preset;
293 }
294 }
295 }
296
297 return null;
298 }
299
304 public function getPresetsByCategory($category)
305 {
306 $presets = array();
307 foreach($this->getPresets() as $preset)
308 {
309 if($preset->getCategory() === $category)
310 {
311 $presets[] = $preset;
312 }
313 }
314
315 uasort(
316 $presets,
317 static function(BasePreset $a, BasePreset $b): int
318 {
319 $aSort = (int)$a->getSort();
320 $bSort = (int)$b->getSort();
321 if ($aSort === $bSort) {
322 return 0;
323 }
324 return ($aSort < $bSort) ? -1 : 1;
325 }
326 );
327
328 return $presets;
329 }
330
331 public function getCategoryList()
332 {
333 return array(
334 self::CATEGORY_PRODUCTS => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_PRODUCTS'),
335 self::CATEGORY_PAYMENT => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_PAYMENT'),
336 self::CATEGORY_DELIVERY => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_DELIVERY'),
337 self::CATEGORY_OTHER => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_OTHER'),
338 );
339 }
340
341 public function getCategoryName($category)
342 {
343 $categoryList = $this->getCategoryList();
344
345 return isset($categoryList[$category])? $categoryList[$category] : '';
346 }
347
348 public function hasCreatedDiscounts(BasePreset $preset)
349 {
350 $countQuery = new Query(DiscountTable::getEntity());
351 $countQuery->addSelect(new ExpressionField('CNT', 'COUNT(1)'));
352 $countQuery->setFilter(array(
353 '=PRESET_ID' => $preset::className(),
354 ));
355 $totalCount = $countQuery->setLimit(null)->setOffset(null)->exec()->fetch();
356
357 return (bool)$totalCount['CNT'];
358 }
359}
static isDirectoryExists($path)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
hasCreatedDiscounts(BasePreset $preset)
Definition manager.php:348