Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Ramirez committed Dec 4, 2011
0 parents commit c56bee5
Show file tree
Hide file tree
Showing 114 changed files with 18,686 additions and 0 deletions.
55 changes: 55 additions & 0 deletions behaviors/EDateFormatBehavior/EDateFormatBehavior.php
@@ -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);
}
}
}
}
38 changes: 38 additions & 0 deletions behaviors/EJsonBehavior/EJsonBehavior.php
@@ -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;
}
}

0 comments on commit c56bee5

Please sign in to comment.