1 | <?php |
---|
2 | /* |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/> |
---|
5 | * Copyright © 2014 Strangecode, LLC |
---|
6 | * |
---|
7 | * This program is free software: you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, either version 3 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | * HTML() |
---|
23 | * |
---|
24 | * Tools for building HTML from PHP data. |
---|
25 | * |
---|
26 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
27 | * @version 1.0 |
---|
28 | * @since 11 Sep 2014 21:08:06 |
---|
29 | * |
---|
30 | * Example of use: |
---|
31 | --------------------------------------------------------------------- |
---|
32 | echo HTML::buttons(array( |
---|
33 | array('name' => 'submit', 'value' => _("Save changes"), 'class' => 'small button', 'accesskey' => 's'), |
---|
34 | array('name' => 'reset', 'value' => _("Reset"), 'class' => 'small button secondary', 'accesskey' => 'r'), |
---|
35 | array('name' => 'cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'), |
---|
36 | )); |
---|
37 | --------------------------------------------------------------------- |
---|
38 | */ |
---|
39 | |
---|
40 | class HTML |
---|
41 | { |
---|
42 | |
---|
43 | // Browsers add names and ids of form controls as properties to the FORM. This results in the properties of the form being replaced. |
---|
44 | // Use this list to warn the programmer if he uses an unsafe name. |
---|
45 | // http://jibbering.com/faq/names/unsafe_names.html |
---|
46 | static $unsafe_form_control_names = array('accept','acceptCharset','action','addBehavior','addEventListener','addEventSource','addRepetitionBlock','addRepetitionBlockByIndex','all','appendChild','applyElement','ariaBusy','ariaChecked','ariaControls','ariaDescribability','ariaDisabled','ariaExpanded','ariaFlowto','ariaHaspopup','ariaHidden','ariaInvalid','ariaLabelledby','ariaLevel','ariaMultiselect','ariaOwns','ariaPosinset','ariaPressed','ariaReadonly','ariaRequired','ariaSecret','ariaSelected','ariaSetsize','ariaValuemax','ariaValuemin','ariaValuenow','attachEvent','attributes','ATTRIBUTE_NODE','autocomplete','baseURI','behaviorUrns','blockDiraction','blur','canHaveChildren','canHaveHTML','CDATA_SECTION_NODE','checkValidity','childElementCount','childNodes','children','className','clearAttributes','click','clientHeight','clientLeft','clientTop','clientWidth','cloneNode','COMMENT_NODE','compareDocumentPosition','componentFromPoint','constructor','contains','contentEditable','currentStyle','data','detachEvent','dir','dispatchEvent','dispatchFormChange','dispatchFormInput','document','DOCUMENT_FRAGMENT_NODE','DOCUMENT_NODE','DOCUMENT_POSITION_CONTAINED_BY','DOCUMENT_POSITION_CONTAINS','DOCUMENT_POSITION_DISCONNECTED','DOCUMENT_POSITION_FOLLOWING','DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC','DOCUMENT_POSITION_PRECEDING','DOCUMENT_TYPE_NODE','dragDrop','elements','ELEMENT_NODE','encoding','enctype','ENTITY_NODE','ENTITY_REFERENCE_NODE','fireEvent','firstChild','firstElementChild','focus','getAdjacentText','getAttribute','getAttributeNode','getAttributeNodeNS','getAttributeNS','getBoundingClientRect','getClientRects','getElementsByClassName','getElementsByTagName','getElementsByTagNameNS','getExpression','getFeature','getUserData','hasAttribute','hasAttributeNS','hasAttributes','hasChildNodes','hasOwnProperty','hideFocus','id','innerHTML','innerText','insertAdjacentElement','insertAdjacentHTML','insertAdjacentText','insertBefore','isContentEditable','isDefaultNamespace','isDefaultNamespaceURI','isDisabled','isEqualNode','isMultiLine','isPrototypeOf','isSameNode','isSupported','isTextEdit','item','lang','language','lastChild','lastElementChild','length','localName','lookupPrefix','mergeAttributes','method','moveRepetitionBlock','msBlockProgression','msBoxSizing','name','namedItem','namespaceURI','nextSibling','nodeName','nodeType','nodeValue','normalize','NOTATION_NODE','offsetHeight','offsetWidth','onabort','onactivate','onbeforeactivate','onbeforecopy','onbeforecut','onbeforedeactivate','onbeforeeditfocus','onbeforepaste','onblur','onchage','onclick','onclosecapture','oncontextmenu','oncopy','oncut','ondblclick','ondeactivate','ondrag','ondragend','ondragenter','ondragleave','ondragover','onerror','onfocus','onfocusin','onfocusout','onhelp','oninput','onkeydown','onkeypress','onkeyup','onmousedown','onmouseenter','onmouseleave','onmousemove','onmousemultiwheel','onmouseout','onmouseover','onmouseup','onmousewheel','onmove','onmoveend','onmovestart','onOffBehavior','onpaste','onpropertychange','onreadystatechange','onresize','onresizeend','onresizestart','onscroll','onsearch','onselect','onselectstart','ontimeerror','onunload','outerHTML','outerText','ownerDocument','parentNode','parentTextEdit','prefix','previousElementSibling','previousSibling','PROCESSING_INSTRUCTION_NODE','propertyIsEnumerable','querySelector','querySelectorAll','quotes','releaseCapture','removeAttribute','removeAttributeNode','removeAttributeNS','removeBehavior','removeChild','removeEventListener','removeEventSource','removeExpression','removeNode','removeRepetitionBlock','repeatMax','repeatMin','repeatStart','repetitionBlocks','repetitionIndex','repetitionTemplate','repetitionType','replace','replaceAdjacentText','replaceChild','replaceNode','reset','resetFromData','role','runtimeStyle','schemaTypeInfo','scopeName','scrollByLines','scrollByPages','scrollHeight','scrollIntoView','scrollLeft','scrollTop','scrollWidth','selectNodes','selectSingleNode','setActive','setAttributeNode','setAttributeNodeNS','setAttributeNS','setCapture','setExpression','setIdAttribute','setIdAttributeNode','setIdAttributeNS','setUserData','sourceIndex','spellcheck','style','submit','swapNode','tabIndex','tagName','tagUrn','target','templateElements','text','textContent','TEXT_NODE','title','toLocaleString','toString','uniqueID','unselectable','unwatch','urns','valueOf','watch','window'); |
---|
47 | |
---|
48 | /** |
---|
49 | * Prints submit buttons based on given array of submit button names and titles. If the array includes an 'href' key, the |
---|
50 | * button is created using a <a> otherwise an <input type="âŠ" /> is used. |
---|
51 | * |
---|
52 | * @access public |
---|
53 | * @param array $buttons Array of buttons, the key being the button name, and value being the title of the button. |
---|
54 | * @return void |
---|
55 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
56 | * @version 1.0 |
---|
57 | * @since 12 Sep 2014 00:17:38 |
---|
58 | */ |
---|
59 | static public function printButtons($buttons=array(), $class='button-group') |
---|
60 | { |
---|
61 | $app =& App::getInstance(); |
---|
62 | |
---|
63 | if (!isset($buttons[0]) || !is_array($buttons[0])) { |
---|
64 | $app =& App::getInstance(); |
---|
65 | $app->logMsg(sprintf('Incorrect parameters passed to HTML::buttons(): %s', getDump($buttons)), LOG_DEBUG, __FILE__, __LINE__); |
---|
66 | return false; |
---|
67 | } |
---|
68 | if (empty($buttons)) { |
---|
69 | return ''; |
---|
70 | } |
---|
71 | ?><ul class="<?php echo oTxt($class) ?>"><?php |
---|
72 | foreach ($buttons as $i => $b) { |
---|
73 | $defaults = array(); |
---|
74 | $defaults['type'] = isset($b['type']) ? $b['type'] : 'submit'; |
---|
75 | $b = array_merge($defaults, $b); |
---|
76 | if (isset($b['href'])) { |
---|
77 | echo '<li><a'; |
---|
78 | foreach (array_diff_key($b, array('value' => null)) as $key => $value) { |
---|
79 | printf(' %s="%s"', $key, oTxt($value)); |
---|
80 | } |
---|
81 | echo '>' . oTxt($b['value']) . '</a></li>'; |
---|
82 | } else if (isset($b['name'])) { |
---|
83 | if (in_array($b['name'], self::$unsafe_form_control_names)) { |
---|
84 | $app->logMsg(sprintf('Unsafe form control name: %s', $b['name']), LOG_NOTICE, __FILE__, __LINE__); |
---|
85 | } |
---|
86 | $defaults['id'] = isset($b['id']) ? $b['id'] : sprintf('sc-%s-button', $b['name']); |
---|
87 | echo '<li><input'; |
---|
88 | foreach ($b as $key => $value) { |
---|
89 | printf(' %s="%s"', $key, oTxt($value)); |
---|
90 | } |
---|
91 | echo ' /></li>'; |
---|
92 | } else { |
---|
93 | $app->logMsg(sprintf('Button missing name or href: %s', getDump($b)), LOG_ERR, __FILE__, __LINE__); |
---|
94 | continue; |
---|
95 | } |
---|
96 | } |
---|
97 | ?></ul><?php |
---|
98 | } |
---|
99 | |
---|
100 | /* |
---|
101 | * Return an array of key-value pairs matching a database query for the given parameters. This is useful for |
---|
102 | * injecting into HTML::printSelectOptions(). |
---|
103 | * |
---|
104 | * @access public |
---|
105 | * @param string $db_table database table to lookup |
---|
106 | * @param string $key_column column containing the human-readable titles for the select menu |
---|
107 | * @param string $val_column column containing the computer values for the select menu |
---|
108 | * @param string $preselected the currently selected value of the menu. compared to the $val_column |
---|
109 | * @param bool $first Optional first item; set true for a blank item, array for item with name and value. |
---|
110 | * @param string $extra_clause SQL exclude clause. Something like "WHERE girls != 'buckteeth'" |
---|
111 | * @return array Array of options suitable to pass into HTML::printSelectOptions(). |
---|
112 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
113 | * @version 1.0 |
---|
114 | * @since 12 Sep 2014 11:43:23 |
---|
115 | */ |
---|
116 | static public function getSelectOptions($db_table, $key_column, $val_column, $preselected, $first=false, $extra_clause='', $sql_format='SELECT %1$s, %2$s FROM %3$s %4$s') |
---|
117 | { |
---|
118 | $db =& DB::getInstance(); |
---|
119 | |
---|
120 | // Sometimes preselected comes as a comma list. |
---|
121 | if (!is_array($preselected)) { |
---|
122 | $preselected = array($preselected); |
---|
123 | } |
---|
124 | |
---|
125 | $options = array(); |
---|
126 | if (true === $first) { |
---|
127 | // Include a blank first option. |
---|
128 | $options[] = array( |
---|
129 | 'value' => '', |
---|
130 | 'selected' => in_array('', $preselected), |
---|
131 | 'text' => '', |
---|
132 | ); |
---|
133 | } else if (is_array($first)) { |
---|
134 | // When the 'blank' first option needs a specific key->val pair. |
---|
135 | foreach ($first as $key => $val) { |
---|
136 | $options[] = array( |
---|
137 | 'value' => $key, |
---|
138 | 'selected' => in_array($key, $preselected), |
---|
139 | 'text' => $val, |
---|
140 | ); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | $db =& DB::getInstance(); |
---|
145 | $qid = $db->query(sprintf($sql_format, $key_column, $val_column, $db_table, $extra_clause), false); |
---|
146 | while ($row = mysql_fetch_assoc($qid)) { |
---|
147 | $options[] = array( |
---|
148 | 'value' => $row[$val_column], |
---|
149 | 'selected' => in_array($row[$val_column], $preselected), |
---|
150 | 'text' => $row[$key_column], |
---|
151 | ); |
---|
152 | } |
---|
153 | return $options; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Prints option fields for a select form. Works only with enum or set |
---|
158 | * data types in table columns. |
---|
159 | * |
---|
160 | * @param string $db_table Database table to lookup |
---|
161 | * @param string $db_col Database column to lookup |
---|
162 | * @param string $preselected The currently selected value of the menu. compared to the $val_column |
---|
163 | * @param bool $first Optional first item; set true for a blank item, array for item with name and value. |
---|
164 | * @param bool $sort Sort the output. |
---|
165 | */ |
---|
166 | static public function getSelectOptionsEnum($db_table, $db_col, $preselected, $first=false, $sort=false) |
---|
167 | { |
---|
168 | // Sometimes preselected comes as a comma list. |
---|
169 | if (!is_array($preselected)) { |
---|
170 | $preselected = array($preselected); |
---|
171 | } |
---|
172 | |
---|
173 | $options = array(); |
---|
174 | if (true === $first) { |
---|
175 | // Include a blank first option. |
---|
176 | $options[] = array( |
---|
177 | 'value' => '', |
---|
178 | 'selected' => in_array('', $preselected), |
---|
179 | 'text' => '', |
---|
180 | ); |
---|
181 | } else if (is_array($first)) { |
---|
182 | // When the 'blank' first option needs a specific key->val pair. |
---|
183 | foreach ($first as $key => $val) { |
---|
184 | $options[] = array( |
---|
185 | 'value' => $key, |
---|
186 | 'selected' => in_array($key, $preselected), |
---|
187 | 'text' => $val, |
---|
188 | ); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | $db =& DB::getInstance(); |
---|
193 | $values = $db->getEnumValues($db_table, $db_col, $sort); |
---|
194 | foreach ($values as $v) { |
---|
195 | $options[] = array( |
---|
196 | 'value' => $v, |
---|
197 | 'selected' => in_array($v, $preselected), |
---|
198 | 'text' => $v, |
---|
199 | ); |
---|
200 | } |
---|
201 | |
---|
202 | return $options; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Prints a select menu containing the specified values and keys of a table. |
---|
207 | * |
---|
208 | */ |
---|
209 | static public function printSelectOptions($options) |
---|
210 | { |
---|
211 | if (!isset($options) || !is_array($options)) { |
---|
212 | $app =& App::getInstance(); |
---|
213 | $app->logMsg(sprintf('Incorrect parameters passed to HTML::printSelectOptions(): %s', getDump($options)), LOG_DEBUG, __FILE__, __LINE__); |
---|
214 | return false; |
---|
215 | } |
---|
216 | if (empty($options)) { |
---|
217 | return ''; |
---|
218 | } |
---|
219 | |
---|
220 | foreach ($options as $o) { |
---|
221 | printf('<option value="%s"%s>%s</option>', |
---|
222 | oTxt($o['value']), |
---|
223 | ($o['selected'] ? ' selected' : ''), |
---|
224 | oTxt($o['text']) |
---|
225 | ); |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|