<?php

namespace IPS\chatbox\Member;

/* 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;
}

/**
 * Personal Conversation Message
 */
class _Post extends \IPS\Content\Comment
{
	use \IPS\Content\Reportable;

	/**
	 * @brief	Application
	 */
	public static $application = 'chatbox';
	
	/**
	 * @brief	Module
	 */
	public static $module = 'conversation';
	
	/**
	 * @brief	[ActiveRecord] Database Table
	 */
	public static $databaseTable = 'chatbox_conversations_posts';
	
	/**
	 * @brief	Database Prefix
	 */
	public static $databasePrefix = 'chat_';	
	
	/**
	 * @brief	[ActiveRecord] Multiton Store
	 */
	protected static $multitons;
	
	/**
	 * @brief	[Content\Comment]	Title
	 */
	public static $title = 'chatbox_conversation_post';
	
	/**
	 * @brief	[Content\Comment]	Icon
	 */
	public static $icon = 'comments';
	
	/**
	 * @brief	[Content\Comment]	Item Class
	 */
	public static $itemClass = 'IPS\chatbox\Member\Conversation';
	
	/**
	 * @brief	[Content\Item]	First "comment" is part of the item?
	 */
	public static $firstCommentRequired = FALSE;
	
	/**
	 * @brief	[Content\Comment]	Database Column Map
	 */
	public static $databaseColumnMap = array(
		'title'			=> 'title',
		'item'			=> 'con',
		'date'			=> 'time',
		'content'		=> 'content',
		'author'		=> 'member_id',
		'ip_address'	=> 'ip_address',
	);
	
	/**
	 * @brief	[Content\Comment]	Language prefix for forms
	 */
	public static $formLangPrefix = 'chatbox_con_msg_';
	
	/**
	 * Get URL for doing stuff
	 *
	 * @param	string|NULL		$action		Action
	 * @return	\IPS\Http\Url
	 */
	public function url( $action='find' )
	{
		return parent::url( $action );
	}
	
	/**
	 * Get items with permisison check
	 *
	 * @param	array		$where				Where clause
	 * @param	string		$order				MySQL ORDER BY clause (NULL to order by date)
	 * @param	int|array	$limit				Limit clause
	 * @param	string		$permissionKey		A key which has a value in the permission map (either of the container or of this class) matching a column ID in core_permission_index
	 * @param	bool|NULL	$includeHiddenItems	Include hidden files? Boolean or NULL to detect if currently logged member has permission
	 * @param	int			$queryFlags			Select bitwise flags
	 * @param	\IPS\Member	$member				The member (NULL to use currently logged in member)
	 * @param	bool		$joinContainer		If true, will join container data (set to TRUE if your $where clause depends on this data)
	 * @param	bool		$joinComments		If true, will join comment data (set to TRUE if your $where clause depends on this data)
	 * @param	bool		$joinReviews		If true, will join review data (set to TRUE if your $where clause depends on this data)
	 * @return	\IPS\Patterns\ActiveRecordIterator|int
	 */
	public static function getItemsWithPermission( $where=array(), $order=NULL, $limit=10, $permissionKey='read', $includeHiddenItems=\IPS\Content\Hideable::FILTER_AUTOMATIC, $queryFlags=0, \IPS\Member $member=NULL, $joinContainer=FALSE, $joinComments=FALSE, $joinReviews=FALSE, $countOnly=FALSE, $joins=NULL, $skipPermission=FALSE, $joinTags=TRUE, $joinAuthor=TRUE, $joinLastCommenter=TRUE, $showMovedLinks=FALSE )
	{
		$joins[] = array(
			'from'		=> array( "chatbox_files", "file" ),
			'where'		=> "file.file_from='con' AND file.file_msg_id=chatbox_conversations_posts.chat_id",
		);

		return parent::getItemsWithPermission( $where, $order, $limit, $permissionKey, $includeHiddenItems, $queryFlags, $member, $joinContainer, $joinComments, $joinReviews, $countOnly, $joins, $skipPermission, $joinTags, $joinAuthor, $joinLastCommenter, $showMovedLinks );
	}
	
	/**
	 * Construct Load Query
	 *
	 * @param	int|string	$id					ID
	 * @param	string		$idField			The database column that the $id parameter pertains to
	 * @param	mixed		$extraWhereClause	Additional where clause(s)
	 * @return	\IPS\Db\Select
	 */
	protected static function constructLoadQuery( $id, $idField, $extraWhereClause )
	{
		$parent = parent::constructLoadQuery( $id, $idField, $extraWhereClause );

		$parent->join( array( "chatbox_files", "file" ), "file.file_from='con' AND file.file_msg_id=chatbox_conversations_posts.chat_id" );
	
		return $parent;
	}
		
	/**
	 * Do not add sitemap
	 *
	 * @return	array
	 */
	public static function sitemapWhere()
	{
		return array( array( 'chatbox_conversations_posts.chat_id=?', 0 ) );
	}	
	
	/**
	 * Can delete?
	 *
	 * @param	\IPS\Member|NULL	$member	The member to check for (NULL for currently logged in member)
	 * @return	bool
	 */
	public function canDelete( $member=NULL )
	{
		$member = $member ?: \IPS\Member::loggedIn();
		return $this->item()->_canDelete( $member->member_id );
	}

	/**
	 * @brief	Cache of normal images
	 */
	protected $_imagesNormal = NULL;

	/**
	 * @brief	Cache of thumbnails
	 */
	protected $_imagesThumbs = NULL;

	/**
	 * Get images
	 * @return	\IPS\File\Iterator
	 */
	public function images( $type=0 )
	{
		switch ( $type )
		{
			case 0:
				if( $this->_imagesNormal !== NULL )
				{
					return $this->_imagesNormal;
				}
				$valueField = 'file_location';
				$property	= "_imagesNormal";
				break;
			case 1:
				if( $this->_imagesThumbs !== NULL )
				{
					return $this->_imagesThumbs;
				}
				$valueField = 'file_thumb';
				$property	= "_imagesThumbs";
				break;
			default:
				throw new \InvalidArgumentException;
		}
		
		$where = array( array( 'file_from=? AND file_msg_id=?', 'con', $this->id ) );

		$iterator = \IPS\Db::i()->select( '*', 'chatbox_files', $where )->setKeyField( 'file_id' );
		$iterator = new \IPS\File\Iterator( $iterator, 'chatbox_File', $valueField, FALSE, 'file_realname' );
		$iterator = new \CachingIterator( $iterator, \CachingIterator::FULL_CACHE );

		$this->$property = $iterator;
		return $this->$property;
	}
	
	
	/**
	 * Delete Record
	 *
	 * @return	void
	 */
	public function delete()
	{
		parent::delete();
		\IPS\Application::load('chatbox')->deleteFilesFromMSG( 'con', $this->id );
	}	
	
}