Framework  3.9
toggle_field_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8 Copyright (c) 2007-2018 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 
44 {
45  var $disable = false;
46  var $template = "Yes/No";
47  var $onClick = null;
48  var $style = "round";
49  var $ragged = false;
50 
55  function ToggleFieldRenderer(&$parent, $ragged, $field = null, $label = null)
56  {
57  $this->FieldRenderer($parent);
58 
59  $this->ragged = $ragged;
60  if ($this->ragged)
61  {
62  $this->hideLabel = true;
63  $this->colspan = 2;
64  }
65 
66  if ($field)
67  {
68  if (!$parent->data->hasField($field))
69  {
70  $parent->add($this, $field, $label);
71  }
72  else
73  {
74  $parent->override($field, $label, $this);
75  }
76  }
77  }
78 
79  function addValidatorsToForm($field, $required = false)
80  {
81  $label = isset($this->label) ? $this->label : $this->parent->prettifyFieldName($field);
82 
83  if($required)
84  {
85  $this->parent->validator->add(new RequiredBooleanValidator($field, $label));
86  }
87 
88  }
89 
90 
91  function renderField($field)
92  {
93 
94 
95  $this->_startField($field);
96  $disable = ($this->disable) ? " disabled" : "";
97  $onClick = ($this->onClick) ? " onclick='return {$this->onClick}(this.checked);'" : "";
98 
99  if ($this->ragged)
100  {
101  echo "<label class='label' for='{$this->parent->id}_{$field}'>".$this->_getLabel($field, false)."</label>\n";
102  }
103 
104  echo "<label class='switch{$disable}'>";
105  echo "<input id='{$this->parent->id}_{$field}' type='checkbox' class='checkbox' value='1' name='$field'";
106  if ($this->parent->data->get($field))
107  {
108  echo " checked";
109  }
110  echo " {$disable}{$onClick}>&nbsp;<span class='slider {$this->style}'></span>";
111  echo "</label>";
112 
113  $this->_endField($field);
114  }
115 
116  function renderSearchField($field, $mode = "match")
117  {
118  echo "<tr>\n";
119  echo "<td colspan='2'";
120  if ($this->parent->labelCSS) echo " class='{$this->parent->labelCSS}'";
121  echo "><input type='checkbox' value='1' name='$field:$mode'";
122  if ($this->parent->params->get($field, $mode))
123  {
124  echo " checked";
125  }
126  echo ">&nbsp;";
127  echo $this->parent->prettifyFieldName($field);
128  echo "</td>\n</tr>\n";
129  }
130 
131  function renderReadOnly($field)
132  {
133  $this->colspan = 1;
134  $this->hideLabel = false;
135 
136  $this->_startField($field);
137  echo $this->format($field);
138  $this->_endField($field);
139  }
140 
141  function format($field)
142  {
143  return BooleanTypeRenderer::format($this->parent->data->get($field), $this->template);
144  }
145 }
146 
static format($value, $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.
RequiredBoolean Validator.
Field renderer for boolean data fields.
renderField($field)
FieldRenderers must override this method to provide the HTML implementation of the control used to ed...
$onClick
Name of Javascript function to be called when checkbox is toggled.
addValidatorsToForm($field, $required=false)
This method is called by the AutoForm to add any default input validators that are required by the Fi...
ToggleFieldRenderer(&$parent, $ragged, $field=null, $label=null)
Create a new BooleanFieldRenderer.
renderSearchField($field, $mode="match")
FieldRenderers must override this method to provide the HTML implementation of the control displayed ...
$template
, alternative to readonly; field can be reenabled thru javascript