Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: bitrix-expert/tools
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.4.0
Choose a base ref
...
head repository: bitrix-expert/tools
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.x
Choose a head ref
  • 18 commits
  • 6 files changed
  • 5 contributors

Commits on Jul 5, 2016

  1. Copy the full SHA
    21b0c21 View commit details
  2. Copy the full SHA
    e22c9fa View commit details

Commits on Jul 8, 2016

  1. Merge pull request #11 from mmjurov/1.x-lite-cache-warmup

    #6 Collecting lite cache after add/update iblock
    niksamokhvalov authored Jul 8, 2016
    Copy the full SHA
    c5c3aef View commit details

Commits on Jul 12, 2016

  1. Copy the full SHA
    aa61716 View commit details

Commits on Jul 21, 2016

  1. Copy the full SHA
    7d47507 View commit details
  2. Merge pull request #12 from mmjurov/1.x-price-type-finder

    Добавил возможность искать типы цен
    niksamokhvalov authored Jul 21, 2016
    Copy the full SHA
    0e8b86b View commit details
  3. 1.5

    niksamokhvalov authored Jul 21, 2016
    Copy the full SHA
    52c4504 View commit details

Commits on Jan 8, 2017

  1. Fix onAfterIBlockUpdate

    change find condition to ['id' => $fields['ID']]
    pelmennoteam authored Jan 8, 2017
    Copy the full SHA
    c5ad6c6 View commit details

Commits on Mar 6, 2017

  1. Merge pull request #13 from pelmennoteam/patch-1

    Fix onAfterIBlockUpdate
    niksamokhvalov authored Mar 6, 2017
    Copy the full SHA
    36f66f8 View commit details
  2. 1.5.1

    niksamokhvalov authored Mar 6, 2017
    Copy the full SHA
    d5643e1 View commit details

Commits on Mar 13, 2017

  1. Copy the full SHA
    a09cd44 View commit details

Commits on Mar 14, 2017

  1. Copy the full SHA
    db575cc View commit details

Commits on Mar 17, 2017

  1. Merge pull request #16 from denispugachev/on_after_iblock_add_constru…

    …ctor_fix
    
    Исправление ошибки отсутствия записи в кеше при создании объекта в хендлере \Bex\Tools\Iblock\IblockFinder::onAfterIBlockAdd
    niksamokhvalov authored Mar 17, 2017
    Copy the full SHA
    9e0aff9 View commit details
  2. Merge pull request #17 from denispugachev/add_id_key_validation_on_af…

    …ter_iblock_update
    
    Добавлена проверка $field['ID'] > 0 в обработчике события после изменения инфоблока
    niksamokhvalov authored Mar 17, 2017
    Copy the full SHA
    15562e8 View commit details

Commits on Mar 27, 2017

  1. 1.5.2

    niksamokhvalov authored Mar 27, 2017
    Copy the full SHA
    5668d07 View commit details

Commits on May 25, 2017

  1. Copy the full SHA
    163a1c7 View commit details
  2. Copy the full SHA
    759dabd View commit details

Commits on Jun 13, 2017

  1. Merge pull request #18 from olegpro/readme-fix

    Поправил неймспейсы для классов HlBlockTools, GroupTools и IblockTools в README.md
    niksamokhvalov authored Jun 13, 2017
    Copy the full SHA
    93fe560 View commit details
Showing with 343 additions and 8 deletions.
  1. +15 −0 CHANGELOG.md
  2. +16 −3 README.md
  3. +238 −0 src/Catalog/GroupFinder.php
  4. +62 −0 src/Catalog/GroupTools.php
  5. +3 −1 src/Iblock/IblockFinder.php
  6. +9 −4 src/Loader.php
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Change log

## 1.5.2 - 2017-03-27
### Fixed

Fixed errors on logic of cleaning cache of the info block.

## 1.5.1 - 2017-03-06
### Fixed

Fixed error on `onAfterIBlockUpdate`.

## 1.5.0

* Added tools and finder for Catalog.
* Fixed logic of collecting lite cache after add/update iblock.

