polyspirit / bitrix-builder
Builder classes for bitrix's IBlock and ISection
Installs: 27
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=7.3.0
README
Builder classes for bitrix's iBlock and iSection
How to install
composer require polyspirit/bitrix-builder
How to use
Create instance of IBlock class
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');
Get list of elements
$iBlock = new IBlock('news'); $arResult = $iBlock->getElements();
Get element detail
$iBlock = new IBlock('news'); $arResultDetail = $iBlock->filter(['ID' => 42])->getElement();
Every result's element has a PROPS property, which contains all element's properties.
// show SOME_CODE property value echo $arResultDetail['PROPS']['SOME_CODE']['VALUE'];
Also every result's element has a PICTURE_SRC property, which contains path to DETAIL_PICTURE by default and PREVIEW_PICTURE if DETAIL_PICTURE not in result's fields.
// show path to picture echo $arResultDetail['PICTURE_SRC']; // /upload/resize_cache/iblock/xxx/xxx_xxx_1/some_picture.ext
Methods
FILTER, SORT AND ETC.
IBlock::active
Get only active elements.
public IBlock::active(): IBlock
// example: $iBlock->active()->getElements();
IBlock::sort
Merge sort by default with your array.
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
Reset default sort value to empty array.
public IBlock::sortReset(): IBlock
// example: $iBlock->sortReset()->sort(['ID' => 'DESC'])->getElements();
IBlock::filter
Filter result's elements.
public IBlock::filter(array $array): IBlock
// example: $iBlock->filter(['>=TIMESTAMP_X' => date('Y-m-d h:i:s', 'yesterday')])->getElements();
IBlock::fields
Merge fields by default with your array. If you don't use this method - all fields will be selected.
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
Navigation parameters.
public IBlock::navs(array $array): IBlock
$iBlock->navs(['nPageSize' => 4, 'iNumPage' => 1, 'checkOutOfRange' => true])->getElements();
IBlock::sizes
Sizes for element's pictures.
public IBlock::sizes(array $array): IBlock
// sizes by default: ['width' => 640, 'height' => 640]
// example: $iBlock->sizes(['width' => 1280, 'height' => 720])->getElements();
IBlock::params
Set all properties in one array
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();
GET
IBlock::getElement
Get first element from result.
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
Get list of elements.
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 }
ADD & MODIFY
IBlock::add
Add a new element with fields and properties.
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
Update element's fields and properties.
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
Delete element by id. If id parameter is not setted, the last added or updated element will be deleted.
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();
OTHER
IBlock::getObResult
Get object
public IBlock::getObResult(): CIBlockResult|int
// example: $arResult = $iBlock->filter(['>=ID' => 42])->getElements(); $obResult = $iBlock->getObResult();
IBlock::includeMeta
Includes element's meta to page.
public IBlock::includeMeta(string|int $elementId): void
// example: (new IBlock('news'))->includeMeta(42);
IBlock::getPropertySubQuery
Get subquery for property.
public IBlock::getPropertySubQuery(string $propName, string $propValue): array
// example: $subquery = $iBlock->getPropertySubQuery('SOME_CODE', 42); $iBlock->filter([$subquery])->getElement();
STATIC
IBlock::getIdByCode
Get subquery for property.
public static IBlock::getIdByCode(string $code = '', $siteId = SITE_ID): false|mixed
// example: $id = IBlock::getIdByCode('some_iblock_code');
IBlock::getResizeImageSrc
Get subquery for property.
public static IBlock::getResizeImageSrc($pictureId, array $sizes): string
// example: $pathToPicture = IBlock::getResizeImageSrc(42, ['width' => 1280, 'height' => 720]);