Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
52795535ae
586 files changed
+27619
No files matched your search
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Catalog;
|
||||
|
||||
use Bitrix\Iblock\ElementTable;
|
||||
use Bitrix\Iblock\IblockTable;
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Data\Cache;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\SystemException;
|
||||
|
||||
class WineStats
|
||||
{
|
||||
private const CACHE_TTL = 86400;
|
||||
private const CACHE_DIR = "/jinove.vinoteca/winestats";
|
||||
|
||||
private ?int $activeCount = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $iblockType,
|
||||
private readonly string $iblockCode,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getActiveCount(): int
|
||||
{
|
||||
if ($this->activeCount !== null) {
|
||||
return $this->activeCount;
|
||||
}
|
||||
|
||||
$cache = Cache::createInstance();
|
||||
$cacheId = "active-count-" . $this->iblockType . "-" . $this->iblockCode;
|
||||
if ($cache->initCache(self::CACHE_TTL, $cacheId, self::CACHE_DIR)) {
|
||||
return $this->activeCount = (int)$cache->getVars()["count"];
|
||||
}
|
||||
|
||||
$cache->startDataCache();
|
||||
$taggedCache = Application::getInstance()->getTaggedCache();
|
||||
$taggedCache->startTagCache(self::CACHE_DIR);
|
||||
try {
|
||||
$iblockId = $this->getIblockId();
|
||||
$count = ElementTable::getCount([
|
||||
"=IBLOCK_ID" => $iblockId,
|
||||
"=ACTIVE" => "Y",
|
||||
]);
|
||||
$taggedCache->registerTag("iblock_id_" . $iblockId);
|
||||
} catch (\Throwable $e) {
|
||||
$taggedCache->abortTagCache();
|
||||
$cache->abortDataCache();
|
||||
throw $e;
|
||||
}
|
||||
$taggedCache->endTagCache();
|
||||
$cache->endDataCache(["count" => $count]);
|
||||
|
||||
return $this->activeCount = $count;
|
||||
}
|
||||
|
||||
private function getIblockId(): int
|
||||
{
|
||||
if (!Loader::includeModule("iblock")) {
|
||||
throw new SystemException("Module iblock is not installed");
|
||||
}
|
||||
$iblock = IblockTable::getRow([
|
||||
"select" => ["ID"],
|
||||
"filter" => ["=IBLOCK_TYPE_ID" => $this->iblockType, "=CODE" => $this->iblockCode],
|
||||
]);
|
||||
if ($iblock === null) {
|
||||
throw new SystemException("Iblock " . $this->iblockType . "/" . $this->iblockCode . " not found");
|
||||
}
|
||||
|
||||
return (int)$iblock["ID"];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user