Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/model/application/application.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 AnModelApplication extends AnDomainModelAbstract
{
	/**
	 * 
	 * 
	 * @var array
	 */
	protected $_info;
	
	/**
	 * 
	 * @var array
	 */
	protected $_gadgets = null;
	
	/**
	 * 
	 * @var array
	 */
	protected $_story_templates = null;
	
	/**
	 * 
	 * @var AnModelApplicationContext
	 */			
	protected $_runtime_context;
	
	/**
	 * Get an application identifier based on the component
	 * @return string
	 * @param $component string
	 */
	public static function getChildApplicationIdentifier($component)
	{		
		$component = 'site::com.'.substr($component, 4).'.model.application';
		
		$path 	   = KLoader::path($component);

		if ( !file_exists($path)) {
			return 'lib.anahita.model.application';
		}
		
		return $component;
	}
	
	/**
	 * 
	 * @return 
	 * @param $model Object
	 */
	public static function describe($application)
	{
		$application->setTable('socialengine_applications AS a');
		
		$application->filter()
			->join('INNER', 'components AS c', 'a.component = c.option')
		;
		$application->property('id',			array('field'=>'a.socialengine_application_id', 'unique'=>true));
		$application->property('name', 			array('field'=>'c.name'));
		$application->property('component', 	array('field'=>'a.component', 'unique'=>true, 'discriminate'=>'AnModelApplication::getChildApplicationIdentifier'));
		$application->property('enabled', 	array('field'=>'a.enabled'));
		$application->property('order', 	array('field'=>'a.order'));
		$application->property('isGlobal', 	array('field'=>'a.global'));
		$application->setRepository('lib.anahita.model.application.repository');
	}
		
	/**
	 * Publish a story, notification or request story object and instantiate its properties.
	 * by the default the owner of the story is set to the subject of the story. By default the subject of the 
	 * story is the viewer. The application of the story is set to the calling applicaiton. That should not be changed
	 * @return AnModelStoryAbstract 
	 * @param $type String
	 * @param $name String
	 */
	public function publishStory($type, $name, $options = array())
	{
		if ( !is_array($options) )
			$options = array(
				'owner'   => $options,
				'subject' => $options
			);
			
		$default = array(
			'target'  => null,
			'owner'   => AnModelAnahita::getSessionViewer(),
			'subject' => AnModelAnahita::getSessionViewer(),
			'author'  => AnModelAnahita::getSessionViewer(),
			'object'  => null
		);
		
		$options = array_merge($default, $options);
		
		$template = $this->getStoryTemplate($type, $name, $options['subject']);
		
		if ( !strpos($type, '.') )		
			if ( $type == 'story' )
				$identifier = 'lib.anahita.model.person.story';
			else
				$identifier = 'lib.anahita.model.person.story.'.$type;
		else
			$identifier 	= $type;
					
		$story      = KFactory::get('lib.anahita.domain.factory.model')->get($identifier);
		
		$story->setProperty(array(
			'type'		  => $type,
			'subject'     => $options['subject'] ,
			'owner'		  => $options['owner']   ,
			'target'	  => $options['target']  ,
			'author'	  => $options['author']  ,
			'object'	  => $options['object'],
			'application' => $this,
			'template'    => $template
		));
		
		return $story;
	}
	
	/**
	 * Publish a request for a target
	 * @return 
	 * @param $name String
	 * @param $options Array
	 */
	public function publishRequest($name, $options)
	{
		return $this->publishStory('request', $name, $options);
	}
	
	/**
	 * 
	 * @return 
	 * @param $name Object
	 * @param $options Object
	 */
	public function publishNotification($name, $options = array())
	{
		return $this->publishStory('notification', $name, $options);	
	}
	
	/**
	 * 
	 * @return 
	 */
	public function awakeFromFetch()
	{
		//load the application langauge file
		KFactory::get('lib.joomla.language')->load( $this->component );
	}
		
	/**
	 * Sets the runtime context of application
	 * @return 
	 * @param $context AnModelApplicationContext
	 */	
	public function setRuntimeContext($context)
	{
		$this->_runtime_context = $context;		
	}
	
	/**
	 * Get the runtime context of application
	 * @return 
	 */
	public function getRuntimeContext()
	{
		if ( !isset($this->_runtime_context) || is_null($this->_runtime_context) ) {
			$this->_runtime_context = KFactory::get('lib.anahita.model.application.context');
		}
		return $this->_runtime_context;
	}
	
	/**
	 * Add a gadget to this application. This usually called by the subclasses to add gadgets 
	 * @return AnModelApplicationGadget
	 * @param $gadget Array
	 */	
	public function addGadget(array $gadget)
	{
		$gadget['component'] = $this->component;
		$gadget = KFactory::tmp('lib.anahita.model.application.gadget', $gadget);
		$this->_gadgets[$gadget->getType()][$gadget->getName()] = $gadget;
		return $gadget;
	}
	
	/**
	 * Get gadget of a type defined by the application itself. 
	 * $app->getGadgets('profile') returns all the gadgets of type profile
	 * $app->getGadgets('dashboard') returns all the gadgets of type dashboard
	 * @return Array
	 * @param $type String Object
	 */
	public function getGadgets($type)
	{
		if  ( is_null($this->_gadgets) ) 
		{
			$this->_gadgets = array(AnModelApplicationGadget::TypeProfile=>array(), AnModelApplicationGadget::TypeDashboard=>array());
			$this->_loadGadgets();
		}

		return $this->_gadgets[$type];
	}
	
	/**
	 * Adds this application to the profile of an actor
	 * @param $actor AnModelActor Object
	 */
	public function addToProfile($actor)
	{
		if ( !$actor->getAcl()->canAddApplication($this) )
			return false;
		
		$setting = $this->getSetting($actor);
		
		$setting->addedToProfile = 1;
		
		return $setting->save();
	}
	
	/**
	 * removes an application from a profile
	 * @param $actor AnModelActor Object
	 */
	public function removeFromProfile($actor)
	{
		if ( !$actor->getAcl()->canRemoveApplication($this) )
			return false;			
		
		$setting = $this->getSetting($actor);
		
		$setting->addedToProfile = 0;
		
		return $setting->save();
	}
	
	/**
	 * Information in application component xml
	 * 
	 * @return mixed
	 */	
	public function getInfo($key)
	{
		if ( !isset($this->_info) ) {		
			
			jimport('joomla.filesystem.folder');
			
			$this->_info[] = array();
			
			$admin_dir = JPATH_ADMINISTRATOR .DS. 'components'.DS.$this->component;
			$site_dir  = JPATH_SITE .DS. 'components'.DS.$this->component;
			
			if ( JFolder::exists($admin_dir) ) {
				$folder = $admin_dir;
				$files = JFolder::files($admin_dir, '.xml$');	
			} else if ( JFolder::exist($site_dir) ) {
				$folder = $site_dir;
				$files  = JFolder::files($site_dir, '.xml$');	
			} else {
				$files = null;
			}
			
			if ( empty($files) ) 
				return;
			
			foreach ($files as $file)
			{
				if ($data = JApplicationHelper::parseXMLInstallFile($folder.DS.$file))
					foreach($data as $key => $value) {
						$this->_info[$key] = $value;
					}
			}
		}
		
		return isset($this->_info[$key]) ? $this->_info[$key] : null;
	}
	
	/**
	 * 
	 * @return 
	 */
	public function getSettingUrl()
	{
		return 'index.php?option='.$this->component.'&view=setting';
	}
		
	/**
	 * Return setting of this application stored for the actor. This is a short method for application to test a 
	 * setting 
	 * @return mixed
	 * @param $actor AnModelActor;
	 */
	public function getActorSettingValue($actor, $key, $default)
	{		
		return $this->getSetting($actor)->getParam($key, $default);
	}
	
	/**
	 * Get setting object of this application and the actor
	 * 
	 * @return AnModelSetting
	 * @param $actor AnModelActor
	 */
	public function getSetting($actor)	
	{
		return KFactory::get('lib.anahita.domain.factory.repository')
			->get('lib.anahita.model.setting')->getSetting($this, $actor);		
	}
	
	/**
	 * Return component configuration value
	 * 
	 * @param string $key
	 * @param mixed $default
	 * @return mixed
	 */
	public function getConfiguration($key, $default=null)
	{
		return JComponentHelper::getParams($this->component)->get($key, $default);
	}
	
	/**
	 * 
	 * @return 
	 * @param $type Object
	 * @param $name Object
	 */
	public function getStoryTemplate($type, $name)
	{
		$this->_getStoryTemplates();
		
		if ( !isset($this->_story_templates[$type]) )
			return null;
		
		$templates = $this->_story_templates[$type];

		if ( !isset($templates[$name]) )
			return null;
				
		return $templates[$name];
	}
	
	/**
	 * Add a story template to the application. Used by the subclasses to add their 
	 * owne story template. Accept an array of data required to instantiate a template object
	 * @return AnModelStoryTemplate
	 * @param $options array
	 */
	public function addStoryTemplate(array $options)
	{
		if ( $options['type'] == 'email' ) {
			$template = new AnModelStoryTemplateEmail( $options );
			$default_filter = KFactory::get('lib.anahita.model.story.template.filter.email');				
		} else {
			$options['component'] = $this->component;
			$template = new AnModelStoryTemplate( $options );		
			$default_filter = KFactory::get('lib.anahita.model.story.template.filter.default');	
		}
		
		$template->addFilter( $default_filter );
		$this->_story_templates[$template->getType()][$template->getName()] = $template;
		return $template;
	}
			
	/**
	 * 
	 * @return 
	 */
	protected function _loadGadgets() 
	{
		//subclass overwrite this method
	}
	
	/**
	 * 
	 * @return 
	 */	
	protected function _getStoryTemplates()
	{
		if  ( is_null($this->_story_templates) ) 
		{		
			$this->_story_templates = array('story'=>array(), 'notification'=>array(), 'request'=>array(),'email'=>array());
			$this->_loadStoryTemplates();
		}
		
		$this->_story_templates;
	}
	
	protected function _loadStoryTemplates() 
	{
		//subclass overwrite this method
	}
//end class
}
Return current item: Anahita Social Engine