2013-10-29 09:53:26 +02:00
|
|
|
|
<?php
|
2014-10-08 08:20:29 +03:00
|
|
|
|
/*
|
2013-10-29 09:53:26 +02:00
|
|
|
|
* 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>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-10-08 08:20:29 +03:00
|
|
|
|
/**
|
|
|
|
|
* Объект управления типом select
|
|
|
|
|
*
|
|
|
|
|
* @package application.modules.property
|
|
|
|
|
* @since 2.0
|
|
|
|
|
*/
|
2014-10-08 11:49:34 +03:00
|
|
|
|
class ModuleProperty_EntityValueTypeSelect extends ModuleProperty_EntityValueType
|
|
|
|
|
{
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function getValueForDisplay()
|
|
|
|
|
{
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
$aValues = $oValue->getDataOne('values');
|
|
|
|
|
return is_array($aValues) ? join(', ', $aValues) : '';
|
|
|
|
|
}
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2015-02-13 13:17:16 +02:00
|
|
|
|
public function isEmpty()
|
|
|
|
|
{
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
$aValues = $oValue->getDataOne('values');
|
|
|
|
|
return $aValues ? false : true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function getValueForForm()
|
|
|
|
|
{
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
$aValues = $oValue->getDataOne('values');
|
|
|
|
|
return $aValues;
|
|
|
|
|
}
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function validate()
|
|
|
|
|
{
|
|
|
|
|
$oProperty = $this->getValueObject()->getProperty();
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
$iValue = $this->getValueForValidate();
|
|
|
|
|
if (is_array($iValue)) {
|
|
|
|
|
$iValue = array_filter($iValue);
|
|
|
|
|
}
|
|
|
|
|
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 'Необходимо выбрать значение';
|
|
|
|
|
}
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function setValue($mValue)
|
|
|
|
|
{
|
|
|
|
|
$this->resetAllValue();
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
$oProperty = $oValue->getProperty();
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
$aValues = array();
|
|
|
|
|
/**
|
|
|
|
|
* Сохраняем с data, т.к. может быть множественный выбор
|
|
|
|
|
*/
|
|
|
|
|
if ($mValue) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$oValue->setData($aValues ? array('values' => $aValues) : array());
|
|
|
|
|
}
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
/**
|
|
|
|
|
* Дополнительная обработка перед сохранением значения
|
|
|
|
|
*/
|
|
|
|
|
public function beforeSaveValue()
|
|
|
|
|
{
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
if ($aValues = $oValue->getData()) {
|
|
|
|
|
foreach ($aValues['values'] 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-06 10:32:39 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function prepareValidateRulesRaw($aRulesRaw)
|
|
|
|
|
{
|
|
|
|
|
$aRules = array();
|
|
|
|
|
$aRules['allowEmpty'] = isset($aRulesRaw['allowEmpty']) ? false : true;
|
|
|
|
|
$aRules['allowMany'] = isset($aRulesRaw['allowMany']) ? true : false;
|
2013-10-29 09:53:26 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
2014-01-08 13:15:46 +02:00
|
|
|
|
|
2014-10-08 11:49:34 +03:00
|
|
|
|
public function removeValue()
|
|
|
|
|
{
|
|
|
|
|
$oValue = $this->getValueObject();
|
|
|
|
|
/**
|
|
|
|
|
* Удаляем значения select'а из дополнительной таблицы
|
|
|
|
|
*/
|
|
|
|
|
if ($aSelects = $this->Property_GetValueSelectItemsByFilter(array(
|
|
|
|
|
'property_id' => $oValue->getPropertyId(),
|
|
|
|
|
'target_id' => $oValue->getTargetId()
|
|
|
|
|
))
|
|
|
|
|
) {
|
|
|
|
|
foreach ($aSelects as $oSelect) {
|
|
|
|
|
$oSelect->Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-29 09:53:26 +02:00
|
|
|
|
}
|