Umfragen
scheduleDate.class.php
Go to the documentation of this file.
1 <?php
2 class scheduleDate extends widget implements input_widget, output_widget{
3  public $typ = "scheduleDate";
4  public $direct_result = true;
5 
6  public function set_default_values($db){
7  $d = date("Y-m-d");
8  $this->set_date("from","$d 00:00");
9  $this->set_date("to","0000-00-00 00:00");
10  }
11 
12 
13  public function display_date_select_options($begin , $end , $selected=0){
14  for($i=$begin ; $i<$end ; ++$i){
15  if( $i == $selected ){
16  echo "<option value='$i' selected='selected'>$i</option>";
17  }else{
18  echo "<option value='$i'>$i</option>";
19  }
20  }
21  }
22 
23  public function get_date($code){
24  $date = explode("~",$this->dispName);
25  if( sizeof($date) != 2 ){
26  $date = array( "0000-00-00 00:00" , "0000-00-00 00:00" );
27  }
28 
29  if( $code == "from" ){
30  return $date[0];
31  }else if($code == "to"){
32  return $date[1];
33  }else{
34  return "0000-00-00 00:00";
35  }
36 
37  }
38 
39  public function set_date($code,$value){
40  $d_from = $this->get_date("from");
41  $d_to = $this->get_date("to");
42 
43  if( $code == "from" ){
44  $d_to = $this->get_date("to");
45  $d_from = $value;
46  }else if($code == "to"){
47  $d_from = $this->get_date("from");
48  $d_to = $value;
49  }else{
50  return;
51  }
52  $this->dispName = "{$d_from}~{$d_to}";
53  }
54 
55  public function get_date_array($datestr){
56  $date_arr = strptime($datestr,"%Y-%m-%d %H:%M"); // can be false!
57  if( $date_arr === false ){
58  $date_arr = array();
59  }
60  if(isset($date_arr["tm_year"])){
61  $date_arr["tm_year"] = 1900+$date_arr["tm_year"]; // years since 1900
62  }else{ $date_arr["tm_year"] = 0;}
63 
64  if(isset($date_arr["tm_mon"])){
65  $date_arr["tm_mon"] = 1+$date_arr["tm_mon"]; // 0=>januar 1=>feb ...
66  }else{ $date_arr["tm_mon"] = 1;}
67 
68  if(!isset($date_arr["tm_mday"])){
69  $date_arr["tm_mday"] = 1;
70  }
71  if(!isset($date_arr["tm_hour"])){
72  $date_arr["tm_hour"] = 0;
73  }
74  if(!isset($date_arr["tm_min"])){
75  $date_arr["tm_min"] = 0;
76  }
77 
78  return $date_arr;
79  }
80 
81 
82  public function display_date_input($code,$code2="date"){
83  $date = $this->get_date($code);
84  if( ! isset($date) ){
85  return ;
86  }
87 
88  $date_arr = $this->get_date_array($date);
89 
90  if($date != "0000-00-00 00:00"){
91  $val = $date_arr["tm_mday"]."-". $date_arr["tm_mon"]."-".$date_arr["tm_year"];
92  }else{
93  if( $code == "from"){
94  $val = date("d-m-Y");
95  }else if($code == "to"){
96  $val = "";
97  }
98  }
99 
100  $id_pref = "date_{$this->ID}_{$code}";
101 
102  if( $code2 == "date" ){
103  echo "<script type='text/javascript'>";
104  echo "document.write(\"<input type='text' id='{$id_pref}_datepicker' name='{$id_pref}_datepicker' placeholder='TT-MM-JJJJ' value='$val' size='7'/>\");";
105  echo "$( \"#{$id_pref}_datepicker\" ).datepicker({dateFormat: \"dd-mm-yy\" });";
106  echo "</script>";
107 
108  echo "<noscript>";
109  echo "<select name='{$id_pref}_day' id='{$id_pref}_day'>";
110  $this->display_date_select_options(1,31,$date_arr["tm_mday"]);
111  echo "</select>";
112  echo "-";
113  echo "<select name='{$id_pref}_month' id='{$id_pref}_month'>";
114  $this->display_date_select_options(1,12,$date_arr["tm_mon"]);
115  echo "</select>";
116  echo "-";
117  echo "<input type='text' value='{$date_arr["tm_year"]}' name='{$id_pref}_year' id='{$id_pref}_year' size='4'/>";
118  echo "</noscript>";
119 
120  }else if( $code2 == "time" ){
121  $h = $this->format_int_2digit($date_arr["tm_hour"]);
122  $m = $this->format_int_2digit($date_arr["tm_min"]);
123  $t = "{$h}:{$m}";
124  if( $t == "00:00" ){
125  $t = "";
126  }
127  echo "<input type='text' size='7' name='{$id_pref}_time' value='$t' placeholder='HH:MM'/>";
128  }
129  }
130 
131 
132  public function display_date_edit($i){
133  $edited ="";
134  if( (isset($_SESSION["last_option_edit"])) AND ( $_SESSION["last_option_edit"] == $this->ID ) ){
135  $edited = "option_last_edit";
136  unset($_SESSION["last_option_edit"] );
137  }
138 
139  $l = $i % 2;
140  echo "<tr class='$edited line$l'>";
141  echo "<td>{$this->ID}</td>";
142  echo "<td style='padding:.5em 0em;'>";
143  $this->display_date_input("from");
144  echo "</td>";
145  echo "<td>";
146  $this->display_date_input("to");
147  echo "</td>";
148 
149  echo "
150  <td>
151  <input type='submit' name='addq_option_ID{$this->ID}' title='Frage 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;' />
152  <input type='submit' name='rmq_option_ID{$this->ID}' title='Frage Löschen' value='' tabindex='-1' style='padding:0px;margin:0px;background-color:transparent;background-image:url( ".APP_ROOT."/icons/22/edit-delete.png );background-repeat:no-repeat;width:26px;height:26px;' />
153  </td>";
154  echo "</tr>";
155  }
156 
157 
158 
159  private function format_int_2digit($i){
160  if( $i < 10){
161  return "0$i";
162  }else{
163  return "$i";
164  }
165  }
166 
167  public function handle_date_edit(){
168  foreach( array("from","to") as $code ){
169  $prefix = "date_{$this->ID}_{$code}";
170 
171  // datepicker
172  if( (isset($_POST["{$prefix}_datepicker"])) AND ($_POST["{$prefix}_datepicker"] != "" ) ){
173  //dd-mm-yy
174  $d = explode("-",$_POST["{$prefix}_datepicker"]);
175  if( (sizeof($d)==3) ){
176  $day = intval($d[0]);
177  $month = intval($d[1]);
178  $year = intval($d[2]);
179  }else{
180  return "Ein Datum ist falsch geschrieben";
181  }
182 
183  // no javascrip fallback
184  }else if( (isset($_POST["{$prefix}_month"])) AND (isset($_POST["{$prefix}_day"])) AND (isset($_POST["{$prefix}_year"])) ){
185  if( (is_numeric($_POST["{$prefix}_month"])) AND (is_numeric($_POST["{$prefix}_day"])) AND (is_numeric($_POST["{$prefix}_year"])) ){
186  $day = intval( $_POST["{$prefix}_day"] );
187  $month = intval( $_POST["{$prefix}_month"] );
188  $year = intval( $_POST["{$prefix}_year"] );
189  }else{
190  if( $code == "to" ){
191  $day = $month = $year = 0;
192  }else if($code == "from"){
193  return "Ein Datum ist falsch geschrieben";
194  }
195  }
196  // date "from" is optional, date "to" missinge gives error
197  }else{
198  if( $code == "to" ){
199  $day = $month = $year = 0;
200  }else if($code == "from"){
201  return "Ein Datum ist falsch geschrieben";
202  }
203  }
204 
205 
206  if( (isset($_POST["{$prefix}_time"])) AND ($_POST["{$prefix}_time"] != "") ){
207  $time = $_POST["{$prefix}_time"];
208  $time = explode(":",$time);
209  if( (sizeof($time) == 2) AND (is_numeric($time[0])) AND (is_numeric($time[1])) ){
210  $hour = intval($time[0]);
211  $min = intval($time[1]);
212  }else{
213  return "Eine Zeitangabe ist falsch angegeben";
214  }
215  }else{
216  $hour = $min = 0;
217  }
218  // format ints to 2-char strings
219  $day_s = $this->format_int_2digit($day);
220  $month_s = $this->format_int_2digit($month);
221  $hour_s = $this->format_int_2digit($hour);
222  $min_s = $this->format_int_2digit($min);
223  $year_s = str_pad("$year",4,"0",STR_PAD_LEFT);
224 
225  if( "$year_s-$month_s-$day_s" != "0000-00-00" ){
226  if (checkdate($month,$day,$year)){
227  if( ($hour>=0) AND ($hour<24) AND ($min>=0) AND ($min<60)){
228  $datetime = "$year-$month_s-$day_s $hour_s:$min_s"; //YYYY-MM-DD HH:MM
229  $this->set_date($code,$datetime);
230  }else{
231  return "Zeitangabe falsch. Stunden zwischen 0 und 23, minuten zwischen 0 und 59";
232  }
233  }else{
234  return "Eines der angegebenen Daten existiert nicht";
235  }
236  }else{
237  $this->set_date($code,"0000-00-00 00:00");
238  }
239 
240 
241  }
242  return true;
243  }
244 
245 
246  public function handle_inpt(){
247  return true;
248  }
249 
250  public function display(){
251 
252  }
253 
254 
255 
256 }
257 // ! no ending newLine
258 ?>