Umfragen
text.class.php
Go to the documentation of this file.
1 <?php
7 class regexp{
8  public $desc_tiny = "";
9  public $desc_short = "";
10  public $desc_long = "";
11  public $err_msg = "";
12  public $exp = "";
13  public $id = "";
14 
20  public function make_err($name){
21  return str_replace("{name}",$name,$this->err_msg);
22  }
23 
29  public function test($str){
30  if( ($this->exp == "") OR ($str == "") ){
31  return true;
32  }
33  $r = preg_match_all($this->exp,$str);
34  if( ($r === 0) OR ($r === false) ){
35  return false;
36  }else if($r === 1){
37  return true;
38  }else{
39  return false;
40  }
41  }
42 
43  public function __toString(){
44  return $this->exp;
45  }
46 }
47 
48 class text extends widget implements iWidget,input_widget,output_widget{
49 
50  public $typ = "text";
51  public $direct_result = true;
52  public $regexp = array();
53 
54  function __construct(){
55  $exp = new regexp();
56  $exp->desc_tiny = "0-9";
57  $exp->desc_short = "Nur zahlen";
58  $exp->desc_long = "Nur Ganzzahlen (0-9) erlaubt.";
59  $exp->err_msg = "Die Frage <b>{name}</b> muss mit einer Ganzzahl beantwortet werden.";
60  $exp->name = "int";
61  $exp->exp = "/^\d*$/";
62  $this->regexp[ $exp->exp ] = $exp;
63 
64  $exp2 = new regexp();
65  $exp2->desc_tiny = "-12,3";
66  $exp2->desc_short = "Zahlen mit - und ,";
67  $exp2->desc_long = "Nur Zahlen erlaubt, optional: komma und -";
68  $exp2->err_msg = "Die Frage <b>{name}</b> muss mit einer einer Zahl beantwortet werden. (erlaubte Zeichen sind 0-9 - ,) z.B: -123,45";
69  $exp2->name = "float";
70  $exp2->exp = "/^-{0,1}\d*(:?,{0,1}\d)$/";
71  $this->regexp[ $exp2->exp ] = $exp2;
72 
73  $exp3 = new regexp();
74  $exp3->desc_tiny = "1-F";
75  $exp3->desc_short = "Hexadezimalzahlen (0-F)";
76  $exp3->desc_long = "Nur Hexadecimalzahlen erlaubt:0-F";
77  $exp3->err_msg = "Die Frage <b>{name}</b> muss mit einer einer Hexadezimalzahl beantwortet werden. (erlaubte Zeichen sind 0-f, 0-F) z.B: 4B0000";
78  $exp3->name = "hex";
79  $exp3->exp = "/^[[:xdigit:]]*$/";
80  $this->regexp[ $exp3->exp ] = $exp3;
81  }
82 
83  public function set_default_values($db){
84  $this->set_dispName("Bitte Fragestellung eingeben");
85  }
86 
87  public function set_config($name,$value){
88  $this->init_config();
89  if( $name == "regexp" ){
90  if( $value != "" ){
91  $this->config["regexp"] = $value;
92  }else{
93  if( isset($this->config["regexp"]) ){
94  unset($this->config["regexp"]);
95  }
96  }
97  }else{
99  }
100 
101  }
102 
103  public function get_config($name){
104  $this->init_config();
105  if( $name == "regexp" ){
106  if( (isset($this->config["regexp"])) AND ($this->config["regexp"] != "") ){
107  if( isset($this->regexp[ $this->config["regexp"] ]) ){
108  return $this->regexp[ $this->config["regexp"] ];
109  }else{
110  return;
111  }
112  }else{
113  return;
114  }
115  }else{
116  return parent::get_config($name);
117  }
118  }
119 
120  public function display_regexp_edit(){
121  $conf = $this->get_config("regexp");
122  echo "<select name='widget_regexp'>";
123  echo "<option value=''>Keine festlegung</option>";
124  foreach( $this->regexp as $reg){
125  if( (isset($conf)) AND ($conf->exp == $reg->exp) ){
126  echo "<option value='{$reg->name}' selected='selected'> {$reg->desc_short} </option>";
127  }else{
128  echo "<option value='{$reg->name}'> {$reg->desc_short} </option>";
129  }
130  }
131  echo "</select>";
132  }
133 
134  public function handle_regexp_edit(){
135  if( (isset($_POST["widget_regexp"])) AND ($_POST["widget_regexp"] != "") ){
136  foreach( $this->regexp as $exp ){
137  if( $exp->name === $_POST["widget_regexp"] ){
138  $this->set_config("regexp",$exp->exp);
139  } // end if found
140  } // end foreach expression
141  }else{
142  $this->set_config("regexp","");
143  } // end if POST
144  return true;
145  }
146 
147 
148  function display_edit(){
149  if($this->is_required){
150  $req = "<span class='required_asterisk'>*</span>";
151  }else{$req = "";}
152 
153  $font_bold= $this->get_config("font-bold");
154  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
155 
156  $reg = $this->get_config("regexp");
157  $regexp ="";
158  if( isset($reg) ){
159  $regexp = "<span style='color:#4B0000;font-size:80%;'>(".$reg->desc_long.")</span>";
160  }
161 
162  $this->display_edit_begin();
163  echo "<label for='{$this->name}' style='$font_bold' >{$this->dispName} $req</label> &nbsp; <br/>";
164  echo "<span class='widget_indent'></span><input type='text' name='{$this->name}' id='{$this->name}' style='margin-top:4px;width:15em'/> $regexp <br/>";
165  echo "</div>".PHP_EOL;
166  }
167 
168 
169  function display(){
170  if( (isset($this->name)) ){
171  if( isset($this->dispName) ){
172  $n = $this->dispName;
173  }else{ $n = $this->name; }
174  $value = htmlspecialchars($this->value,ENT_QUOTES,"UTF-8",true);
175  $font_family= $this->get_config("font-family");
176  $font_size= $this->get_config("font-size");
177  $font_bold= $this->get_config("font-bold");
178 
179  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
180  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
181  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
182 
183  $reg = $this->get_config("regexp");
184  $regexp ="";
185  if( isset($reg) ){
186  $regexp = "<span style='color:#4B0000;font-size:80%;'>(".$reg->desc_long.")</span>";
187  }
188 
189  if($this->is_required){
190  $req = "<span class='required_asterisk'>*</span>";
191  }else{$req = "";}
192  echo "<div class='widget_container text_widget' style='$font_family;$font_size'>";
193  echo "<label for='{$this->name}' style='$font_bold' class='widget_question'>$n $req</label> &nbsp; <br/>";
194  echo "<span class='widget_indent'></span><input type='text' name='{$this->name}' id='{$this->name}' value='{$value}' style='margin-top:4px;width:15em' /> $regexp <br/>";
195  echo "</div>".PHP_EOL;
196  }else{
197  throw new Exception("Widget name or default value not set");
198  }
199  }
200 
201  function display_child(){
202  if( isset($this->dispName) ){
203  $n = $this->dispName;
204  }else{ $n = $this->name; }
205 
206  $value = htmlspecialchars($this->value,ENT_QUOTES,"UTF-8",true);
207  $font_family= $this->get_config("font-family");
208  $font_size= $this->get_config("font-size");
209  $font_bold= $this->get_config("font-bold");
210  $width= $this->get_config("width");
211  $margin_left= $this->get_config("margin-left");
212  $margin_right= $this->get_config("margin-right");
213 
214  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
215  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
216  if( $font_bold == true){ $font_bold = "font-weight:bold"; }else{ $font_bold=""; }
217  if( $width != -1){ $width = "width:{$width}em"; }else{ $width=""; }
218  if( $margin_left != 0){ $margin_left = "margin-left:{$margin_left}em"; }else{ $margin_left=""; }
219  if( $margin_right != 0){ $margin_right = "margin-right:{$margin_right}em"; }else{ $margin_right=""; }
220 
221  if($this->is_required){
222  $req = "<span class='required_asterisk'>*</span>";
223  }else{$req = "";}
224  echo "<div class='widget_container text_child_widget' style='$font_family;$font_size'>";
225  //echo "<label for='{$this->name}' style='$font_bold'>$n $req</label> &nbsp; <br/>";
226  echo "<input type='text' name='{$this->name}' id='{$this->name}' value='{$value}' style='margin-top:4px;$width;$margin_left;$margin_right't placeholder='{$this->dispName}' title='{$this->dispName}'/><br/>";
227  echo "</div>".PHP_EOL;
228 
229  }
230 
231 
232  function handle_inpt(){
233  $req = $this->check_required();
234  $reg = $this->get_config("regexp");
235  if($req !== true) {
236  return $req;
237  }else{
238  if( (!isset($this->name)) OR ($this->name == "") ){
239  throw new varException("Widget Name not set!");
240  }
241  if( strlen($_POST[ $this->name ]) < 256){
242  if( isset($reg) ){
243  $err = $reg->test($_POST[ $this->name ]);
244  if( $err === false ){
245  return $reg->make_err($this->dispName);
246  }
247  }
248  // regexp test passed!
249  $this->value= $_POST[ $this->name ];
250  return true;
251  }else{
252  return "Das Textfeld <b>{$this->dispName}</b> enthält mehr als 256 Zeichen! Bitte kürtze deine Antwort etwas.";
253  }
254  }
255  }
256 
257 
258 }
259 // ! no ending newLine
260 ?>