Location: PHPKode > projects > Agile Toolkit > atk4-atk4-addons-efc6814/mvc/Form/Field/reference.php
 * Base class for reference-showing fields

class Form_Field_reference extends Form_Field_dropdown {
class Form_Field_reference2 extends Form_Field_ValueList {
	   Reference field enhances ValueList with support for references. In other words - list of values could rely on
	   other model to get itself from.

	   This class will also provide API interface to set up behavor for adding new values, however in order to
	   see them - you would have to use one of field types which supports it, such as "autocomplete" or "reference_pluss"


	function setController($c){
		return $this;
	function dictionary($d=null){
		return $this->dictionary;
	}private $dictionary=null;

	function setValueList($model_or_list, $field_definition=null){
		if($model_or_list instanceof FieldDefinition){
			// ommit first argument

			// find out if this field can be empty

			return parent::setValueList($model_or_list);
			// we have been passed a Model, we can do more with this
			if(!($model_or_list instanceof AbstractModel))
				throw new BaseException('Only Model or Array can be specified to this function');
		return $this;

	function allowAdd($b=null){
		return $this->allow_add;
	}private $allow_add=false;

	function emptyValue($val){
		return $this;

	// This field enhances value list by allowing to store "Model" object inside
	// $this->value_list. You can access the field with getValueList and setValueList functions
	// and also you can still use arays.

	// Should we allow user to add new field? This will be automatically initialised if you
	// initialise this field with controller.

	public $required=false;
	// If field can be empty then use this. You can set this argument to string, which will be
	// displayed instead of empty field. $this->allow_empty="Select category";

	public $empty_value='';
	// For dropdown - display empty value

	// ====== Redefine the following functions if necessary ========
	function getShowAddDialogJS($js=null){
		// This function have to use provided JS which will be executed when client desires
		// to add new element

		return $js->atk4_reference('showAddDialog',$this->getAddURL());
	function addEntry($value){
		// This function is called when new entry needs to be added. Value can be either
		// empty or it can contain array with key=>value.
		return $this->getDictionary()->addDefaultEntity($value);

	// ===== Call the following functions ========
	function includeDictionary(array $fields=array()){
		 * We are already including providing form with id=>value from the model. However sometimes
		 * it makes sense to provide additional values from the referred model.
		 * You can then use univ().bindFillInFields() to automatically fill-in the fields whenever current
		 * field is changed.
		foreach($fields as $key=>$row){
		foreach($o as $row){
		return $this;

	function validate(){
		if(!$list || !$this->value)return parent::validate();
				$this->ajax()->displayAlert($this->short_name.": Please select to continue")
				$this->owner->displayError($this,'Please select value from the list');
			$this->owner->errors[$this->short_name]="Please value from the list";
		return parent::validate();
	function getInput($attr=array()){

		foreach($list as $value=>$descr){
			// Check if a separator is not needed identified with _separator<
			$descr = trim($descr);
			if ($value === '_separator') {
			} else {
		return $output;
	function getOption($value){
		return $this->getTag('option',array(
					'selected'=>$value == $this->value

	function getValueList(){
			return parent::getValueList();

		   // REVERT back sorting (from #1345) becasue it breaks vat rates

		if(method_exists($this->dictionary(),'getSortColumn') && $sc=$this->dictionary()->getSortColumn()){


		// getRows do not support sorting, so we do ourselves.
		// This is to avoid changing getRows in the core, which would have much more severe
		// impact on the system!!
		// TODO: refactor during next major release

		// fucking good to sort in some certainly the-only-really-ever-needed order. yeah, in the core library.
		foreach($o as $row){
		return $res;
	private function getShortName(){
		//return $this->short_name;
		return join('_',$r);
	function set($value){
	function setDictionary($model){
		 * Please refer to a model descendand of Model_dictionary
		throw new BaseException('please use standard method setValueList and pass array, controller or fieldDefinition');
		return $this;
	function render(){

function usort_cmp($a,$b){
	return strcasecmp($a['name'],$b['name']);
Return current item: Agile Toolkit