<?php

namespace App\Form\Input;

/**
 * User autocomplete field
 *
 */
class Autocomplete_MiceTypeAndProject extends Autocomplete_GeneralAbstract
{

	protected $class = "autocomplete";

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::getUrl()
	 */
	public function getUrl()
	{
		return ($this->url?:\Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/RemoteData/AutocompleteFieldData', '', ['class'=>'MiceTypeAndProject']));
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::getValue()
	 */
	public function getValue()
	{
		$v = parent::getValue();
		if ($v==='|') {
			$v = '';
		}
		return $v;
	}

	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete::determineDisplayValue()
	 */
	public function determineDisplayValue($displayId) :string
	{
		if ($displayId) {
			list($idProjectType, $idProject) = explode('|', $displayId);

			if ($idProject) {
				return (string)\Entity\MiceProject::select($idProject)->name;
			} elseif ($idProjectType) {
				return (string)\Entity\MiceProjectType::select($idProjectType)->name;
			} else {
				return '';
			}
		}
		return '';
	}

}
