Framework  3.9
phone_number_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 
58 /*
59  * Render a string phone number into separate
60  * boxes for area code then 3 digits, 4, and a space for
61  * extension, if applicable.
62  *
63  */
64 
65 
67 {
68  var $disable = false; // alternative to readonly; field can be reenabled thru javascript
69  var $template = "standard"; // PhoneNumberTypeRenderer template for reaonly format (e.g., "(123) 456-7890 ext 123")
70  var $extension = false;
71 
73  {
74  $this->FieldRenderer($parent);
75  }
76 
77  function renderScript($field)
78  {
79  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field)
80  || array_key_exists($field, $this->parent->hidden)) return "";
81 
83 ?>
84 <script type='text/javascript'>
85 <?php
86  foreach($parts as $part => $limit)
87  {
88  if($part == "extension" && !$this->extension)
89  continue;
90  $partField = ($part) ? "{$field}_{$part}" : $field;
91  $this->renderScriptOnePart($partField, $limit);
92  }
93 ?></script><?php
94  }
95 
96  function renderScriptOnePart($field, $limit)
97  {
98 ?>
99 function <?echo $this->parent->id?>_<?echo $field ?>_keyup(event)
100 {
101  var ctrl = document.id('<?echo "{$this->parent->id}_{$field}" ?>');
102  if(!ctrl) return;
103 
104  if (event.key == "Tab" || event.key == "Shift") return;
105 
106  if(ctrl.value.length == <?php echo $limit ?>)
107  {
108  var next = ctrl.getNext("input");
109  if(next && next.getAttribute('data-name') == ctrl.getAttribute('data-name'))
110  {
111  next.focus();
112  }
113  }
114  else if (ctrl.value.length >= <?echo $limit ?>)
115  {
116  ctrl.value = ctrl.value.substring(0, <?echo $limit ?>);
117  }
118 
119 }
120 <?php
121  }
122 
123  function renderField($field)
124  {
126 
127  $this->_startField($field);
128 
129  $value = $this->parent->data->get($field);
130 
131  $valueParts = PhoneNumberTypeRenderer::getValueParts($value);
132 
133  $idx = 0;
134  foreach($parts as $part => $limit)
135  {
136  if($part == "extension" && !$this->extension)
137  continue;
138 
139  if($part == "area_code")
140  $pre = "(&nbsp;";
141  elseif($part == "exchange")
142  $pre = ")&nbsp;&nbsp;";
143  elseif($part == "extension")
144  $pre = "ext ";
145  else
146  $pre = "<b>&#65293;</b>";
147 
148  $partField = ($part) ? "{$field}_{$part}" : $field;
149 
150  $onkeypress = " onkeyup='{$this->parent->id}_{$partField}_keyup(event);'";
151  $disable = ($this->disable) ? "disabled='disabled'" : "";
152  echo "$pre<input id='{$this->parent->id}_{$partField}' style='width: auto' data-name='{$field}' type='text' name='{$partField}' value='".htmlspecialchars($valueParts[$part], ENT_QUOTES, 'UTF-8')."' size='$limit' onkeypress='return maskInput(event, false);' $disable $onkeypress/>&nbsp;";
153  $idx++;
154  }
155 
156  $this->_endField($field);
157  }
158 
159 
160  function renderReadOnly($field)
161  {
162  $this->_startField($field);
163 
164  echo $this->format($field);
165 
166  $this->_endField($field);
167  }
168 
169  function renderSearchField($field, $mode = "equal")
170  {
171  if ($mode == "range")
172  {
173  $from = htmlspecialchars($this->parent->params->get($field, "from"), ENT_QUOTES, 'UTF-8');
174  $to = htmlspecialchars($this->parent->params->get($field, "to"), ENT_QUOTES, 'UTF-8');
175 
176  echo "<tr>\n";
177  $this->_printLabel($field);
178  echo "<td>From <input type='text' id='$field' name='$field:from' value='{$from}' size='25'> to <input type='text' name='$field:to' value='{$to}' size='25'></td>\n";
179  echo "</tr>\n";
180  }
181  else
182  {
183  $value = htmlspecialchars($this->parent->params->get($field, $mode), ENT_QUOTES, 'UTF-8');
184  echo "<tr>\n";
185  $this->_printLabel($field);
186  echo "<td><input type='text' id='$field' name='$field:$mode' value='{$value}' size='50'></td>\n";
187  echo "</tr>\n";
188  }
189  }
190 
191  function preProcess($field = "")
192  {
194 
195  $partValues = array();
196 
197  foreach($parts as $part => $limit)
198  {
199  $partField = ($part) ? "{$field}_{$part}" : $field;
200  $partValues[] = $_POST[$partField];
201  }
202 
203  $value = implode($partValues, "");
204  $this->parent->data->set($field, $value);
205  }
206 
207 
208  /*
209  * Instead of a custom validator, use regexp to
210  * validate each individual part of the phone number.
211  * The extension needs no validation because it can
212  * be blank and is already guaranteed to be only digits
213  * by the keyup event on the input element.
214  */
215  function addValidatorsToForm($field, $required = false)
216  {
217  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
218 
220 
221  $message = "Please supply phone as a 10-digit number.";
222  foreach($parts as $part => $limit)
223  {
224  $partField = ($part) ? "{$field}_{$part}" : $field;
225  if($part == "extension") continue;
226  $regexp = "^\\d{" . $limit . "}$";
227  $this->parent->validator->add(new RegularExpressionValidator($partField, $label, $regexp, $message));
228  }
229 
230  if($required)
231  $this->parent->validator->add(new RequiredValidator($field, $label));
232  }
233 
234 
235  function format($field)
236  {
237  return PhoneNumberTypeRenderer::format($this->parent->data->get($field), $this->template);
238  }
239 
240 }
241 
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.
Field renderer for zipcode data fields.
renderSearchField($field, $mode="equal")
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 ...
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
preProcess($field="")
FieldRenderers can override this method to provide behavior that occurs prior to the saving of the pa...
static format($value, $template="")
Tests wheteher a field's value matches the supplied regular expression.
Definition: validation.inc:950
RequiredField Validator.
Definition: validation.inc:76