1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-01 05:55:02 +03:00
ifhub.club/application/classes/hooks/HookProperty.class.php

119 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* LiveStreet CMS
* Copyright © 2013 OOO "ЛС-СОФТ"
*
* ------------------------------------------------------
*
* Official site: www.livestreetcms.com
* Contact e-mail: office@livestreetcms.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* ------------------------------------------------------
*
* @link http://www.livestreetcms.com
* @copyright 2013 OOO "ЛС-СОФТ"
* @author Maxim Mzhelskiy <rus.engine@gmail.com>
*
*/
/**
* Хук для работы свойств(дополнительных полей сущности)
*/
class HookProperty extends Hook {
public function RegisterHook() {
$this->AddHook('lang_init_start','InitStart',null,-10000);
$this->AddHook('module_orm_GetItemsByFilter_after','GetItemsByFilterAfter',null,10000);
$this->AddHook('module_orm_GetItemsByFilter_before','GetItemsByFilterBefore',null,10000);
$this->AddHook('module_orm_GetByFilter_before','GetItemsByFilterBefore',null,10000);
}
public function InitStart() {
/**
* Регистрируем кастомный загрузчик классов
*/
spl_autoload_register(array($this,'autoload'),true,true);
/**
* Добавляем через наследование в объекты необходимый функционал по работе со свойствами EAV
*/
$aTargetTypes=$this->Property_GetTargetTypes();
$aUseEntities=array();
foreach($aTargetTypes as $sType=>$aParams) {
/**
* Защита от дубля при наследовании
*/
if (!in_array($aParams['entity'],$aUseEntities)) {
$this->Plugin_Inherit($aParams['entity'],'ModuleProperty_Target_'.$aParams['entity'],'ModuleProperty');
$aUseEntities[]=$aParams['entity'];
}
}
}
/**
* Дополнительная пост-обработка результатов запроса ORM
*
* @param $aParams
*/
public function GetItemsByFilterAfter($aParams) {
$aEntities=$aParams['aEntities'];
$aFilter=$aParams['aFilter'];
$this->Property_RewriteGetItemsByFilter($aEntities,$aFilter);
}
/**
* Обработка фильтра для запросов к ORM
*
* @param $aParams
*/
public function GetItemsByFilterBefore($aParams) {
$aFilter=$this->Property_RewriteFilter($aParams['aFilter'],$aParams['sEntityFull']);
$aParams['aFilter']=$aFilter;
}
/**
* Автозагрузчик классов
* Создает новый фейковый класс для создания цепочки наследования
* Поддерживаются только ORM сущности
* TODO: продумать использование сценариев валидации отличных от дефолтного
*
* @param string $sClassName
*/
public function autoload($sClassName) {
if (preg_match("#^ModuleProperty_Target_(.+)$#i",$sClassName,$aMatch)) {
$sClass="
class {$sClassName} extends ModuleProperty_Inherit_{$aMatch[1]} {
public function Init() {
parent::Init();
\$this->aValidateRules[]=array('properties','properties_check');
}
public function ValidatePropertiesCheck() {
return \$this->Property_ValidateEntityPropertiesCheck(\$this);
}
protected function afterSave() {
parent::afterSave();
\$this->Property_UpdatePropertiesValue(\$this->getPropertiesObject(),\$this);
}
protected function afterDelete() {
parent::afterDelete();
\$this->Property_RemovePropertiesValue(\$this);
}
public function getPropertyValue(\$sPropertyId) {
return \$this->Property_GetEntityPropertyValue(\$this,\$sPropertyId);
}
public function getProperty(\$sPropertyId) {
return \$this->Property_GetEntityProperty(\$this,\$sPropertyId);
}
public function getPropertyList() {
return \$this->Property_GetEntityPropertyList(\$this);
}
}";
eval($sClass);
}
}
}