<?php
/**
 * @brief		contactus
 *
 * @copyright	(c) 2001 - 2016 Invision Power Services, Inc.
 *
 * @package		IPS Community Suite
 * @since		29 Sep 2016
 * @version		SVN_VERSION_NUMBER
 */

namespace IPS\core\modules\admin\settings;

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

/**
 * contactus
 */
class _contactus extends \IPS\Dispatcher\Controller
{
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		\IPS\Dispatcher::i()->checkAcpPermission( 'contactus_manage' );
		parent::execute();
	}

	/**
	 * Manage Settings
	 *
	 * @return	void
	 */
	protected function manage()
	{
		$form = $this->_getConfigForm();

		if ( $values = $form->values() )
		{
			$form->saveAsSettings();

			/* Clear guest page caches */
			\IPS\Data\Cache::i()->clearAll();

			\IPS\Session::i()->log( 'acplogs__contactus_settings' );
			\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=settings&controller=contactus' ), 'saved' );

		}

		\IPS\Output::i()->title		= \IPS\Member::loggedIn()->language()->addToStack( 'r__contactus' );
		\IPS\Output::i()->output	.= \IPS\Theme::i()->getTemplate( 'global' )->block( '', $form );
	}

	/**
	 * Build the configuration form
	 *
	 * @return \IPS\Helpers\Form
	 */
	protected function _getConfigForm()
	{
		$form = new \IPS\Helpers\Form;
		$options = array();
		$toggles = array();
		$disabled = array();
		$formFields = array();

		$form->add( new \IPS\Helpers\Form\Select( 'contact_access', ( \IPS\Settings::i()->contact_access == '*' ) ? '*' : explode( ',', \IPS\Settings::i()->contact_access ), FALSE, array(
			'options' 	=> array_combine( array_keys( \IPS\Member\Group::groups() ), array_map( function( $_group ) { return (string) $_group; }, \IPS\Member\Group::groups() ) ),
			'multiple' 	=> true,
			'unlimited'		=> '*',
			'unlimitedLang'	=> 'everyone'
		), NULL, NULL, NULL, 'contact_access' ) );

		/* Get extensions */
		$extensions = \IPS\Application::allExtensions( 'core', 'ContactUs', FALSE, 'core', 'InternalEmail', TRUE );

		foreach ( $extensions as $k => $class )
		{
			$class->process( $form, $formFields, $options, $toggles, $disabled );
		}

		$form->add( new \IPS\Helpers\Form\Radio( 'contact_type', \IPS\Settings::i()->contact_type, FALSE, array(
			'options' => $options,
			'toggles' => $toggles,
			'disabled' => $disabled
		) ) );

		foreach ( $formFields AS $field )
		{
			$form->add( $field );
		}

		return $form;
	}
	
	// Create new methods with the same name as the 'do' parameter which should execute it
}