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

Доработка дополнительных полей + новый тип поля "Дата"

This commit is contained in:
Mzhelskiy Maxim 2014-02-11 13:44:52 +07:00
parent ed96a3f1ed
commit 344d5d3357
6 changed files with 236 additions and 5 deletions

View file

@ -88,9 +88,25 @@ class ModuleProperty_EntityProperty extends EntityORM {
return true;
}
/**
* Выполняется перед сохранением сущности
*
* @return bool
*/
protected function beforeSave() {
if ($this->_isNew()) {
$this->setDateCreate(date("Y-m-d H:i:s"));
$oValue=Engine::GetEntity('ModuleProperty_EntityValue',array('property_type'=>$this->getType(),'property_id'=>$this->getId(),'target_type'=>$this->getTargetType(),'target_id'=>$this->getId()));
$oValueType=$oValue->getValueTypeObject();
/**
* Выставляем дефолтные значения параметров
*/
$this->setParams($oValueType->getParamsDefault());
/**
* Выставляем дефолтные значения параметров валидации
*/
$this->setValidateRules($oValueType->getValidateRulesDefault());
}
return true;
}
@ -130,6 +146,16 @@ class ModuleProperty_EntityProperty extends EntityORM {
}
return $aData;
}
/**
* Возвращает экранированный список правил валидации
*
* @return array
*/
public function getValidateRulesEscape() {
$aRules=$this->getValidateRules();
func_htmlspecialchars($aRules);
return $aRules;
}
/**
* Возвращает конкретное правило валидации
*
@ -164,6 +190,16 @@ class ModuleProperty_EntityProperty extends EntityORM {
}
return $aData;
}
/**
* Возвращает экранированный список параметров
*
* @return array
*/
public function getParamsEscape() {
$aParams=$this->getParams();
func_htmlspecialchars($aParams);
return $aParams;
}
/**
* Устанавливает список дополнительных параметров поля
*

View file

@ -22,6 +22,7 @@
class ModuleProperty_EntityValueType extends Entity {
public function getValueForDisplay() {
// TODO: getValue() всегда вернет null
return $this->getValueObject()->getValue();
}
@ -33,7 +34,7 @@ class ModuleProperty_EntityValueType extends Entity {
return 'Неверное значение';
}
protected function validateStandart($sTypeValidator,$aParamsAdditional=array()) {
protected function validateStandart($sTypeValidator,$aParamsAdditional=array(),$sFieldForValidate='value_for_validate') {
$oProperty=$this->getValueObject()->getProperty();
/**
* Получаем параметры валидации
@ -45,7 +46,7 @@ class ModuleProperty_EntityValueType extends Entity {
$aParams=array_merge($aParams,$aParamsAdditional);
$oValidator=$this->Validate_CreateValidator($sTypeValidator,$this,null,$aParams);
$oValidator->fields=array('value_for_validate');
$oValidator->fields=array($sFieldForValidate);
$oValidator->validateEntity($this);
if ($this->_hasValidateErrors()) {
return $this->_getValidateError();
@ -64,6 +65,7 @@ class ModuleProperty_EntityValueType extends Entity {
$oValue->setValueFloat(null);
$oValue->setValueVarchar(null);
$oValue->setValueText(null);
$oValue->setValueDate(null);
$oValue->setData(null);
/**
* Удаляем из таблицы тегов
@ -79,10 +81,18 @@ class ModuleProperty_EntityValueType extends Entity {
return array();
}
public function getValidateRulesDefault() {
return array();
}
public function prepareParamsRaw($aParamsRaw) {
return array();
}
public function getParamsDefault() {
return array();
}
public function beforeSaveValue() {
}

View file

@ -0,0 +1,149 @@
<?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_EntityValueTypeDate extends ModuleProperty_EntityValueType {
protected $sFormatDateInput='dd.MM.yyyy';
public function getValueForDisplay() {
$oValue=$this->getValueObject();
$oProperty=$oValue->getProperty();
return $oValue->getValueDate() ? date($oProperty->getParam('format_out'),strtotime($oValue->getValueDate())) : '';
}
public function getValueForForm() {
$oValue=$this->getValueObject();
$sDate=$oValue->getValueDate();
// TODO: нужен конвертор формата дат вида Y в yyyy для учета $this->sFormatDateInput
return $sDate ? date('d.m.Y',strtotime($sDate)) : '';
}
public function getValueTimeH() {
$sDate=$this->getValueObject()->getValueDate();
return $sDate ? date('H',strtotime($sDate)) : '';
}
public function getValueTimeM() {
$sDate=$this->getValueObject()->getValueDate();
return $sDate ? date('i',strtotime($sDate)) : '';
}
public function validate() {
/**
* Данные поступают ввиде массива array( 'date'=>'..', 'time' => array( 'h' => '..', 'm' => '..' ) )
*/
$aValue=$this->getValueForValidate();
$this->setValueForValidateDate(isset($aValue['date']) ? $aValue['date'] : '');
/**
* Сначала проверяем корректность даты
* В инпуте дата идет в формате d.m.Y
*/
$mRes=$this->validateStandart('date',array('format'=>$this->sFormatDateInput),'value_for_validate_date');
if ($mRes===true) {
/**
* Теперь проверяем на требование указывать дату
*/
$iTimeH=0;
$iTimeM=0;
$oValueObject=$this->getValueObject();
$oProperty=$oValueObject->getProperty();
if ($oProperty->getParam('use_time')) {
$iTimeH=isset($aValue['time']['h']) ? $aValue['time']['h'] : 0;
$iTimeM=isset($aValue['time']['m']) ? $aValue['time']['m'] : 0;
if ($iTimeH<0 or $iTimeH>23) {
$iTimeH=0;
}
if ($iTimeM<0 or $iTimeM>59) {
$iTimeM=0;
}
}
/**
* Формируем полную дату
*/
if ($this->getValueForValidateDate()) {
$sTimeFull=strtotime($this->getValueForValidateDate())+60*$iTimeM+60*60*$iTimeH;
/**
* Проверка на ограничение даты
*/
if ($oProperty->getValidateRuleOne('disallowFuture')) {
if ($sTimeFull>time()) {
return "{$oProperty->getTitle()}: дата не может быть в будущем";
}
}
/**
* Проверка на ограничения только если это новая запись, либо старая с изменениями
*/
if ($oValueObject->_isNew() or strtotime($oValueObject->getValueDate())!=$sTimeFull ) {
if ($oProperty->getValidateRuleOne('disallowPast')) {
if ($sTimeFull<time()) {
return "{$oProperty->getTitle()}: дата не может быть в прошлом";
}
}
}
} else {
$sTimeFull=null;
}
/**
* Переопределяем результирующее значение
*/
$this->setValueForValidate($sTimeFull ? date('Y-m-d H:i:00',$sTimeFull) : null);
return true;
} else {
return $mRes;
}
}
public function setValue($mValue) {
$this->resetAllValue();
$oValue=$this->getValueObject();
$oValue->setValueDate($mValue ? $mValue : null);
}
public function prepareValidateRulesRaw($aRulesRaw) {
$aRules=array();
$aRules['allowEmpty']=isset($aRulesRaw['allowEmpty']) ? false : true;
$aRules['disallowFuture']=isset($aRulesRaw['disallowFuture']) ? true : false;
$aRules['disallowPast']=isset($aRulesRaw['disallowPast']) ? true : false;
return $aRules;
}
public function prepareParamsRaw($aParamsRaw) {
$aParams=array();
$aParams['use_time']=isset($aParamsRaw['use_time']) ? true : false;
if (isset($aParamsRaw['format_out'])) {
$aParams['format_out']=$aParamsRaw['format_out'];
}
return $aParams;
}
public function getParamsDefault() {
return array(
'format_out'=>'Y-m-d H:i:s',
'use_time'=>true,
);
}
}

