Skip to content

Commit

Permalink
Moved plugin events from model to behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Sep 3, 2013
1 parent 7fb5d43 commit 5a22c58
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 63 deletions.
69 changes: 6 additions & 63 deletions application/models/LSActiveRecord.php
Expand Up @@ -20,13 +20,16 @@ class LSActiveRecord extends CActiveRecord
*
* Below is a list of all behaviors we register:
* @see CTimestampBehavior
*
* @see PluginEventBehavior
* @return array
*/
public function behaviors(){
$sCreateFieldName=($this->hasAttribute('created')?'created':null);
$sCreateFieldName=($this->hasAttribute('created')?'created':null);
$sUpdateFieldName=($this->hasAttribute('modified')?'modified':null);
return array(
'PluginEventBehavior' => array(
'class' => 'application.models.behaviors.PluginEventBehavior'
),
'CTimestampBehavior' => array(
'class' => 'zii.behaviors.CTimestampBehavior',
'createAttribute' => $sCreateFieldName,
Expand Down Expand Up @@ -85,49 +88,6 @@ public function findAllAsArray($condition = '', $params = array())
}


/**
* This method is invoked before saving a record (after validation, if any).
* The default implementation raises the {@link onBeforeSave} event.
* You may override this method to do any preparation work for record saving.
* Use {@link isNewRecord} to determine whether the saving is
* for inserting or updating record.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the saving should be executed. Defaults to true.
*/
public function beforeSave()
{
$this->dispatchPluginModelEvent('before'.get_class($this).'Save');
$this->dispatchPluginModelEvent('beforeModelSave');
return parent::beforeSave();
}

/**
* This method is invoked before deleting a record.
* The default implementation raises the {@link onBeforeDelete} event.
* You may override this method to do any preparation work for record deletion.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the record should be deleted. Defaults to true.
*/
public function beforeDelete()
{
$this->dispatchPluginModelEvent('before'.get_class($this).'Delete');
$this->dispatchPluginModelEvent('beforeModelDelete');
return parent::beforeDelete();
}

/**
* This method is invoked after saving a record.
* The default implementation raises the {@link onAfterSave} event.
* You may override this method to do any work that needs to be done after saving a record.
* Make sure you call the parent implementation so that the event is raised properly.
*/
public function afterSave()
{
$this->dispatchPluginModelEvent('after'.get_class($this).'Save');
$this->dispatchPluginModelEvent('afterModelSave');
parent::afterSave();
}

/**
* Return the max value for a field
*
Expand Down Expand Up @@ -165,6 +125,7 @@ public function getMaxId($field = null, $forceRefresh = false) {
}

/**
* @todo This should also be moved to the behavior at some point.
* This method overrides the parent in order to raise PluginEvents for Bulk delete operations.
*
* Filter Criteria are wrapped into a CDBCriteria instance so we have a single instance responsible for holding the filter criteria
Expand All @@ -183,26 +144,8 @@ public function deleteAllByAttributes($attributes,$condition='',$params=array())
$builder=$this->getCommandBuilder();
$table=$this->getTableSchema();
$criteria=$builder->createColumnCriteria($table,$attributes,$condition,$params);

$this->dispatchPluginModelEvent('before'.get_class($this).'DeleteMany', $criteria);
$this->dispatchPluginModelEvent('beforeModelDeleteMany', $criteria);
return parent::deleteAllByAttributes(array(), $criteria, array());
}

/**
* method for dispatching plugin events
*
* See {@link find()} for detailed explanation about $condition and $params.
* @param string $sEventName event name to dispatch
* @param array $criteria array containing attributes, conditions and params for the filter query
* @return PluginEvent the dispatched event
*/
private function dispatchPluginModelEvent($sEventName, $criteria=false) {
$oPluginEvent = new PluginEvent($sEventName, $this);
$oPluginEvent->set('model', $this);
if ($criteria !== false) {
$oPluginEvent->set('filterCriteria', $criteria);
}
return App()->getPluginManager()->dispatchEvent($oPluginEvent);
}
}
49 changes: 49 additions & 0 deletions application/models/behaviors/PluginEventBehavior.php
@@ -0,0 +1,49 @@
<?php
class PluginEventBehavior extends CModelBehavior
{
public function events()
{
return array_merge(parent::events(), array(
'onBeforeSave' => 'beforeSave',
'onAfterSave' => 'afterSave',
'onBeforeDelete' => 'beforeDelete',
));
}

public function afterSave(CModelEvent $event)
{
$this->dispatchPluginModelEvent('after'.get_class($this->owner).'Save');
$this->dispatchPluginModelEvent('afterModelSave');
}
public function beforeSave(CModelEvent $event)
{
$this->dispatchPluginModelEvent('before'.get_class($this->owner).'Save');
$this->dispatchPluginModelEvent('beforeModelSave');
}

public function beforeDelete(CModelEvent $event)
{
$this->dispatchPluginModelEvent('before'.get_class($this->owner).'Delete');
$this->dispatchPluginModelEvent('beforeModelDelete');
}

/**
* method for dispatching plugin events
*
* See {@link find()} for detailed explanation about $condition and $params.
* @param string $sEventName event name to dispatch
* @param array $criteria array containing attributes, conditions and params for the filter query
* @return PluginEvent the dispatched event
*/
private function dispatchPluginModelEvent($sEventName, $criteria = null)
{
$oPluginEvent = new PluginEvent($sEventName, $this);
$oPluginEvent->set('model', $this->owner);
if (isset($criteria))
{
$oPluginEvent->set('filterCriteria', $criteria);
}
return App()->getPluginManager()->dispatchEvent($oPluginEvent);
}
}
?>

0 comments on commit 5a22c58

Please sign in to comment.