Formular-Klasse

Diese Klasse baut ein Formular zusammen. Diese Klasse wird auf freakstuff.de verwendet. Zusammen mit der Checkbox-Klasse lässt sich jede Art von Formularen relativ dynamisch zusammenbauen. Die Farben, der Hintergrund und Schriftarten können Cascading Stylesheets formatiert werden.

Autor: Oliver Gueffroy
   
  1 class formular
  2 {
  3   var $action;
  4   var $name;
  5   var $method;
  6   var $params;
  7   var $content;
  8   var $buttons;
  9 
 10   function formular($action, $name, $method = 'post', $params = '')
 11   {
 12     $this->action  = $action;
 13     $this->name    = $name;
 14     $this->method  = $method;
 15     $this->params  = $params;
 16     $this->content = array();
 17     $this->buttons = array(
 18       'left' => array(),
 19       'right' => array()
 20     );
 21   }
 22 
 23   function add_field($title, $field_data, $description = '')
 24   {
 25     if(is_array($this->content))
 26     {
 27       $this->content[] = array(
 28         'title' => $title,
 29         'field_data' => $field_data,
 30         'description' => $description
 31       );
 32     }
 33   }
 34 
 35   function set_content($content)
 36   {
 37     $this->content = $content;
 38   }
 39 
 40   function add_button($align, $button_data)
 41   {
 42     if($align != 'left' && $align != 'right')
 43       $align = 'left';
 44 
 45     $this->buttons[$align][] = $button_data;
 46   }
 47 
 48   function get_output()
 49   {
 50     $output  = '<!-- sub: ' . $this->name . ' -->' . "\n" .
 51                '<form action="' . $this->action . '" name="' . $this->name . '_form" method="' . $this->method . '"';
 52 
 53     if(strlen(trim($this->params)) > 0)
 54       $output .= ' ' . $this->params;
 55 
 56     $output .= '><input type="hidden" name="' . $this->name . '_submit" value="true" />' . "\n";
 57 
 58     if(is_array($this->content))
 59     {
 60       if(sizeof($this->content) > 0)
 61       {
 62         $output .= ' <div class="form_area">' . "\n" .
 63                    '  <table class="tbl_form" border="0" cellpadding="0" cellspacing="0">' . "\n";
 64 
 65         for($i = 0, $n = sizeof($this->content); $i < $n; $i++)
 66         {
 67           $html_class = $i % 2 == 0 ? 'even' : 'odd';
 68 
 69           $output .= '   <tr class="' . $html_class . '">' . "\n" .
 70                      '    <th>' . $this->content[$i]['title'] . ':</th>' . "\n" .
 71                      '    <td>' . $this->content[$i]['field_data'] . '</td>' . "\n" .
 72                      '   </tr>' . "\n";
 73 
 74           if(strlen(trim($this->content[$i]['description'])) > 0)
 75           {
 76             $output .= '   <tr class="' . $html_class . '">' . "\n" .
 77                        '    <td class="description"><br /></td>' . "\n" .
 78                        '    <td class="description">' . $this->content[$i]['description'] . '<br /></td>' . "\n" .
 79                        '   </tr>' . "\n";
 80           }
 81         }
 82 
 83         $output .= '  </table>' . "\n" .
 84                    ' </div>' . "\n";
 85 
 86       }
 87     }
 88     else
 89       $output .= $this->content . "\n";
 90 
 91     /* I feel bad using a table to position this buttons, but the internet
 92      * explorer gave me strange results when using float: left and right. I
 93      * tried different things, but nothing worked as expected...
 94      */
 95     $landr = sizeof($this->buttons['left']) > 0 && sizeof($this->buttons['right']) > 0;
 96     $tag   = $landr ? 'td' : 'div';
 97     $space = $landr ? '   ' : ' ';
 98 
 99     if($landr)
100       $output .= ' <!-- Sucking table, does anyone know how to do it better? -->' . "\n" .
101                  ' <table style="width: 100%" border="0" cellpadding="0" cellspacing="0">' . "\n" .
102                  '  <tr>' . "\n";
103 
104     for($i = 0, $n = sizeof($this->buttons['left']); $i < $n; $i++)
105     {
106       $output .= $space . '<' . $tag . ' class="align_left">' .
107                  $this->buttons['left'][$i] . '</' . $tag . '>' . "\n";
108     }
109 
110     for($i = 0, $n = sizeof($this->buttons['right']); $i < $n; $i++)
111     {
112       $output .= $space . '<' . $tag . ' class="align_right">' .
113                  $this->buttons['right'][$i] . '</' . $tag . '>' . "\n";
114     }
115 
116     if($landr)
117       $output .= '  </tr>' . "\n" .
118                  ' </table>' . "\n";
119 
120     $output .= '</form>' . "\n" .
121                '<!-- /sub: ' . $this->name . '_eof -->';
122 
123     return $output;
124   }
125 }
   
Die Klasse baut für die Plazierung der Formularelemente eine Tabelle mit zwei Spalten. Wenn das Formular mehrere nebeneinander stehende Felder haben soll, ist dies die falsche Klasse.
  
 1 $form = new formular();
 2 
 3 $form->add_field(
 4   'Titel', html_input_text('titel'),
 5   'Hier den Titel eingeben'
 6 );
 7 
 8 // Weitere Formularfelder...
 9 
10 echo $form->get_output();
  

Kommentare (0 Stück)

Du musst registriert und eingeloggt sein, um Kommentare erstellen zu dürfen!