View file

@ -36,7 +36,8 @@ class ModuleProperty_MapperProperty extends Mapper {
p.code as prop_code,
p.title as prop_title,
p.date_create as prop_date_create,
p.sort as prop_sort
p.sort as prop_sort,
p.params as prop_params
FROM ".Config::Get('db.table.property')." AS p
LEFT JOIN ".Config::Get('db.table.property_value')." as v on ( v.property_id=p.id and v.target_id = ?d )
WHERE
@ -87,7 +88,8 @@ class ModuleProperty_MapperProperty extends Mapper {
p.code as prop_code,
p.title as prop_title,
p.date_create as prop_date_create,
p.sort as prop_sort
p.sort as prop_sort,
p.params as prop_params
FROM ".Config::Get('db.table.property')." AS p
LEFT JOIN ".Config::Get('db.table.property_value')." as v on ( v.property_id=p.id and v.target_id IN ( ?a ) )
WHERE

View file

@ -0,0 +1,28 @@
{$oValue = $oProperty->getValue()}
{$oValueType = $oValue->getValueTypeObject()}
{include file="forms/fields/form.field.text.tpl"
sFieldName = "property[{$oProperty->getId()}][date]"
sFieldValue = $oValue->getValueForForm()
sFieldClasses = 'width-150 date-picker'
sFieldNote = $oProperty->getDescription()
sFieldLabel = $oProperty->getTitle()}
{if $oProperty->getParam('use_time')}
<select name="property[{$oProperty->getId()}][time][h]">
{section name=time_h start=0 loop=24 step=1}
<option value="{$smarty.section.time_h.index}" {if $oValueType->getValueTimeH()==$smarty.section.time_h.index}selected="selected" {/if}>{$smarty.section.time_h.index}</option>
{/section}
</select>
:
<select name="property[{$oProperty->getId()}][time][m]">
{section name=time_m start=0 loop=60 step=5}
<option value="{$smarty.section.time_m.index}" {if $oValueType->getValueTimeM()==$smarty.section.time_m.index}selected="selected" {/if}>{$smarty.section.time_m.index}</option>
{/section}
</select>
<br/>
<br/>
{/if}

View file

@ -482,4 +482,10 @@ ALTER TABLE `prefix_poll_vote`
-- 05.02.2014
ALTER TABLE `prefix_poll_vote` DROP FOREIGN KEY `prefix_poll_vote_ibfk_2` ;
ALTER TABLE `prefix_poll_vote` DROP `answer_id` ;
ALTER TABLE `prefix_poll_vote` ADD `answers` VARCHAR( 500 ) NOT NULL AFTER `user_id` ;
ALTER TABLE `prefix_poll_vote` ADD `answers` VARCHAR( 500 ) NOT NULL AFTER `user_id` ;
-- 11.02.2014
ALTER TABLE `prefix_property` CHANGE `type` `type` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'text';
ALTER TABLE `prefix_property_value` ADD `value_date` DATETIME NULL DEFAULT NULL AFTER `value_varchar` ,
ADD INDEX ( `value_date` ) ;