## 1.4

* Added tools and finder for Highloadblock.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ Tools for developers on Bitrix CMS:

```php
<?php
use Bex\Tools\IblockTools;
use Bex\Tools\Iblock\IblockTools;

$iblockFinder = IblockTools::find('iblock_type', 'iblock_code');

@@ -25,7 +25,7 @@ $propEnumId = $iblockFinder->propEnumId('PROP_CODE', 'VALUE_XML_ID');

```php
<?php
use Bex\Tools\GroupTools;
use Bex\Tools\Group\GroupTools;

$groupFinder = GroupTools::find('group_code');

@@ -39,7 +39,7 @@ $groupCode = GroupTools::findById(3)->code();

```php
<?php
use Bex\Tools\HlBlockTools;
use Bex\Tools\HlBlock\HlBlockTools;

$hlBlockFinder = HlBlockTools::find('ReferenceName');

@@ -48,6 +48,19 @@ $hlBlockName = HlBlockTools::findById(2)->name();

```

* Catalog\GroupTools: finder for catalog groups (price types) by it's names? id's or "BASE" flag.

```php
<?php
use Bex\Tools\Catalog\GroupTools;

$catalogGroupFinder = GroupTools::find('RETAIL');
$priceTypeId = $catalogGroupFinder->id();

$priceTypeName = GroupTools::findBase()->name();

```

* Prevents the creation of infoblocks with the same codes.
* Prevents the creation of user groups with the same string id.

238 changes: 238 additions & 0 deletions src/Catalog/GroupFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Tools\Catalog;

use Bex\Tools\Finder;
use Bex\Tools\ValueNotFoundException;
use Bitrix\Catalog\GroupTable;
use Bitrix\Main\Application;
use Bitrix\Main\ArgumentNullException;
use Bitrix\Main\Loader;

/**
* Finder of the catalog groups (price types)
*
* @author Mikhail Zhurov <mmjurov@gmail.com>
*/
class GroupFinder extends Finder
{
protected static $cacheDir = 'bex_tools/catalog_groups';
protected static $cacheTag = 'bex_catalog_groups';
protected $id;
protected $name;

/**
* @inheritdoc
*
* @throws ArgumentNullException Empty parameters in the filter
* @throws \LogicException When catalog module is not found
*/
public function __construct(array $filter, $silenceMode = false)
{
if (!Loader::includeModule('catalog')) {
throw new \LogicException('Failed include module "catalog"');
}

parent::__construct($filter, $silenceMode);

$filter = $this->prepareFilter($filter);

if (isset($filter['base']) && $filter['base'] === true) {

$this->name = $this->id = null;
$this->base = true;
$this->id = $this->getFromCache([
'type' => 'base'
]);

} else {

if (isset($filter['name'])) {
$this->name = $filter['name'];
}

if (!isset($this->id)) {
if (!isset($this->name)) {
throw new ArgumentNullException('name');
}

$this->id = $this->getFromCache([
'type' => 'id'
]);
}

}
}

/**
* @inheritdoc
*
* @throws ArgumentNullException
*/
protected function prepareFilter(array $filter)
{
foreach ($filter as $code => &$value) {
if ($code === 'id') {
intval($value);

if ($value <= 0) {
throw new ArgumentNullException($code);
}
} elseif ($code === 'base') {

if (!is_bool($value)) {
throw new ArgumentNullException($code);
}

} else {
trim(htmlspecialchars($value));

if (strlen($value) <= 0) {
throw new ArgumentNullException($code);
}
}
}

return $filter;
}

/**
* Handler after catalog group delete event
*
* @param int $id Group ID
*/
public static function onGroupDelete($id)
{
static::deleteCacheByTag(static::getCacheTag());
}

/**
* Gets tag of cache.
*
* @return string
*/
public static function getCacheTag()
{
return static::$cacheTag;
}

/**
* Handler after catalog group add event
*
* @param array $fields Group fields
*/
public static function onGroupAdd($groupId, $fields)
{
static::deleteCacheByTag(static::getCacheTag());
}

/**
* Handler after catalog group update event
*
* @param int $id Group ID
* @param array $fields Group fields
*/
public static function onGroupUpdate($id, $fields)
{
static::deleteCacheByTag(static::getCacheTag());
}

/**
* Gets catalog group ID.
*
* @return integer
*/
public function id()
{
return $this->getFromCache(
['type' => 'id']
);
}

/**
* Gets catalog group name.
*
* @return string
*/
public function name()
{
return $this->getFromCache(
['type' => 'name']
);
}

/**
* @inheritdoc
*/
protected function getValue(array $cache, array $filter, $shard)
{
switch ($filter['type']) {
case 'id':
if (isset($this->id)) {
return $this->id;
}

$value = (int)$cache['GROUPS_ID'][$this->name];

if ($value <= 0) {
throw new ValueNotFoundException('Catalog group ID', 'group name "' . $this->name . '"');
}
break;

case 'name':
$value = $cache['GROUPS_NAME'][$this->id];

if (strlen($value) <= 0) {
throw new ValueNotFoundException('Catalog group name', 'ID #' . $this->id);
}
break;

case 'base':
$value = $cache['GROUPS_BASE'];

if ($value <= 0) {
throw new ValueNotFoundException('Catalog group base', false);
}
break;

default:
throw new \InvalidArgumentException('Invalid type of filter');
break;
}

return $value;
}

/**
* @inheritdoc
*/
protected function getItems($shard)
{
$items = [];

$rsGroups = GroupTable::query()
->setSelect(['ID', 'NAME', 'BASE'])
->exec();

while ($group = $rsGroups->fetch()) {
if ($group['NAME']) {
$items['GROUPS_ID'][$group['NAME']] = $group['ID'];
$items['GROUPS_NAME'][$group['ID']] = $group['NAME'];
}

if ($group['BASE'] == 'Y') {
$items['GROUPS_BASE'] = $group['ID'];
}
}

if (!empty($items)) {
Application::getInstance()->getTaggedCache()->registerTag(static::getCacheTag());
}

return $items;
}
}
62 changes: 62 additions & 0 deletions src/Catalog/GroupTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Tools\Catalog;

