<?php
namespace Entity;

class CustomerSegment 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;

	protected $addAndUpdateDateTimeFormat = 'timestamp'; // TODO: to be eliminated after the table columns are renamed and replaced with datetime

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

	/**
	 * (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 \Entity\User::select($this->fields['idAddUser'], $this->selectForUpdate);
			}

			// updateUser
			case 'updateUser': {
				return \Entity\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)
	{
		// check that operation is okay
		if ($this->operation!='insert' and $this->operation!='update') {
			throw new \Exception('You can use __set magic method only with insert/update operation in `'.__CLASS__.'` class.');
		}

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

			// name
			case 'name': {
				$this->fields[$var]=$value;
				break;
			}

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

	/**
	 * (non-PHPdoc)
	 * @see App\Entity.GeneralAbstract::save_postProcess()
	 */
	protected function save_postProcess(Array $saveData)
	{
		if ($this->operation == 'delete') {
			// delete customers associations
			$this->save_postProcess_deleteAssocCustomers($saveData);
		}
	}


	/**
	 * Remove associations with customers if the segemnt is deleted
	 * @param Array $saveData
	 */
	private function save_postProcess_deleteAssocCustomers(Array $saveData)
	{
		\App\Loader_Lang::getInstance()->loadAppFile("_customLoad/entities/CustomerSegment.lang.php");
		$_LANG = \App\Loader_Lang::getInstance()->getAll();
		try {
			$custObj = new \App\EntityList\Customers();
			$custObj->setFilter('cust.idSegment', "=", $this->id);
			$custObj->setForUpdate(true);
			$customers = $custObj->getExecution();

			foreach ($customers as $customer) {
				$cust = \Entity\Customer::update($customer['id']);
				$cust->idSegment = 0;
				$cust->save();
			}
		} catch (\App\Entity\Exception $e) {
			$msgs = new \App\MessagesCollector();
			$msgs->appendMessages($e->getMessagesCollector(), $_LANG['globalEntityMessages']['::CustomerSegment']['delete_failCustomersAssoc']);
			$this->messages->removeSuccess();
			$this->messages->addMessageCollector($msgs);
			$this->messages->throwMessages();
		}
	}
}