<?php
namespace common\behaviors;
use yii\base\Behavior;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;
use yii\base\Event;
/**
* Class DateFormatTranslator
* @package common\behaviors
*/
class DateFormatTranslator extends Behavior
{
public $machineFormat = 'php:Y-m-d';
public $humanFormat = 'php:d.m.Y';
public $attributes;
/**
* @throws InvalidConfigException
*/
public function init()
{
if(empty($this->attributes)){
throw new InvalidConfigException('attributes can not be empty');
}
return parent::init();
}
/**
* @return array
*/
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_UPDATE => 'toMachineFormat',
ActiveRecord::EVENT_BEFORE_INSERT => 'toMachineFormat',
ActiveRecord::EVENT_AFTER_INSERT => 'toHumanFormat',
ActiveRecord::EVENT_AFTER_FIND => 'toHumanFormat',
];
}
/**
* @param $event Event
* @var $owner ActiveRecord
*/
public function toHumanFormat(Event $event){
$formatter = \Yii::$app->formatter;
$owner = $event->sender;
foreach ($this->attributes as $attribute){
if(!empty($owner->$attribute)){
$owner->$attribute = $formatter->asDate($owner->$attribute, $this->humanFormat);
}
}
}
/**
* @param Event $event
*/
public function toMachineFormat(Event $event){
$formatter = \Yii::$app->formatter;
$owner = $event->sender;
foreach ($this->attributes as $attribute){
if(!empty($owner->$attribute)) {
$owner->$attribute = $formatter->asDate($owner->$attribute, $this->machineFormat);
}
}
}
}
AppointmentForm[email]
'user' => [
'controllerMap' => [
'recovery' => [
'class' => 'dektrium\user\controllers\RecoveryController',
'layout' => '@backend/views/layouts/main-login'
],
'security' => [
'class' => 'dektrium\user\controllers\SecurityController',
'layout' => '@backend/views/layouts/main-login'
]
],
'modelMap' => [
'LoginForm' => 'backend\models\LoginForm',
],
]
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'rowOptions' => function ($model, $key, $index, $grid)
{
if($model->status == Contract::STATUS_OPEN){
$now = new \DateTime(date('Y-m-d'));
$date = new \DateTime($model->date_stop);
if($date == $now){
return ['class' => 'success'];
}
if($date < $now){
return ['class' => 'danger'];
}
}
},
]);
ApcCache requires PHP apc extension to be loaded.
'components' => [
'view' => [
'class' => 'common\components\View'
],
....
]
<?php
namespace common\components;
class View extends \yii\web\View
{
public function beforeRender($viewFile, $params)
{
$this->params = Settings::GetAllSettings();
return parent::beforeRender($viewFile, $params); // TODO: Change the autogenerated stub
}
}