<?php
namespace Entity;

class GdsProfileField extends \App\Entity\GeneralAbstract
{

	/**
	 * Indicate if the operations available for the current entity must be done inside a transaction or not.
	 * @var Boolean
	 */
	protected $saveInsideTransaction = true;


	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::setTableName()
	 */
	protected function setTableName(): void
	{
		global $DBTGdsProfileFields;
		$this->tableName=$DBTGdsProfileFields;
	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\SelectAbstract::getAttribute()
	 */
	protected function getAttribute($var)
	{

		// CHECK EVERY POSSIBLE VALUES THAT $var MAY HAVE -----------------------------------------
		switch ($var) {

			// addUser
			case 'addUser': {
				return User::select($this->fields['idAddUser'], $this->selectForUpdate);
			}

			case 'possibleValues': {
				if (!is_array($this->fields[$var])) {
					$possibleValues = unserialize($this->fields['possibleValues']);
					$this->fields[$var] = ($possibleValues) ?: [];
				}
				return $this->fields[$var];
			}

			case 'validator': {
				return unserialize($this->fields['validator']);
			}

			case 'idProfileTemplates': {
				if (!$this->fields[$var]) {
					global $DBTGdsProfileTemplatesFields;
					$db = \DB::getInstance();

					$sql = "SELECT `idProfileTemplate` FROM `{$DBTGdsProfileTemplatesFields}` WHERE `idProfileField`=?";
					$this->fields[$var] = $db->GetCol($sql, [$this->id]);
					if ($db->ErrorNo()) {
						throw new \DBException('SQL error while getting `idProfileTemplates`.');
					}
				}
				return $this->fields[$var];
			}

			// the rest of the fields (most of them are just copied)
			default: {
				return parent::getAttribute($var);
			}

		}

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::setAttribute()
	 */
	protected function setAttribute($var, $value)
	{

		// check that operation is okay
		if ($this->operation!='insert' and $this->operation!='update') {
			throw new \Exception('You can call '.__METHOD__.' only for insert/update operations.');
		}

		// CHECK EACH POSSIBLE FIELD VALUE --------------------------------------------------------
		switch ($var) {

			case 'category':
				if ($this->operation != 'insert') {
					throw new \Exception("The field `{$var}` can only be set for insert operation");
				}
				$this->fields[$var] = $value;
			break;

			case 'label': case 'description': case 'inputType': case 'validator': case 'mandatory':
			case 'remarkVar': case 'tagSabre': case 'tagAmadeus': case 'tagWorldspan': case 'tagGalileo': case 'tagApollo':
			case 'type': {
				$this->fields[$var]=$value;
				break;
			}

			case 'possibleValues': {
				if (!is_array($value)) {
					throw new \Exception('Possible values list can only be an array');
				}
				$this->fields[$var] = $value;
				break;
			}

			case 'validator': {
				if (!is_array($value)) {
					throw new \Exception('Validator can only be an array');
				}
				$this->fields[$var] = $value;
				break;
			}

			// the field is not valid
			default: {
				throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity.");
			}

		}

	}


	/**
	 * (non-PHPdoc)
	 * @see \App\Entity\GeneralAbstract::save_filterSaveData()
	 */
	protected function save_filterSaveData(Array $saveData)
	{
		// serialize possible values
		if (array_key_exists('possibleValues', $saveData)) {
			$saveData['possibleValues'] = ($saveData['possibleValues'] ? serialize($saveData['possibleValues']) : '');
		}

		// serialize validator
		if (array_key_exists('validator', $saveData)) {
			$validator = array_filter($saveData['validator']);
			$saveData['validator'] = ($validator ? serialize($validator) : '');
		}

		// clean possible values if not needed for this type of field
		if (array_key_exists('inputType', $saveData) && !in_array($saveData['inputType'], ['radio', 'select'])) {
			$saveData['possibleValues'] = '';
		}

		// mandatory (1/0, not null)
		if (array_key_exists('mandatory', $saveData)) {
			$saveData['mandatory'] = ($saveData['mandatory'] ? 1 : 0);
		}

		return $saveData;
	}

	/**
	 * {@inheritDoc}
	 * @see \App\Entity\GeneralAbstract::save_postProcess()
	 */
// 	protected function save_postProcess(Array $saveData)
// 	{
// 		global $DBTGdsProfileTemplatesFields;

// 		// delete field from all templates
// 		if ($this->operation=='delete') {
// 			$this->save_executeSql_delete($DBTGdsProfileTemplatesFields, 'idProfileField', $this->id);
// 		}

// 	}

}

