Umfragen
widgetBaseClass.class.php
Go to the documentation of this file.
1 <?php
13 class widget{
14  // db vars
15  public $pollID;
16  public $name;
17  public $value;
18  public $config;
19  public $is_required = false;
20  public $dispName;
21  public $index;
22  public $ID;
23  public $is_child = false;
24  public $parent = -1;
25  private $data = array();
26 
27  public $long_result = false;
28  public $direct_result = true;
29 
33  public function serialize_config(){
34  if( is_array($this->config) ){
35  $this->config = serialize( $this->config );
36  }
37  }
38 
42  public function unserialize_config(){
43  if( is_string($this->config)){
44  if( $this->config != ""){
45  $this->config = unserialize($this->config);
46  }
47  if( !is_array($this->config) ){
48  $this->config = array();
49  }
50  }
51  }
52 
53 
59  public function init_config(){
60  if( (!isset($this->config)) OR ($this->config == "") ){
61  $this->config = array();
62  }
63  if( (isset($this->config)) AND (is_string($this->config)) AND ($this->config != "") ){
64  $this->unserialize_config();
65  }
66  if( !is_array($this->config) ){
67  throw new exception("Fehler beim Laden der Einstellungen. Datenbank möglicherweise fehlerhaft.");
68  }
69  return true;
70  }
71 
78  public function set_config($name,$val){
79  $err_reason = "";
80 
81  $this->init_config();
82  if(is_array($this->config) ){
83  // linebreak on child widgets of containers
84  if($name == "br"){
85  if ( is_bool($val) ){
86  $this->config["br"] = $val;
87  }
88  // for standalone checkbox value to be set in db as bool
89  }else if ( ($name == "true") OR ($name == "false") ){
90  $value_clean = htmlspecialchars($val , ENT_QUOTES , "UTF-8",true);
91  if( (strlen($value_clean) > 256) ){
92  $err_reason = "wert enthält mehr als 256 Zeichen";
93  }else{
94  if($name == "true"){
95  $this->config["true"] = $value_clean;
96  }else{
97  $this->config["false"] = $value_clean;
98  }
99  }
100  // radiobutton lists as drop down
101  }else if ( $name == "dropdown"){
102  if( $val == "true" ){
103  $this->config["dropdown"] = true;
104  }else{
105  $this->config["dropdown"] = false;
106  }
107  }else if ($name == "max"){
108  if( is_numeric($val ) ){
109  $this->config["max"] = intval($val);
110  }else{
111  $this->config["max"] = -1;
112  }
113  // display font
114  }else if( $name == "font-family" ){
115  $this->config["font-family"] = $val;
116  }else if( $name == "font-size" ){
117  $this->config["font-size"] = $val;
118  }else if($name == "font-bold"){
119  if( $val == true){
120  $this->config["font-bold"] = true;
121  }else{
122  $this->config["font-bold"] = false;
123  }
124  }else if($name == "numberQuestion"){
125  if( $val == true){
126  $this->config["numberQuestion"] = true;
127  }else{
128  $this->config["numberQuestion"] = false;
129  }
130  }else if($name == "chartType"){
131  $allowed = array("pieChart","barChart","barChart2","radarChart","doughnutChart");
132  if( in_array($val,$allowed) ){
133  $this->config["chartType"] = $val;
134  }
135  }else if( $name == "showCharts" ){
136  if( $val == true){
137  $this->config["showCharts"] = true;
138  }else{
139  $this->config["showCharts"] = false;
140  }
141  }else if( $name == "width" ){
142  if( is_numeric($val) AND (floatval($val) >= 0) ){
143  $this->config["width"] = floatval($val);
144  }else{
145  $this->config["width"] = -1;
146  }
147  }else if( $name == "margin-left" ){
148  if( is_numeric($val) AND (floatval($val) >= 0) ){
149  $this->config["margin-left"] = floatval($val);
150  }else{
151  $this->config["margin-left"] = 0;
152  }
153  }else if( $name == "margin-right" ){
154  if( is_numeric($val) AND (floatval($val) >= 0) ){
155  $this->config["margin-right"] = floatval($val);
156  }else{
157  $this->config["margin-right"] = 0;
158  }
159  }else if( $name == "float" ){
160  if( in_array($val,array("left","right")) ){
161  $this->config["float"] = $val;
162  }else{
163  $this->config["float"] = "left";
164  }
165  }
166  } // end is_array
167 
168  if($err_reason == ""){
169  return true;
170  }else{
171  return $err_reason;
172  }
173 
174  }
175 
181  public function get_config($name){
182  $this->init_config();
183  if( is_array($this->config) ){
184  // linebreak on child widgets of containers
185  if($name == "br"){
186  if( isset($this->config["br"]) ){
187  return $this->config["br"];
188  }else{
189  return false;
190  }
191  // for standalone checkbox value to be set in db as bool
192  }else if($name == "true"){
193  if( isset($this->config["true"]) ){
194  return $this->config["true"];
195  }else{
196  return "Ja";
197  }
198  // for standalone checkbox value to be set in db as bool
199  }else if($name == "false"){
200  if( isset($this->config["false"]) ){
201  return $this->config["false"];
202  }else{
203  return "Nein";
204  }
205  }else if($name == "dropdown"){
206  if( isset($this->config["dropdown"]) ){
207  return $this->config["dropdown"];
208  }else{
209  return false;
210  }
211 
212  } else if( $name == "max"){
213  if( isset($this->config["max"] )) {
214  return $this->config["max"];
215  }else{
216  return -1;
217  }
218  }else if( $name == "font-family" ){
219  if( isset( $this->config["font-family"] )) {
220  return $this->config["font-family"];
221  }else{
222  return "";
223  }
224  }else if( $name == "font-size" ){
225  if( isset( $this->config["font-size"] )) {
226  return $this->config["font-size"];
227  }else{
228  return "";
229  }
230  }else if($name == "font-bold"){
231  if( isset( $this->config["font-bold"] )) {
232  return $this->config["font-bold"];
233  }else{
234  return false;
235  }
236  }else if($name == "numberQuestion"){
237  if( isset( $this->config["numberQuestion"] )) {
238  return $this->config["numberQuestion"];
239  }else{
240  return false;
241  }
242  }else if($name == "chartType"){
243  if( isset( $this->config["chartType"] ) ){
244  return $this->config["chartType"];
245  }else{
246  return "";
247  }
248  }else if($name == "showCharts"){
249  if( isset( $this->config["showCharts"] ) ){
250  return $this->config["showCharts"];
251  }else{
252  return false;
253  }
254 
255  }else if($name == "width"){
256  if( isset( $this->config["width"] ) ){
257  return $this->config["width"];
258  }else{
259  return -1;
260  }
261 
262  }else if($name == "margin-left"){
263  if( isset( $this->config["margin-left"] ) ){
264  return $this->config["margin-left"];
265  }else{
266  return 0;
267  }
268 
269  }else if($name == "margin-right"){
270  if( isset( $this->config["margin-right"] ) ){
271  return $this->config["margin-right"];
272  }else{
273  return 0;
274  }
275 
276  }else if($name == "float"){
277  if( isset( $this->config["float"] ) ){
278  return $this->config["float"];
279  }else{
280  return "left";
281  }
282 
283  } // else if ...
284  }// end if array
285  }
286 
293  public function set_data($name,$val){
294  if($name == "count"){
295  if( is_numeric($val) ){
296  $this->data["count"] = $val;
297  }
298  }
299  }
300 
306  public function get_data($name){
307  if( $name == "count" ){
308  if(isset($this->data["count"])){
309  return $this->data["count"];
310  }else{
311  return 0;
312  }
313  }
314  }
315 
321  public function check_vital_vars(){
322  if ( !(isset($this->typ)) OR !(isset($this->name)) OR !(isset($this->index)) OR !(isset($this->pollID)) ){
323  throw new varException("Widget vars not set");
324  }
325  if( ($this->typ == "") OR ($this->name == "") ){
326  throw new varException("Widget vars not set");
327  }
328  return true;
329  }
330 
335  function check_required(){
336  if($this->is_required == false){
337  return true;
338  }
339 
340  $name = "mit der ID {$this->ID}";
341  if( (isset($this->dispName) ) AND ( $this->dispName != "") ){
343  }else if ( (isset($this->name) ) AND ( $this->name != "") ){
345  }
346 
347  if( !isset($_POST[ $this->name ]) ){
348  return "Das Feld <b>$name</b> ist ein Plichtfeld. Bitte dieses angeben.";
349  }else{
350  if( $_POST[ $this->name ] =="" ){
351  return "Das Feld <b>$name</b> ist ein Plichtfeld. Bitte dieses angeben.";
352  }else{
353  return true;
354  }
355  }
356  }
357 
358 
363  public function check_max(){
364  // get name
365  $name = "mit der ID {$this->ID}";
366  if( (isset($this->dispName) ) AND ( $this->dispName != "") ){
368  }else if ( (isset($this->name) ) AND ( $this->name != "") ){
370  }
371 
372  // get config and check
373  $max = $this->get_config("max");
374  if($max == -1){
375  return true;
376  }
377  if( $this->get_data("count") >= $max ){
378  return "Das Feld <b>$name</b> ist leider nicht mehr verfügbar! Bitte wähle ein Anderes.";
379  }
380  // if not already returned then ok
381  return true;
382  }
383 
389  public function set_dispName($dispName){
390  $dispName = htmlspecialchars($dispName , ENT_QUOTES , "UTF-8",true);
391 
392  $err_reason = "";
393  if( (strlen($dispName) > 256) ){
394  $err_reason = "anzeigeName enthält mehr als 256 Zeichen";
395  }
396 
397  if($err_reason == ""){
398  $this->dispName = $dispName;
399  return true;
400  }else{
401  return $err_reason;
402  }
403 
404  }
405 
411  public function set_name($name){
412  $name = htmlspecialchars($name , ENT_QUOTES , "UTF-8",true);
413 
414  $err_reason = "";
415  if( (strlen($name) <= 25) ){
416  $m = array();
417  if( preg_match_all("/[^0-9^a-z^A-Z^_]/", $name, $m) !== 0){
418  $err_reason = "Datenbankname enthält ungültige Zeichen, gültige Zeichen sind a-z, A-Z oder 0-9";
419  }
420  }else{
421  $err_reason = "Der Datenbankname ist zu lang (max 25 Zeichen)";
422  }
423  if( (strlen($name) == 0) ){
424  if( isset($this->ID) ){
425  $name = strval($this->ID);
426  }else{
427  $err_reason = "Bitte einen Datenbanknamen angeben!";
428  }
429  }
430 
431  if($err_reason == ""){
432  $this->name = $name;
433  return true;
434  }else{
435  return $err_reason;
436  }
437 
438  }
439 
443  public function display_edit_begin(){
444  $font_family= $this->get_config("font-family");
445  $font_size= $this->get_config("font-size");
446  if( $font_family != ""){ $font_family = "font-family:".$font_family; }
447  if( $font_size != ""){ $font_size = "font-size:".$font_size; }
448  $lasted="";
449  if( (isset($_SESSION["last_widget_edit"])) AND ($_SESSION["last_widget_edit"] == $this->ID) ){
450  echo "<script type='text/javascript'>
451  $(document).ready(function(){
452  window.scrollBy(0,-150);
453  });
454  </script>";
455  $lasted = " widget_last_edit";
456  unset( $_SESSION["last_widget_edit"] );
457  }
458 
459  echo "<div class='widget_container$lasted' style='position:relative;$font_family;$font_size;z-index:100;background-color:rgba(255,255,255,0.5)' id='widget_container_{$this->ID}'>";
460  echo "<div class='widget_edit_toolbar'>";
461  echo "<a href='action.widget.php?pollID={$this->pollID}&amp;widgetID={$this->ID}' title='Bearbeiten' ><img src='".APP_ROOT."/icons/22/preferences-system.png' alt='edit'/></a>";
462  echo "<a href='action.widget.php?pollID={$this->pollID}&amp;widgetID={$this->ID}&amp;action=duplicate_widget' title='Kopieren'><img src='".APP_ROOT."/icons/22/edit-copy.png' alt='remove'/></a>";
463  echo "<a href='action.widget.php?pollID={$this->pollID}&amp;widgetID={$this->ID}&amp;action=rmwid' style='margin-left:0px' title='Löschen' ><img src='".APP_ROOT."/icons/22/edit-delete.png' alt='remove'/></a>";
464  echo "<span style='margin-left:7px'>&nbsp;</span>";
465  echo "<a href='action.widget.php?pollID={$this->pollID}&amp;widgetID={$this->ID}&amp;action=mvup' title='weiter nach Oben schieben'><img src='".APP_ROOT."/icons/22/go-up.png' alt='mv up'/></a>";
466  echo "<a href='action.widget.php?pollID={$this->pollID}&amp;widgetID={$this->ID}&amp;action=mvdwn' title='weiter nach Unten schieben'><img src='".APP_ROOT."/icons/22/go-down.png' alt='mv down'/></a>";
467  echo "</div>";
468 
469  echo "<a href='edit.php?pollID={$this->pollID}&amp;widgetID={$this->ID}' class='widget_edit_link' id='widget{$this->ID}'>&nbsp;</a>";
470 
471  }
472 
477  public function display_name_edit(){
478  echo "<label for='widget_name'>Datenbankname </label>
479  <input type='text' name='widget_name' id='widget_name' size='40' value='{$this->name}' placeholder='Spaltenname in der Datenbank'/>
480  <span style='font-size:80%;color:#370500'>(nur A-Z a-z 0-9 / 25 Zeichen)</span>";
481  }
482 
488  public function handle_name_edit(){
489  if(isset($_POST["widget_name"])){
490  $r = $this->set_name($_POST["widget_name"]);
491  if($r !== true){
492  return $r;
493  }else{
494  return true;
495  }
496  }else{
497  return "Name nicht angegeben!";
498  }
499  }
500 
505  public function display_dispName_edit(){
506  echo "<label for='widget_dispname'>Titel / sichtbarer Name </label>
507  <input type='text' name='widget_dispname' id='widget_dispname' value='{$this->dispName}' placeholder='Fragestellung' size='40'/>
508  <span style='font-size:80%;color:#370500'>(256 Zeichen)</span>";
509  }
510 
516  public function handle_dispName_edit(){
517  if(isset($_POST["widget_dispname"])){
518  $r = $this->set_dispName($_POST["widget_dispname"]);
519  if($r !== true){
520  return $r;
521  }else{
522  return true;
523  }
524  }else{
525  return "Anzeige Name nicht angegeben!";
526  }
527  }
528 
533  public function display_required_edit(){
534  if($this->is_required){
535  $presel = "checked='checked'";
536  }else{$presel = "";}
537  echo "<input type='checkbox' name='widget_is_required' id='widget_is_required' value='true' $presel/>
538  <label for='widget_is_required'>Pflichtfeld</label>";
539  }
540 
546  public function handle_required_edit(){
547  if( (isset($_POST["widget_is_required"])) AND ($_POST["widget_is_required"] === "true") ){
548  $this->is_required = true;
549  }else{
550  $this->is_required = false;
551  }
552  return true;
553  }
554 
561  public function display_chartType_edit_generic($allowed,$allowed_names){
562  $conf = $this->get_config("chartType");
563 
564  echo "Diagrammtyp: <select name='widget_chartType'>";
565  echo "<option value='' >Standard</option>";
566  for( $i=0 ; $i<sizeof($allowed) ; ++$i){
567  $selected= "";
568  if( $allowed[$i] == $conf ){
569  $selected = "selected='selected'";
570  }
571  echo "<option value='{$allowed[$i]}' $selected >{$allowed_names[$i]}</option>";
572  }
573  echo "</select>";
574  }
575 
582  public function handle_chartType_edit_generic($allowed){
583  if( (isset($_POST["widget_chartType"])) AND ($_POST["widget_chartType"] !="") ){
584  if (in_array($_POST["widget_chartType"],$allowed)){
585  return $this->set_config("chartType",$_POST["widget_chartType"]);
586  }else{
587  return "Ungültiger diagrammtyp";
588  }
589  }
590  return true;
591  }
592 
593 
599  public function save_config($db){
600  $this->serialize_config();
601  $r = $db->update_widget_field($this , "config" , $this->config);
602  $this->unserialize_config();
603  return $r;
604  }
605 
611  public function save_dispName($db){
612  return $db->update_widget_field($this , "dispName" , $this->dispName);
613  }
614 
620  public function save_required($db){
621  return $db->update_widget_field($this , "is_required" , $this->is_required);
622  }
623 
629  public function save_name($db,$new_name){
630  $db->update_widget_name($this,$new_name);
631  }
632 
633 
634 }
635 
636 
642 //class output_widget extends widget{
643 
644 //}
645 
651 //class input_widget extends widget{
652 
653 //}
654 
655 
656 // ! no ending newLine !
657 ?>