<?php

namespace Modules\Admin\Config\Server\Email;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$form = \Engine\Form\General::create();

		$label = new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['description']);
		$form->addElement($label);

		// email settings
		$fieldsetEmailSettings = \Engine\Form\Container\Fieldset::create($this->lang['fieldsets']['emailSettings']);
		$this->emailSettingsForm($fieldsetEmailSettings);
		$form->addContainer($fieldsetEmailSettings);

		// autosender email addressess
		$fieldsetEmailAddresses = \Engine\Form\Container\Fieldset::create($this->lang['fieldsets']['emailAddresses']);
		$this->emailAddressesForm($fieldsetEmailAddresses);
		$form->addContainer($fieldsetEmailAddresses);

		//save button
		$form->addElement(new \Engine\Form\Input\Hidden(null, 'save', 1));
		$form->addButton(new \Engine\Form\Button\Submit('save'));

		return $form;
	}

	/**
	 * Add email settings form elements
	 * @param \Engine\Form\Container\Fieldset $fieldset
	 */
	private function emailSettingsForm($fieldset)
	{
		// details label
		$fldDetails = new \Engine\Form\Input\Label();
		$fldDetails->setDetails($this->lang['emailSettings']['form_details']);
		$fldDetails->setStyle("noLabel", true);
		$fieldset->addElement($fldDetails);

		// protocol field
		$fldProtocol = new \Engine\Form\Input\Radio($this->lang['emailSettings']['formFields']['protocol'], 'emailCfg[protocol]', $this->values['emailCfg']['protocol']);
		$fldProtocol->setPossibleValues(['smtp'=>$this->lang['emailSettings']['formFields']['protocolOptionSmtp'], 'sendmail'=>$this->lang['emailSettings']['formFields']['protocolOptionSendmail']]);
		$fldProtocol->setMandatory(true);
		$fieldset->addElement($fldProtocol);

		// smtp host
		$fldSmtpHost=new \Engine\Form\Input\Text($this->lang['emailSettings']['formFields']['host'], 'emailCfg[smtpHost]', $this->values['emailCfg']['smtpHost']);
		$fldSmtpHost->setMandatory(true);
		$fieldset->addElement($fldSmtpHost);

		// smtp port
		$fldSmtpPort=new \Engine\Form\Input\Text($this->lang['emailSettings']['formFields']['port'], 'emailCfg[smtpPort]', $this->values['emailCfg']['smtpPort']);
		$fldSmtpPort->setMandatory(false);
		$fldSmtpPort->setValidatorParam('type', 'digits');
		$fldSmtpPort->setTooltip($this->lang['emailSettings']['tooltips']['port']);
		$fieldset->addElement($fldSmtpPort);

		// smtp user
		$fldSmtpUser=new \Engine\Form\Input\Text($this->lang['emailSettings']['formFields']['user'], 'emailCfg[smtpUser]', $this->values['emailCfg']['smtpUser']);
		$fldSmtpUser->setTooltip($this->lang['emailSettings']['tooltips']['user']);
		$fieldset->addElement($fldSmtpUser);

		// smtp pass
		$fldSmtpPass=new \Engine\Form\Input\Password($this->lang['emailSettings']['formFields']['pass'], 'emailCfg[smtpPass]', '');
		$fldSmtpPass->setTooltip($this->lang['emailSettings']['tooltips']['pass']);
		$fieldset->addElement($fldSmtpPass);

		// smtp secure
		$fldSmtpSecure=new \Engine\Form\Input\Select($this->lang['emailSettings']['formFields']['secure'], 'emailCfg[smtpSecure]', $this->values['emailCfg']['smtpSecure']);
		$fldSmtpSecure->setPossibleValues([
			''		=> $this->lang['emailSettings']['formFields']['secureOptionNot'],
			'ssl'	=> $this->lang['emailSettings']['formFields']['secureOptionSsl'],
			'tls'	=> $this->lang['emailSettings']['formFields']['secureOptionTls']
		]);
		$fieldset->addElement($fldSmtpSecure);

		// smtp ignore certificates
		$fldSmtpIgnoreCertif=new \Engine\Form\Input\Checkbox($this->lang['emailSettings']['formFields']['ignoreCertif'], 'emailCfg[smtpIgnoreCertif]', $this->values['emailCfg']['smtpIgnoreCertif']);
		$fldSmtpIgnoreCertif->setTooltip($this->lang['emailSettings']['tooltips']['ignoreCertif']);
		$fieldset->addElement($fldSmtpIgnoreCertif);

		// SMTP SSL ciphers
		$fldSmtpSslCiphers = new \Engine\Form\Input\Text($this->lang['emailSettings']['formFields']['sslCiphers'], 'emailCfg[smtpSslCiphers]', $this->values['emailCfg']['smtpSslCiphers']);
		$fldSmtpSslCiphers->setTooltip(sprintf($this->lang['emailSettings']['tooltips']['smtpSslCiphers'], 'DEFAULT@SECLEVEL=1'));
		$fieldset->addElement($fldSmtpSslCiphers);

		// smtp force sender from username
		$fldForceSenderFromUsername=new \Engine\Form\Input\Checkbox($this->lang['emailSettings']['formFields']['forceSenderFromSmtpUser'], 'emailCfg[forceSenderFromSmtpUser]', $this->values['emailCfg']['forceSenderFromSmtpUser']);
		$fldForceSenderFromUsername->setTooltip($this->lang['emailSettings']['tooltips']['forceSenderFromSmtpUser']);
		$fieldset->addElement($fldForceSenderFromUsername);

		// show/hide fields depending on protocol
		$fldProtocol->setControlledElements( [ 'smtp'=> [ $fldSmtpHost, $fldSmtpPort, $fldSmtpUser, $fldSmtpPass, $fldSmtpSecure, $fldSmtpIgnoreCertif, $fldSmtpSslCiphers, $fldForceSenderFromUsername ] ] , 'hide');
	}

	/**
	 * EMAIL ADDRESSES
	 * @param \Engine\Form\Container\Fieldset $fieldset
	 */
	private function emailAddressesForm($fieldset)
	{
		// details label
		$fldDetails = new \Engine\Form\Input\Label();
		$fldDetails->setDetails($this->lang['emailAddresses']['form_details']);
		$fldDetails->setStyle("noLabel", true);
		$fieldset->addElement($fldDetails);

		$senderTypes = \App\Config\AutoEmailSender::getInstance()->getSenderTypes();
		$emailAddresses = $this->values['autoSenderEmailAddress'];

		foreach($senderTypes as $emailType) {
			// email address
			${'fld'.$emailType.'Email'} = new \Engine\Form\Input\Text($this->lang['emailAddresses']['formFields'][$emailType.'Email'], 'autoSenderEmailAddress['.$emailType.'][email]', $emailAddresses[$emailType]['email']);
			${'fld'.$emailType.'Email'}->setTooltip($this->lang['emailAddresses']['tooltips'][$emailType.'Email']);
			${'fld'.$emailType.'Email'}->setValidator(['type'=>'email']);
			if (!in_array($emailType, ['invoice', 'cashing'])) {
				${'fld'.$emailType.'Email'}->setMandatory();
			}
			$fieldset->addElement(${'fld'.$emailType.'Email'});

			// sender name
			${'fld'.$emailType.'Name'}=new \Engine\Form\Input\Text($this->lang['emailAddresses']['formFields'][$emailType.'Name'], 'autoSenderEmailAddress['.$emailType.'][name]', $emailAddresses[$emailType]['name']);
			${'fld'.$emailType.'Name'}->setTooltip($this->lang['emailAddresses']['tooltips'][$emailType.'Name']);
			$fieldset->addElement(${'fld'.$emailType.'Name'});
		}
	}
}
