Exemplo n.º 1
0
 /**
  * create the dropdowns based on the previously set query or result array
  * @throws \Exception
  */
 protected function createDropdowns()
 {
     if (empty($this->data)) {
         throw new \Exception('Error creating dropdowns, because there are no results to create dropdowns from.');
     }
     // the separator we use to glue the values of the different columns together to ensure uniqueness
     $separator = '_';
     // collect all values with their parent value into $options
     $options = array();
     foreach ($this->data as $row) {
         $class = null;
         foreach ($row as $colname => $value) {
             // we store $class . $separator . $value instead of just $val, because it is possible that different level1's have same level2's
             // and by collecting this way, they will not get overwritten
             // Example: [ADSL2+][1. Geen mening]  and  [Fiber][1. Geen mening]
             $title = $class . $separator . $value;
             $options[$colname][$title] = array('value' => $value, 'label' => $value, 'class' => $class);
             $class = $title;
             // for the next column
         }
     }
     $selected = $this->getSelected();
     foreach ($options as $ddName => $ddOptionsAttributes) {
         // create the dropdown
         $dropdown = new Dropdown($ddName);
         $dropdown->setLabel(initcap($ddName));
         $dropdown->prependOption('', '-- all --');
         $dropdown->appendOptionClass('');
         // also add dummy values here
         $dropdown->appendOptionTitle('');
         // create the options with class and title
         foreach ($ddOptionsAttributes as $title => $attributes) {
             // add as option if label is not empty
             if (!empty($attributes['label'])) {
                 $dropdown->appendOption($attributes['value'], $attributes['label']);
                 // replace spaces and other invalid class characters with underscores
                 // based on the class we search for parent options with title equals that class, so title needs the same replacements
                 $dropdown->appendOptionClass($this->toValidHtmlId($attributes['class'], '_'));
                 $dropdown->appendOptionTitle($this->toValidHtmlId($title, '_'));
             }
         }
         // set the selected value
         if (is_array($selected) && sizeof($selected)) {
             $dropdown->setSelected(array_shift($selected));
         }
         // add to array of dropdowns
         $this->dropdowns[] = $dropdown;
     }
     // refresh the posted values
     if ($this->isPosted()) {
         $this->setPosted();
     }
 }