Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antonio Ramirez
committed
Dec 4, 2011
0 parents
commit c56bee5
Showing
114 changed files
with
18,686 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* EDateFormatBehavior class | ||
* | ||
* @author Antonio Ramirez <antonio@ramirezcobos.com> | ||
*/ | ||
class EDateFormatBehavior extends CActiveRecordBehavior | ||
{ | ||
//.. array of columns that have dates to be converted | ||
public $dateColumns = array(); | ||
public $dateTimeColumns = array(); | ||
|
||
public $dateFormat = 'm/d/Y'; | ||
public $dateTimeFormat = 'm/d/Y H:i'; | ||
/** | ||
* Convert from $dateFormat to UNIX timestamp dates before saving | ||
*/ | ||
public function beforeSave($event) | ||
{ | ||
$this->format($this->dateColumns, $this->dateFormat); | ||
$this->format($this->dateTimeColumns, $this->dateTimeFormat); | ||
return parent::beforeSave($event); | ||
} | ||
/** | ||
* Converts UNIX timestamp dates to $dateFormat after read from database | ||
*/ | ||
public function afterFind($event) | ||
{ | ||
$this->format($this->dateColumns, $this->dateFormat, false); | ||
$this->format($this->dateTimeColumns, $this->dateTimeFormat, false); | ||
return parent::afterFind($event); | ||
} | ||
/** | ||
* | ||
* Formats to UNIX timestamp or $dateFormat as specified. Note that | ||
* if using $dateFormat then assumed timestamp value | ||
* @param array $columns the columns attributes to format | ||
* @param string $format the format to convert the date to | ||
* @param boolean $strtotime if boolean, will convert to UNIX timestamp | ||
* @return void | ||
*/ | ||
protected function format($columns, $format, $strtotime=true) | ||
{ | ||
if(empty($columns)) return; | ||
|
||
foreach($this->getOwner()->getAttributes() as $key=>$value) | ||
{ | ||
if(in_array($key, $columns) && !empty($value)) | ||
{ | ||
$dt = $this->getOwner()->{$key}; | ||
$this->getOwner()->{$key} = $strtotime ? strtotime($dt) : date($format,$dt); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
class EJsonBehavior extends CBehavior{ | ||
|
||
private $owner; | ||
private $relations; | ||
|
||
public function toJSON(){ | ||
$this->owner = $this->getOwner(); | ||
|
||
if (is_subclass_of($this->owner,'CActiveRecord')){ | ||
|
||
$attributes = $this->owner->getAttributes(); | ||
$this->relations = $this->getRelated(); | ||
|
||
$jsonDataSource = array('jsonDataSource'=>array('attributes'=>$attributes,'relations'=>$this->relations)); | ||
|
||
return CJSON::encode($jsonDataSource); | ||
} | ||
return false; | ||
} | ||
private function getRelated() | ||
{ | ||
$related = array(); | ||
|
||
$obj = null; | ||
|
||
$md=$this->owner->getMetaData(); | ||
|
||
foreach($md->relations as $name=>$relation){ | ||
|
||
$obj = $this->owner->getRelated($name); | ||
|
||
$related[$name] = $obj instanceof CActiveRecord ? $obj->getAttributes() : $obj; | ||
} | ||
|
||
return $related; | ||
} | ||
} |
Oops, something went wrong.