Skip to content

Commit

Permalink
Reverted last change
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammaye committed Oct 1, 2013
1 parent 578841b commit 63cdabe
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions behaviors/EMongoTimestampBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Rewritten to work with MongoDB
*/

/**
/**
* EMongoTimestampBheaviour will automatically fill date and time related attributes.
*
* EMongoTimestampBheaviour will automatically fill date and time related attributes when the active record
Expand All @@ -24,7 +24,7 @@
* 'class' => 'EMongoTimestampBheaviour',
* 'createAttribute' => 'create_time_attribute',
* 'updateAttribute' => 'update_time_attribute',
* 'on' => array('scenarioName'),
* 'onScenario' => array('scenarioName'),
* )
* );
* }
Expand All @@ -42,48 +42,48 @@

class EMongoTimestampBehaviour extends CActiveRecordBehavior {
/**
* @var mixed The name of the attribute to store the creation time. Set to null to not
* use a timestamp for the creation attribute. Defaults to 'create_time'
*/
* @var mixed The name of the attribute to store the creation time. Set to null to not
* use a timestamp for the creation attribute. Defaults to 'create_time'
*/
public $createAttribute = 'create_time';
/**
* @var mixed The name of the attribute to store the modification time. Set to null to not
* use a timestamp for the update attribute. Defaults to 'update_time'
*/
* @var mixed The name of the attribute to store the modification time. Set to null to not
* use a timestamp for the update attribute. Defaults to 'update_time'
*/
public $updateAttribute = 'update_time';

/**
* @var array set attributes only on this scenarios
*/
public $on = array();
public $onScenario = array();

/**
* @var array not set attributes only on this scenarios
*/
public $notOn = array();
*/
public $notOnScenario = array();

/**
* @var bool Whether to set the update attribute to the creation timestamp upon creation.
* Otherwise it will be left alone. Defaults to false.
* @var bool Whether to set the update attribute to the creation timestamp upon creation.
* Otherwise it will be left alone. Defaults to false.
*/
public $setUpdateOnCreate = false;

/**
* @var mixed The expression that will be used for generating the timestamp.
* This can be either a string representing a PHP expression (e.g. 'time()'),
* or a {@link CDbExpression} object representing a DB expression (e.g. new CDbExpression('NOW()')).
* Defaults to null, meaning that we will attempt to figure out the appropriate timestamp
* automatically. If we fail at finding the appropriate timestamp, then it will
* fall back to using the current UNIX timestamp
*/
* @var mixed The expression that will be used for generating the timestamp.
* This can be either a string representing a PHP expression (e.g. 'time()'),
* or a {@link CDbExpression} object representing a DB expression (e.g. new CDbExpression('NOW()')).
* Defaults to null, meaning that we will attempt to figure out the appropriate timestamp
* automatically. If we fail at finding the appropriate timestamp, then it will
* fall back to using the current UNIX timestamp
*/
public $timestampExpression;

/**
* Responds to {@link CModel::onBeforeSave} event.
* Sets the values of the creation or modified attributes as configured
*
* @param CModelEvent $event event parameter
*/
* Responds to {@link CModel::onBeforeSave} event.
* Sets the values of the creation or modified attributes as configured
*
* @param CModelEvent $event event parameter
*/
public function beforeSave($event) {
if ($this->checkScenarios()) {
if ($this->getOwner()->getIsNewRecord() && ($this->createAttribute !== null)) {
Expand All @@ -96,44 +96,42 @@ public function beforeSave($event) {
}

/**
* Gets the approprate timestamp depending on the column type $attribute is
*
* @param string $attribute $attribute
* @return mixed timestamp (eg unix timestamp or a mysql function)
*/
* Gets the approprate timestamp depending on the column type $attribute is
*
* @param string $attribute $attribute
* @return mixed timestamp (eg unix timestamp or a mysql function)
*/
protected function getTimestampByAttribute($attribute) {
if($this->timestampExpression instanceof MongoDate)
return $this->timestampExpression;
elseif ($this->timestampExpression !== null)
return @eval('return '.$this->timestampExpression.';');
return @eval('return '.$this->timestampExpression.';');
return new MongoDate();
}

protected function checkScenarios() {
if (!is_array($this->on) or !is_array($this->notOn))
throw new CException('on and notOn must be an array');
if (count($this->on)) {
if (count($this->notOn))
throw new CException('You can not specify both the parameter and on notOn');

// Check to see if the models scenario is in the array of scenarios we brought in
if (in_array($this->getOwner()->getScenario(), $this->on)) {
if (!is_array($this->onScenario) or !is_array($this->notOnScenario))
throw new CException('onScenario and notOnScenario must be an array');
if (count($this->onScenario)) {
if (count($this->notOnScenario))
throw new CException('You can not specify both the parameter and onScenario notOnScenario');
//TODO CHECK ENGLISH TEXT VERSION!!!
if (in_array($this->getOwner()->getScenario(), $this->onScenario)) {
return true;
} else {
return false;
}
}
if (count($this->notOn)) {
if (count($this->on))
throw new CException('You can not specify both the parameter and on notOn');

// Check to see if the models scenario is in the array of scenarios we brought in
if (in_array($this->getOwner()->getScenario(), $this->notOn)) {
if (count($this->notOnScenario)) {
if (count($this->onScenario))
throw new CException('You can not specify both the parameter and onScenario notOnScenario');
//TODO CHECK ENGLISH TEXT VERSION!!!
if (in_array($this->getOwner()->getScenario(), $this->notOnScenario)) {
return false;
} else {
return true;
}
}
return true;
}
}
}

0 comments on commit 63cdabe

Please sign in to comment.