<?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']['descr']);
		$form->addElement($label);

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


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