Skip to content

Instantly share code, notes, and snippets.

@allyraza
Forked from kailash657/DateFormatterBehavior.php
Last active August 29, 2015 14:17
Show Gist options
  • Save allyraza/073f04cf10ae55fafc00 to your computer and use it in GitHub Desktop.
Save allyraza/073f04cf10ae55fafc00 to your computer and use it in GitHub Desktop.
/*add me to the config file*/
'params'=>array(
// this is used in contact page
'date_formats' => array(
'long' => 'd/M/yyyy H:m s',
'medium' => 'd/M/yyyy H:m',
'short' => 'd/M/yyyy')
),
<?php
class DateFormatterBehavior extends CActiveRecordBehavior
{
public $dateFields = array();
public $allowNull = array(); // do not auto create a date for this field
public $dateFormat = 'short';
private $owner;
public function attach($owner)
{
$this->owner = $owner;
parent::attach($owner);
}
public function parseDate($date)
{
if(! empty($date) && ! is_numeric($date) )
return parse_date($this->dateFormat, $date);
else
return $date;
}
public function formatDate($time)
{
if(! empty($time) && is_numeric($time) )
return df($this->dateFormat, $time);
else
return $time;
}
public function formatDates()
{
foreach($this->dateFields as $field)
$this->owner->$field = $this->formatDate($this->owner->$field);
}
public function beforeSave($event)
{
$owner = $this->owner;
foreach($this->dateFields as $field)
{
//auto populate date field if empty
if($owner->isNewRecord && !$owner->$field && !in_array($field, $this->allowNull))
$owner->$field = time();
if(is_numeric($owner->$field)) //this is for having timestamps consistent with format
$owner->$field = $this->formatDate($owner->$field);
$owner->$field = $this->parseDate($owner->$field);
}
}
public function checkDate($attribute,$params = array())
{
$owner = $this->owner;
if(empty($owner->{$attribute})) return;
if(! $this->parseDate($owner->{$attribute}))
$owner->addError($attribute,'The date is incorrect.');
}
}
<?php
/*
* date HELPERS
*
*/
function df($format, $timestamp) //date_format, user in views
{
return Yii::app()->dateFormatter->format(
Yii::app()->params['date_formats'][$format],
(int) $timestamp
);
}
function parse_date($format, $date_string)
{
return CDateTimeParser::parse($date_string,
Yii::app()->params['date_formats'][$format]);
}
<?php
/**
* This is the model class for table "person".
*
* The followings are the available columns in table 'person':
* @property integer $id
* @property string $name
* @property string $surname
* @property string $birthdate
* @property string $address
* @property string $city
* @property string $telephone_n
* @property integer $created_on
*/
class Person extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Person the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function primaryKey()
{
return 'id';
}
public function tableName()
{
return 'person';
}
/*me + yii behaviours = LOVE */
public function behaviors()
{
return array(
'DateFormatterBehavior'=>array(
'class'=>'ext.DateFormatterBehavior',
'dateFields'=>array('birthdate','last_checkin'),
'dateFormat' => 'short'
)
);
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, surname', 'required'),
array('name, surname, birthdate, address, city, telephone_n', 'length', 'max'=>255),
array('birthdate, last_checkin', 'behaviorCheckDate'),
array('name, surname, birthdate, address, city, telephone_n', 'safe', 'on'=>'search'),
);
}
//validation callback to behavior
public function behaviorCheckDate($attribute,$params)
{
return $this->checkDate($attribute,$params); //I come from the behaviour
}
/**
* @return array relational rules.
*/
public function relations()
{
return array(
'services' => array(self::HAS_MANY, 'Services', 'person_id'),
);
}
public function beforeSave()
{
return parent::beforeSave();
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
'surname' => 'Surname',
'birthdate' => 'Birth Date',
'address' => 'Address',
'city' => 'City',
'telephone_n' => 'Telephone Number',
'created_on' => 'Created On',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('surname',$this->surname,true);
$criteria->compare('birthdate', self::parseDate($this->birthdate),true);
$criteria->compare('address',$this->address,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('telephone_n',$this->telephone_n,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment