<?php
/**
 * @brief		chatboxWidget Widget
 * @author		<a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
 * @copyright	(c) Invision Power Services, Inc.
 * @license		https://www.invisioncommunity.com/legal/standards/
 * @package		Invision Community
 * @subpackage	chatbox
 * @since		03 Dec 2018
 */

namespace IPS\chatbox\widgets;

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

/**
 * chatboxWidget Widget
 */
class _chatboxWidget extends \IPS\Widget
{
	/**
	 * @brief	Widget Key
	 */
	public $key = 'chatboxWidget';
	
	/**
	 * @brief	App
	 */
	public $app = 'chatbox';
		
	/**
	 * @brief	Plugin
	 */
	public $plugin = '';
	
	/**
	 * Initialise this widget
	 *
	 * @return void
	 */ 
	public function init()
	{
		$this->template( array( \IPS\Theme::i()->getTemplate( 'widgets', $this->app, 'front' ), $this->key ) );
		
		parent::init();
	}

	/**
	 * Specify widget configuration
	 *
	 * @param	null|\IPS\Helpers\Form	$form	Form object
	 * @return	null|\IPS\Helpers\Form
	 */
	public function configuration( &$form=null )
	{
		$form = parent::configuration( $form );

		$options = array();
		
		foreach( \IPS\chatbox\Rooms\Room::roots() as $room )
		{
			$options[ $room->id ] = $room->_title;
		}

		$form->add( new \IPS\Helpers\Form\Node( 'room_to_show', $this->configuration['room_to_show'], TRUE, array(
			'class' 	=> 'IPS\chatbox\Rooms\Room', 
			'clubs'		=> TRUE,
			'multiple' 	=> FALSE
		) ) );	
		

		$form->add( new \IPS\Helpers\Form\CheckboxSet( 'devices_to_mini', isset( $this->configuration['devices_to_mini'] ) ? $this->configuration['devices_to_mini'] : array(), FALSE, array( 'options' => array( 'Phone' => \IPS\Member::loggedIn()->language()->addToStack( 'device_phone' ), 'Tablet' => \IPS\Member::loggedIn()->language()->addToStack( 'device_tablet' ), 'Desktop' => \IPS\Member::loggedIn()->language()->addToStack( 'device_desktop' ) ) ), NULL, NULL, NULL, 'devices_to_mini' ) );

		$form->add( new \IPS\Helpers\Form\Radio( 'chatbox_where_to_show', $this->configuration['chatbox_where_to_show'], TRUE, array(
			'options' => array( 'all' => 'chatbox_where_to_show_all', 'pages' => 'chatbox_where_to_show_pages' ),
			'toggles' => array(
				'pages'	=> array( 'chatbox_pages' )
			)
		) ) );		
		
		$form->add( new \IPS\Helpers\Form\TextArea( 'chatbox_pages', isset( $this->configuration['chatbox_pages'] ) ? $this->configuration['chatbox_pages'] : $_SERVER["HTTP_REFERER"], FALSE, array('rows' => 3), null, null, null, 'chatbox_pages' ) );		
		
 		return $form;
 	} 

	
 	 /**
 	 * Ran before saving widget configuration
 	 *
 	 * @param	array	$values	Values from form
 	 * @return	array
 	 */
 	public function preConfig( $values )
 	{
		$values['room_to_show'] = $values['room_to_show']->id;
		return $values;
	}
	
	
	/**
	 * Render a widget
	 *
	 * @return	string
	 */
		 
	public function render()
	{
		$canShow = true;
		
		if ( $this->configuration['chatbox_where_to_show'] == 'pages' && $this->configuration['chatbox_pages'] )
		{
			$canShow = false;
			
			$currentpageURL = (string) \IPS\Request::i()->url();
			$pages = explode("\n", str_replace("\r", '', $this->configuration['chatbox_pages']));
			foreach( $pages as $page )
			{
				if ( $currentpageURL == $page || preg_match( "~^" . str_replace( '\*', '.*?', preg_quote( $page ) ) . "$~i", htmlspecialchars_decode( $currentpageURL ) ) )
				{
					$canShow = true;
					break;
				}
			}
		}
		
		if ( $canShow )
		{
			try
			{
				$room = \IPS\chatbox\Rooms\Room::load( $this->configuration['room_to_show'] );
				
				if ( $room->_canView() )
				{
					return $this->output( $room, $this->configuration );
				}
			}
			catch( \OutOfRangeException $e ) {}
		}
		return '';
	}	
}