<?php

namespace App\Form\Input;

/**
 * User autocomplete field
 *
 */
class Autocomplete_OfficeAndUser 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'=>'OfficeAndUser']));
	}

	/**
	 * (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($idOffice, $idUser) = explode('|', $displayId);

			if ($idUser) {
				return (string)\Entity\User::select($idUser)->name;
			} else {
				return (string)\Entity\Office::select($idOffice)->name;
			}
		}
		return '';
	}

}
