-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6174644
commit 52699b8
Showing
1 changed file
with
130 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* Набор методов для работы с highloadblock Bitrix | ||
* User: darkfriend <[email protected]> | ||
* Date: 25.04.2017 | ||
* @version 1.0.2 | ||
*/ | ||
|
||
namespace Darkfriend; | ||
|
@@ -11,8 +12,8 @@ | |
\Bitrix\Main\Entity, | ||
\Bitrix\Main\Loader; | ||
|
||
class HLHelpers { | ||
|
||
class HLHelpers | ||
{ | ||
private static $instance; | ||
public static $LAST_ERROR; | ||
|
||
|
@@ -22,9 +23,10 @@ class HLHelpers { | |
* @param string $iblockType | ||
* @return HLHelpers | ||
*/ | ||
public static function getInstance() { | ||
public static function getInstance() | ||
{ | ||
if (!self::$instance) { | ||
Loader::includeModule('highloadblock'); | ||
Loader::includeModule('highloadblock'); | ||
self::$instance = new HLHelpers(); | ||
} | ||
return self::$instance; | ||
|
@@ -37,12 +39,13 @@ public static function getInstance() { | |
* @param array $arMoreParams остальные параметры select|group|limit|offset|count_total|runtime|data_doubling | ||
* @return array | ||
*/ | ||
public function getList($arOrder=[],$arFilter=[],$arMoreParams=[]) { | ||
public function getList($arOrder = [], $arFilter = [], $arMoreParams = []) | ||
{ | ||
$arParams = []; | ||
if($arOrder) $arParams['order'] = $arOrder; | ||
if($arFilter) $arParams['filter'] = $arFilter; | ||
if($arMoreParams) { | ||
foreach ($arMoreParams as $k=>$arMoreParam) { | ||
if ($arOrder) $arParams['order'] = $arOrder; | ||
if ($arFilter) $arParams['filter'] = $arFilter; | ||
if ($arMoreParams) { | ||
foreach ($arMoreParams as $k => $arMoreParam) { | ||
$key = mb_strtolower($k); | ||
$arParams[$key] = $arMoreParam; | ||
} | ||
|
@@ -56,7 +59,8 @@ public function getList($arOrder=[],$arFilter=[],$arMoreParams=[]) { | |
* @param int $hlblockID - идентификатор таблицы HL | ||
* @return Entity\DataManager|bool | ||
*/ | ||
public function getEntityTable($hlblockID){ | ||
public function getEntityTable($hlblockID) | ||
{ | ||
if (!$hlblockID) return false; | ||
$hlblock = HL\HighloadBlockTable::getById($hlblockID)->fetch(); | ||
if (!$hlblock) return false; | ||
|
@@ -73,15 +77,16 @@ public function getEntityTable($hlblockID){ | |
* @param array $arMoreParams остальные параметры group|limit|offset|count_total|runtime|data_doubling | ||
* @return \Bitrix\Main\DB\Result | ||
*/ | ||
public function getElementsResource($hlblockID,$arFilter=[],$arOrder=["ID" => "ASC"],$arSelect=['*'],$arMoreParams=[]){ | ||
public function getElementsResource($hlblockID, $arFilter = [], $arOrder = ["ID" => "ASC"], $arSelect = ['*'], $arMoreParams = []) | ||
{ | ||
$entity = $this->getEntityTable($hlblockID); | ||
$arParams = []; | ||
if($arFilter) $arParams['filter'] = $arFilter; | ||
if($arOrder) $arParams['order'] = $arOrder; | ||
if($arSelect) $arParams['select'] = $arSelect; | ||
if($arMoreParams) { | ||
foreach ($arMoreParams as $k=>$arMoreParam) { | ||
if(!$arMoreParam) continue; | ||
if ($arFilter) $arParams['filter'] = $arFilter; | ||
if ($arOrder) $arParams['order'] = $arOrder; | ||
if ($arSelect) $arParams['select'] = $arSelect; | ||
if ($arMoreParams) { | ||
foreach ($arMoreParams as $k => $arMoreParam) { | ||
if (!$arMoreParam) continue; | ||
$key = mb_strtolower($k); | ||
$arParams[$key] = $arMoreParam; | ||
} | ||
|
@@ -98,11 +103,12 @@ public function getElementsResource($hlblockID,$arFilter=[],$arOrder=["ID" => "A | |
* @param array $arMoreParams остальные параметры group|limit|offset|count_total|runtime|data_doubling | ||
* @return array|bool | ||
*/ | ||
public function getElementList($hlblockID,$arFilter=[],$arOrder=["ID" => "ASC"],$arSelect=['*'],$arMoreParams=[]){ | ||
if(!$hlblockID) return false; | ||
$rsData = $this->getElementsResource($hlblockID,$arFilter,$arOrder,$arSelect,$arMoreParams); | ||
public function getElementList($hlblockID, $arFilter = [], $arOrder = ["ID" => "ASC"], $arSelect = ['*'], $arMoreParams = []) | ||
{ | ||
if (!$hlblockID) return false; | ||
$rsData = $this->getElementsResource($hlblockID, $arFilter, $arOrder, $arSelect, $arMoreParams); | ||
$arResult = []; | ||
while($arData = $rsData->Fetch()) { | ||
while ($arData = $rsData->Fetch()) { | ||
$arResult[] = $arData; | ||
} | ||
return $arResult; | ||
|
@@ -116,9 +122,10 @@ public function getElementList($hlblockID,$arFilter=[],$arOrder=["ID" => "ASC"], | |
* @param array $arMoreParams | ||
* @return array|false | ||
*/ | ||
public function getElement($hlblockID, $arFilter=[], $arSelect=['*'], $arMoreParams=[]){ | ||
if(!$hlblockID) return false; | ||
return $this->getElementsResource($hlblockID,$arFilter,[],$arSelect,$arMoreParams)->Fetch(); | ||
public function getElement($hlblockID, $arFilter = [], $arSelect = ['*'], $arMoreParams = []) | ||
{ | ||
if (!$hlblockID) return false; | ||
return $this->getElementsResource($hlblockID, $arFilter, [], $arSelect, $arMoreParams)->Fetch(); | ||
} | ||
|
||
/** | ||
|
@@ -128,9 +135,10 @@ public function getElement($hlblockID, $arFilter=[], $arSelect=['*'], $arMorePar | |
* @param array $arMoreParams | ||
* @return array|false | ||
*/ | ||
public function getElementById($hlblockID, $id, $arMoreParams=[]){ | ||
if(!$hlblockID) return false; | ||
return $this->getElement($hlblockID,['ID'=>$id],[],$arMoreParams); | ||
public function getElementById($hlblockID, $id, $arMoreParams = []) | ||
{ | ||
if (!$hlblockID) return false; | ||
return $this->getElement($hlblockID, ['ID' => $id], [], $arMoreParams); | ||
} | ||
|
||
/** | ||
|
@@ -139,11 +147,12 @@ public function getElementById($hlblockID, $id, $arMoreParams=[]){ | |
* @param array $arFields - поля | ||
* @return bool|int | ||
*/ | ||
public function addElement($hlblockID,$arFields=[]){ | ||
if(!$hlblockID||!$arFields) return false; | ||
public function addElement($hlblockID, $arFields = []) | ||
{ | ||
if (!$hlblockID || !$arFields) return false; | ||
$entity = $this->getEntityTable($hlblockID); | ||
$result = $entity::add($arFields); | ||
if($result->isSuccess()) { | ||
if ($result->isSuccess()) { | ||
return $result->getId(); | ||
} else { | ||
self::$LAST_ERROR = $result->getErrors(); | ||
|
@@ -157,11 +166,12 @@ public function addElement($hlblockID,$arFields=[]){ | |
* @param integer $ID - идентификатор элемента | ||
* @return bool | ||
*/ | ||
public function deleteElement($hlblockID, $ID=null) { | ||
if(!$hlblockID||!$ID) return false; | ||
public function deleteElement($hlblockID, $ID = null) | ||
{ | ||
if (!$hlblockID || !$ID) return false; | ||
$entity = $this->getEntityTable($hlblockID); | ||
$result = $entity::delete($ID); | ||
if($result->isSuccess()) { | ||
if ($result->isSuccess()) { | ||
return true; | ||
} else { | ||
self::$LAST_ERROR = $result->getErrors(); | ||
|
@@ -176,11 +186,12 @@ public function deleteElement($hlblockID, $ID=null) { | |
* @param array $arFields - обновляемые поля | ||
* @return bool | ||
*/ | ||
public function updateElement($hlblockID, $ID=null, $arFields=[]) { | ||
if(!$hlblockID||!$ID||!$arFields) return false; | ||
public function updateElement($hlblockID, $ID = null, $arFields = []) | ||
{ | ||
if (!$hlblockID || !$ID || !$arFields) return false; | ||
$entity = $this->getEntityTable($hlblockID); | ||
$result = $entity::update($ID, $arFields); | ||
if($result->isSuccess()) { | ||
if ($result->isSuccess()) { | ||
return true; | ||
} else { | ||
self::$LAST_ERROR = $result->getErrors(); | ||
|
@@ -194,12 +205,13 @@ public function updateElement($hlblockID, $ID=null, $arFields=[]) { | |
* @param int $fieldID идентификатор значения | ||
* @return bool|mixed | ||
*/ | ||
public function getFieldValue($fieldName='',$fieldID=null) { | ||
$arResult = $this->getFieldValuesList([],[ | ||
'USER_FIELD_NAME'=>$fieldName, | ||
'ID'=>$fieldID, | ||
public function getFieldValue($fieldName = '', $fieldID = null) | ||
{ | ||
$arResult = $this->getFieldValuesList([], [ | ||
'USER_FIELD_NAME' => $fieldName, | ||
'ID' => $fieldID, | ||
]); | ||
if($arResult[0]) { | ||
if ($arResult[0]) { | ||
return $arResult[0]; | ||
} | ||
return false; | ||
|
@@ -211,8 +223,9 @@ public function getFieldValue($fieldName='',$fieldID=null) { | |
* @param array $arSort сортировка | ||
* @return array | ||
*/ | ||
public function getFieldValues($fieldName='',$arSort=['SORT'=>'ASC']) { | ||
return $this->getFieldValuesList($arSort,['USER_FIELD_NAME'=>$fieldName]); | ||
public function getFieldValues($fieldName = '', $arSort = ['SORT' => 'ASC']) | ||
{ | ||
return $this->getFieldValuesList($arSort, ['USER_FIELD_NAME' => $fieldName]); | ||
} | ||
|
||
/** | ||
|
@@ -221,12 +234,13 @@ public function getFieldValues($fieldName='',$arSort=['SORT'=>'ASC']) { | |
* @param array $arFilter условия выборки | ||
* @return array | ||
*/ | ||
public function getFieldValuesList($arSort=['SORT'=>'ASC'],$arFilter=[]) { | ||
public function getFieldValuesList($arSort = ['SORT' => 'ASC'], $arFilter = []) | ||
{ | ||
$oFieldEnum = new \CUserFieldEnum; | ||
$rsValues = $oFieldEnum->GetList($arSort, $arFilter); | ||
$arResult = []; | ||
while($value = $rsValues->Fetch()) { | ||
$arResult[]=$value; | ||
while ($value = $rsValues->Fetch()) { | ||
$arResult[] = $value; | ||
} | ||
return $arResult; | ||
} | ||
|
@@ -237,60 +251,79 @@ public function getFieldValuesList($arSort=['SORT'=>'ASC'],$arFilter=[]) { | |
* @param string $codeName XML_ID значения списка | ||
* @return bool|array | ||
*/ | ||
public function getFieldValueByCode($fieldName='',$codeName='') { | ||
$arResult = $this->getFieldValuesList([],['USER_FIELD_NAME'=>$fieldName,"XML_ID"=>$codeName]); | ||
if($arResult[0]) return $arResult[0]; | ||
public function getFieldValueByCode($fieldName = '', $codeName = '') | ||
{ | ||
$arResult = $this->getFieldValuesList([], ['USER_FIELD_NAME' => $fieldName, "XML_ID" => $codeName]); | ||
if ($arResult[0]) return $arResult[0]; | ||
return false; | ||
} | ||
|
||
/** | ||
* Создает таблицу для HighloadBlock | ||
* @param string $nameHLBlock - название HL-блока, должно начинаться с заглавной буквы и состоять только из латинских букв и цифр | ||
* @param string $tableName - название таблицы для HL-блока, должно состоять только из строчных латинских букв, цифр и знака подчеркивания | ||
* @return bool|int - id HL-блока | ||
*/ | ||
public function create($nameHLBlock,$tableName){ | ||
$result = HL\HighloadBlockTable::add([ | ||
'TABLE_NAME' => $tableName, | ||
'NAME' => $nameHLBlock, | ||
]); | ||
$id = false; | ||
if (!$result->isSuccess()) { | ||
$msg = $result->getErrorMessages(); | ||
if($msg) $msg = implode(PHP_EOL,$msg); | ||
self::$LAST_ERROR = $msg; | ||
} else { | ||
$id = $result->getId(); | ||
} | ||
return $id; | ||
} | ||
/** | ||
* Создает таблицу для HighloadBlock | ||
* @param string $nameHLBlock - название HL-блока, должно начинаться с заглавной буквы и состоять только из латинских букв и цифр | ||
* @param string $tableName - название таблицы для HL-блока, должно состоять только из строчных латинских букв, цифр и знака подчеркивания | ||
* @return bool|int - id HL-блока | ||
*/ | ||
public function create($nameHLBlock, $tableName) | ||
{ | ||
$result = HL\HighloadBlockTable::add([ | ||
'TABLE_NAME' => $tableName, | ||
'NAME' => $nameHLBlock, | ||
]); | ||
$id = false; | ||
if (!$result->isSuccess()) { | ||
$msg = $result->getErrorMessages(); | ||
if ($msg) $msg = implode(PHP_EOL, $msg); | ||
self::$LAST_ERROR = $msg; | ||
} else { | ||
$id = $result->getId(); | ||
} | ||
return $id; | ||
} | ||
|
||
/** | ||
* Добавляет поле в HighloadBlock | ||
* @param integer $hlblockID - идентификатор HighloadBlock | ||
* @param array $arFields - поля, подробности https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=43&LESSON_ID=3496 | ||
* @throws $LAST_ERROR | ||
* @return int | ||
*/ | ||
public function addField($hlblockID,$arFields) { | ||
global $APPLICATION; | ||
$oUserTypeEntity = new \CUserTypeEntity(); | ||
if(empty($arFields['ENTITY_ID'])) { | ||
$arFields['ENTITY_ID'] = 'HLBLOCK_'.$hlblockID; | ||
} | ||
$id = $oUserTypeEntity->Add($arFields); | ||
if(!$id) { | ||
self::$LAST_ERROR = $APPLICATION->GetException(); | ||
} | ||
return $id; | ||
} | ||
/** | ||
* Добавляет поле в HighloadBlock | ||
* @param integer $hlblockID - идентификатор HighloadBlock | ||
* @param array $arFields - поля, подробности https://dev.1c-bitrix.ru/learning/course/?COURSE_ID=43&LESSON_ID=3496 | ||
* @return int | ||
* @throws $LAST_ERROR | ||
*/ | ||
public function addField($hlblockID, $arFields) | ||
{ | ||
global $APPLICATION; | ||
$oUserTypeEntity = new \CUserTypeEntity(); | ||
if (empty($arFields['ENTITY_ID'])) { | ||
$arFields['ENTITY_ID'] = 'HLBLOCK_' . $hlblockID; | ||
} | ||
$id = $oUserTypeEntity->Add($arFields); | ||
if (!$id) { | ||
self::$LAST_ERROR = $APPLICATION->GetException(); | ||
} | ||
return $id; | ||
} | ||
|
||
/** | ||
* Удаляет HighloadBlock по $hlblockID | ||
* @param integer $hlblockID - идентификатор HighloadBlock | ||
* @return \Bitrix\Main\DB\Result|Entity\DeleteResult | ||
*/ | ||
public function deleteHighloadBlock($hlblockID) { | ||
return HL\HighloadBlockTable::delete($hlblockID); | ||
} | ||
/** | ||
* Возвращает поля таблица | ||
* @param int $hlblockID - идентификатор таблицы HL | ||
* @return \Bitrix\Main\ORM\Fields\Field[]|bool | ||
* @since 1.0.2 | ||
*/ | ||
public function getFields($hlblockID) | ||
{ | ||
if (!$hlblockID) return false; | ||
$hlblock = HL\HighloadBlockTable::getById($hlblockID)->fetch(); | ||
if (!$hlblock) return false; | ||
$entity = HL\HighloadBlockTable::compileEntity($hlblock); | ||
return $entity->getFields(); | ||
} | ||
|
||
/** | ||
* Удаляет HighloadBlock по $hlblockID | ||
* @param integer $hlblockID - идентификатор HighloadBlock | ||
* @return \Bitrix\Main\DB\Result|Entity\DeleteResult | ||
*/ | ||
public function deleteHighloadBlock($hlblockID) | ||
{ | ||
return HL\HighloadBlockTable::delete($hlblockID); | ||
} | ||
} |