1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-06-30 21:45:01 +03:00
ifhub.club/application/classes/modules/property/entity/ValueTypeSelect.entity.class.php

180 lines
6.2 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>
*
*/
/**
* Объект управления типом select
*
* @package application.modules.property
* @since 2.0
*/
class ModuleProperty_EntityValueTypeSelect extends ModuleProperty_EntityValueType
{
public function getValueForDisplay()
{
$oValue = $this->getValueObject();
$aValues = $oValue->getDataOne('values');
return is_array($aValues) ? join(', ', $aValues) : '';
}
public function isEmpty()
{
$oValue = $this->getValueObject();
$aValues = $oValue->getDataOne('values');
return $aValues ? false : true;
}
public function getValueForForm()
{
$oValue = $this->getValueObject();
$aValues = $oValue->getDataOne('values');
return $aValues;
}
public function validate()
{
$oProperty = $this->getValueObject()->getProperty();
$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 $this->Lang_Get('property.notices.validate_value_select_max', array('count' => $oProperty->getValidateRuleOne('max')));
}
if ($oProperty->getValidateRuleOne('min') and count($iValue) < $oProperty->getValidateRuleOne('min')) {
return $this->Lang_Get('property.notices.validate_value_select_min', array('count' => $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 $this->Lang_Get('property.notices.validate_value_select_wrong');
}
} elseif (count($iValue) == 1) {
$iValue = (int)reset($iValue);
} else {
return $this->Lang_Get('property.notices.validate_value_select_only_one');
}
}
/**
* Проверяем значение
*/
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 ($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());
}
/**
* Дополнительная обработка перед сохранением значения
*/
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();
}
}
}
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;
}
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();
}
}
}
}