Blame view

framework/forms/LookupField.php 2.19 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  <?php
  
  /**
   * Read-only complement of {@link DropdownField}.
   *
   * Shows the "human value" of the dropdown field for the currently selected 
   * value.
   *
   * @package forms
   * @subpackage fields-basic
   */
  class LookupField extends DropdownField {
  
  	/**
  	 * @var boolean $readonly
  	 */
  	protected $readonly = true;
  	
  	/**
  	 * Returns a readonly span containing the correct value.
  	 *
  	 * @param array $properties
  	 *
  	 * @return string
  	 */
  	public function Field($properties = array()) {
  		$source = $this->getSource();
  		
  		// Normalize value to array to simplify further processing
  		if(is_array($this->value) || is_object($this->value)) {
  			$values = $this->value;
  		} else {
  			$values = array(trim($this->value));
  		}
  
  		$mapped = array();
  
  		if($source instanceof SQLMap) {
  			foreach($values as $value) {
  				$mapped[] = $source->getItem($value);
  			}
  		} else if($source instanceof ArrayAccess || is_array($source)) {
  			$source = ArrayLib::flatten($source);
  			
  			foreach($values as $value) {
  				if(isset($source[$value])) {
  					$mapped[] = $source[$value];
  				}
  			}
  		} else {
  			$mapped = array();
  		}
  
  		// Don't check if string arguments are matching against the source,
  		// as they might be generated HTML diff views instead of the actual values
  		if($this->value && !is_array($this->value) && !$mapped) {
  			$mapped = array(trim($this->value));
  			$values = array();
  		}
  		
  		if($mapped) {
  			$attrValue = implode(', ', array_values($mapped));
  			
  			if(!$this->dontEscape) {
  				$attrValue = Convert::raw2xml($attrValue);
  			}
  
  			$inputValue = implode(', ', array_values($values)); 
  		} else {
  			$attrValue = '<i>('._t('FormField.NONE', 'none').')</i>';
  			$inputValue = '';
  		}
  
  		return "<span class=\"readonly\" id=\"" . $this->id() .
  			"\">$attrValue</span><input type=\"hidden\" name=\"" . $this->name .
  			"\" value=\"" . $inputValue . "\" />";
  	}
  	
  	/**
  	 * @return LookupField
  	 */
  	public function performReadonlyTransformation() {
  		$clone = clone $this;
  
  		return $clone;
  	}
  
  	/**
  	 * @return string
  	 */
  	public function Type() {
  		return "lookup readonly";
  	}
  	
  	/**
  	 * Override parent behavior by not merging arrays.
  	 *
  	 * @return array
  	 */
  	public function getSource() {
  		return $this->source;
  	}
  }