<?php
abstract class FlexActiveRecord extends CActiveRecord
{
	
	protected function beforeValidate()
	{
		//parent::beforeValidate();

		if($this->isNewRecord)
		{
             $this->language="ar";      
		}
		

		return parent::beforeValidate();
	}
	
     protected function beforeSave()
	{
		

		if(!$this->isNewRecord)
		
		{
			if(Yii::app()->user->id!=null){
				
			
			//create new log
			$log= new Log;
			$log->date=new CDbExpression('NOW()');
			$log->user_id=Yii::app()->user->id;
			$log->object_id=$this->id;
			$log->object_type=get_class($this);
			$log->opration='update';
			$log->save();
			}
		}
		

		return parent::beforeSave();
	}

 protected function afterSave()
	{
		//create new log
		

		if($this->isNewRecord)
		
		{
			if(Yii::app()->user->id!=null)
			{
			$log= new Log;
			$log->date=new CDbExpression('NOW()');
			$log->user_id=Yii::app()->user->id;
			$log->object_id=$this->id;
			$log->object_type=get_class($this);
			$log->opration='create';
            $log->save();
			}
		}
		

		return parent::afterSave();
	} 	
  
    protected function beforeDelete()
	{
		if(Yii::app()->user->id!=null){
			
		
		//create new log
		$log= new Log;
		$log->date=new CDbExpression('NOW()');
		$log->user_id=Yii::app()->user->id;
		$log->object_id=$this->id;
		$log->object_type=get_class($this);
		$log->opration='Delete';
		$log->save();
		}

		return parent::beforeDelete();
	}  

public function getExcerpt($text,$words_to_return=null)
{
    $words_in_text = str_word_count($text,1);
	if($words_to_return==null)
    $words_to_return = 100;
    $result = array_slice($words_in_text,0,$words_to_return);
    return implode(" ",$result);
}	
  
        
        
}
