Example #1
0
 /**
  * Generates a drop down box from almost anything
  * @param string $name nameof the select
  * @param array $displayList
  * @param array $attribs html attributes to generate
  * @param string $defaultValue matches the key in the displayList
  * @param boolean $blank true if you want to generate a blank row
  * @returns string The resulting HTML
  */
 static function selectWithCategories($name, $resultset, $attribs = array())
 {
     if (isset($attribs['default'])) {
         $defaultValue = $attribs['default'];
         unset($attribs['default']);
     } else {
         $defaultValue = Request::get($name, null);
     }
     $hasBlank = false;
     if (isset($attribs['hasBlank'])) {
         $hasBlank = $attribs['hasBlank'];
         unset($attribs['hasBlank']);
     }
     $optGroupAttrib = array();
     if (isset($attribs['optGroupAttrib'])) {
         $optGroupAttrib = $attribs['optGroupAttrib'];
         unset($attribs['optGroupAttrib']);
     }
     if (is_array($resultset)) {
         if (count($resultset) == 0) {
             $tag = "* None Available *";
             if ($defaultValue != null) {
                 $tag .= Tag::hidden($name, $defaultValue);
             }
         } else {
             $tag = Tag::select($name, $attribs);
             if ($hasBlank) {
                 $tag .= Tag::optiontag(' ', '', !isset($defaultValue) || $defaultValue == false);
             }
             foreach ($resultset as $category => $list) {
                 $tag .= Tag::hTag('optgroup', array_merge($optGroupAttrib, array('label' => $category)));
                 foreach ($list as $key => $val) {
                     $tag .= Tag::optiontag(trim($key), $val, $defaultValue == $key);
                 }
                 $tag .= Tag::_hTag('optgroup');
             }
             $tag .= Tag::_select();
         }
     } else {
         if (is_object($resultset)) {
             /** FIXME **/
             $table = new DBTable($resultset, DB::FETCH_NUM);
             if ($table->rowCount() == 0) {
                 $tag = "* None Available *";
                 if ($defaultValue != null) {
                     $tag .= Tag::hidden($name, $defaultValue);
                 }
             } else {
                 $tag = Tag::select($name, $attribs);
                 if ($hasBlank) {
                     $tag .= Tag::optiontag(' ', '', !isset($defaultValue) || $defaultValue == false);
                 }
                 $prevCategory = '';
                 foreach ($table as $row) {
                     if ($prevCategory != $row[0]) {
                         if ($prevCategory != '') {
                             $tag .= Tag::_hTag('optgroup');
                         }
                         $tag .= Tag::hTag('optgroup', array('label' => $row[0]));
                         $prevCategory = $row[0];
                     }
                     $tag .= Tag::optiontag($row[1], $row[2], $defaultValue == $row[1]);
                 }
                 $tag .= Tag::_hTag('optgroup');
                 $tag .= Tag::_select();
             }
         }
     }
     return $tag;
 }