Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/model/medium/medium.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Engine™
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.anahitapolis.com
 */

class AnModelMedium extends AnModelNode implements AnModelCommentCommentable
{	
	protected static $_medium_alias; 
	
	/**
	 * Initialize a new medium entity
	 * @return 
	 */
	public function awakeFromInsert()
	{
		$this->type = (string)$this->getIdentifier();
	}
	
	/**
	 * Set title of the medium. The alias of the medium is by default enforced to be the same as title
	 * @param $title String
	 */		
	public function setTitle($title)
	{
		$this->_set('title', $title);		
		$this->alias = $title;
	}
						
	/**
	 * A hook called when a comment is inserted for this medium
	 * @return 
	 * @param $comment Object
	 */
	public function _notifyCommentInserted($comment)
	{		
		$this->numOfComments   = $this->comments->getTotal();
		$this->lastCommenter   = $comment->author;
		$this->lastCommentTime = $comment->creationTime;
	}
	
	/**
	 * A hook called when a comment is deleted for this medium
	 * @return 
	 * @param $comment Object
	 */
	public function _notifyCommentDeleted($comment)
	{
		$this->numOfComments  = $this->comments->getTotal();
	}
	
	/**
	 * A hook called when a comment is updated for this medium
	 * @return 
	 * @param $comment Object
	 */
	public function _notifyCommentUpdated($comment)
	{
		$this->lastCommenter   = $comment->author;
		$this->lastCommentTime = $comment->creationTime;
	}
	
	/**
	 * update the medium comment status 
	 * @return 
	 * @param $comment AnModelComment
	 */
	public function updateCommentStatus(AnModelComment $comment)
	{
		$this->numOfComments    = $this->comments->getTotal();
		$this->lastCommenter   = $comment->author;
		$this->lastCommentTime = $comment->creationTime;
	}	
	
	/**
	 * Set the medium author. the person who has created this medium. It must be an instance of AnModelPerson
	 * @param $author AnModelPerson Object
	 */
	public function setAuthor(AnModelPerson $author)
	{
		$this->_set('author', $author);
		$this->editor = $author;
		$this->lastUpdateTime = KFactory::tmp('lib.anahita.type.date');
	}
	
	/**
	 * Register a hook to delete all comments
	 * @return 
	 */
	protected function _registerHooks()
	{
		$this->registerFunctionAfter('delete','__hookDeleteCommentsAfterDelete');
		$this->registerFunctionBefore('delete','__hookDeleteStoriesBeforeDelete');
	}
	
	/**
	 * Delete all the comment after a medium has been deleted
	 * @return 
	 */
	public function __hookDeleteCommentsAfterDelete()
	{	
		KFactory::get('lib.koowa.database')->delete('socialengine_comments', 'WHERE node_id = '.$this->id);
	}
	
	/**
	 * Delete all the stories after a medium has been deleted
	 * @return 
	 */
	public function __hookDeleteStoriesBeforeDelete()
	{	
		foreach($this->stories as $story)
			$story->markDeleted();
	}
	
	static public function namespacefield($string)
	{
		return self::$_medium_alias.'.'.$string;
	}
		
	/**
	 * Meta Description of a medium model
	 */		
	static function describe($medium)
	{	
		self::$_medium_alias = $medium->getModelIdentifier()->name.'_medium';
		
		$medium->setTable('socialengine_nodes AS '.self::$_medium_alias);

		$medium->property('id', 	 	  array('field'=>self::$_medium_alias.'.socialengine_node_id', 'unique'=>true));
		
		$medium->property('title',     array('required'=>true, 'field'=>self::$_medium_alias.'.medium_title', 'filter'=>'string'));
		
		$medium->property('alias',     array('required'=>true, 'field'=>self::$_medium_alias.'.medium_alias', 'filter'=>'alias'));
				
		$medium->property('body',      array('field'=>self::$_medium_alias.'.medium_body'));
		
		$medium->property('type',	  array('required'=>true,  'field'=>self::$_medium_alias.'.medium_type'));
		
		$medium->property('mimeType',	  array('required'=>true,  'field'=>self::$_medium_alias.'.medium_mime_type', 'filter'=>'string'))->setDefault('text/plain');
		
		$medium->property('ordering',  array('field'=>self::$_medium_alias.'.medium_ordering'));
		
		$medium->property('published', array('field'=>self::$_medium_alias.'.medium_published', 'default'=>1));
						
		$medium->belongsTo('application',	  array('required'=>true, 'model'=>'lib.anahita.model.application', 	'child_key'=>self::$_medium_alias.'.medium_component', 'parent_key'=>'component' ));
			
		$medium->property('meta',  array('class'=>'lib.anahita.type.json', 'initialize_with'=>array('value'=>self::$_medium_alias.'.meta')));
		
		$medium->belongsTo('owner',	array(
			'base_model'	   => 'lib.anahita.model.actor' , 
			'polymorphic'      => true,
			'type_field' 	   => self::$_medium_alias.'.medium_owner_type',
			'child_key'		   => self::$_medium_alias.'.medium_owner_id'));
		
		$medium->belongsTo('author',	    	array('required'=>true, 'model'=>'lib.anahita.model.actor',	 'child_key'=>self::$_medium_alias.'.medium_created_by'));
		
		$medium->belongsTo('editor',	    	array('model'=>'lib.anahita.model.actor',	'child_key'=>self::$_medium_alias.'.medium_modified_by'));
		
		$medium->belongsTo('parent',	     	array('model'=>'lib.anahita.model.medium',  'child_key'=>self::$_medium_alias.'.medium_parent_id'));
		
		$medium->property('numOfComments',	 array('field'=>self::$_medium_alias.'.medium_comment_count'));
		
		$medium->property('openToComment',	 array('field'=>self::$_medium_alias.'.medium_comment_status'))->setDefault(0);

		$medium->belongsTo('lastCommenter',     array('model'=>'lib.anahita.model.actor',	 'child_key'=>self::$_medium_alias.'.medium_last_comment_by' ));
				
		$medium->property('creationTime',       array('required'=>true, 'class'=>'lib.anahita.type.date',	'initialize_with'=>array('date'=>self::$_medium_alias.".medium_created_on")));

		$medium->property('lastUpdateTime',     array('class'=>'lib.anahita.type.date',	'initialize_with'=>array('date'=>self::$_medium_alias.".medium_modified_on")));
		
		$medium->property('hits', array('field'=>self::$_medium_alias.'.medium_hits'))->setDefault(0);

		$medium->property('lastCommentTime',    array('class'=>'lib.anahita.type.date',	'initialize_with'=>array('date'=>self::$_medium_alias.".medium_last_comment_on")))
			->setDefault(null);
				
		$medium->has('many', 'comments' ,array(
			'model'     => 'lib.anahita.model.comment' ,
			'child_key' => 'node'
		));
		
		$medium->has('many', 'stories', array(
			'model'	=> 'lib.anahita.model.story',
			'child_key'=>'object'
		));
		
		KFactory::get('lib.anahita.model.acl.rules')->add($medium->getModelIdentifier(), KFactory::tmp('lib.anahita.model.medium.acl'));		
		
		$medium->setRepository('lib.anahita.model.medium.repository');
	}
	
//end class	
}
Return current item: Anahita Social Engine