Framework  3.9
date_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 $onChange = "";
52  var $onInput = "";
53  var $template = "";
54 
56  {
57  $this->FieldRenderer($parent);
58  }
59 
60  function addValidatorsToForm($field, $required = false)
61  {
62  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
63 
64  $this->parent->validator->add(new DateValidator($field, $label));
65 
66  if($required)
67  {
68  $this->parent->validator->add(new RequiredValidator($field, $label));
69  }
70 
71  }
72 
73  function renderScript($field)
74  {
75  $script = "";
76 
77 // if (!$this->_includedDateScript)
78 // {
79 // echo "<script src='{$this->parent->componentPath}/calendar/sonjara_calendar.js' type='text/javascript'></script>\n";
80 // echo "<link href='{$this->parent->componentPath}/calendar/sonjara_calendar.css' type='text/css' rel='stylesheet'>\n";
81 // $this->_includedDateScript = true;
82 // }
83 
84  $calendar = "{$this->parent->id}_{$field}_calendar";
85  echo "<script type='text/javascript'>\n";
86  echo "var $calendar = new Calendar('$calendar', '', '{$this->parent->id}_{$field}');\n";
87  echo "</script>\n";
88  }
89 
90  function renderField($field)
91  {
92  $obj = $this->parent->data;
93  if ($obj instanceof CompositeDataItem)
94  {
95  $obj = $obj->findSubObject($field);
96  }
97  $date = $obj->format("{{$field}:m/d/Y}");
98 
99  $onchange = ($this->onChange) ? "onchange='$this->onChange(this)'" : "";
100  $oninput = ($this->onInput) ? "oninput='$this->onInput(this)'" : "";
101 
102  $this->_startField($field);
103  echo "<input id='{$this->parent->id}_{$field}' type='text' name='$field' value='$date' $onchange $oninput style='width: auto' class='date' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$this->parent->id}_{$field}_calendar.toggle(this)'>\n";
104  $this->_endField($field);
105  }
106 
107  function renderSearchScript($field, $mode)
108  {
109  $script = "";
110 
111  if (!$this->_includedDateScript)
112  {
113  echo "<script src='{$this->parent->componentPath}/calendar/sonjara_calendar.js' type='text/javascript'></script>\n";
114  echo "<link href='{$this->parent->componentPath}/calendar/sonjara_calendar.css' type='text/css' rel='stylesheet'>\n";
115  $this->_includedDateScript = true;
116  }
117 
118  if ($mode == "range")
119  {
120  $calendar_from = "{$field}_calendar_from";
121  $calendar_to = "{$field}_calendar_to";
122 
123  echo "<script type='text/javascript'>\n";
124  echo "var $calendar_from = new Calendar('$calendar_from', '{$this->parent->id}', '$field:from');\n";
125  echo "var $calendar_to = new Calendar('$calendar_to', '{$this->parent->id}', '$field:to');\n";
126  echo "</script>\n";
127  }
128  else
129  {
130  $calendar = "{$field}_calendar_{$mode}";
131  echo "<script type='text/javascript'>\n";
132  echo "var $calendar = new Calendar('$calendar', '{$this->parent->id}', '$field:$mode');\n";
133  echo "</script>\n";
134  }
135  }
136 
137  function renderSearchField($field, $mode)
138  {
139  $this->_startField($field);
140 
141  if ($mode == "range")
142  {
143  $label = $this->parent->layout->getLabel($field, $this);
144  $toLabel = (preg_match("/Between/i", $label)) ? "and" : "to";
145 
146  $date_from = $this->parent->params->get($field, "from");
147  $date_to = $this->parent->params->get($field, "to");
148  echo "<input type='text' name='$field:from' value='$date_from' class='date' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar_from.toggle(this)'>";
149  echo "&nbsp;<label for='date_to'>$toLabel</label>&nbsp;";
150  echo "<input type='text' name='$field:to' value='$date_to' class='date' size='12'>&nbsp;<img src='{$this->parent->componentPath}/calendar/calendar.gif' alt='Popup Calendar' align='absmiddle' border='0' onclick='{$field}_calendar_to.toggle(this)'>\n";
151  }
152  else
153  {
154  $date = $this->parent->params->get($field, $mode);
155 
156  echo "<input type='text' name='$field:$mode' value='$date' class='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)'>\n";
157  }
158 
159  $this->_endField($field);
160  }
161 
162  function renderReadOnly($field)
163  {
164  $this->_startField($field);
165 
166  echo $this->format($this->parent->data->get($field));
167 
168  $this->_endField($field);
169  }
170 
171  function renderDateRangeReadOnly($field, $date_from, $date_to)
172  {
173  $label = $this->parent->layout->getLabel($field, $this);
174 
175  if($date_from && $date_to)
176  {
177  $toLabel = (preg_match("/Between/i", $label)) ? "and" : "to";
178  }
179  else if($date_from && !$date_to)
180  {
181  $this->label = preg_replace("/Between/i", "From", $label);
182  }
183  else // date to and no date_from
184  {
185  $this->label = preg_replace("/(Between)|(From)/i", "Up To", $label);
186  }
187  echo "<tr>\n";
188  $this->_printLabel($field);
189  echo "<td";
190  if ($this->parent->valueCSS) echo " class='{$this->parent->valueCSS}'";
191  echo ">".$this->format($date_from). "&nbsp;<label for='date_to'>$toLabel</label>&nbsp;" . $this->format($date_to) ."</td>\n";
192  echo "</tr>\n";
193  }
194 
195  function format($value)
196  {
197  return DateTypeRenderer::format($value, $this->template);
198  }
199 }
200 ?>
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...
$onInput
optional javascript callback for 'input' event on date text field
renderDateRangeReadOnly($field, $date_from, $date_to)
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 ...
$onChange
optional javascript callback for 'change' event on date text field
$template
template for date display in readonly mode - default reaonly format is short style
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
static format($date, $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.
RequiredField Validator.
Definition: validation.inc:76