Location: PHPKode > projects > Anahita Social Engine > site/models/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 ComSocialEngineModelApplication extends AnModelApplication
{	
	
	/**
	 * Socialengine Application Icon
	 * @return 
	 */
	public function getIconUrl()
	{
		return 'media/com_socialengine/images/icons/person.png';
	}
	
	/**
	 * Get dashboard stories of the logged in person
	 * @return 
	 * @param $limit Object
	 * @param $offset Object
	 */
	public function getDashboardStories($limit, $offset)
	{		
		$viewer    = AnModelAnahita::getSessionViewer();
 		$graph 	   = KFactory::get('lib.anahita.model.person.graph', array('owner'=>$viewer));
		
		$people    =  $graph->getLeaders()->getArray();

		$people[] = $viewer;
		
		if ( empty($people) ) return array();
		
		$query	   =  KFactory::get('lib.anahita.domain.factory.query')->get('lib.anahita.model.person.story')

				->where('owner', 'IN', $people)
				->limit($limit, $offset)
				->order(array('date','lastCommentTime'), 'DESC')
				;
						
		$query->group[] = 'IF(story_bundle_id, story_bundle_id,  socialengine_node_id)';
	
		return $query->fetchAll();
	}
		
	/**
	 * 
	 * @return 
	 * @param $story Object
	 */
	public function publishStoryCommentNotification($view, $comment)
	{	/*
		$authors = AnUtilArrayHelper::collect($comment->node->comments, 'author');
		$array   = array();
		foreach($authors as $author) {
			if ( !in_array($author, $authors) && $comment->author->id != $author->id ) {
				
			}
		}*/
	}
	
	/**
	 * Publish story when the viewer leaves a message for the target
	 * @return 
	 * @param $target AnModelPerson
	 * @param $msg String
	 */
	public function publishPublicMessage($target, $msg)
	{
		
		$notification = $this->publishNotification('publicmessage');
		$notification->target = $target;
		$notification->owner  = $target;
		$notification->data->addText('message', $msg);
					
		$story = $this->publishStory('story', 'publicmessage_announcement');
		$story->target = $target;

		$story1 = $this->publishStory('story', 'publicmessage');
		$story1->data->addText('message', $msg);
		$story1->target = $target;
		$story1->owner  = $target;
	
		$story->bundleWith($story1);
		//create a notification;
				
		return $story1;
	}
	
	/**
	 * Publish a story update
	 * @return 
	 * @param $update Object
	 */
	public function publishStoryUpdate($update)
	{		
		$story   = $this->publishStory('story', 'story_update');
		$story->data->addText('story_update', $update);
		$story->subject->status = $update;
		return $story;
	}
	
	/**
	 * Publish profile update story
	 * @return 
	 * @param $owner Object
	 * @param $fields Object
	 */
	public function publishProfileUpdateStory($owner, $fields)
	{	
		$fields = array_unique($fields);
		
		if ( !count($fields) )
			return null;
			
		foreach($fields as &$field) {
			
			$local = 'AN-SE-PERSON-FIELD-'.strtoupper(str_replace('_','-',KInflector::underscore($field)));
			
			if ( $local == JText::_($local) ) {
				$field = KInflector::humanize(KInflector::underscore($field));
			} else 
				$field = JText::_($local)
				;
			$field = KFactory::tmp('lib.anahita.model.story.data.text', array('text'=>strtolower($field)));
		}

		$story = $this->publishStory('story',	'profile_update', $owner);
		
		$story->data['updatedFields'] = $fields;
		
		return $story;
	}
	
	/**
	 * Publish avatar story update
	 * @return 
	 * @param $owner Object
	 */
	public function publishAvatarStory($owner)
	{
		return $this->publishStory('story',	'avatar_update', $owner);
	}
	
	/**
	 * Publish a story when viewer is following the target
	 * @return 
	 * @param $target Object
	 */
	public function publishFollowStory($target, $message = null)
	{
						
		//send a notification to target
		$notification =  $this->publishNotification('person_follow');
		$notification->target = $target;
		$notification->owner  = $target;
		
		if ( $message )
		{
			$message = AnUtilStringHelper::truncate($message, array('length'=>500));
			$notification->data->addText('message', "\n\n\n$message\n\n\n");
		}
			
		$story = $this->publishStory('story',	'person_follow');
		$story->target = $target;
		$story->owner  = $target;
						
		$story2 = $this->publishStory('story',	'person_follow');
		$story2->target = $target;
		
		$story2->bundleWith($story);
		
		return $story;
	}
	
	/**
	 * SocialEngien Gadgets
	 * @return 
	 * @param $application Object
	 */
	protected function _loadGadgets()
	{
		$owner   = $this->getRuntimeContext()->getActor();

		$viewer  =  $this->getRuntimeContext()->getViewer();
					
		if ( $owner ) {
			$this->addGadget(array(
				'type'			=> 'profile' ,		
				'name'  		=> 'stories' ,
				'title' 		=> JText::_('AN-SE-GADGET-LABEL-STORY-UPDATES'),
				'url'   		=> JRoute::_('index.php?option=com_socialengine&view=stories&layout=profile&oid='.$owner->id),
				'icon_url'		=> 'media/com_socialengine/images/icons/stories.png'
			));
			
			$this->addGadget(array(
				'type'			=> 'profile'     ,
				'name'  		=> 'socialgraph' ,
				'title' 		=> JText::_('AN-SE-GADGET-LABEL-SOCIALGRAPH'),
				'title_url' 	=> JRoute::_('index.php?option=com_socialengine&view=socialgraph&oid='.$owner->id.'&alias='.$owner->alias),
				'url'   		=> JRoute::_('index.php?option=com_socialengine&view=socialgraph&layout=profile&oid='.$owner->id),
				'icon_url'		=> 'media/com_socialengine/images/icons/socialgraph.png'
			));
		}
	}
	
	/**
	 * Socialengine Story Template
	 * @return 
	 * @param $application Object
	 */
	protected function _loadStoryTemplates()
	{				
		$actor  = $this->getRuntimeContext()->getActor();
				
		$viewer = $this->getRuntimeContext()->getViewer();
		
		$this->addStoryTemplate(array(
				'icon' 			=> $actor ? 'media/com_socialengine/images/icons/edit_person.png' : null,
				'type'			=> 'story',
				'name'  		=> 'profile_update',
				'title'			=> 	'AN-SE-STORY-TITLE-UPDATE-PROFILE'
		));
		
		$this->addStoryTemplate(array(
				'icon' 			=> $actor ? 'media/com_socialengine/images/icons/edit_person.png' : null,
				'type'			=> 'story',
				'name'  		=> 'avatar_update',
				'title'			=> 'AN-SE-STORY-TITLE-UPDATE-AVATAR'
		));
		
		
		$this->addStoryTemplate(array(
				'icon'  		=> $actor ? 'media/com_socialengine/images/icons/person_follow.png' : null,		
				'type'			=> 'story',
				'name'  		=> 'person_follow',
				'title'			=> 	'AN-SE-STORY-TITLE-FOLLOW-ACTIVITY' 
		));
			
		$this->addStoryTemplate(array(
				'icon'  		=> 'media/com_socialengine/images/icons/person_follow.png',		
				'type'			=> 'notification' ,
				'name'  		=> 'person_follow',
				'title'			=> 	'AN-SE-STORY-TITLE-FOLLOW-ACTIVITY'
		));
		
		$this->addStoryTemplate(array(
				'type'			=> 'email',
				'name'  		=> 'person_follow',
				'subject'			=> 'AN-SE-STORY-FOLLOW-EMAIL-NOTIFICATION-SUBJECT',
				'body'				=> 'AN-SE-STORY-FOLLOW-EMAIL-NOTIFICATION-BODY'
		));			
								
		$this->addStoryTemplate(array(
				'type'			=>  'story',
				'name'  		=>  'publicmessage_announcement',
				'icon'  		=>  $actor ? 'media/com_socialengine/images/icons/publicmessage.png' : null,
				'title'			=> 	'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE-ANNOUNCEMENT',
				'commentable'	=> false
		));
		
		if ( $actor ) {
			$this->addStoryTemplate(array(
					'type'			=>  'story',
					'name'  		=>  'publicmessage',
					'title'			=> 	'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE',
					'body'			=> '${message}'
			));
		} else {
			
			$this->addStoryTemplate(array(
				
					'type'			=>  'story',
					'name'  		=>  'publicmessage',
					'title'			=> 	'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE-ANNOUNCEMENT',
					'body'			=> '${message}'
			));			
		}
		
		$this->addStoryTemplate(array(
			
				'type'			=> 'notification',
				'name'  		=> 'publicmessage',
				'title'			=> 	'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE-ANNOUNCEMENT',
				'body'			=> '${message}'
		));
		
		$this->addStoryTemplate(array(
				'type'			=> 'email',
				'name'  		=> 'publicmessage',
				'subject'			=> 'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE-EMAIL-NOTIFICATION-SUBJECT',
				'body'				=> 'AN-SE-STORY-TITLE-POST-PUBLIC-MESSAGE-EMAIL-NOTIFICATION-BODY'
		));		
		
		$this->addStoryTemplate(array(
				'type'			=> 'story',
				'name'  		=> 'story_update',
				'title'			=> 	'AN-SE-STORY-UPDATE',
				'body'  		=> '${story_update}'
		));	
		
	}
}
Return current item: Anahita Social Engine