1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-06-16 23:00:51 +03:00

Поддержка полей с типом select - выбор из списка

This commit is contained in:
Mzhelskiy Maxim 2013-10-29 14:53:26 +07:00
parent c9cef23a45
commit d142237439
9 changed files with 271 additions and 2 deletions

View file

@ -29,6 +29,7 @@ class ModuleProperty extends ModuleORM {
const PROPERTY_TYPE_CHECKBOX='checkbox';
const PROPERTY_TYPE_TAGS='tags';
const PROPERTY_TYPE_VIDEO_LINK='video_link';
const PROPERTY_TYPE_SELECT='select';
protected $oMapper=null;
/**
@ -37,7 +38,7 @@ class ModuleProperty extends ModuleORM {
* @var array
*/
protected $aPropertyTypes=array(
self::PROPERTY_TYPE_INT,self::PROPERTY_TYPE_FLOAT,self::PROPERTY_TYPE_VARCHAR,self::PROPERTY_TYPE_TEXT,self::PROPERTY_TYPE_CHECKBOX,self::PROPERTY_TYPE_TAGS,self::PROPERTY_TYPE_VIDEO_LINK
self::PROPERTY_TYPE_INT,self::PROPERTY_TYPE_FLOAT,self::PROPERTY_TYPE_VARCHAR,self::PROPERTY_TYPE_TEXT,self::PROPERTY_TYPE_CHECKBOX,self::PROPERTY_TYPE_TAGS,self::PROPERTY_TYPE_VIDEO_LINK,self::PROPERTY_TYPE_SELECT
);
/**
* Список разрешенных типов
@ -493,6 +494,7 @@ class ModuleProperty extends ModuleORM {
return array_combine($aKeys,array_values($aArray));
}
/**
* TODO: нужно учитывать сброс кеша
* Удаляет теги свойства у сущности
*
* @param string $sTargetType Тип объекта сущности
@ -504,6 +506,19 @@ class ModuleProperty extends ModuleORM {
public function RemoveValueTagsByTarget($sTargetType,$iTargetId,$iPropertyId) {
return $this->oMapper->RemoveValueTagsByTarget($sTargetType,$iTargetId,$iPropertyId);
}
/**
* TODO: нужно учитывать сброс кеша
* Удаляет значения типа select
*
* @param string $sTargetType Тип объекта сущности
* @param int $iTargetId ID объекта сущности
* @param int $iPropertyId ID свойства
*
* @return mixed
*/
public function RemoveValueSelectsByTarget($sTargetType,$iTargetId,$iPropertyId) {
return $this->oMapper->RemoveValueSelectsByTarget($sTargetType,$iTargetId,$iPropertyId);
}
/**
* Возвращает список тегов/знаяений свойства. Используется для авкомплиттера тегов.
*

View file

@ -32,7 +32,7 @@ class ModuleProperty_EntityProperty extends EntityORM {
);
protected $aRelations=array(
'selects' => array(self::RELATION_TYPE_HAS_MANY,'ModuleProperty_EntitySelect','property_id',array('#order'=>array('sort'=>'desc'))),
);
public function ValidateCheckType() {
@ -89,6 +89,20 @@ class ModuleProperty_EntityProperty extends EntityORM {
}
return $aData;
}
/**
* Возвращает конкретное правило валидации
*
* @param string $sRule
*
* @return null|mixed
*/
public function getValidateRuleOne($sRule) {
$aData=$this->getValidateRules();
if (isset($aData[$sRule])) {
return $aData[$sRule];
}
return null;
}
/**
* Устанавливает правила валидации поля
*

View file

@ -0,0 +1,32 @@
<?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 ModuleProperty_EntitySelect extends EntityORM {
protected $aValidateRules=array(
);
protected $aRelations=array(
);
}

View file

@ -0,0 +1,28 @@
<?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 ModuleProperty_EntityValueSelect extends EntityORM {
protected $aRelations=array(
);
}

View file

@ -69,6 +69,10 @@ class ModuleProperty_EntityValueType extends Entity {
* Удаляем из таблицы тегов
*/
$this->Property_RemoveValueTagsByTarget($oValue->getTargetType(),$oValue->getTargetId(),$oValue->getPropertyId());
/**
* Удаляем из таблицы селектов
*/
$this->Property_RemoveValueSelectsByTarget($oValue->getTargetType(),$oValue->getTargetId(),$oValue->getPropertyId());
}
public function prepareValidateRulesRaw($aRulesRaw) {

View file

@ -0,0 +1,120 @@
<?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 ModuleProperty_EntityValueTypeSelect extends ModuleProperty_EntityValueType {
public function getValueForDisplay() {
return $this->getValueObject()->getValueText();
}
public function getValueForForm() {
$oValue=$this->getValueObject();
$aValues=$oValue->getDataOne('values');
return $aValues;
}
public function validate() {
$oProperty=$this->getValueObject()->getProperty();
$iValue=$this->getValueForValidate();
if (!$iValue and $oProperty->getValidateRuleOne('allowEmpty')) {
return true;
}
if (is_array($iValue)) {
if ($oProperty->getValidateRuleOne('allowMany')) {
if ($oProperty->getValidateRuleOne('max') and count($iValue)>$oProperty->getValidateRuleOne('max')) {
return 'Максимально можно выбрать только '.$oProperty->getValidateRuleOne('max').' элемента';
}
if ($oProperty->getValidateRuleOne('min') and count($iValue)<$oProperty->getValidateRuleOne('min')) {
return 'Минимально можно выбрать только '.$oProperty->getValidateRuleOne('min').' элемента';
}
/**
* Для безопасности
*/
$aValues=array();
foreach($iValue as $iV) {
$aValues[]=(int)$iV;
}
if (count($aValues)==count($this->Property_GetSelectItemsByFilter(array('property_id'=>$oProperty->getId(),'id in'=>$aValues)))) {
$this->setValueForValidate($aValues);
return true;
} else {
return 'Проверьте корректность выбранных элементов';
}
} elseif (count($iValue)==1) {
$iValue=(int)reset($iValue);
} else {
return 'Можно выбрать только один элемент';
}
}
/**
* Проверяем значение
*/
if ($oSelect=$this->Property_GetSelectByIdAndPropertyId($iValue,$oProperty->getId())) {
return true;
}
return 'Необходимо выбрать значение';
}
public function setValue($mValue) {
$this->resetAllValue();
$oValue=$this->getValueObject();
$oProperty=$oValue->getProperty();
$aValues=array();
/**
* Сохраняем с data, т.к. может быть множественный выбор
*/
if (is_array($mValue)) {
$aSelectItems=$this->Property_GetSelectItemsByFilter(array('property_id'=>$oProperty->getId(),'id in'=>$mValue));
foreach($aSelectItems as $oSelect) {
$aValues[$oSelect->getId()]=$oSelect->getValue();
}
} else {
if ($oSelect=$this->Property_GetSelectByIdAndPropertyId($mValue,$oProperty->getId())) {
$aValues[$oSelect->getId()]=$oSelect->getValue();
}
}
foreach($aValues as $k=>$v) {
$oSelect=Engine::GetEntity('ModuleProperty_EntityValueSelect');
$oSelect->setPropertyId($oValue->getPropertyId());
$oSelect->setTargetType($oValue->getTargetType());
$oSelect->setTargetId($oValue->getTargetId());
$oSelect->setSelectId($k);
$oSelect->Add();
}
$oValue->setData($aValues ? array('values'=>$aValues) : array());
}
public function prepareValidateRulesRaw($aRulesRaw) {
$aRules=array();
$aRules['allowEmpty']=isset($aRulesRaw['allowEmpty']) ? false : true;
$aRules['allowMany']=isset($aRulesRaw['allowMany']) ? true : false;
if (isset($aRulesRaw['max']) and is_numeric($aRulesRaw['max'])) {
$aRules['max']=(int)$aRulesRaw['max'];
}
if (isset($aRulesRaw['min']) and is_numeric($aRulesRaw['min'])) {
$aRules['min']=(int)$aRulesRaw['min'];
}
return $aRules;
}
}

View file

@ -118,6 +118,22 @@ class ModuleProperty_MapperProperty extends Mapper {
return false;
}
public function RemoveValueSelectsByTarget($sTargetType,$iTargetId,$iPropertyId) {
$sql = "DELETE
FROM ".Config::Get('db.table.property_value_select')."
WHERE
target_id = ?d
and
target_type = ?
and
property_id = ?d
";
if ($this->oDb->query($sql,$iTargetId, $sTargetType, $iPropertyId)!==false) {
return true;
}
return false;
}
public function GetPropertyTagsByLike($sTag,$iPropertyId,$iLimit) {
$sTag=mb_strtolower($sTag,"UTF-8");
$sql = "SELECT

View file

@ -284,8 +284,10 @@ $config['db']['table']['geo_city'] = '___db.table.prefix___geo_city';
$config['db']['table']['geo_target'] = '___db.table.prefix___geo_target';
$config['db']['table']['user_changemail'] = '___db.table.prefix___user_changemail';
$config['db']['table']['property'] = '___db.table.prefix___property';
$config['db']['table']['property_select'] = '___db.table.prefix___property_select';
$config['db']['table']['property_value'] = '___db.table.prefix___property_value';
$config['db']['table']['property_value_tag'] = '___db.table.prefix___property_value_tag';
$config['db']['table']['property_value_select'] = '___db.table.prefix___property_value_select';
$config['db']['tables']['engine'] = 'InnoDB'; // InnoDB или MyISAM

View file

@ -101,4 +101,42 @@ CREATE TABLE IF NOT EXISTS `prefix_property_value_tag` (
KEY `target_id` (`target_id`),
KEY `text` (`text`),
KEY `property_id` (`property_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 29-10-2013
--
-- Структура таблицы `prefix_property_select`
--
CREATE TABLE IF NOT EXISTS `prefix_property_select` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`property_id` int(11) NOT NULL,
`target_type` varchar(50) NOT NULL,
`value` varchar(250) NOT NULL,
`sort` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `property_id` (`property_id`),
KEY `target_type` (`target_type`),
KEY `sort` (`sort`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Структура таблицы `prefix_property_value_select`
--
CREATE TABLE IF NOT EXISTS `prefix_property_value_select` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`property_id` int(11) NOT NULL,
`target_type` varchar(50) NOT NULL,
`target_id` int(11) NOT NULL,
`select_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `target_type` (`target_type`),
KEY `target_id` (`target_id`),
KEY `property_id` (`property_id`),
KEY `select_id` (`select_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;