33 private static $instance;
37 private $restrictedGroupsMode =
false;
45 if (!isset(self::$instance))
47 self::$instance =
new self;
50 return self::$instance;
59 if (!$this->isAlreadyRegisteredAutoLoader())
61 \spl_autoload_register(array($this,
'autoLoad'),
true);
65 private function isAlreadyRegisteredAutoLoader()
67 $autoLoaders = spl_autoload_functions();
73 foreach ($autoLoaders as $autoLoader)
75 if(!is_array($autoLoader))
80 list($object, $method) = $autoLoader;
82 if ($object instanceof $this)
91 private function __construct()
93 $this->errorCollection =
new ErrorCollection;
98 private function __clone()
103 $this->restrictedGroupsMode = $state ===
true;
108 return $this->restrictedGroupsMode;
113 $file = ltrim($className,
"\\");
114 $file = strtr($file, Loader::ALPHA_UPPER, Loader::ALPHA_LOWER);
116 $documentRoot = $documentRoot = rtrim($_SERVER[
"DOCUMENT_ROOT"],
"/\\");
118 if(preg_match(
"#[^\\\\/a-zA-Z0-9_]#", $file))
123 $file = str_replace(
'\\',
'/', $file);
124 $fileParts = explode(
"/", $file);
126 if($fileParts[0] !==
"sale" || $fileParts[1] !==
"handlers" || $fileParts[2] !==
'discountpreset')
130 array_shift($fileParts);
132 $filePath = $documentRoot .
"/bitrix/modules/sale/" . implode(
"/", $fileParts) .
".php";
134 if(file_exists($filePath))
136 require_once($filePath);
140 private function buildPresets()
142 if($this->presetList ===
null)
144 $this->presetList = array_filter(
146 $this->buildDefaultPresets(),
147 $this->buildCustomPresets()
159 private function buildCustomPresets()
161 $presetList = array();
163 $event =
new Event(
'sale',
'OnSaleDiscountPresetBuildList');
166 foreach($event->getResults() as $evenResult)
168 if($evenResult->getType() != EventResult::SUCCESS)
173 $result = $evenResult->getParameters();
174 if(!is_array($result))
176 throw new SystemException(
'Wrong event result by building preset list. Must be array.');
179 foreach($result as $preset)
181 if(empty($preset[
'CLASS']))
183 throw new SystemException(
'Wrong event result by building preset list. Could not find CLASS.');
186 if(is_string($preset[
'CLASS']) && class_exists($preset[
'CLASS']))
188 $preset = $this->createPresetInstance($preset[
'CLASS']);
191 $presetList[] = $preset;
196 throw new SystemException(
"Wrong event result by building preset list. Could not find class by CLASS {$preset['CLASS']}");
204 private function buildDefaultPresets()
210 throw new SystemException(
'Could not find folder with default presets. ' . self::DEFAULT_PRESET_DIRECTORY);
213 $defaultList = array();
214 $directory =
new Directory($documentRoot . self::DEFAULT_PRESET_DIRECTORY);
215 foreach($directory->getChildren() as $presetFile)
217 if(!$presetFile->isFile() || !$presetFile->getName())
222 $className = $this->getClassNameFromPath($presetFile->getPath());
225 $preset = $this->createPresetInstance($className);
228 $defaultList[] = $preset;
240 private function createPresetInstance($className)
244 $class = new \ReflectionClass($className);
247 $instance = $class->newInstanceArgs([]);
252 catch (\ReflectionException $exception)
259 private function getClassNameFromPath($path)
261 return "Sale\\Handlers\\DiscountPreset\\" . getFileNameWithoutExtension($path);
271 return $this->buildPresets()->presetList;
282 if(class_exists($id))
284 return $this->createPresetInstance($id);
288 foreach($this->buildPresets()->presetList as $preset)
290 if($preset::className() === $id)
309 if($preset->getCategory() === $category)
311 $presets[] = $preset;
321 if ($aSort === $bSort) {
324 return ($aSort < $bSort) ? -1 : 1;
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'),
345 return isset($categoryList[$category])? $categoryList[$category] :
'';
350 $countQuery =
new Query(DiscountTable::getEntity());
352 $countQuery->setFilter(array(
353 '=PRESET_ID' => $preset::className(),
355 $totalCount = $countQuery->setLimit(
null)->setOffset(
null)->exec()->fetch();
357 return (
bool)$totalCount[
'CNT'];
static isDirectoryExists($path)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)