<?php


namespace IPS\chatbox\modules\front\conversation;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * conversation
 */
class _conversation extends \IPS\Content\Controller
{
	/**
	 * [Content\Controller]	Class
	 */
	protected static $contentModel = 'IPS\chatbox\Member\Conversation';
	
	
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		# Load room
		try
		{
			$this->convo = \IPS\chatbox\Member\Conversation::load( \IPS\Request::i()->id );
		}
		catch ( \OutOfRangeException $e )
		{
			\IPS\Output::i()->error( 'node_error', '2CHATBOX105/1', 404 );
		}

		parent::execute();
	}

	/**
	 * ...
	 *
	 * @return	void
	 */
	protected function manage()
	{
		// This is the default method if no 'do' parameter is specified
	}
	
	/**
	 * Find post
	 */
	protected function getPost()
	{
		try
		{
			$msg = \IPS\chatbox\Member\Post::load( \IPS\Request::i()->comment );
		}
		catch ( \OutOfRangeException $e )
		{
			\IPS\Output::i()->error( 'node_error', '2CHATBOX105/2', 404 );
		}
		
		return $msg;
	}
		
	/**
	 * Find post
	 */
	protected function findComment()
	{
		$opts = \IPS\chatbox\Member\Conversation::options();
		
		# Load post
		$msg = $this->getPost();

		$chats[] = $msg;
		$files = \IPS\Application::load('chatbox')->showFiles( $chats, 'con' );
		$content = \IPS\Application::load('chatbox')->parseContent( $opts, $msg->content ) . $files[ $id ];

		# Output
		$title = \IPS\Member::loggedIn()->language()->addToStack( 'chatbox_view_msg' );
		
		unset( \IPS\Output::i()->breadcrumb['module'] );
		\IPS\Output::i()->breadcrumb[] = array( $msg->item()->url(), $msg->item()->title );
		\IPS\Output::i()->breadcrumb[] = array( NULL, $title );
		
		\IPS\Output::i()->title = $title;
		\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'conversation', 'chatbox', 'front' )->viewMsg( $msg, $content );		
	}
	
	/**
	 * edit comment
	 */
	protected function editComment()
	{
		\IPS\Output::i()->redirect( $this->convo->url()->setQueryString( array( 'do' => 'findComment', 'comment' => \IPS\Request::i()->comment ) ) );
	}
	
	
}