Skip to content

Commit 906cff5

Browse files
committed
Stuff
1 parent 7c5ca02 commit 906cff5

File tree

4 files changed

+296
-61
lines changed

4 files changed

+296
-61
lines changed

Services/Iblock/IblockManager.php

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Iblock;
44

5+
use Bitrix\Iblock\IblockTable;
56
use Bitrix\Main\ArgumentException;
67
use CFile;
78
use CIBlock;
@@ -13,88 +14,57 @@
1314
*/
1415
class IblockManager
1516
{
17+
/**
18+
* @var integer $defaultCacheTTl Время жизни кэша по умолчанию.
19+
*/
20+
private $defaultCacheTTl = 86400;
21+
1622
/** ID инфоблока по коду.
1723
*
1824
* @param string $iblockType Тип инфоблока.
1925
* @param string $iblockCode Код инфоблока.
2026
* @return mixed
2127
*
22-
* @throws ArgumentException
28+
* @throws ArgumentException Когда инфоблок не найден.
2329
*/
24-
public function getIBlockIdByCode(string $iblockType, string $iblockCode)
30+
public function getIBlockIdByCode(string $iblockType, string $iblockCode) : int
2531
{
26-
$res = CIBlock::GetList(
27-
[],
28-
['ACTIVE' => 'Y', 'TYPE' => $iblockType, 'CODE' => $iblockCode]
29-
);
30-
$arResult = $res->Fetch();
31-
if ($arResult['ID'] > 0) {
32-
return $arResult['ID'];
32+
$iBlockTable = IblockTable::getList([
33+
'select' => ['ID'],
34+
'filter' => ['=CODE' => $iblockCode, "=TYPE" => $iblockType],
35+
'cache' => ['ttl' => $this->defaultCacheTTl],
36+
]);
37+
38+
if ($iblockData = $iBlockTable->fetch()) {
39+
return (int)$iblockData['ID'];
3340
}
3441

3542
throw new ArgumentException('Инфоблок '.$iblockCode.' не найден', $iblockCode);
3643
}
3744

38-
/**
39-
* ID инфоблока по его коду из кэша.
40-
*
41-
* @param string $sIBlockType Тип инфоблока.
42-
* @param string $sIBlockCode Код инфоблока.
43-
*
44-
* @return integer
45-
*/
46-
public function getIBlockIdByCodeCached(string $sIBlockType, string $sIBlockCode)
47-
{
48-
$cacher = CacherFacade::setCacheId($sIBlockType . $sIBlockCode)
49-
->setCallback([$this, 'getIBlockIdByCode'])
50-
->setCallbackParams($sIBlockType, $sIBlockCode)
51-
->setTtl(604800);
52-
53-
54-
return $cacher->returnResultCache();
55-
}
56-
5745
/**
5846
* Описание инфоблока.
5947
*
60-
* @param string $typeIblock
61-
* @param string $codeIblock
48+
* @param string $iblockType Тип инфоблока.
49+
* @param string $iblockCode Код инфоблока.
50+
*
6251
* @return mixed
63-
* @throws ArgumentException
52+
* @throws ArgumentException Когда инфоблок не найден.
6453
*/
65-
public function getIBlockDescriptionByCode(string $typeIblock, string $codeIblock)
54+
public function getIBlockDescriptionByCode(string $iblockType, string $iblockCode)
6655
{
67-
$res = CIBlock::GetList(
68-
[],
69-
['ACTIVE' => 'Y', 'TYPE' => $typeIblock, 'CODE' => $codeIblock]
70-
);
56+
$iBlockTable = IblockTable::getList([
57+
'select' => ['ID', 'DESCRIPTION'],
58+
'filter' => ['=ACTIVE' => 'Y', '=CODE' => $iblockCode, "=TYPE" => $iblockType],
59+
'cache' => ['ttl' => $this->defaultCacheTTl],
60+
]);
7161

72-
$arResult = $res->Fetch();
62+
$arResult = $iBlockTable->Fetch();
7363
if ($arResult['ID'] > 0) {
7464
return $arResult['DESCRIPTION'];
7565
}
7666

77-
throw new ArgumentException('Инфоблок '.$codeIblock.' не найден', $codeIblock);
78-
}
79-
80-
/**
81-
* Описание инфоблока по его коду из кэша.
82-
*
83-
* @param string $sIBlockType Тип инфоблока.
84-
* @param string $sIBlockCode Код инфоблока.
85-
*
86-
* @return string
87-
*/
88-
public function getIBlockDescriptionByCodeCached($sIBlockType, $sIBlockCode)
89-
{
90-
/** @noinspection PhpUndefinedMethodInspection */
91-
$cacher = CacherFacade::setCacheId('iblockDescription'.$sIBlockType . $sIBlockCode)
92-
->setCallback([$this, 'getIBlockDescriptionByCode'])
93-
->setCallbackParams($sIBlockType, $sIBlockCode)
94-
->setTtl(604800);
95-
96-
97-
return $cacher->returnResultCache();
67+
throw new ArgumentException('Инфоблок '.$iblockCode.' не найден', $iblockCode);
9868
}
9969

10070
/**
@@ -120,7 +90,6 @@ public function getDescriptionById(int $iblockId) : string
12090
*/
12191
public function getDescriptionByIdCached(int $iblockId) : string
12292
{
123-
/** @noinspection PhpUndefinedMethodInspection */
12493
$cacher = CacherFacade::setCacheId('iblockDescription' . $iblockId)
12594
->setCallback([$this, 'getDescriptionById'])
12695
->setCallbackParams($iblockId)
@@ -152,7 +121,6 @@ public function getNameById(int $iblockId) : string
152121
*/
153122
public function getNameByIdCached(int $iblockId) : string
154123
{
155-
/** @noinspection PhpUndefinedMethodInspection */
156124
$cacher = CacherFacade::setCacheId('iblockName' . $iblockId)
157125
->setCallback([$this, 'getNameById'])
158126
->setCallbackParams($iblockId)
@@ -212,7 +180,6 @@ public function getIblockUrlByCode($arParams = ['TYPE', 'CODE'])
212180
*/
213181
public function getIblockUrlByCodeCached($sIBlockType, $sIBlockCode)
214182
{
215-
/** @noinspection PhpUndefinedMethodInspection */
216183
$cacher = CacherFacade::setCacheId('iblockurl'.$sIBlockType . $sIBlockCode)
217184
->setCallback([$this, 'getIblockUrlByCode'])
218185
->setCallbackParams($sIBlockType, $sIBlockCode)
@@ -301,4 +268,12 @@ private function getFieldValue(int $iblockId, string $field)
301268

302269
return $arData[$field];
303270
}
271+
272+
/**
273+
* @param integer $defaultCacheTTl
274+
*/
275+
public function setCacheTTl(int $defaultCacheTTl): void
276+
{
277+
$this->defaultCacheTTl = $defaultCacheTTl;
278+
}
304279
}

Services/ORM/CaptchaTable.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\ORM;
4+
5+
use Bitrix\Main\ORM\Data\DataManager,
6+
Bitrix\Main\ORM\Fields\DatetimeField,
7+
Bitrix\Main\ORM\Fields\StringField,
8+
Bitrix\Main\ORM\Fields\Validators\LengthValidator,
9+
Bitrix\Main\SystemException,
10+
Bitrix\Main\ArgumentTypeException;
11+
12+
/**
13+
* Class CaptchaTable
14+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\ORM
15+
*
16+
* Fields:
17+
* <ul>
18+
* <li> ID string(32) mandatory
19+
* <li> CODE string(20) mandatory
20+
* <li> IP string(15) mandatory
21+
* <li> DATE_CREATE datetime mandatory
22+
* </ul>
23+
*/
24+
class CaptchaTable extends DataManager
25+
{
26+
/**
27+
* Returns DB table name for entity.
28+
*
29+
* @return string
30+
*/
31+
public static function getTableName(): string
32+
{
33+
return 'b_captcha';
34+
}
35+
36+
/**
37+
* Returns entity map definition.
38+
*
39+
* @return array
40+
* @throws SystemException
41+
*/
42+
public static function getMap(): array
43+
{
44+
return [
45+
'ID' => new StringField('ID', [
46+
'primary' => true,
47+
'autocomplete' => true,
48+
'title' => '',
49+
]),
50+
'CODE' => new StringField('CODE', [
51+
'validation' => [__CLASS__, 'validateCode'],
52+
'title' => '',
53+
]),
54+
'IP' => new StringField('IP', [
55+
'validation' => [__CLASS__, 'validateIp'],
56+
'title' => '',
57+
]),
58+
'DATE_CREATE' => new DatetimeField('DATE_CREATE', [
59+
'title' => '',
60+
]),
61+
];
62+
}
63+
64+
/**
65+
* Returns validators for CODE field.
66+
*
67+
* @return array
68+
* @throws ArgumentTypeException
69+
*/
70+
public static function validateCode(): array
71+
{
72+
return [
73+
new LengthValidator(null, 20),
74+
];
75+
}
76+
77+
/**
78+
* Returns validators for IP field.
79+
*
80+
* @return array
81+
* @throws ArgumentTypeException
82+
*/
83+
public static function validateIp(): array
84+
{
85+
return [
86+
new LengthValidator(null, 15),
87+
];
88+
}
89+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\ORM;
4+
5+
use Bitrix\Iblock\PropertyEnumerationTable;
6+
use Bitrix\Main\ArgumentTypeException;
7+
use Bitrix\Main\Entity\FloatField;
8+
use Bitrix\Main\Entity\IntegerField;
9+
use Bitrix\Main\Localization\Loc;
10+
use Bitrix\Main\ORM\Data\DataManager;
11+
use Bitrix\Main\ORM\Fields\Relations\Reference;
12+
use Bitrix\Main\ORM\Fields\StringField;
13+
use Bitrix\Main\ORM\Fields\Validators\LengthValidator;
14+
use Bitrix\Main\ORM\Query\Join;
15+
use Bitrix\Main\SystemException;
16+
17+
/**
18+
* Class ElementPropertysTable
19+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\ORM
20+
*/
21+
class ElementPropertysTable extends DataManager
22+
{
23+
/**
24+
* @var integer $iblockId ID инфоблока.
25+
*/
26+
protected static $iblockId;
27+
28+
/**
29+
* Returns DB table name for entity.
30+
*
31+
* @return string
32+
*/
33+
public static function getTableName(): string
34+
{
35+
return 'b_iblock_element_prop_s' . static::$iblockId;
36+
}
37+
38+
/**
39+
* Returns entity map definition.
40+
*
41+
* @return array
42+
* @throws SystemException
43+
*/
44+
public static function getMap(): array
45+
{
46+
$map = [
47+
'IBLOCK_ELEMENT_ID' => new IntegerField('IBLOCK_ELEMENT_ID', [
48+
'primary' => true,
49+
'autocomplete' => true,
50+
'title' => Loc::getMessage('IBLOCK_ELEMENT_ID_FIELD'),
51+
]),
52+
];
53+
54+
$properties = \Bitrix\Iblock\PropertyTable::getList([
55+
'select' => ['ID', 'PROPERTY_TYPE'],
56+
'filter' => ['=IBLOCK_ID' => self::$iblockId],
57+
])->fetchAll();
58+
59+
foreach ($properties as $property) {
60+
$fieldName = 'PROPERTY_' . $property['ID'];
61+
$fieldEnumName = 'PROPERTY_' . $property['ID'] . '_ENUM';
62+
63+
switch ($property['PROPERTY_TYPE']) {
64+
case 'L':
65+
$map[$fieldName] = new IntegerField($fieldName);
66+
$map[$fieldEnumName] = new Reference(
67+
$fieldEnumName,
68+
PropertyEnumerationTable::class,
69+
Join::on("this.$fieldName", 'ref.ID')
70+
);
71+
break;
72+
case 'F':
73+
case 'E':
74+
case 'G':
75+
$map[$fieldName] = new IntegerField($fieldName);
76+
break;
77+
case 'N':
78+
$map[$fieldName] = new FloatField($fieldName);
79+
break;
80+
case 'S':
81+
default:
82+
$map[$fieldName] = new StringField($fieldName);
83+
}
84+
}
85+
86+
return $map;
87+
}
88+
89+
/**
90+
* Returns validators for ID field.
91+
*
92+
* @return array
93+
* @throws ArgumentTypeException
94+
*/
95+
public static function validateId(): array
96+
{
97+
return [
98+
new LengthValidator(null, 32),
99+
];
100+
}
101+
102+
/**
103+
* Returns validators for CODE field.
104+
*
105+
* @return array
106+
* @throws ArgumentTypeException
107+
*/
108+
public static function validateCode(): array
109+
{
110+
return [
111+
new LengthValidator(null, 20),
112+
];
113+
}
114+
115+
/**
116+
* @param integer $iblockId
117+
*
118+
* @return DataManager
119+
*/
120+
public static function compileEntity(int $iblockId): DataManager
121+
{
122+
self::$iblockId = $iblockId;
123+
$class = 'ElementPropertys' . self::$iblockId . 'Table';
124+
125+
if (!class_exists($class)) {
126+
$eval = "class {$class} extends " . __CLASS__ . "{}";
127+
eval($eval);
128+
}
129+
130+
return new $class;
131+
}
132+
}

0 commit comments

Comments
 (0)