Location: PHPKode > projects > Porte > porte-0.2.2/src/plugins/Temporal.php
<?php

/**
 * Copyright (c) 2008, SARL Adaltas. All rights reserved.
 * Code licensed under the BSD License:
 * http://porte.adaltas.com/en/developer/license.html
 */

/**
 * PorteTemporal
 *
 * Plugin to add 2 new properties "creation_date" and "modification_date".
 * 
 * The date of creation is set only once when a record is first created.
 * The date of modification is updated each time a record is updated.
 * 
 * @package    Porte
 * @subpackage plugin
 * @author     David Worms info(at)adaltas.com
 * @copyright  2008 Adaltas
 */
class PorteTemporal{
	
	public static function _modelTableAfter($models,$type){
		$model = $models->{$type};
		if(empty($model['temporal'])) return;
		if(!is_array($model['temporal'])){
			$model['temporal'] = array();
		}
		if(empty($model['temporal']['creation_field'])){
			$model['temporal']['creation_field'] = 'creation_date';
		}
		if(empty($model['temporal']['modification_field'])){
			$model['temporal']['modification_field'] = 'modification_date';
		}
		$models->{$type} = $model;
		$propertyModel = array('type'=>'timestamp');
		if(isset($model['properties'][$model['temporal']['creation_field']])){
			$propertyModel = array_merge($propertyModel,$model['properties'][$model['temporal']['creation_field']]);
		}
		$models->{$type}['properties'][$model['temporal']['creation_field']] = $propertyModel;
		//PorteModel::sanitizeProperty($models,$type,$model['temporal']['creation_field'],$propertyModel);
		$propertyModel = array('type'=>'timestamp');
		if(isset($model['properties'][$model['temporal']['modification_field']])){
			$propertyModel = array_merge($propertyModel,$model['properties'][$model['temporal']['modification_field']]);
		}
		$models->{$type}['properties'][$model['temporal']['modification_field']] = $propertyModel;
		//PorteModel::sanitizeProperty($models,$type,$model['temporal']['modification_field'],$propertyModel);
	}
	
	public static function _recordModelBefore(PorteRecord $record,$type){
		if(!empty($record->porte->models->{$type}['temporal'])){
			$record->events->connect('record_create_before',array('PorteTemporal','_recordCreateBefore'));
			$record->events->connect('record_update_before',array('PorteTemporal','_recordUpdateBefore'));
		}
	}
	
	public static function _recordCreateBefore(PorteRecord $record){
		$temporalModel = $record->porte->models->{$record->type}['temporal'];
		$record->attributes[$temporalModel['creation_field']] = PorteUtils::convertTimeStampToSql(time());
		$record->attributes[$temporalModel['modification_field']] = PorteUtils::convertTimeStampToSql(time());
	}
	
	public static function _recordUpdateBefore(PorteRecord $record){
		$temporalModel = $record->porte->models->{$record->type}['temporal'];
		$record->attributes[$temporalModel['modification_field']] = PorteUtils::convertTimeStampToSql(time());
	}
	
}

PorteEvents::connect('model_table_before',array('PorteTemporal','_modelTableAfter'));
PorteEvents::connect('record_model_after',array('PorteTemporal','_recordModelBefore'));

?>
Return current item: Porte