1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
manager.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Discount\Preset;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\ErrorCollection;
7use Bitrix\Main\Event;
8use Bitrix\Main\EventResult;
9use Bitrix\Main\IO\Directory;
10use Bitrix\Main\Loader;
11use Bitrix\Main\Localization\Loc;
12use Bitrix\Main\SystemException;
13use Bitrix\Sale\Internals\DiscountTable;
14
15final class Manager
16{
17 const DEFAULT_PRESET_DIRECTORY = '/bitrix/modules/sale/handlers/discountpreset/';
18
22 const CATEGORY_OTHER = 7;
23
27 private static Manager $instance;
29 private array $presetList;
31 private bool $restrictedGroupsMode = false;
32
37 public static function getInstance(): Manager
38 {
39 if (!isset(self::$instance))
40 {
41 self::$instance = new self;
42 }
43
44 return self::$instance;
45 }
46
51 public function registerAutoLoader()
52 {
53 if (!$this->isAlreadyRegisteredAutoLoader())
54 {
55 \spl_autoload_register([$this, 'autoLoad'], true);
56 }
57 }
58
59 private function isAlreadyRegisteredAutoLoader()
60 {
61 $autoLoaders = spl_autoload_functions();
62 if(!$autoLoaders)
63 {
64 return false;
65 }
66
67 foreach ($autoLoaders as $autoLoader)
68 {
69 if(!is_array($autoLoader))
70 {
71 continue;
72 }
73
74 list($object, $method) = $autoLoader;
75
76 if ($object instanceof $this)
77 {
78 return true;
79 }
80 }
81
82 return false;
83 }
84
85 private function __construct()
86 {
87 $this->errorCollection = new ErrorCollection;
88
89 $this->registerAutoLoader();
90 }
91
92 private function __clone()
93 {}
94
95 public function enableRestrictedGroupsMode($state)
96 {
97 $this->restrictedGroupsMode = $state === true;
98 }
99
101 {
102 return $this->restrictedGroupsMode;
103 }
104
105 public function autoLoad($className)
106 {
107 $file = ltrim($className, "\\"); // fix web env
108 $file = strtr($file, Loader::ALPHA_UPPER, Loader::ALPHA_LOWER);
109
110 $documentRoot = $documentRoot = rtrim($_SERVER["DOCUMENT_ROOT"], "/\\");
111
112 if(preg_match("#[^\\\\/a-zA-Z0-9_]#", $file))
113 {
114 return;
115 }
116
117 $file = str_replace('\\', '/', $file);
118 $fileParts = explode("/", $file);
119
120 if($fileParts[0] !== "sale" || $fileParts[1] !== "handlers" || $fileParts[2] !== 'discountpreset')
121 {
122 return;
123 }
124 array_shift($fileParts);
125
126 $filePath = $documentRoot . "/bitrix/modules/sale/" . implode("/", $fileParts) . ".php";
127
128 if(file_exists($filePath))
129 {
130 require_once($filePath);
131 }
132 }
133
134 private function buildPresets()
135 {
136 if (!isset($this->presetList))
137 {
138 $this->presetList = array_filter(
139 array_merge(
140 $this->buildDefaultPresets(),
141 $this->buildCustomPresets()
142 ),
143 function(BasePreset $preset)
144 {
145 return $preset->getPossible();
146 }
147 );
148 }
149
150 return $this;
151 }
152
153 private function buildCustomPresets()
154 {
155 $presetList = array();
156
157 $event = new Event('sale', 'OnSaleDiscountPresetBuildList');
158 $event->send();
159
160 foreach($event->getResults() as $evenResult)
161 {
162 if($evenResult->getType() != EventResult::SUCCESS)
163 {
164 continue;
165 }
166
167 $result = $evenResult->getParameters();
168 if(!is_array($result))
169 {
170 throw new SystemException('Wrong event result by building preset list. Must be array.');
171 }
172
173 foreach($result as $preset)
174 {
175 if(empty($preset['CLASS']))
176 {
177 throw new SystemException('Wrong event result by building preset list. Could not find CLASS.');
178 }
179
180 if(is_string($preset['CLASS']) && class_exists($preset['CLASS']))
181 {
182 $preset = $this->createPresetInstance($preset['CLASS']);
183 if($preset)
184 {
185 $presetList[] = $preset;
186 }
187 }
188 else
189 {
190 throw new SystemException("Wrong event result by building preset list. Could not find class by CLASS {$preset['CLASS']}");
191 }
192 }
193 }
194
195 return $presetList;
196 }
197
198 private function buildDefaultPresets()
199 {
200 $documentRoot = Application::getDocumentRoot();
201
202 if(!Directory::isDirectoryExists($documentRoot . self::DEFAULT_PRESET_DIRECTORY))
203 {
204 throw new SystemException('Could not find folder with default presets. ' . self::DEFAULT_PRESET_DIRECTORY);
205 }
206
207 $defaultList = array();
208 $directory = new Directory($documentRoot . self::DEFAULT_PRESET_DIRECTORY);
209 foreach($directory->getChildren() as $presetFile)
210 {
211 if(!$presetFile->isFile() || !$presetFile->getName())
212 {
213 continue;
214 }
215
216 $className = $this->getClassNameFromPath($presetFile->getPath());
217 if($className)
218 {
219 $preset = $this->createPresetInstance($className);
220 if($preset)
221 {
222 $defaultList[] = $preset;
223 }
224 }
225 }
226
227 return $defaultList;
228 }
229
234 private function createPresetInstance($className)
235 {
236 try
237 {
238 $class = new \ReflectionClass($className);
239
241 $instance = $class->newInstanceArgs([]);
242 $instance->enableRestrictedGroupsMode($this->isRestrictedGroupsModeEnabled());
243
244 return $instance;
245 }
246 catch (\ReflectionException $exception)
247 {
248 }
249
250 return null;
251 }
252
253 private function getClassNameFromPath($path)
254 {
255 return "Sale\\Handlers\\DiscountPreset\\" . getFileNameWithoutExtension($path);
256 }
257
263 public function getPresets()
264 {
265 return $this->buildPresets()->presetList;
266 }
267
274 public function getPresetById($id)
275 {
276 if(class_exists($id))
277 {
278 return $this->createPresetInstance($id);
279 }
280 else
281 {
282 foreach($this->buildPresets()->presetList as $preset)
283 {
284 if($preset::className() === $id)
285 {
286 return $preset;
287 }
288 }
289 }
290
291 return null;
292 }
293
298 public function getPresetsByCategory($category): array
299 {
300 $presets = [];
301 foreach ($this->getPresets() as $preset)
302 {
303 if ($preset->getCategory() === $category)
304 {
305 $presets[] = $preset;
306 }
307 }
308
309 uasort(
310 $presets,
311 static function(BasePreset $a, BasePreset $b): int
312 {
313 $aSort = (int)$a->getSort();
314 $bSort = (int)$b->getSort();
315 if ($aSort === $bSort) {
316 return 0;
317 }
318 return ($aSort < $bSort) ? -1 : 1;
319 }
320 );
321
322 return $presets;
323 }
324
325 public function getCategoryList(): array
326 {
327 return [
328 self::CATEGORY_PRODUCTS => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_PRODUCTS'),
329 self::CATEGORY_PAYMENT => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_PAYMENT'),
330 self::CATEGORY_DELIVERY => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_DELIVERY'),
331 self::CATEGORY_OTHER => Loc::getMessage('SALE_PRESET_DISCOUNT_MANAGER_CATEGORY_OTHER'),
332 ];
333 }
334
335 public function getCategoryName($category)
336 {
337 $categoryList = $this->getCategoryList();
338
339 return $categoryList[$category] ?? '';
340 }
341
342 public function hasCreatedDiscounts(BasePreset $preset): bool
343 {
344 $count = DiscountTable::getCount([
345 '=PRESET_ID' => $preset::className()
346 ]);
347
348 return $count > 0;
349 }
350}
$path
Определения access_edit.php:21
$count
Определения admin_tab.php:4
const DEFAULT_PRESET_DIRECTORY
Определения manager.php:17
isRestrictedGroupsModeEnabled()
Определения manager.php:100
hasCreatedDiscounts(BasePreset $preset)
Определения manager.php:342
const CATEGORY_DELIVERY
Определения manager.php:21
const CATEGORY_PRODUCTS
Определения manager.php:19
const CATEGORY_OTHER
Определения manager.php:22
getCategoryName($category)
Определения manager.php:335
const CATEGORY_PAYMENT
Определения manager.php:20
autoLoad($className)
Определения manager.php:105
enableRestrictedGroupsMode($state)
Определения manager.php:95
static getInstance()
Определения manager.php:37
getPresetsByCategory($category)
Определения manager.php:298
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
foreach(['Bitrix\\Main'=> '/lib', 'Psr\\Container'=> '/vendor/psr/container/src', 'Psr\\Log'=> '/vendor/psr/log/src', 'Psr\\Http\\Message'=> '/vendor/psr/http-message/src', 'Psr\\Http\\Client'=> '/vendor/psr/http-client/src', 'Http\\Promise'=> '/vendor/php-http/promise/src', 'PHPMailer\\PHPMailer'=> '/vendor/phpmailer/phpmailer/src', 'GeoIp2'=> '/vendor/geoip2/geoip2/src', 'MaxMind\\Db'=> '/vendor/maxmind-db/reader/src/MaxMind/Db', 'PhpParser'=> '/vendor/nikic/php-parser/lib/PhpParser', 'Recurr'=> '/vendor/simshaun/recurr/src/Recurr',] as $namespace=> $namespacePath) $documentRoot
Определения autoload.php:27
$event
Определения prolog_after.php:141
else $a
Определения template.php:137
$method
Определения index.php:27