Framework  3.9
date_of_birth_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2014 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 
45 {
46  var $template = ""; // default reaonly format is short style
47  var $minimumAge = 0;
49 
51  {
52  $this->FieldRenderer($parent);
53  }
54 
55  function addValidatorsToForm($field, $required = false)
56  {
57  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
58 
59  if($required)
60  {
61  $this->parent->validator->add(new RequiredValidator("{$field}_mm", "$label Month"));
62  $this->parent->validator->add(new RequiredValidator("{$field}_dd", "$label Day"));
63  $this->parent->validator->add(new RequiredValidator("{$field}_yy", "$label Year"));
64  }
65 
66  if ($this->minimumAge)
67  {
68  $this->parent->validator->add(new MinimumAgeValidator($field, $label, $this->minimumAge, $this->minimumAgeMessage));
69  }
70  }
71 
72  function setMinimumAge($min, $message = "")
73  {
74  $this->minimumAge = $min;
75  $this->minimumAgeMessage = $message ? $message : "You must be at least {$min} years of age";
76  return $this;
77  }
78 
79  function renderScript($field)
80  {
81  }
82 
83  function renderField($field)
84  {
85  $obj = $this->parent->data;
86  if ($obj instanceof CompositeDataItem)
87  {
88  $obj = $obj->findSubObject($field);
89  }
90 
91  $month = intval($obj->format("{{$field}:n}"));
92  $day = intval($obj->format("{{$field}:j}"));
93  $year = intval($obj->format("{{$field}:Y}"));
94 
95  trace("** Month: $month, Day: $day, Year: $year, Field: {$obj->$field}", 3);
96 
97  $this->_startField($field);
98  $default = !$month ? " selected" : "";
99 
100  echo "<select id='{$this->parent->id}_{$field}_mm' type='text' name='{$field}_mm'><option value='' disabled {$default}>Month</option>";
101 
102  for($i = 1; $i <= 12; ++$i)
103  {
104  $mm = new DateTime("{$i}/1/1971");
105  $monthName = $mm->format("M");
106  option($i, $monthName, $month);
107  }
108 
109  $default = !$day ? " selected" : "";
110  echo "</select> / <select id='{$this->parent->id}_{$field}_dd' type='text' name='{$field}_dd'><option value='' disabled {$default}>Day</option>";
111 
112  for($i = 1; $i < 32; ++$i)
113  {
114  option($i, $i, $day);
115  }
116 
117  $default = !$year ? " selected" : "";
118  echo "</select> / <select id='{$this->parent->id}_{$field}_yy' type='text' name='{$field}_yy'><option value='' disabled {$default}>Year</option>";
119 
120  for($i = date('Y'); $i >= 1900; --$i)
121  {
122  option($i, $i, $year);
123  }
124 
125  echo "</select>";
126 
127  $this->_endField($field);
128  }
129 
130  function renderSearchScript($field, $mode)
131  {
132  }
133 
134  function renderSearchField($field, $mode)
135  {
136  }
137 
138  function renderReadOnly($field)
139  {
140  $this->_startField($field);
141 
142  echo $this->format($this->parent->data->get($field));
143 
144  $this->_endField($field);
145  }
146 
147  function format($value)
148  {
149  return DateTypeRenderer::format($value, $this->template);
150  }
151 
152  function preProcess($field = "")
153  {
154  trace("Preprocessing data of birth", 3);
155 
156  $mm = checkNumeric($_POST["{$field}_mm"]);
157  $dd = checkNumeric($_POST["{$field}_dd"]);
158  $yy = checkNumeric($_POST["{$field}_yy"]);
159 
160  if ($mm < 10) $mm = "0{$mm}";
161  if ($dd < 10) $dd = "0{$dd}";
162 
163  $dob = "$yy-$mm-$dd";
164 
165  trace("** Preprocessing date of birth: $dob", 3);
166 
167  $this->parent->data->set($field, $dob);
168  }
169 
170 }
171 ?>
Field renderer for date of birth data fields.
renderSearchField($field, $mode)
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
renderScript($field)
FieldRenderers can override this method to provide any Javascript that their control requires for an ...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
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...
renderSearchScript($field, $mode)
FieldRenderers can override this method to provide any Javascript that the control requires when bein...
static format($date, $template="")
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.
FieldRenderer($parent)
Constructor.
AgeRangeValidator can be used with DateOfBirth fields.
Definition: validation.inc:390
RequiredField Validator.
Definition: validation.inc:76
checkNumeric($p)
Security helper function.
Definition: functions.inc:630
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010
option($value, $text, $sel="")
Write out an option tag, marking as selected if applicable.
Definition: functions.inc:888