<?php

namespace Modules\Utils\AppReports;

class View_Utils_FormExportConfig extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 * Add file names that should be ignored in the exportConfig form
	 * @var array
	 */
	private $ignoreFiles = ['dbAuthData.php', ];

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$configPath = $CFG['paths']['config'];

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

		// NEW CONTAINER ----------------------------------------------------------------------------------
		$fieldset=\Engine\Form\Container\Fieldset::create("Export configs as XML");
		$form->addContainer($fieldset);

		$fld = new \Engine\Form\Input\Checkboxes("Export options", 'exportOptions', $this->values['exportOptions']);
		$configFileList =  \Engine\Dir::getFileListRecursive($configPath);

		$configOptions = [];
		foreach ($configFileList as $configFile) {

			$fileName = \Engine\File::getName($configFile);

			if(in_array($fileName.".".\Engine\File::getExtension($configFile), $this->ignoreFiles)) {
				continue;
			}

			// get the folder name, in case it exists
			$pathName = \Engine\File::getPath($configFile);

			// get the position of /files/config/ where it starts in the pathname of the document
			$pathPosition = strpos($pathName, substr($configPath, 2));
			$folderName = substr($pathName, $pathPosition+strlen($configPath)-2);

			$configOptions[$folderName.$fileName.".".\Engine\File::getExtension($configFile)] = $folderName.$fileName.".".\Engine\File::getExtension($configFile);
		}

		$fld->setPossibleValues($configOptions);
		$fld->setValue($this->cfg['defaultValues']['formExportConfig']);
		$fld->setStyle('renderMode', 'block');
		$fieldset->addElement($fld);


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

		return $form;

	}

}