00001 <?php
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 class InputWidgetMultiselectBase extends InputWidgetBase {
00011         protected $use_checkboxes = false;
00012         
00013 
00014 
00015 
00016         protected function render_input($attrs, $params, $name, $title, $value, $policy) {
00017                 $ret = '';
00018                 $options = Arr::get_item($attrs, 'options', array());
00019                 $c = count($options);
00020                 unset($attrs['options']);
00021 
00022                 $ret .= html::input('hidden', $name, '');
00023                 
00024                 $name .= '[]';
00025                 $value = Arr::force($value, false);
00026                 
00027                 $this->use_checkboxes = ( 
00028                         ($c <= APP_MULTISELECT_THRESHOLD && !Common::flag_is_set($policy, WidgetInput::FORCE_SELECT_BOX)) 
00029                         || 
00030                         (Common::flag_is_set($policy, WidgetInput::FORCE_CHECKBOXES))
00031                 );
00032 
00033                 if ($this->use_checkboxes) {
00034                         $ret .= $this->build_multiselect_checkboxes($name, $options, $value, $attrs, Arr::get_item($params, 'label:class', ''));
00035                 }
00036                 else {
00037                         $ret .= $this->build_multiselect_select($name, $options, $value, $attrs);
00038                 }
00039                 return $ret;
00040         }       
00041 
00042 
00043 
00044 
00045         protected function render_label($widget, $html_attrs, $params, $name, $title, $value, $policy) {
00046                 if ($this->use_checkboxes) {
00047                         $policy |= WidgetInput::NO_LABEL;
00048                 }
00049                 return parent::render_label($widget, $html_attrs, $params, $name, $title, $value, $policy);
00050         }       
00051         
00052 
00053 
00054 
00055         protected function render_postprocess($output, $policy) {
00056                 if ($this->use_checkboxes) {
00057                         $output .= '<div class="multiselect-end"></div>';
00058                 }
00059                 return parent::render_postprocess($output, $policy); 
00060         }
00061 
00062 
00063 
00064 
00065         protected function build_multiselect_select($name, $options, $value, $attrs) {
00066                 $c = count($options);
00067                 $attrs['size'] = Arr::get_item($attrs, 'size', $c > APP_MULTISELECT_THRESHOLD ? APP_MULTISELECT_THRESHOLD : $c);
00068                 $attrs['multiple'] = 'multiple';
00069                 return html::select($name, $options, $value, $attrs);
00070         }
00071 
00072 
00073 
00074 
00075         protected function build_multiselect_checkboxes($name, $options, $value, $attrs, $lbl_class) {
00076                 $ret = '';
00077                 foreach($options as $key => $display) {
00078                         if (is_array($display)) {
00079                                 $ret .= $this->build_mutiselect_checkbox_group($name, $key, $display, $value, $attrs, $lbl_class);
00080                         }
00081                         else {
00082                                 $ret .= $this->build_multiselect_checkbox($name, $key, $display, $value, $attrs, $lbl_class);
00083                         }
00084                 }
00085                 return $ret;
00086         }
00087         
00088 
00089 
00090 
00091         protected function build_mutiselect_checkbox_group($name, $title, $options, $value, $attrs, $lbl_class) {
00092                 $ret = '';
00093                 $ret .= '<div class="multiselect-group">';
00094                 $ret .= html::div($title,  'label multiselect-group-title ' . $lbl_class);
00095                 $ret .= $this->build_multiselect_checkboxes($name, $options, $value, $attrs, $lbl_class);
00096                 $ret .= '</div>';
00097                 return $ret;
00098         }
00099         
00100 
00101 
00102 
00103         protected function build_multiselect_checkbox($name, $key, $display, $values, $attrs, $lbl_class) {
00104                 $ret = '';
00105                 $attrs['value'] = $key;
00106                 if (in_array($key, $values)) {
00107                         $attrs['checked'] = 'checked';
00108                 }
00109                 unset($attrs['id']);
00110                 $checkbox_html = html::input('checkbox', $name, $attrs);
00111                 $checkbox_html .= ' ' . $display;
00112                 $ret .= html::label($checkbox_html, '', 'spanning ' . $lbl_class);
00113                 return $ret;
00114         }
00115 }