<?php

// TODO: elimimare list assoc - avem DepartmentsList

namespace Entity;

/**
 * The departments management class.
 * @author Gabriel Grosu
 */
class Department 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 = false;


	/**
	 * Indicate how many seconds the external cache is valid. 0 for unlimited, -1 for no cache.
	 * @var Int
	 */
	const cacheTtl=604800;

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

	/**
	 * The instantiation of the current entity
	 * @return Department
	 */
	static public function current()
	{
		return self::select(CURRENT_ID_DEPT);
	}


	/**
	 * (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);
			}

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

			// 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)
	{

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

		switch ($var) {
			// simple fields
			case 'name': case 'code': {
				$this->fields[$var]=$value;
				break;
			}
			// the field is not valid
			default: {
				throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity.");
			}
		}

	}

}

