| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Import dependencies. |
| 7 |
cssJSToolbox::import('framework:html:field.inc.php'); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTListField extends CJTHTMLField { |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
protected static $instances = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @var mixed |
| 25 |
*/ |
| 26 |
protected $items; |
| 27 |
|
| 28 |
/** |
| 29 |
* put your comment there... |
| 30 |
* |
| 31 |
* @var mixed |
| 32 |
*/ |
| 33 |
protected $mandatory; |
| 34 |
|
| 35 |
/** |
| 36 |
* put your comment there... |
| 37 |
* |
| 38 |
* @var mixed |
| 39 |
*/ |
| 40 |
protected $moreIntoTag; |
| 41 |
|
| 42 |
/** |
| 43 |
* put your comment there... |
| 44 |
* |
| 45 |
* @var mixed |
| 46 |
*/ |
| 47 |
protected $optional; |
| 48 |
|
| 49 |
/** |
| 50 |
* put your comment there... |
| 51 |
* |
| 52 |
* @var mixed |
| 53 |
*/ |
| 54 |
protected $options = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* put your comment there... |
| 58 |
* |
| 59 |
* @var mixed |
| 60 |
*/ |
| 61 |
protected $propText = 'text'; |
| 62 |
|
| 63 |
/** |
| 64 |
* put your comment there... |
| 65 |
* |
| 66 |
* @var mixed |
| 67 |
*/ |
| 68 |
protected $propValue = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* put your comment there... |
| 72 |
* |
| 73 |
*/ |
| 74 |
public function __construct($form, $name, $value, $id = null, $classesList = '', $propText = null, $propValue = null, $moreIntoTag = null, $optional = null) { |
| 75 |
parent::__construct($form, $name, $value, $id, $classesList); |
| 76 |
// Initialize local vars. |
| 77 |
$this->propText = $propText ? $propText : 'text'; |
| 78 |
$this->propValue = $propValue; |
| 79 |
$this->moreIntoTag = $moreIntoTag; |
| 80 |
$this->optional = $optional; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* put your comment there... |
| 85 |
* |
| 86 |
*/ |
| 87 |
public function getInput($options = null) { |
| 88 |
$this->options = ($options !== null) ? $options : array(); |
| 89 |
// Prepare items. |
| 90 |
$this->prepareItems(); |
| 91 |
if ($this->optional) { |
| 92 |
$this->items = array('' => (object) array( |
| 93 |
$this->propText => htmlentities($this->optional), |
| 94 |
'__params__' => (object) array('className' => 'optional')))+ $this->items; |
| 95 |
} |
| 96 |
// Build HTML select. |
| 97 |
$listName = (isset($this->options['standard']) && ($this->options['standard'] == true)) ? "name='{$this->name}'" : ''; |
| 98 |
$list = "<select id='{$this->id}' {$listName} class='{$this->classesList}' {$this->moreIntoTag}>"; |
| 99 |
foreach ($this->items as $key => $item) { |
| 100 |
// Standrize the use of object. |
| 101 |
$item = (object) $item; |
| 102 |
// Fetch display text. |
| 103 |
$text = cssJSToolbox::__($item->{$this->propText}); |
| 104 |
// No value prop defined then use item KEY. |
| 105 |
$value = ($this->propValue == null) ? $key : $item->{$this->propValue}; |
| 106 |
$selected = ($value == $this->value) ? ' selected="selected"' : ''; |
| 107 |
if (isset($item->__params__->className)) { |
| 108 |
$class = "class='{$item->__params__->className}'"; |
| 109 |
} |
| 110 |
else { |
| 111 |
$class = ''; |
| 112 |
} |
| 113 |
$list .= "<option {$class} value='{$value}'{$selected}>{$text}</option>" ; |
| 114 |
} |
| 115 |
$list .= '</select>'; |
| 116 |
// If this is the first instance to be outputed for the current form output the control field. |
| 117 |
$fieldKey = "{$this->form}-{$this->name}"; |
| 118 |
if ((!isset($this->options['standard'])) && (!in_array($fieldKey, self::$instances))) { |
| 119 |
// Output control fields. |
| 120 |
$list .= "<input type='hidden' name='{$this->name}' value='{$this->value}' />"; |
| 121 |
// Mark form as instantiated! |
| 122 |
self::$instances[] = $fieldKey; |
| 123 |
} |
| 124 |
return $list; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* put your comment there... |
| 129 |
* |
| 130 |
* @param mixed $type |
| 131 |
* @param mixed $form |
| 132 |
* @param mixed $name |
| 133 |
* @param mixed $value |
| 134 |
* @param mixed $id |
| 135 |
* @param mixed $classesList |
| 136 |
* @param mixed $propText |
| 137 |
* @param mixed $propValue |
| 138 |
* @param mixed $moreIntoTag |
| 139 |
*/ |
| 140 |
public static function getInstance($type, $form, $name, $value, $id = null, $classesList = '', $propText = 'text', $propValue = null, $moreIntoTag = null, $optional = null, $prefix = 'CJT') |
| 141 |
{ |
| 142 |
/* * @ todo Code to importing file and instantiating class should be in CJTHTMLField class not here!! */ |
| 143 |
// Create an instance. |
| 144 |
$type = str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $type))); |
| 145 |
$className = "{$prefix}{$type}Field"; |
| 146 |
return new $className($form, $name, $value, $id, $classesList, $propText, $propValue, $moreIntoTag, $optional); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* put your comment there... |
| 151 |
* |
| 152 |
*/ |
| 153 |
public function & getItems() { |
| 154 |
return $this->items; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* put your comment there... |
| 159 |
* |
| 160 |
*/ |
| 161 |
protected function prepareItems() { |
| 162 |
$this->items = array(); |
| 163 |
} |
| 164 |
|
| 165 |
} // End class. |
| 166 |
|