| [ Index ] |
PHP Cross Reference of Quick Form DataBase |
[Summary view] [Print] [Text view]
1 <?php 2 if (!defined('QFDB_SECURITY')) { 3 die("You can't access this file directly..."); 4 } 5 6 7 /** 8 * Form QFDB Object: Build a Checkbox Form Field 9 * 10 * @author Everaldo Wanderlei Uavniczak <everaldouav [at] gmail.com> 11 * @since 0.1 12 * @package quickformdb 13 * @see http://www.qfdb.net/ 14 * @license BSD License <http://www.qfdb.net/bsd_license.txt> 15 * @link http://w3schools.com/tags/tag_input.asp 16 * @todo implement reference to error_message 17 * @todo implement support to insert and update values of checkbox in other table of relational database 18 * @todo implement error_msg if is used both 'element_query' and 'element_array' 19 * @todo redefine (or fix) struct of 'element_query' and 'element_array' 20 * @todo define error message in 'element_emptymsg' (if number of elements is equal a 0) 21 * @todo document all error and warning 22 * @todo define warning message if 'cols'< 1 23 */ 24 25 26 27 class qfdbobj_checkbox { 28 29 static function obj_struct(&$qfdb) 30 { 31 $qfdb->m['object'] = 'checkbox'; 32 $qfdb->m['status'] = '1234'; 33 $qfdb->m['if'] = ''; 34 $qfdb->m['alias'] = ''; 35 36 $qfdb->m['db_field'] = ''; 37 $qfdb->m['form_field'] = ''; 38 39 $qfdb->m['element_query'] = ''; 40 $qfdb->m['element_array'] = ''; 41 $qfdb->m['element_class'] = $qfdb->get_css_value('qfdb_checkbox_element'); 42 $qfdb->m['element_others'] = ''; 43 $qfdb->m['element_label_class'] = $qfdb->get_css_value('qfdb_checkbox_element_label'); 44 $qfdb->m['element_label_others'] = ''; 45 46 $qfdb->m['nullval_value'] = ''; 47 $qfdb->m['nullval_label'] = ''; 48 $qfdb->m['nullval_others_label'] = ''; 49 $qfdb->m['nullval_others_element'] = ''; 50 51 $qfdb->m['cols'] = '3'; 52 $qfdb->m['cols_container_class'] = $qfdb->get_css_value('qfdb_checkbox_container_cols'); 53 $qfdb->m['cols_container_others'] = ''; 54 $qfdb->m['cols_class'] = $qfdb->get_css_value('qfdb_checkbox_cols'); 55 $qfdb->m['cols_style'] = ''; 56 $qfdb->m['cols_others'] = ''; 57 $qfdb->m['cols_break'] = '<br />'; 58 59 $qfdb->m['title'] = ''; 60 $qfdb->m['id'] = ''; 61 $qfdb->m['disabled'] = ''; 62 $qfdb->m['required'] = ''; 63 $qfdb->m['value'] = ''; 64 $qfdb->m['help'] = ''; 65 $qfdb->m['beggin'] = ''; 66 $qfdb->m['end'] = ''; 67 68 $qfdb->m['display'] = 'two_cell'; 69 70 $qfdb->m['label'] = ''; 71 $qfdb->m['label_class'] = $qfdb->get_css_value('qfdb_tpl_label'); 72 $qfdb->m['label_others'] = ''; 73 74 $qfdb->m['colspan_class'] = $qfdb->get_css_value('qfdb_tpl_colspan'); 75 $qfdb->m['colspan_others'] = ''; 76 $qfdb->m['left_class'] = $qfdb->get_css_value('qfdb_tpl_left'); 77 $qfdb->m['left_others'] = ''; 78 $qfdb->m['right_class'] = $qfdb->get_css_value('qfdb_tpl_right'); 79 $qfdb->m['right_others'] = ''; 80 $qfdb->m['validate'] = ''; 81 82 $qfdb->m['object_properties'] = count($qfdb->m) +2; 83 } // end func 84 85 86 static function obj_html(&$qfdb) 87 { 88 /////////////////////////////////////////////// 89 // generate elements -> format 90 // $checkbox[ $value ]['label'] 91 // $checkbox[ $value ]['others_element'] 92 // $checkbox[ $value ]['others_label'] 93 94 if (strlen($qfdb->m['nullval_value'])) { 95 $checkbox[ $qfdb->m['nullval_value'] ]['label'] = $qfdb->m['nullval_label']; 96 $checkbox[ $qfdb->m['nullval_value'] ]['others_element'] = $qfdb->m['nullval_others_element']; 97 $checkbox[ $qfdb->m['nullval_value'] ]['others_label'] = $qfdb->m['nullval_others_label']; 98 } 99 100 // query generate checkbox elements 101 if ($qfdb->m['element_query']) { 102 if ($result = $qfdb->sql_query($qfdb->m['element_query'])) { 103 if ($qfdb->sql_numrows() < 1) { 104 # @todo generate error message 105 die('A query não retornou nenhum resultado.'); 106 } 107 while ($row = $qfdb->sql_fetchrow($result)) { 108 if (!isset($row[0]) || !isset($row[1]) || isset($row[4])) { 109 $qfdb->add_critical_error( sprintf(QFDBOBJ_CHECKBOX_QUERY_WRONG_RETURN, $qfdb->m['element_query']) ); 110 } 111 $checkbox[ $row[0] ]['label'] = (isset($row[1])) ? ' '.$row[1] : ''; 112 $checkbox[ $row[0] ]['others_element'] = (isset($row[2])) ? ' '.$row[2] : ''; 113 $checkbox[ $row[0] ]['others_label'] = (isset($row[3])) ? ' '.$row[3] : ''; 114 } 115 116 } else { 117 $qfdb->add_critical_error( sprintf(QFDBOBJ_CHECKBOX_ELEMENT_QUERY_INVALID, $qfdb->m['element_query']) ); 118 return false; 119 } 120 } elseif ($qfdb->m['element_array']) { // array generate checkbox elements 121 $var = $qfdb->m['element_array']; 122 123 if (!isset($GLOBALS[ $var ])) { 124 # @todo generate error message 125 die('Var '.$var.' não existe para gerar o checkbox.'); 126 } 127 128 if (!is_array($GLOBALS[ $var ])) { 129 # @todo generate error message 130 die('Var '.$var.' não é um array - para gerar o checkbox.'); 131 } 132 133 $var = $GLOBALS[ $var ]; 134 135 if (is_array($var)) { 136 foreach ($var as $value => $label) { 137 $label = explode('|||', $label); 138 $checkbox[ $value ]['label'] = $label[0]; 139 $checkbox[ $value ]['others_element'] = (isset($label[1])) ? ' '.trim($label[1]) : ''; 140 $checkbox[ $value ]['others_label'] = (isset($label[2])) ? ' '.trim($label[2]) : ''; 141 } 142 } else { 143 $qfdb->add_critical_error( sprintf(QFDBOBJ_CHECKBOX_ELEMENT_ARRAY_INVALID, $qfdb->m['element_array'], gettype($var) ) ); 144 } 145 } else { 146 # @todo generate error message 147 die('defina uma query ou um array para o campo checkbox'); 148 } 149 150 151 /////////////////////////////////////////////// 152 // generate input type=checkbox element prototype 153 $checkbox_element = '<input type="checkbox" '; 154 if ($qfdb->m['form_field']) { 155 $checkbox_element .= 'name="'.$qfdb->m['form_field'].'[]"'; 156 } 157 if ($qfdb->m['disabled'] == 'true') { 158 $checkbox_element .= ' disabled="disabled"'; 159 } 160 if ($qfdb->m['element_class']) { 161 $checkbox_element .= ' class="'.$qfdb->m['element_class'].'"'; 162 } 163 if ($qfdb->m['element_others']) { 164 $checkbox_element .= ' '.$qfdb->m['element_others']; 165 } 166 167 /////////////////////////////////////////////// 168 // generate label of input type=checkbox element prototype 169 $checkbox_label = '<label'; 170 if ($qfdb->m['element_label_class']) { 171 $checkbox_label .= ' class="'.$qfdb->m['element_label_class'].'"'; 172 } 173 if ($qfdb->m['element_label_others']) { 174 $checkbox_label .= ' '.$qfdb->m['element_label_others']; 175 } 176 177 /////////////////////////////////////////////// 178 // generate container (TD) to all input type=checkbox element prototype 179 $checkbox_table = $checkbox_sltable = $checkbox_td = $checkbox_sltd = $checkbox_tr = $checkbox_sltr = ''; 180 if ($qfdb->m['cols'] > 1) { 181 $checkbox_table .= $qfdb->i(3)."<table width=\"100%\""; 182 if ($qfdb->m['cols_container_class']) { 183 $checkbox_table .= ' class="'.$qfdb->m['cols_container_class'].'"'; 184 } 185 if ($qfdb->m['cols_container_others']) { 186 $checkbox_table .= ' '.$qfdb->m['cols_container_others']; 187 } 188 if ($qfdb->m['title']) { 189 $checkbox_table .= ' title="'.$qfdb->m['title'].'"'; 190 } 191 $checkbox_table .=">\n"; 192 193 $checkbox_sltable .= $qfdb->i(3)."</table>"; 194 195 $checkbox_tr .= $qfdb->i(3)."<tr>\n"; 196 $checkbox_sltr .= $qfdb->i(3)."</tr>\n"; 197 198 $td_size = floor(100/$qfdb->m['cols']); 199 $checkbox_td .= $qfdb->i(4).'<td width="'.$td_size.'%"'; 200 if ($qfdb->m['cols_class']) { 201 $checkbox_td .= ' class="'.$qfdb->m['cols_class'].'"'; 202 } 203 if ($qfdb->m['cols_style']) { 204 $checkbox_td .= ' style="'.$qfdb->m['cols_style'].'"'; 205 } 206 if ($qfdb->m['cols_others']) { 207 $checkbox_td .= ' '.$qfdb->m['cols_others']; 208 } 209 $checkbox_td .= ">\n"; 210 $checkbox_sltd = $qfdb->i(4)."</td>\n"; 211 212 } else { 213 if ($qfdb->m['title']) { 214 $checkbox_table = $qfdb->i(3).'<div title="'.$qfdb->m['title'].'">'."\n"; 215 $checkbox_sltable = $qfdb->i(3).'</div>'; 216 } 217 } 218 219 /////////////////////////////////////////////// 220 // generate label of input type=checkbox element prototype 221 $elements_in_cols = ceil( count($checkbox)/$qfdb->m['cols'] ); 222 223 $object = "\n"; 224 225 if ($qfdb->m['beggin']) { 226 $object .= $qfdb->i(2)."{$qfdb->m['beggin']}\n\n"; 227 } 228 229 230 $object .= $checkbox_table . $checkbox_tr; 231 232 233 $j = $i = 0; 234 foreach ($checkbox as $key => $value) { 235 if (!$j) { 236 $object .= $checkbox_td; 237 } 238 $j++; $i++; 239 $key = $key; 240 $qfdb->m['value'] = ' '.$qfdb->m['value'].' '; 241 $checkbox_checked = (eregi(" $key ", $qfdb->m['value'])) ? ' checked="checked"': ''; 242 $object .= $qfdb->i(5)."$checkbox_element id=\"{$qfdb->m['id']}_{$key}\"{$value['others_element']} value=\"$key\" {$checkbox_checked} /> $checkbox_label for=\"{$qfdb->m['id']}_{$key}\" {$value['others_label']}>{$value['label']}</label>{$qfdb->m['cols_break']}\n"; 243 244 if ($j == $elements_in_cols) { 245 $object .= $checkbox_sltd; 246 $j = 0; 247 $next_sltd = ''; 248 continue; 249 } else { 250 $next_sltd = $checkbox_sltd; 251 } 252 } 253 $object .= $next_sltd . $checkbox_sltr . $checkbox_sltable; 254 255 if ($qfdb->m['end']) { 256 $object .= "\n\n".$qfdb->i(2)."{$qfdb->m['end']}"; 257 } 258 $object .= "\n"; 259 260 261 if ($qfdb->m['display'] == 'object_only') { 262 $qfdb->add_output('html', $object."\n"); 263 } elseif ($qfdb->m['display'] == 'one_cell') { 264 $label = qfdb_themes::themes_label($qfdb->m); 265 $qfdb->add_output('html', qfdb_themes::themes_container_one_cell( array('content'=>$object, 'others'=>$qfdb->m['colspan_others'], 'class'=>$qfdb->m['colspan_class'], $label) )); 266 } else { // $qfdb->m['display'] == 'two_cell' 267 $label = qfdb_themes::themes_label($qfdb->m); 268 $qfdb->add_output('html', qfdb_themes::themes_container_two_cells( array('label'=>$label, 'content'=>$object, 'label_others'=>$qfdb->m['left_others'], 'content_others'=>$qfdb->m['right_others'], 'label_class'=>$qfdb->m['left_class'], 'content_class'=>$qfdb->m['right_class']) )); 269 } 270 } // end func 271 272 273 static function obj_db(&$qfdb) 274 { 275 } // end func 276 277 278 static function obj_debug(&$qfdb) 279 { 280 } // end func 281 282 283 static function obj_fixed(&$qfdb) 284 { 285 // element_query AND element_array 286 if (!$qfdb->m['element_query'] && !$qfdb->m['element_array']) { 287 $qfdb->add_critical_error(QFDBOBJ_CHECKBOX_QUERYARRAY_NONE); 288 } 289 if ($qfdb->m['element_query'] && $qfdb->m['element_array']) { 290 $qfdb->add_critical_error(QFDBOBJ_CHECKBOX_QUERYARRAY_BOTH); 291 } 292 293 // nullval_label AND nullval_value 294 if ((strlen($qfdb->m['nullval_value']) && !strlen($qfdb->m['nullval_label'])) || 295 (!strlen($qfdb->m['nullval_value']) && strlen($qfdb->m['nullval_label']))) { 296 $qfdb->add_critical_error( sprintf(QFDBOBJ_CHECKBOX_NULLVALL_INCONSISTENT, $qfdb->m['nullval_value'], $qfdb->m['nullval_label']) ); 297 } 298 299 $qfdb->fix_radiocheck_cols(); 300 $qfdb->fix_disabled(); 301 $qfdb->fix_required(); 302 $qfdb->fix_display_3(); 303 304 // cols 305 $qfdb->m['cols'] = intval($qfdb->m['cols']); 306 if ($qfdb->m['cols']<1) { 307 $qfdb->m['cols'] = 1; 308 } 309 310 if (!$qfdb->m['id']) { 311 if ($qfdb->m['form_field']) { 312 $qfdb->m['id'] = $qfdb->m['form_field']; 313 } else { 314 $qfdb->m['id'] = 'q'.substr( md5(microtime()), 0, 10); 315 } 316 } 317 $qfdb->m['id'] = eregi_replace('[^a-zA-Z0-9_-]', '_', $qfdb->m['id']); 318 } // end func 319 320 } // end class
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri Nov 14 17:48:13 2008 | Cross-referenced by PHPXref 0.7 |