Framework  3.9
datetime_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__))."/../field_renderers.inc";
39 
49 {
51  var $requireTime = true; // whether the time component of the field is also required
52  var $template = ""; // default reaonly format is short style
53 
55  {
56  $this->FieldRenderer($parent);
57  }
58 
59  function addValidatorsToForm($field, $required = false)
60  {
61  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
62 
63  $this->parent->validator->add(new DateValidator($field, $label));
64  $this->parent->validator->add(new TimeValidator($field, $label));
65 
66  if($required)
67  {
68  $this->parent->validator->add(new RequiredValidator($field, $label));
69  if($this->requireTime)
70  $this->parent->validator->add(new RequiredTimeValidator($field, $label . " Time"));
71  }
72  }
73 
74  function renderScript($field)
75  {
76  $script = "";
77 
78  if (!$this->_includedDateScript)
79  {
80  echo "<script src='{$this->parent->componentPath}/calendar/sonjara_calendar.js' type='text/javascript'></script>\n";
81  echo "<link href='{$this->parent->componentPath}/calendar/sonjara_calendar.css' type='text/css' rel='stylesheet'>\n";
82  $this->_includedDateScript = true;
83  }
84 
85  $calendar = "{$field}_calendar";
86  echo "<script type='text/javascript'>\n";
87  echo "var $calendar = new Calendar('$calendar', '', '{$this->parent->id}_{$field}');\n";
88  echo "</script>\n";
89  }
90 
91  function renderField($field)
92  {
93  $obj = $this->parent->data;
94  $value = $obj->get($field);
95 
96  // JDG 10/1/2010 check for empty saved string
97  $date = "";
98  if($value AND $value != '0000-00-00 00:00:00')
99  $date = DateTimeTypeRenderer::format($obj->reformatFromSQLDate($value), "m/d/Y");
100 
101  $this->_startField($field);
102  echo "<input id='{$this->parent->id}_{$field}' type='text' name='$field' value='$date' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar.toggle(this)'>\n";
103 
104  // If no time component, show as blank, not 12:00 AM
105  if ($value AND !preg_match("/00:00:00$/", $value))
106  {
107  $text = date("h/i/A", strtotime($value));
108  list($hh, $mm, $aa) = explode("/", $text);
109  }
110 
111  echo "<div id='{$this->parent->id}_{$field}_block' style='display: inline'>\n";
112  echo "&nbsp;&nbsp;<input type='text' value='$hh' name='{$field}_hh' size='2' maxlength='2' onkeydown='return maskInput(event);'>
113  &nbsp;:&nbsp;<input type='text' value='$mm' name='{$field}_mm' size='2' maxlength='2' onkeydown='return maskInput(event);'>";
114  echo "&nbsp;<select name='{$field}_aa'><option";
115  if ($aa == 'AM') echo " selected";
116  echo ">AM</option><option";
117  if ($aa == 'PM') echo " selected";
118  echo ">PM</option></select>";
119  echo "</div>\n";
120 
121  $this->_endField($field);
122  }
123 
124  function renderSearchScript($field, $mode)
125  {
126  $script = "";
127 
128  if (!$this->_includedDateScript)
129  {
130  echo "<script src='{$this->parent->componentPath}/calendar/sonjara_calendar.js' type='text/javascript'></script>\n";
131  echo "<link href='{$this->parent->componentPath}/calendar/sonjara_calendar.css' type='text/css' rel='stylesheet'>\n";
132  $this->_includedDateScript = true;
133  }
134 
135  if ($mode == "range")
136  {
137  $calendar_from = "{$field}_calendar_from";
138  $calendar_to = "{$field}_calendar_to";
139 
140  echo "<script type='text/javascript'>\n";
141  echo "var $calendar_from = new Calendar('$calendar_from', '{$this->parent->id}', '$field:from');\n";
142  echo "var $calendar_to = new Calendar('$calendar_to', '{$this->parent->id}', '$field:to');\n";
143  echo "</script>\n";
144  }
145  else
146  {
147  $calendar = "{$field}_calendar_{$mode}";
148  echo "<script type='text/javascript'>\n";
149  echo "var $calendar = new Calendar('$calendar', '{$this->parent->id}', '$field:$mode');\n";
150  echo "</script>\n";
151  }
152  }
153 
154  function renderSearchField($field, $mode)
155  {
156  if ($mode == "range")
157  {
158  $date_from = $this->parent->params->get($field, "from");
159  $date_to = $this->parent->params->get($field, "to");
160  echo "<tr>\n";
161  $this->_printLabel($field);
162  echo "<td>";
163  echo "<input type='text' name='$field:from' value='$date_from' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar_from.toggle(this)'>";
164  echo " to <input type='text' name='$field:to' value='$date_to' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar_to.toggle(this)'></td>\n";
165  echo "</td>\n";
166  echo "</tr>\n";
167  }
168  else
169  {
170  $date = $this->parent->params->get($field, $mode);
171  echo "<tr>\n";
172  $this->_printLabel($field);
173  echo "<td>";
174  echo "<input type='text' name='$field:$mode' value='$date' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar_{$mode}.toggle(this)'></td>\n";
175  echo "</td>\n";
176  echo "</tr>\n";
177  }
178  }
179 
180  function renderReadOnly($field)
181  {
182  $this->_startField($field);
183 
184  echo $this->format($field);
185 
186  // AJG - if the field is read-only but the form is not, add a hidden field to
187  // retain the field value upon submission.
188  if (!$this->parent->readOnlyForm)
189  {
190  $obj = $this->parent->data;
191  $value = $obj->get($field);
192  echo "<input type='hidden' name='$field' value='{$value}'/>";
193  }
194 
195  $this->_endField($field);
196  }
197 
198  function preProcess($field = "")
199  {
200  $hh = checkNumeric($_POST["{$field}_hh"]);
201  if(intval($hh) == 0) $hh = "00";
202  $mm = checkNumeric($_POST["{$field}_mm"]);
203  if(intval($mm) == 0) $mm = "00";
204  $aa = $_POST["{$field}_aa"];
205  $dd = $_POST["{$field}"];
206 
207  if (!$dd)
208  {
209  $this->parent->data->set($field, $this->parent->data->reformatToSQLDate("00/00/0000 00:00:00"));
210  return;
211  }
212  if($hh == "00" && $mm == "00")
213  {
214  $this->parent->data->set($field, $this->parent->data->reformatToSQLDate("$dd"));
215  return;
216  }
217 
218  if ($aa != "AM" && $aa != "PM") throw new FakoliException("Invalid meridiem");
219  if ($aa == "PM" && $hh != "12") $hh += 12;
220  if ($aa == "AM" && $hh == "12") $hh = "00";
221  $this->parent->data->set($field, $this->parent->data->reformatToSQLDate("$dd $hh:$mm:00"));
222  }
223 
224  function format($field)
225  {
226  return DateTimeTypeRenderer::format($this->parent->data->get($field), $this->template);
227  }
228 
229 }
230 ?>
Field renderer for date data fields.
renderSearchField($field, $mode)
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
renderSearchScript($field, $mode)
FieldRenderers can override this method to provide any Javascript that the control requires when bein...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
static format($datetime, $template="")
Date Validator.
Definition: validation.inc:338
FieldRenderer is the abstract base class for all FieldRenderers.
_startField($field, $styles="")
Internal method to generate the starting HTML for the field (including the label)
_endField($field)
Internal method to generate the closing HTML for the field.
_printLabel($field, $colspan=1, $styles="", $annotation="")
Internal method to generate the HTML for the field label.
FieldRenderer($parent)
Constructor.
RequiredTimeValidator.
Definition: validation.inc:702
RequiredField Validator.
Definition: validation.inc:76
TimeValidator.
Definition: validation.inc:658
checkNumeric($p)
Security helper function.
Definition: functions.inc:630