Umfragen
container.class.php
Go to the documentation of this file.
1 <?php
10 class object_list implements ArrayAccess,Iterator,countable{
11  private $object_list = array();
12  private $position = 0;
13 
14  public $parentID;
15  public $pollID;
16 
22  public function __construct($pollID,$parentID) {
23  $this->position = 0;
24  $this->parentID = $parentID;
25  $this->pollID = $pollID;
26  }
27 
28 
29  /*----------------------------
30  * ARRAYACCESS
31  *----------------------------*/
32  public function offsetSet($offset, $object) {
33  //Append
34  if (is_null($offset)) {
35  $i = sizeof($this->object_list) ;
36  if( $object->name == ""){
37  $object->name = "-";//$this->name;
38  }
39  $object->pollID = $this->pollID;
40  $object->index = $i;
41  //$opt->id = $this->name."{$opt->index}";
42  $object->is_child = true;
43  $object->is_required = (bool) $object->is_required ;
44  $object->parent = $this->parentID;
45 
46  $this->object_list[] = $object;
47  //Insert
48  }else {
49  // check $offset ...
50  if ( (is_numeric($offset)) AND ($offset >= 0) AND ($offset < sizeof($this->object_list)) ){
51  // use temp array
52  $temp = array();
53  for( $i=0 ; $i<sizeof($this->object_list) ; ++$i){
54  // just copy the other options
55  $temp[] = $this->object_list[$i];
56  // once incerted the index is shifted by one
57  if($i > $offset){
58  $temp[sizeof($temp)-1]->index = $temp[sizeof($temp)-1]->index+1;
59  }
60 
61  // insert option if reached the index
62  if($i == $offset){
63  //$option = new checkBox();
64  $object->name = "-";//$this->name;
65  $object->pollID = $this->pollID;
66  $object->index = $i+1;
67  //$opt->id = $this->name."{$opt->index}";
68  $object->is_child = true;
69  $object->is_required = (bool) $object->is_required ;
70  $object->parent = $this->parentID;
71  $temp[] = $object;
72  }
73  }
74  $this->object_list = $temp;
75  }
76  //$this->object_list[$offset] = $value;
77  }
78  }
79 
80  public function offsetExists($offset) {
81  return isset($this->object_list[$offset]);
82  }
83 
84  public function offsetUnset($offset) {
85  $temp = array();
86  $del_w_passed = false;
87  // shift indexes
88  for( $i=0 ; $i<sizeof($this->object_list) ; ++$i){
89  if( $i != $offset ){
90  // index verschiebt sich
91  if($del_w_passed == true){
92  $this->object_list[$i]->index = $this->object_list[$i]->index-1;
93  }
94  $temp[] = $this->object_list[$i];
95  }else{
96  // das zu löschende widget
97  $del_w_passed = true;
98  }
99  }
100  $this->object_list = $temp;
101  //unset($this->object_list[$offset]);
102  }
103 
104  public function offsetGet($offset) {
105  if( isset($this->object_list[$offset]) ){
106  return $this->object_list[$offset];
107  }else{
108  return null;
109  }
110  }
111 
112  /*----------------------------
113  * ITERATOR
114  *----------------------------*/
115  public function rewind() {
116  $this->position = 0;
117  }
118 
119  public function current() {
120  return $this->object_list[$this->position];
121  }
122 
123  public function key() {
124  return $this->position;
125  }
126 
127  public function next() {
128  ++$this->position;
129  }
130 
131  public function valid() {
132  return isset($this->object_list[$this->position]);
133  }
134 
135  /*----------------------------
136  * COUNTABLE
137  *----------------------------*/
138  public function count(){
139  return count($this->object_list);
140  }
141 
142 
143  /*----------------------------
144  * CUSTOM
145  *----------------------------*/
151  public function get_by_ID($ID){
152  foreach($this->object_list as &$opt){
153  if ($opt->ID == $ID){
154  return $opt;
155  }
156  }
157  }
158 
163  public function delete_by_ID($id){
164  $temp = array();
165  $del_w_passed = false;
166  // shift indexes
167  for( $i=0 ; $i<sizeof($this->object_list) ; ++$i){
168  if( $this->object_list[$i]->ID != $id ){
169  // index verschiebt sich
170  if($del_w_passed == true){
171  $this->object_list[$i]->index = $this->object_list[$i]->index-1;
172  }
173  $temp[] = $this->object_list[$i];
174  }else{
175  // das zu löschende widget
176  $del_w_passed = true;
177  }
178  }
179  $this->object_list = $temp;
180  }
181 
188  public function move($index1,$index2){
189  if( (isset($this->object_list[$index1])) AND (isset($this->object_list[$index2])) ){
190  $temp = $this->object_list[$index2];
191 
192  $this->object_list[$index2] = $this->object_list[$index1];
193  $this->object_list[$index2]->index = $index2;
194 
195  $this->object_list[$index1] = $temp;
196  $this->object_list[$index1]->index = $index1;
197  return true;
198  }
199  return false;
200  }
201 
202 }
203 
204 
205 
209 class container extends widget{
210  public $typ = "container";
211  public $direct_result = false;
212  public $child_widgets_whitelist = array("*");
213  public $child_options_whitelist = array("*");
214 
215  public $option_list = array();
216  public $widget_list = array();
217 
218 
219  function __construct(){
220  //$this->option_list = new object_list($this->pollID,$this->ID);
221  }
222 
229  return $this->option_list->get_by_ID($ID);
230  }
231 
238  return $this->widget_list->get_by_ID($ID);
239  }
240 
245  public function get_all_childs(){
246  $widgets = array();
247  foreach( $this->widget_list as $widget ){
248  $widgets[] = $widget;
249  }
250  foreach( $this->option_list as $option){
251  $widgets[] = $option;
252  }
253  return $widgets;
254  }
255 
260  function set_options($options){
261  $this->option_list = new object_list($this->pollID,$this->ID);
262  foreach($options as $option){
263  $option->unserialize_config();
264  $this->option_list[] = $option;
265  }
266  }
267 
272  function set_widgets($widgets){
273  $this->widget_list = new object_list($this->pollID,$this->ID);
274  foreach($widgets as $widget){
275  $widget->unserialize_config();
276  $this->widget_list[] = $widget;
277  }
278  }
279 
286  public function insert_option($index=-1,&$opt){
287  if ( isset($this->option_list[$index]) ){
288  $this->option_list[$index] = $opt;
289  }else{
290  $index = -1;
291  }
292 
293  if( $index === -1 ){
294  $this->option_list[] = $opt;
295  }
296  }
297 
304  public function insert_widget($index=-1,&$opt){
305  if ( isset($this->widget_list[$index]) ){
306  $this->widget_list[$index] = $opt;
307  }else{
308  $index = -1;
309  }
310 
311  if( $index === -1 ){
312  $this->widget_list[] = $opt;
313  }
314  }
315 
321  public function delete_option($db,$id){
322  $this->option_list->delete_by_ID($id);
323  // do delete
324  $db->delete_widget($this->pollID,$id);
325  $this->save_option_list($db);
326  }
327 
333  public function delete_widget($db,$id){
334  $this->widget_list->delete_by_ID($id);
335  // do delete
336  $db->delete_widget($this->pollID,$id);
337  $this->save_widget_list($db);
338 
339  }
340 
348  function move_option($db,$index1,$index2){
349  $r = $this->option_list->move($index1,$index2);
350  if( $r === true ){
351  $r1 = $db->update_widget_field($this->option_list[$index1],"index",$this->option_list[$index1]->index);
352  $r2 = $db->update_widget_field($this->option_list[$index2],"index",$this->option_list[$index2]->index);
353  return ($r1 AND $r2);
354  }else{
355  return $r;
356  }
357  }
358 
366  function move_widget($db,$index1,$index2){
367  $r = $this->widget_list->move($index1,$index2);
368  if( $r === true ){
369  $r1 = $db->update_widget_field($this->widget_list[$index1],"index",$this->widget_list[$index1]->index);
370  $r2 = $db->update_widget_field($this->widget_list[$index2],"index",$this->widget_list[$index2]->index);
371  return ($r1 AND $r2);
372  }else{
373  return $r;
374  }
375  }
376 
384  public function set_default_values($db){
385  if( (isset($this->pollID)) AND (isset($this->ID)) AND (!empty($this->pollID)) AND (!empty($this->ID)) AND ($this->pollID != -1) AND ($this->ID != -1) ){
386  $this->widget_list = new object_list($this->pollID,$this->ID);
387  $this->option_list = new object_list($this->pollID,$this->ID);
388  }
389  }
390 
396  public function check_required(){
397  $required_err = array();
398  foreach( $this->widget_list as $widget ){
399  if( $widget->is_required === true ){
400  $r = $widget->check_required();
401  if( $r !== true ){
402  $require_err[] = $r;
403  }
404  }
405  }
406  if( sizeof($required_err) != 0 ){
407  return $required_err;
408  }else{
409  return true;
410  }
411  }
412 
419  public function display_option_list_edit($polltype){
420  echo "<table class='table_1' > ";
421  if($polltype === POLLTYPE_ADVANCED ){
422  echo "<tr> <th>ID</th> <th>Option</th> <th>Wert</th> <th style='max-width:2em'>Bearbeiten</th> </tr>";
423  }else if ($polltype === POLLTYPE_SIMPLE){
424  echo "<tr> <th>ID</th> <th>Option</th> <th style='max-width:2em'>Bearbeiten</th> </tr>";
425  }
426  if( sizeof($this->option_list) != 0 ){
427  foreach($this->option_list as $opt){
428  $opt->display_option_edit($polltype);
429  }
430  }else{
431  echo "<td></td> <td></td>
432  <td> <input type='submit' name='add_option_ID{$this->ID}' title='Option Einfügen' value='' style='padding:0px;margin:0px;background-color:transparent;background-image:url( ".APP_ROOT."/icons/22/list-add.png );background-repeat:no-repeat;width:26px;height:26px;' /> </td>";
433  }
434  echo "</table>";
435  }
436 
443  public function handle_option_list_edit(){
444  // format: option_IDxx_name option_IDxx_value etc
445  $all_errs = "";
446  $options_seen = array();
447  // loop POST variable
448  $post_keys = array_keys($_POST);
449  foreach($post_keys as $post){
450  // if post array index beginns with "option_ID"
451  if( substr($post,0,strlen("option_ID")) == "option_ID"){
452  // get properety
453  if( substr($post,-1*strlen("name")) == "name"){
454  $prop = "name";
455  }else if( substr($post,-1*strlen("value")) == "value" ){
456  $prop = "value";
457  }else if( substr($post,-1*strlen("linebr")) == "linebr" ){
458  $prop = "linebr";
459  }else if( substr($post,-1*strlen("max")) == "max" ){
460  $prop = "max";
461  }
462  }
463  if (isset($prop)){
464  // get id
465  $id = substr( $post , strlen("option_ID") , strlen($post)-strlen("option_ID")-strlen("_".$prop) );
466  // handle option edit only once .. here we have 3 times this Id
467  if( ($id != false) AND (is_numeric($id)) AND (!in_array(intval($id),$options_seen) ) ){
468  $option = $this->get_option_by_id( intval($id) );
469  $err_reason = $option->handle_option_edit();
470  if($err_reason !== true){
471  $all_errs = $all_errs."<br/>".$err_reason;
472  }
473  $options_seen[] = $id;
474  }
475  }
476  }
477  if($all_errs !== ""){
478  return $all_errs;
479  }else{
480  return true;
481  }
482  }
483 
488  public function save_option_list($db){
489  $db->mass_update_widget($this->option_list);
490  }
491 
496  public function save_widget_list($db){
497  $db->mass_update_widget($this->widget_list);
498  }
499 
500  function display(){
501 
502  }
503 
504  function handle_inpt(){
505  return $this->check_required();
506  }
507 
508 
509 }
510 
511 
518 
519  public function get_config($name){
520  if(!is_array($this->config)){
521  $this->unserialize_config();
522  }
523 
524  if( is_array($this->config) ){
525 
526  if( $name == "columns" ){
527  if( isset($this->config["columns"]) ){
528  return $this->config["columns"];
529  }else{
530  return "columns";
531  }
532 
533  }else{
534  return parent::get_config($name);
535  }
536  }
537  }
538 
539 
540  public function set_config($name,$val){
541  if( (!isset($this->config)) OR ($this->config == "") ){
542  $this->config = array();
543  }
544  if( (isset($this->config)) AND (is_string($this->config)) AND ($this->config != "") ){
545  $this->unserialize_config();
546  }
547 
548  if( $name == "columns" ){
549  if( in_array($val,array("columns","rows")) ){
550  $this->config["columns"] = $val;
551  }
552 
553  }else{
554  return parent::set_config($name,$val);
555  }
556  return true;
557  }
558 
559 
560 
561  public function display_chartType_edit(){
562  $allowed = array("pieChart","barChart","doughnutChart","radarChart");
563  $allowed_names = array("Kreisdiagramm","Säulendiagramm","Ringdiagramm","Netzdiagramm");
564  $this->display_chartType_edit_generic($allowed,$allowed_names);
565  }
566 
567  public function handle_chartType_edit(){
568  $allowed = array("pieChart","barChart","doughnutChart","radarChart");
569  return $this->handle_chartType_edit_generic($allowed);
570  }
571 
572  public function display_columns_edit(){
573  $options = array( "rows" => "Zeilenumbruch" , "columns" => "Spaltenumbruch" );
574  $cnf = $this->get_config("columns");
575  echo "Umbrüche als: <select name='widget_columns_break'>";
576  foreach( array_keys($options) as $name ){
577  if( $cnf == $name ){
578  echo "<option value='$name' selected='selected'>{$options[$name]}</option>";
579  }else{
580  echo "<option value='$name'>{$options[$name]}</option>";
581  }
582  }
583  echo "</select>";
584  }
585 
586  public function handle_columns_edit(){
587  $options = array("rows","columns");
588  if( (isset($_POST["widget_columns_break"])) AND (in_array($_POST["widget_columns_break"],$options)) ){
589  $r = $this->set_config("columns",$_POST["widget_columns_break"]);
590  }
591  return true;
592  }
593 
594  public function display_show_charts_edit(){
595  $cnf = $this->get_config("showCharts");
596  $ckd = "";
597  if($cnf == true ){ $ckd = "checked='checked'"; }
598  echo "<input type='checkbox' name='show_charts' value='true' id='show_charts' $ckd/>
599  <label for='show_charts'>Aswertung der Frage nach dem Ausfüllen der Umfrage anzeigen</label>";
600  }
601 
602  public function handle_show_charts_edit(){
603  if( (isset($_POST["show_charts"])) AND ($_POST["show_charts"]=="true") ){
604  $r = $this->set_config("showCharts",true);
605  if($r !== true){
606  return $r;
607  }else{
608  return true;
609  }
610 
611  }else{
612  $r = $this->set_config("showCharts",false);
613  return $r;
614  }
615  }
616 
617 
622  public function rotate_option_list(){
623  $option_list = array(); // final result
624 
625  $column_list = array( array() ); // array with array representing the columns
626  $count = array(0); // used for getting the longest column
627  $c = 0; // actual column index
628  // go through options and sort ...
629  foreach( $this->option_list as $option ){
630  $column_list[$c][] = $option;
631  // options with br attribute create a new column
632  if( $option->get_config("br") ){
633  $count[$c] = sizeof( $column_list[$c] ); // old row
634  ++$c;
635  $column_list[$c] = array(); // new row
636  }
637  }
638  // delete last column if it is not used (left over if the last object makes a new column)
639  if( sizeof( $column_list[ sizeof( $column_list) -1 ] ) == 0 ){
640  $column_list = array_slice($column_list, 0, sizeof($column_list)-1 );
641  }
642  // if we have no br, there is no "count" even if there is one single column
643  if( sizeof($column_list) == 1 ){
644  $count[0] = sizeof( $column_list[0] );
645  }
646 
647  $max = max($count); // fill columns with dummy until this size
648  $row_list = array(); // array of arrays representing the rows of the final table
649 
650  // go through columns
651  for( $i=0 ; $i<sizeof($column_list) ; ++$i){ // column number
652  // make each column as long as the maximum value
653  for( $j=0 ; $j<$max ; ++$j){ // row number
654 
655  // there is a option
656  if( isset($column_list[$i][$j]) ){
657  // if the next column in this row is not set it is time to beak
658  if( !isset($column_list[$i+1][$j]) ){
659  $column_list[$i][$j]->set_data("br",true);
660  }
661  // add to or create a row array
662  if( isset($row_list[$j]) ){
663  $row_list[$j][] = $column_list[$i][$j];
664  }else{
665  $row_list[$j] = array($column_list[$i][$j]);
666  }
667 
668  // insert dummy option because there is no real option
669  }else{
670  $obj = new dummyRadioButton();
671  // dummy objects need also to be able to make a line break
672  if( !isset($column_list[$i+1][$j]) ){
673  $obj->set_data("br",true);
674  }
675 
676  if( isset($row_list[$j]) ){
677  $row_list[$j][] = $obj ;
678  }else{
679  $row_list[$j] = array($obj);
680  }
681  } // end if option
682  } // end go through row
683  } // end go through lines
684 
685  // put all together, so the draw function can handle it
686  foreach( $row_list as $row ){
687  foreach( $row as $option ){
688  $option_list[] = $option;
689  }
690  }
691 
692  return $option_list;
693  }
694 
695 
696 
697 }
698 
699 // ! no ending newLine
700 ?>