Blame view

framework/forms/NullableField.php 3.73 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  <?php
  /**
   * NullableField is a field that wraps other fields when you want to allow the user to specify whether the value of
   * the field is null or not.
   * 
   * The classic case is to wrap a TextField so that the user can distinguish between an empty string and a null string.
   * $a = new NullableField(new TextField("Field1", "Field 1", "abc"));
   * 
   * It displays the field that is wrapped followed by a checkbox that is used to specify if the value is null or not.
   * It uses the Title of the wrapped field for its title.
   * When a form is submitted the field tests the value of the "is null" checkbox and sets its value accordingly.
   * You can retrieve the value of the wrapped field from the NullableField as follows:
   * $field->Value() or $field->dataValue()
   * 
   * You can specify the label to use for the "is null" checkbox.  If you want to use I8N for this label then specify it
   * like this:
   * 
   * $field->setIsNullLabel(_T(SOME_MODULE_ISNULL_LABEL, "Is Null");
   * 
   * @author Pete Bacon Darwin
   * @package forms
   * @subpackage fields-basic
   */
  class NullableField extends FormField {
  	/**
  	 * The field that holds the value of this field
  	 * @var FormField
  	 */
  	protected $valueField;
  		
  	/**
  	 * The label to show next to the is null check box.
  	 * @var string
  	 */
  	protected $isNullLabel;
  	
  
  	/**
  	 * Create a new nullable field
  	 * @param $valueField
  	 * @return NullableField
  	 */
  	public function __construct(FormField $valueField, $isNullLabel = null) {
  		$this->valueField = $valueField;
  		$this->isNullLabel = $isNullLabel;
  		if ( is_null($this->isNullLabel) ) {
  			// Set a default label if one is not provided.
  			$this->isNullLabel = _t('NullableField.IsNullLabel', 'Is Null');
  		}
  		parent::__construct($valueField->getName(), $valueField->Title(), $valueField->Value(),
  			$valueField->getForm(), $valueField->RightTitle());
  		$this->readonly = $valueField->isReadonly();
  	}
  	
  	/**
  	 * Get the label used for the Is Null checkbox.
  	 * @return string
  	 */
  	public function getIsNullLabel() {
  		return $this->isNullLabel;
  	}
  	/**
  	 * Set the label used for the Is Null checkbox.
  	 * @param $isNulLabel string
  	 */
  	public function setIsNullLabel(string $isNulLabel){
  		$this->isNullLabel = $isNulLabel;
  		return $this;
  	}
  	
  	/**
  	 * Get the id used for the Is Null check box.
  	 * @return string
  	 */
  	public function getIsNullId() {
  		return $this->getName() . "_IsNull";
  	}
  
  	/**
  	 * (non-PHPdoc)
  	 * @see framework/forms/FormField#Field()
  	 */
  	public function Field($properties = array()) {
  		if ( $this->isReadonly()) {
  			$nullableCheckbox = new CheckboxField_Readonly($this->getIsNullId());
  		} else {
  			$nullableCheckbox = new CheckboxField($this->getIsNullId());
  		}
  		$nullableCheckbox->setValue(is_null($this->dataValue()));
  
  		return $this->valueField->Field() . ' ' . $nullableCheckbox->Field()
  			. '&nbsp;<span>' . $this->getIsNullLabel().'</span>';
  	}
  
  	/**
  	 * Value is sometimes an array, and sometimes a single value, so we need to handle both cases
  	 */
  	public function setValue($value, $data = null) {
  		if ( is_array($data) && array_key_exists($this->getIsNullId(), $data) && $data[$this->getIsNullId()] ) {
  			$value = null;
  		}
  		$this->valueField->setValue($value);
  		parent::setValue($value);
  
  		return $this;
  	}
  	
  	/**
  	 * (non-PHPdoc)
  	 * @see forms/FormField#setName($name)
  	 */
  	public function setName($name) {
  		// We need to pass through the name change to the underlying value field.
  		$this->valueField->setName($name);
  		parent::setName($name);
  
  		return $this;
  	}
  
  	/**
  	 * (non-PHPdoc)
  	 * @see framework/forms/FormField#debug()
  	 */
  	public function debug() {
  		$result = "$this->class ($this->name: $this->title : <font style='color:red;'>$this->message</font>) = ";
  		$result .= (is_null($this->value)) ? "<<null>>" : $this->value;
  		return result;
  	}
  }