mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-06-16 23:00:51 +03:00
Доработка категорий - учет количество объектов/элементов для каждой категории
This commit is contained in:
parent
49a39600aa
commit
7019ed553d
|
@ -201,10 +201,11 @@ class ModuleCategory extends ModuleORM {
|
|||
*
|
||||
* @param array $aCategoryId
|
||||
* @param int $iType
|
||||
* @param bool $bReturnObjects
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function ValidateCategoryArray($aCategoryId,$iType) {
|
||||
public function ValidateCategoryArray($aCategoryId,$iType,$bReturnObjects=false) {
|
||||
if (!is_array($aCategoryId)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -212,12 +213,12 @@ class ModuleCategory extends ModuleORM {
|
|||
foreach($aCategoryId as $iId) {
|
||||
$aIds[]=(int)$iId;
|
||||
}
|
||||
if ($aIds and $aCategories=$this->GetCategoryItemsByFilter(array('id in'=>$aIds,'type_id'=>$iType))) {
|
||||
$aResultId=array();
|
||||
foreach($aCategories as $oCategory) {
|
||||
$aResultId[]=$oCategory->getId();
|
||||
if ($aIds and $aCategories=$this->GetCategoryItemsByFilter(array('id in'=>$aIds,'type_id'=>$iType,'#index-from-primary'))) {
|
||||
if ($bReturnObjects) {
|
||||
return $aCategories;
|
||||
} else {
|
||||
return array_keys($aCategories);
|
||||
}
|
||||
return $aResultId;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -226,8 +227,9 @@ class ModuleCategory extends ModuleORM {
|
|||
*
|
||||
* @param $oTarget
|
||||
* @param $sTargetType
|
||||
* @param $mCallbackCountTarget
|
||||
*/
|
||||
public function SaveCategories($oTarget,$sTargetType) {
|
||||
public function SaveCategories($oTarget,$sTargetType,$mCallbackCountTarget=null) {
|
||||
$aCategoriesId=$oTarget->_getDataOne('_categories_for_save');
|
||||
if (!is_array($aCategoriesId)) {
|
||||
return;
|
||||
|
@ -235,22 +237,63 @@ class ModuleCategory extends ModuleORM {
|
|||
/**
|
||||
* Удаляем текущие связи
|
||||
*/
|
||||
$this->RemoveRelation($oTarget->_getPrimaryKeyValue(),$sTargetType);
|
||||
$aCategoryIdChanged=$this->RemoveRelation($oTarget->_getPrimaryKeyValue(),$sTargetType);
|
||||
/**
|
||||
* Создаем
|
||||
*/
|
||||
$this->CreateRelation($aCategoriesId,$oTarget->_getPrimaryKeyValue(),$sTargetType);
|
||||
/**
|
||||
* Полный список категорий, которые затронули изменения
|
||||
*/
|
||||
$aCategoryIdChanged=array_merge($aCategoryIdChanged,$aCategoriesId);
|
||||
/**
|
||||
* Подсчитываем количество новое элементов для каждой категории
|
||||
*/
|
||||
$this->UpdateCountTarget($aCategoryIdChanged,$sTargetType,$mCallbackCountTarget);
|
||||
|
||||
$oTarget->_setData(array('_categories_for_save'=>null));
|
||||
}
|
||||
/**
|
||||
* Обновляет количество элементов у категорий (поле count_target в таблице категорий)
|
||||
*
|
||||
* @param $aCategoryId
|
||||
* @param $sTargetType
|
||||
* @param null $mCallback
|
||||
*/
|
||||
protected function UpdateCountTarget($aCategoryId,$sTargetType,$mCallback=null) {
|
||||
if (!is_array($aCategoryId)) {
|
||||
$aCategoryId=array($aCategoryId);
|
||||
}
|
||||
if (!count($aCategoryId)) {
|
||||
return;
|
||||
}
|
||||
$aCategories=$this->GetCategoryItemsByArrayId($aCategoryId);
|
||||
foreach($aCategories as $oCategory) {
|
||||
if ($mCallback) {
|
||||
if (is_string($mCallback)) {
|
||||
$mCallback=array($this,$mCallback);
|
||||
}
|
||||
$iCount=call_user_func_array($mCallback,array($oCategory,$sTargetType));
|
||||
} else {
|
||||
$iCount=$this->GetCountItemsByFilter(array('category_id'=>$oCategory->getId()),'ModuleCategory_EntityTarget');
|
||||
}
|
||||
$oCategory->setCountTarget($iCount);
|
||||
$oCategory->Update();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Удаляет категории у объекта
|
||||
*
|
||||
* @param $oTarget
|
||||
* @param $sTargetType
|
||||
* @param $mCallbackCountTarget
|
||||
*/
|
||||
public function RemoveCategories($oTarget,$sTargetType) {
|
||||
$this->RemoveRelation($oTarget->_getPrimaryKeyValue(),$sTargetType);
|
||||
public function RemoveCategories($oTarget,$sTargetType,$mCallbackCountTarget=null) {
|
||||
$aCategoryIdChanged=$this->RemoveRelation($oTarget->_getPrimaryKeyValue(),$sTargetType);
|
||||
/**
|
||||
* Подсчитываем количество новое элементов для каждой категории
|
||||
*/
|
||||
$this->UpdateCountTarget($aCategoryIdChanged,$sTargetType,$mCallbackCountTarget);
|
||||
}
|
||||
/**
|
||||
* Создает новую связь конкретного объекта с категориями
|
||||
|
@ -294,7 +337,7 @@ class ModuleCategory extends ModuleORM {
|
|||
* @param int $iTargetId
|
||||
* @param int|string $iType type_id или target_type
|
||||
*
|
||||
* @return bool
|
||||
* @return bool|array
|
||||
*/
|
||||
public function RemoveRelation($iTargetId,$iType) {
|
||||
if (!is_numeric($iType)) {
|
||||
|
@ -304,11 +347,13 @@ class ModuleCategory extends ModuleORM {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
$aRemovedCategory=array();
|
||||
$aTargets=$this->GetTargetItemsByTargetIdAndTypeId($iTargetId,$iType);
|
||||
foreach($aTargets as $oTarget) {
|
||||
$oTarget->Delete();
|
||||
$aRemovedCategory[]=$oTarget->getCategoryId();
|
||||
}
|
||||
return true;
|
||||
return $aRemovedCategory;
|
||||
}
|
||||
/**
|
||||
* Возвращает список категорий по категории
|
||||
|
@ -374,5 +419,4 @@ class ModuleCategory extends ModuleORM {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -39,6 +39,9 @@ class ModuleCategory_BehaviorEntity extends Behavior {
|
|||
'validate_from_request'=>true,
|
||||
'validate_min'=>1,
|
||||
'validate_max'=>5,
|
||||
'validate_only_without_children'=>false,
|
||||
|
||||
'callback_count_target'=>null,
|
||||
);
|
||||
/**
|
||||
* Список хуков
|
||||
|
@ -80,14 +83,14 @@ class ModuleCategory_BehaviorEntity extends Behavior {
|
|||
* Выполняется после сохранения сущности
|
||||
*/
|
||||
public function CallbackAfterSave() {
|
||||
$this->Category_SaveCategories($this->oObject,$this->getParam('target_type'));
|
||||
$this->Category_SaveCategories($this->oObject,$this->getParam('target_type'),$this->getParam('callback_count_target'));
|
||||
}
|
||||
/**
|
||||
* Коллбэк
|
||||
* Выполняется после удаления сущности
|
||||
*/
|
||||
public function CallbackAfterDelete() {
|
||||
$this->Category_RemoveCategories($this->oObject,$this->getParam('target_type'));
|
||||
$this->Category_RemoveCategories($this->oObject,$this->getParam('target_type'),$this->getParam('callback_count_target'));
|
||||
}
|
||||
/**
|
||||
* Дополнительный метод для сущности
|
||||
|
@ -121,24 +124,31 @@ class ModuleCategory_BehaviorEntity extends Behavior {
|
|||
/**
|
||||
* Проверяем наличие категорий в БД
|
||||
*/
|
||||
$aCategoriesId=$this->Category_ValidateCategoryArray($mValue,$oTypeCategory->getId());
|
||||
if (!$aCategoriesId) {
|
||||
$aCategoriesId=array();
|
||||
$aCategories=$this->Category_ValidateCategoryArray($mValue,$oTypeCategory->getId(),true);
|
||||
if (!$aCategories) {
|
||||
$aCategories=array();
|
||||
}
|
||||
|
||||
if ($this->getParam('validate_require') and !$aCategoriesId) {
|
||||
if ($this->getParam('validate_require') and !$aCategories) {
|
||||
return 'Необходимо выбрать категорию';
|
||||
}
|
||||
if (!$this->getParam('multiple') and count($aCategoriesId)>1) {
|
||||
$aCategoriesId=array_slice($aCategoriesId,0,1);
|
||||
if (!$this->getParam('multiple') and count($aCategories)>1) {
|
||||
$aCategories=array_slice($aCategories,0,1);
|
||||
}
|
||||
if ($this->getParam('multiple') and $aCategoriesId and ( count($aCategoriesId)<$this->getParam('validate_min') or count($aCategoriesId)>$this->getParam('validate_max'))) {
|
||||
if ($this->getParam('multiple') and $aCategories and ( count($aCategories)<$this->getParam('validate_min') or count($aCategories)>$this->getParam('validate_max'))) {
|
||||
return 'Количество категорий должно быть от '.$this->getParam('validate_min').' до '.$this->getParam('validate_max');
|
||||
}
|
||||
if ($this->getParam('validate_only_without_children')) {
|
||||
foreach($aCategories as $oCategory) {
|
||||
if ($oCategory->getChildren()) {
|
||||
return 'Для выбора доступны только конечные категории';
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Сохраняем необходимый список категорий для последующего сохранения в БД
|
||||
*/
|
||||
$this->oObject->_setData(array('_categories_for_save'=>$aCategoriesId));
|
||||
$this->oObject->_setData(array('_categories_for_save'=>array_keys($aCategories)));
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
|
|
|
@ -166,4 +166,77 @@ class ModuleCategory_EntityCategory extends EntityORM {
|
|||
public function getUrlAdminRemove() {
|
||||
return Router::GetPath('admin/categories/'.$this->getTypeByCacheLife()->getTargetType().'/remove/'.$this->getId());
|
||||
}
|
||||
/**
|
||||
* Возвращает список дополнительных данных
|
||||
*
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getData() {
|
||||
$aData=@unserialize($this->_getDataOne('data'));
|
||||
if (!$aData) {
|
||||
$aData=array();
|
||||
}
|
||||
return $aData;
|
||||
}
|
||||
/**
|
||||
* Устанавливает список дополнительня данных
|
||||
*
|
||||
* @param $aRules
|
||||
*/
|
||||
public function setData($aRules) {
|
||||
$this->_aData['data']=@serialize($aRules);
|
||||
}
|
||||
/**
|
||||
* Возвращает данные по конкретному ключу
|
||||
*
|
||||
* @param $sKey
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function getDataOne($sKey) {
|
||||
$aData=$this->getData();
|
||||
if (isset($aData[$sKey])) {
|
||||
return $aData[$sKey];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Устанваливает данные для конкретного ключа
|
||||
*
|
||||
* @param $sKey
|
||||
* @param $mValue
|
||||
*/
|
||||
public function setDataOne($sKey,$mValue) {
|
||||
$aData=$this->getData();
|
||||
$aData[$sKey]=$mValue;
|
||||
$this->setData($aData);
|
||||
}
|
||||
/**
|
||||
* Возвращает сумму значений по ключу для всех потомков, включая себя
|
||||
*
|
||||
* @param $sKey
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function getDataOneSumDescendants($sKey) {
|
||||
$iResult=$this->getDataOne($sKey);
|
||||
$aChildren=$this->getDescendants();
|
||||
foreach($aChildren as $oItem) {
|
||||
$iResult+=$oItem->getDataOne($sKey);
|
||||
}
|
||||
return $iResult;
|
||||
}
|
||||
/**
|
||||
* Возвращает количество таргетов (объектов) для всех потомков, включая себя
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCountTargetOfDescendants() {
|
||||
$iCount=$this->getCountTarget();
|
||||
$aChildren=$this->getDescendants();
|
||||
foreach($aChildren as $oItem) {
|
||||
$iCount+=$oItem->getCountTarget();
|
||||
}
|
||||
return $iCount;
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
Subproject commit f909f214de3318a4a3037ad5801f277f7b361d15
|
||||
Subproject commit 1680148b56ddf820fefb1aec3fbd53415fb94aad
|
Loading…
Reference in a new issue