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: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
<?php
class FormFieldMultipleSelectChoice extends AbstractFormField
{
private $selected_options;
private $options = array();
private $size = 4;
public function __construct($id, $label, array $selected_options, array $available_options, array $field_options = array(), array $constraints = array())
{
parent::__construct($id, $label, $selected_options, $field_options, $constraints);
$this->set_css_form_field_class('form-field-multi-select');
foreach ($available_options as $option)
{
$this->add_option($option);
}
$this->set_selected_options($selected_options);
}
public function set_selected_options(array $selected_options)
{
$value = array();
foreach ($selected_options as $option)
{
if (is_string($option))
{
$value[] = $this->get_option($option);
}
else if ($option instanceof FormFieldSelectChoiceOption)
{
$value[] = $option;
}
else
{
throw new FormBuilderException('option ' . $option . ' isn\'t recognized');
}
}
$this->selected_options = $value;
}
public function retrieve_value()
{
$request = AppContext::get_request();
if ($request->has_parameter($this->get_html_id()))
{
$selected_options = $request->get_array($this->get_html_id());
$value = array();
foreach ($this->get_options() as $option)
{
if (in_array($option->get_raw_value(), $selected_options))
{
$value[] = $option;
}
}
$this->set_value($value);
}
else
{
$this->set_value(array());
}
}
public function display()
{
$template = $this->get_template_to_use();
$this->assign_common_template_variables($template);
$template->assign_block_vars('fieldelements', array(
'ELEMENT' => $this->get_html_code()->render(),
));
return $template;
}
private function get_html_code()
{
$tpl = new FileTemplate('framework/builder/form/FormFieldMultipleSelectChoice.tpl');
$lang = LangLoader::get('main');
$tpl->put_all(array(
'NAME' => $this->get_html_id(),
'ID' => $this->get_id(),
'HTML_ID' => $this->get_html_id(),
'FORM_ID' => $this->get_form_id(),
'SIZE' => $this->size,
'CSS_CLASS' => $this->get_css_class(),
'C_DISABLED' => $this->is_disabled(),
'C_REQUIRED' => $this->is_required(),
'L_SELECT_ALL' => $lang['select_all'],
'L_UNSELECT_ALL' => $lang['select_none'],
'L_SELECT_EXPLAIN' => $lang['explain_select_multiple']
));
foreach ($this->get_options() as $multiple_select_option)
{
if ($multiple_select_option instanceof FormFieldSelectChoiceGroupOption)
{
foreach ($multiple_select_option->get_options() as $option)
{
$select = $this->is_selected($option);
if ($select)
{
$option->set_active();
}
}
}
else
{
$select = $this->is_selected($multiple_select_option);
if ($select)
{
$multiple_select_option->set_active();
}
}
$tpl->assign_block_vars('options', array(
'OPTION' => $multiple_select_option->display()
));
}
return $tpl;
}
protected function compute_options(array &$field_options)
{
foreach($field_options as $attribute => $value)
{
$attribute = TextHelper::strtolower($attribute);
switch ($attribute)
{
case 'size':
$this->size = $value;
unset($field_options['size']);
break;
}
}
parent::compute_options($field_options);
}
private function is_selected($option)
{
return in_array($option, $this->selected_options);
}
protected function get_default_template()
{
return new FileTemplate('framework/builder/form/FormField.tpl');
}
private function add_option(FormFieldEnumOption $option)
{
$option->set_field($this);
$this->options[] = $option;
}
private function get_options()
{
return $this->options;
}
private function get_option($identifier)
{
foreach ($this->options as $option)
{
if ($option->get_raw_value() == $identifier || $option->get_label() == $identifier)
{
return $option;
}
}
return null;
}
protected function get_js_specialization_code()
{
return 'field.getValue = function()
{
return jQuery("#'.$this->get_html_id().' :selected").length;
}';
}
}
?>