<?php
/**
* @package		EasySocial
* @copyright	Copyright (C) 2010 - 2012 Stack Ideas Sdn Bhd. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* EasySocial is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
defined( '_JEXEC' ) or die( 'Unauthorized Access' );

class KunenaActivityEasySocial extends KunenaActivity
{
	protected $params = null;

	public function __construct($params)
	{
		$this->params = $params;
	}

	public function assignBadge( $command , $message , $target = null )
	{
		$user 	= Foundry::user( $target );
		$badge 	= Foundry::badges();

		return $badge->log( 'com_kunena' , $command , $user->id , $user->id );
	}

	public function assignPoints( $command , $target = null )
	{
		$user 	= Foundry::user( $target );

		$points = Foundry::points();

		return $points->assign( $command , 'com_kunena' , $user->id );
	}

	public function onAfterPost($message)
	{
		if (JString::strlen($message->message) > $this->params->get('activity_points_limit', 0))
		{
			$this->assignPoints( 'thread.new' );
		}

		if(JString::strlen($message->message) > $this->params->get('activity_badge_limit', 0))
		{
			$this->assignBadge( 'thread.new' , JText::_( 'PLG_KUNENA_EASYSOCIAL_BADGE_NEW_TITLE' ) );
		}

		$stream 	= Foundry::stream();

		$tmpl 	= $stream->getTemplate();

		$tmpl->setActor( $message->userid , SOCIAL_TYPE_USER );
		$tmpl->setContext( $message->thread , 'kunena' );
		$tmpl->setVerb( 'create' );
		$tmpl->setPublicStream( 'core.view' );

		$stream->add( $tmpl );
	}

	public function onAfterReply($message)
	{
		if(JString::strlen($message->message) > $this->params->get('activity_points_limit', 0))
		{
			$this->assignPoints( 'thread.reply' );
		}

		if(JString::strlen($message->message) > $this->params->get('activity_badge_limit', 0))
		{
			$this->assignBadge( 'thread.reply' , JText::_( 'PLG_KUNENA_EASYSOCIAL_BADGE_REPLY_TITLE' ) );
		}

		$tmpl 		= Foundry::stream()->getTemplate();

		$tmpl->setActor( $message->userid , SOCIAL_TYPE_USER );
		$tmpl->setContext( $message->id , 'kunena' );
		$tmpl->setVerb( 'reply' );
		$tmpl->setPublicStream( 'core.view' );

		Foundry::stream()->add( $tmpl );
	}

	public function onAfterThankyou( $actor , $target , $message)
	{
		if (JString::strlen($message->message) > $this->params->get('activity_points_limit', 0))
		{
			$this->assignPoints( 'thread.thanks' , $target );
		}

		$this->assignBadge( 'thread.thanks' , JText::_( 'PLG_KUNENA_EASYSOCIAL_BADGE_THANKED_TITLE' ) , $target );

		$tmpl 		= Foundry::stream()->getTemplate();

		$tmpl->setActor( $actor , SOCIAL_TYPE_USER );
		$tmpl->setTarget( $target );
		$tmpl->setContext( $message->id , 'kunena' );
		$tmpl->setVerb( 'thanked' );
		$tmpl->setPublicStream( 'core.view' );

		Foundry::stream()->add( $tmpl );
	}

	public function onBeforeDeleteTopic( $target )
	{
		Foundry::stream()->delete( $target->id , 'thread.new' );
	}

	public function onAfterDeleteTopic( $topic )
	{
		Foundry::stream()->delete( $topic->id , 'thread.new' );
	}

}
