<?php

/* format the output */

/*go on with the given value from <pot ? >*/
$formatedsettings = $pothelper->_formatsettings($potsettings);

/*check if we have to set pagedefault and set it to cargo*/
if (isset($formatedsettings['set'])) {
	$none = preg_replace('/\s*display\s*=\s*none\s*/', 'display=inline', $formatedsettings['pagedefault']); //replace var and field to avoid circles ?
	$this->cargo['settings'][$formatedsettings['set']] = $none;
}

/*check if we have a shortcut set, use the pagedefault needed */
$defsettings = array();
if (isset($formatedsettings['defaultsettings'])) {
	$def = $this->cargo['settings'][$formatedsettings['defaultsettings']];
	$defsettings = $pothelper->_formatsettings($def);
}

/* start with some default */
// default TODO: get it from config
/* last we create the array from the config default */
$configdefault = $this->getConf('default_settings');
$configdefault = $pothelper->_formatsettings($configdefault);
$backupdefault = "potid=pot_1 | decimals=0 | currency= | width=0 | formula=false | display=flex | sepmil=. | sepdec=,";
$backupdefault = $pothelper->_formatsettings($backupdefault);
$defaultsettings = array_merge($backupdefault, $configdefault);


/*set potid,check if it is unique, set it from input or ad a new number at the end */
$forpotid = $formatedsettings['potid'];
if (array_key_exists('potid', $formatedsettings)) {
	if (array_key_exists($forpotid, $this->cargo)) {
		$timestamp = time();
		$potid = $forpotid."_".uniqid();
		$this->cargo[$potid]['output']['error'] = "potid already exists: $forpotid ";
		
	} else {
		$potid = $formatedsettings['potid'];
	}
} else {
	preg_match('/^(.*?)(\d*?)$/', $this->cargo['pot'], $potidarr);
	$potidval = $potidarr[1];
	$potidnr = intval($potidarr[2]) + 1;
	$potid = $potidval.$potidnr;
}

/* merge general default and pagedefault and potsettings */
$finalsettings = array_merge($defaultsettings, $defsettings, $formatedsettings);

$this->cargo['pot'] = $potid; // set the var to use it global
$finalsettings['potid'] = $potid; // yes this value is double , why?
$this->cargo[$potid]['settings'] = $finalsettings; // set the var to use it global