polyspirit/bitrix-builder

bitrix的IBlock和ISection的构建类

安装: 25

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

类型:package

v1.2.3 2022-06-29 09:16 UTC

This package is auto-updated.

Last update: 2024-09-25 17:51:15 UTC


README

bitrix的iBlock和iSection的构建类

如何安装

composer require polyspirit/bitrix-builder

如何使用

创建IBlock类的实例

use \polyspirit\Bitrix\Builder\IBlock;

// set iBlock ID as first parameter
$iBlockById = new IBlock(12);

// iBlock CODE as first parameter
$iBlockByCode = new IBlock('news');

// iBlock CODE as first parameter and site ID as second parameter
$iBlockByCodeAndSiteID = new IBlock('news', 's1');

获取元素列表

$iBlock = new IBlock('news');
$arResult = $iBlock->getElements();

获取元素详情

$iBlock = new IBlock('news');
$arResultDetail = $iBlock->filter(['ID' => 42])->getElement();

每个结果的元素都有一个PROPS属性,其中包含所有元素的属性。

// show SOME_CODE property value
echo $arResultDetail['PROPS']['SOME_CODE']['VALUE'];

同时,每个结果的元素都有一个PICTURE_SRC属性,默认包含DETAIL_PICTURE的路径,如果DETAIL_PICTURE不在结果字段中,则包含PREVIEW_PICTURE。

// show path to picture
echo $arResultDetail['PICTURE_SRC']; // /upload/resize_cache/iblock/xxx/xxx_xxx_1/some_picture.ext

方法

过滤器、排序等

IBlock::active

仅获取活动元素。

public IBlock::active(): IBlock
// example: 
$iBlock->active()->getElements();

IBlock::sort

默认与您的数组合并排序。

public IBlock::sort(array $array): IBlock
// sort by default: 
['sort' => 'ASC', 'date_active_from' => 'DESC', 'created_date' => 'DESC']
// example: 
$iBlock->sort(['sort' => 'DESC', 'ID' => 'DESC'])->getElements();

IBlock::sortReset

将默认排序值重置为空数组。

public IBlock::sortReset(): IBlock
// example: 
$iBlock->sortReset()->sort(['ID' => 'DESC'])->getElements();

IBlock::filter

过滤结果元素。

public IBlock::filter(array $array): IBlock
// example:
$iBlock->filter(['>=TIMESTAMP_X' => date('Y-m-d h:i:s', 'yesterday')])->getElements();

IBlock::fields

默认与您的数组合并字段。如果您不使用此方法,则将选择所有字段。

public IBlock::fields(array $array): IBlock
// fields by default: 
['ID', 'IBLOCK_ID']

// fields by default if method is not used: 
['*']
// example (select ID, IBLOCK_ID, NAME, PREVIEW_PICTURE): 
$iBlock->fields(['NAME', 'PREVIEW_PICTURE'])->getElements();

IBlock::navs

导航参数。

public IBlock::navs(array $array): IBlock
$iBlock->navs(['nPageSize' => 4, 'iNumPage' => 1, 'checkOutOfRange' => true])->getElements();

IBlock::sizes

元素图片的大小。

public IBlock::sizes(array $array): IBlock
// sizes by default: 
['width' => 640, 'height' => 640]
// example: 
$iBlock->sizes(['width' => 1280, 'height' => 720])->getElements();

IBlock::params

在一个数组中设置所有属性

public IBlock::params(array $array): IBlock
// example: 
$params = [
    'sort' => ['ID' => 'DESC'],
    'filter' => ['>=TIMESTAMP_X' => date('Y-m-d h:i:s', 'yesterday')],
    'navs' => ['nPageSize' => 4, 'iNumPage' => 1, 'checkOutOfRange' => true],
    'fields' => ['NAME', 'CODE'],
    'sizes' => ['width' => 1280, 'height' => 720]
];
$iBlock->params($params)->getElements();

获取

IBlock::getElement

从结果中获取第一个元素。

public IBlock::getElement(Closure $closure = null): array
// example: 
$handler = function (&$element) {
    $element['ID_CODE'] = $element['ID'] . '|' . $element['CODE'];
}
$arDetail = $iBlock->filter(['ID' => 42])->fields(['CODE'])->getElement($handler);

echo $arDetail['ID_CODE']; // 42|ELEMENT_CODE

IBlock::getElements

获取元素列表。

public IBlock::getElements(Closure $closure = null): array
// example: 
$handler = function (&$element) {
    $element['ID_CODE'] = $element['ID'] . '|' . $element['CODE'];
}
$arResult = $iBlock->filter(['>=ID' => 42])->fields(['CODE'])->getElements($handler);

foreach ($arResult as $element) {
    echo $element['ID_CODE']; // 42|ELEMENT_CODE
}

添加 & 修改

IBlock::add

使用字段和属性添加新元素。

public IBlock::add(array $fields, array $props = []): int
// example:
(new IBlock('news'))->add(
    [
        'NAME' => 'Some',
        'PREVIEW_TEXT' => 'Some text'
    ], 
    [
        'SOME_PROPERTY_CODE' => 42
    ]
);

IBlock::update

更新元素的字段和属性。

public IBlock::update(string|int $id, array $fields, array $props = []): bool
// example:
(new IBlock('news'))->update(
    [
        'NAME' => 'Updated name',
        'PREVIEW_TEXT' => 'Updated text'
    ], 
    [
        'SOME_PROPERTY_CODE' => 24
    ]
);

IBlock::delete

通过id删除元素。如果未设置id参数,则将删除最后添加或更新的元素。

public IBlock::delete(string|int|null $id = null): bool
// example:
(new IBlock('news'))->delete(42);

// or:
$iBlock = new IBlock('news');
$iBlock->add(['NAME' => 'SOME']);
$iBlock->delete();

其他

IBlock::getObResult

获取对象

public IBlock::getObResult(): CIBlockResult|int
// example:
$arResult = $iBlock->filter(['>=ID' => 42])->getElements();
$obResult = $iBlock->getObResult();

IBlock::includeMeta

将元素的元数据包含到页面中。

public IBlock::includeMeta(string|int $elementId): void
// example:
(new IBlock('news'))->includeMeta(42);

IBlock::getPropertySubQuery

获取属性的子查询。

public IBlock::getPropertySubQuery(string $propName, string $propValue): array
// example:
$subquery = $iBlock->getPropertySubQuery('SOME_CODE', 42);
$iBlock->filter([$subquery])->getElement();

静态

IBlock::getIdByCode

获取属性的子查询。

public static IBlock::getIdByCode(string $code = '', $siteId = SITE_ID): false|mixed
// example:
$id = IBlock::getIdByCode('some_iblock_code');

IBlock::getResizeImageSrc

获取属性的子查询。

public static IBlock::getResizeImageSrc($pictureId, array $sizes): string
// example:
$pathToPicture = IBlock::getResizeImageSrc(42, ['width' => 1280, 'height' => 720]);