<?php

namespace Modules\Admin\DocumentTemplates;

class View_Utils_FormUserAccess extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Creates the form for customer insert/update.
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		///////////////////////////////////////////////////////////////////////////////////////////
		$usersListObj = new \App\EntityList\Users();
		$usersListObj->setColumn('user.idOffice');
		$usersListObj->setColumn('CONCAT(lastName, " ", firstName)', 'name');
		$usersListObj->setColumn('office.name', 'officeName');
		$usersListObj->setOrderBy('office.name', 'ASC');
		$usersListObj->setOrderBy('user.lastName', 'ASC');
		$usersListObj->setOrderBy('user.firstName', 'ASC');
		$usersList = $usersListObj->exportRecords();

		// group users by office
		// parse the final list, and build the final array to be added to the inputs
		$checkboxTreeArr = [];
		$checkboxTreeValuesArr = [];
		foreach ($usersList as $userInfo) {
			$checkboxTreeArr[$userInfo['officeName']][$userInfo['id']] = $userInfo['name'];
			if ($this->values['validUsersList']) {
				$checkboxTreeValuesArr[$userInfo['officeName']][$userInfo['id']]  = in_array($userInfo['id'], $this->values['validUsersList']);
			}
		}
		///////////////////////////////////////////////////////////////////////////////////////////

		$form = \Engine\Form\General::create();

		$fieldset = \Engine\Form\Container\Fieldset::create();
		$form->addContainer($fieldset);

		// users
		$usersTree = \Engine\Form\Container\Tree::create($this->lang['access']['formFields']['users']);
		$usersTree->setStyle('asElement', true);
		$usersTree->setName('validUsersList');
		$usersTree->importSimpleTree($checkboxTreeArr, $checkboxTreeValuesArr);
		$usersTree->showCheckAll( \Engine\Form\Input\Checkbox::create($this->lang['access']['formFields']['allUsers'], 'allUsers', $this->values['allUsers'])->setStyle('highlight', 'highlight'));
		$fieldset->addContainer($usersTree);

		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));

		return $form;
	}
}