<?php

namespace Modules\Utils\AppReports;


class SqlManager extends \App\Config\XmlDynamicRecordsAbstract
{
	/**
	 * gets the files from temporary due to the modification of constructInit()
	 * @var string
	 */
	protected $fileName = 'appReports.xml';


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::constructInit()
	 */
	protected function constructInit()
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// check if the file name is okay
		if (!$this->fileName) {
			throw new \Exception('You did not set the `fileName` attribute for the class `'.get_called_class().'`.');
		}

		// set the path to temporary
		$this->fileNameWithPath = APP_REAL_PATH . $CFG['paths']['temporary'] . $this->fileName;

		//\App\UserTrail::getInstance()->storeTrail(false);

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_list()
	 */
	protected function getAll_rawData_extractDataFromFile_list(\SimpleXMLElement $xml)
	{
		$return=[];

		foreach($xml->report  as $appReport) {
			$return[(String)$appReport->name] = [
				"name"				=>	(String)$appReport->name,
				"sql"				=>	(String)$appReport->sql,
				"monthTable"		=>	(String)$appReport->monthTable,
			];

		}
		return $return;

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_default()
	 */
	protected function getAll_rawData_extractDataFromFile_default(\SimpleXMLElement $xml)
	{
		return '';
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlDynamicRecordsAbstract::getAll_rawData_extractDataFromFile_mandatory()
	 */
	protected function getAll_rawData_extractDataFromFile_mandatory(\SimpleXMLElement $xml)
	{
		return false;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Config\XmlDynamicRecordsAbstract::itemCheck()
	 */
	protected function itemCheck($code, $itemDetails)
	{

		if (!$itemDetails) $this->addErrorMessage('The appReport data details was not defined.');

		if (!$code) $this->addErrorMessage('The app report name is not set.');

		if (!$itemDetails['name']) {
			$this->addErrorMessage('The name is not set.');
		}

		if (!$itemDetails['sql']) {
			$this->addErrorMessage('The sql is not set.');
		}

		if ($itemDetails['sql'] and strtoupper(substr($itemDetails['sql'], 0, 6))!='SELECT') {
			$this->addErrorMessage('The sql does not contain `SELECT`.');
		}

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\BaseAbstract::save_check()
	 */
	protected function save_check()
	{

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Config\XmlBasicAbstract::save_write_do_getXml()
	 */
	protected function save_write_do_getXml()
	{
		$xml=simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><reports />');
		foreach ($this->itemsList as $currReport) {

			$currentXML = $xml->addChild('report', '');
			$currentXML->name = $currReport['name'];
			$currentXML->sql = $currReport['sql'];
			$currentXML->monthTable = $currReport['monthTable'];

		}

		// return the xml content of the current object
		return $xml;

	}



}