/**
* Tools for working with catalog groups.
*
* @author Mikhail Zhurov <mmjurov@gmail.com>
*/
class GroupTools
{
/**
* Gets Finder for catalog groups by group name.
*
* @param string $code Group name
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException
* (if value was be not found) is returned null.
*
* @return GroupFinder
*/
public static function find($name, $silenceMode = false)
{
return new GroupFinder(
['name' => $name],
$silenceMode
);
}

/**
* Gets Finder for catalog groups by group ID.
*
* @param int $id Group ID
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException
* (if value was be not found) is returned null.
* @return GroupFinder
*/
public static function findById($id, $silenceMode = false)
{
return new GroupFinder(['id' => $id],
$silenceMode
);
}

/**
* Gets Finder for catalog groups by base flag.
*
* @param bool $silenceMode When you use silence mode instead of an exception \Bex\Tools\ValueNotFoundException
* (if value was be not found) is returned null.
*
* @return GroupFinder
*/
public static function findBase($silenceMode = false)
{
return new GroupFinder(['base' => true],
$silenceMode
);
}
}
4 changes: 3 additions & 1 deletion src/Iblock/IblockFinder.php
Original file line number Diff line number Diff line change
@@ -429,15 +429,17 @@ public static function onAfterIBlockAdd(&$fields)
{
if ($fields['ID'] > 0) {
static::deleteCacheByTag('bex_iblock_new');
new static(['id' => $fields['ID']]);
static::delayCacheCollector($fields['ID']);
}
}

public static function onAfterIBlockUpdate(&$fields)
{
if ($fields['RESULT']) {
if ($fields['RESULT'] && $fields['ID'] > 0) {
static::deleteCacheByTag('bex_iblock_' . $fields['ID']);
static::deleteCacheByTag('bex_iblock_new');
new static(['id' => $fields['ID']]);
static::delayCacheCollector($fields['ID']);
}
}
Loading