Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/model/person/graph.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 AnModelPersonGraph extends KObject
{
	/**
	 * 
	 */
	protected $_acl;
	
	/**
	 * 
	 */		
	protected $_query;
	
	/**
	 * 
	 */
	protected $_db;
	
	/**
	 * 
	 */
	public $owner;
	
	/**
	 * 
	 */
	public $owner2;
	
	
	/**
	 * 
	 */
	static $_graphs = array();
	
	/**
	 * 
	 * @return 
	 * @param $options Object
	 */
	static public function factory($options)
	{
		$owner = $options['owner'];
		
		if ( !isset(self::$_graphs[$owner->id]) ) {
			self::$_graphs[$owner->id] = new AnModelPersonGraph($options);
		}
		
		return self::$_graphs[$owner->id];
		
	}
	
	/**
	 * CONSTRUCT
	 * @param array of $options
	 * @return null
	 */
	public function __construct(array $options=array())
	{
		$this->owner	   = $options['owner'];

		$this->_query	   = KFactory::get('lib.anahita.domain.factory.query')->get('lib.anahita.model.person');

		$this->_db		   = $this->_query->getAdapter();				
	}
		
	/**
	 * 
	 * @return 
	 */
	public function getOwner()
	{
		return $this->owner;	
	}
	
	/**
	 * 
	 * @return 
	 */
	public function query()
	{
		return $this->_query;
	}

	/**
	 * Method to remove a person from socialgraph
	 * @param person actor node object
	 */
	public function remove($person)
	{
		$edge = KFactory::get('lib.anahita.domain.factory')->getQuery('lib.anahita.model.person.edge')
				->where('nodeA', '=',$this->getOwner())
				->where('nodeB', '=',$person)
				->fetch();
				;
		
		if ( $edge ) {
			$edge->delete();
		}
	}
			
	/**
	 * Method to add a person to a socialgraph
	 * @param person actor node
	 * @param graph type constant
	 * @return 
	 */
	public function add($person, $type)
	{
		$method = $type == 'follow' ? 'canFollow' : 'canBlock';
	
		if ( !$person->getACl()->$method() ) 
			return null;
		

		$edge = KFactory::get('lib.anahita.domain.factory.query')->get('lib.anahita.model.person.edge')
				->where('nodeA', '=',$this->getOwner())
				->where('nodeB', '=',$person)
				->fetch();
				;
		
		if ( !$edge ) {
			$edge = KFactory::get('lib.anahita.domain.factory.model')->get('lib.anahita.model.person.edge', array(
				'nodeA'  => $this->getOwner(),
				'nodeB'  => $person
			));
		}
		
		if ( $type == 'block' )
			$edge->setToBlock();
		else
			$edge->setToFollow();
		
		return $edge;
	}
	
	/**
	 * 
	 * @return 
	 */
	public function getCommonMutual()
	{
		if ( $this->owner === $this->owner2 )
			return $this->getMutual();
			
		$query = $this->_getQuery(array('direction'=>'both'));
		$id = $this->owner2->id;
		$query
				   ->join('INNER', 'socialengine_edges AS e3',  array('e2.node_b_id  = e3.node_a_id'	, 'e3.node_b_id  = '.$id))
				   ->join('INNER', 'socialengine_edges AS e4',	array('e3.node_a_id  = e4.node_b_id'    , 'e4.node_a_id  = '.$id))			
				   ->where('e3.type','=','social')
				   ->where('e3.meta','=','')
				   ->where('e4.type','=','social')
				   ->where('e4.meta','=','')
				 	;
		
		return $query->fetchAll();
	}
	
	/**
	 * Method to obtain the entire socialgraph of an owner
	 * @return array of person node entities
	 */
	public function getAll()
	{
		return $this->_getPeople();
	}
	
	/**
	 * Method to obtain the leaders in an owner's socialgraph
	 * @return array of person node entities
	 */
	public function getLeaders()
	{
		return $this->_getPeople(array('direction'=>'out'));
	}

	/**
	 * Method to obtain the followers in an owner's socialgraph
	 * @return array of person node entities
	 */
	public function getFollowers()
	{
		return $this->_getPeople(array('direction'=>'in'));
	}
	
	/**
	 * Method to obtain the mutual relationships in an owner's socialgraph
	 * @return array of person node entities
	 */
	public function getMutual()
	{		
		return $this->_getPeople(array('direction'=>'both'));
	}
	
	/**
	 * Method to obtain the blocked relationships in an owner's socialgraph
	 * @return array of person node entities
	 */
	public function getBlocked()
	{
		return $this->_getPeople(array('direction'=>'out', 'meta'=>'block'));
	}

	/**
	 * Method to obtain an owner's socialgraph
	 * @param array of options
	 * @return array of person node entities
	 */
	protected function _getPeople(array $options=array())
	{
		$query = $this->_getQuery($options);
		return $query->fetchAll();
	}
	
	/**
	 * 
	 * @return 
	 * @param $options Object
	 */
	protected function _getQuery($options = array())
	{
		$options  = array_merge(array('direction'=>false, 'meta'=>''), $options);
		
		$query 	  = clone $this->query();

		$id 	  = $this->owner->getId();
		
		$id_field = $query->getMapper()->getProperty('id')->getField();
		
		$query->distinct();

		$query->where('e.type','=','social');
		
		$query->where('e.component','=','com_socialengine');
		
		$query->where('e.meta','=',$options['meta']);
		
		if ( !$options['direction'] ) {
			$query->join[]  = array('type'=>'INNER', 'table'=>'jos_socialengine_edges AS e', 'condition'=>array("(e.node_a_id = $id_field AND e.node_b_id = {$id}) OR (e.node_b_id = $id_field AND e.node_a_id = {$id})"));
		} else if ( $options['direction'] == 'in' )
			$query->join('INNER', 'socialengine_edges AS e',	array('node_a_id = '.$id_field,	'node_b_id = '.$id));
		else if ( $options['direction'] == 'out' )
			$query->join('INNER', 'socialengine_edges AS e',	array('node_b_id = '.$id_field,	'node_a_id = '.$id));
		else if ( $options['direction'] == 'both' ) {
			$query
				   ->join('INNER', 'socialengine_edges AS e' ,  array($id_field.'  = e.node_a_id', 'e.node_b_id  = '.$id))
				   ->join('INNER', 'socialengine_edges AS e2',  array('e.node_a_id  = e2.node_b_id'			     , 'e2.node_a_id = '.$id))
				   ->where('e2.type','=','social')
				   ->where('e2.meta','=',$options['meta'])
					;
					
		}
		
		return $query;
	}

//end class
}
Return current item: Anahita Social Engine