KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00022 class Form 00023 { 00030 protected static $_radios = array(); 00031 00038 protected static $_multipart = FALSE; 00039 00050 protected static function _getFieldData ($field, $autoload = TRUE) 00051 { 00052 // Obtiene considerando el patron de formato form.field 00053 $formField = explode('.', $field, 2); 00054 00055 // Si tiene el formato form.field 00056 if(isset($formField[1])) { 00057 // Id de campo 00058 $id = "{$formField[0]}_{$formField[1]}"; 00059 // Nombre de campo 00060 $name = "{$formField[0]}[{$formField[1]}]"; 00061 00062 // Sin autocarga 00063 if(!$autoload) { 00064 return array('id' => $id, 'name' => $name); 00065 } 00066 00067 // Obtiene el controller 00068 $controller = Dispatcher::get_controller(); 00069 // Valor por defecto 00070 $value = NULL; 00071 00072 // Si existe un valor cargado 00073 if(isset($controller->{$formField[0]})) { 00074 $form = $controller->{$formField[0]}; 00075 if (is_object($form) && isset($form->{$formField[1]})) { 00076 $value = $form->{$formField[1]}; 00077 } elseif (is_array($form) && isset($form[$formField[1]])) { 00078 $value = $form[$formField[1]]; 00079 } 00080 } elseif(isset($_POST[$formField[0]][$formField[1]])) { 00081 $value = $_POST[$formField[0]][$formField[1]]; 00082 } 00083 } else { // Formato de campo comun 00084 // Sin autocarga 00085 if(!$autoload) { 00086 return array('id' => $formField[0], 'name' => $formField[0]); 00087 } 00088 00089 // Nombre de campo y id 00090 $id = $name = $formField[0]; 00091 // Obtiene el controller 00092 $controller = Dispatcher::get_controller(); 00093 // Valor por defecto 00094 $value = NULL; 00095 00096 // Si existe un valor cargado 00097 if(isset($controller->$name)) { 00098 $value = $controller->$name; 00099 } elseif(isset($_POST[$name])) { 00100 $value = $_POST[$name]; 00101 } 00102 } 00103 00104 // Filtrar caracteres especiales 00105 if($value) { 00106 $value = htmlspecialchars($value, ENT_COMPAT, APP_CHARSET); 00107 } 00108 00109 return array('id' => $id, 'name' => $name, 'value' => $value); 00110 } 00111 00118 public static function getFieldValue($field) 00119 { 00120 // Obtiene considerando el patron de formato form.field 00121 $formField = explode('.', $field, 2); 00122 00123 // Obtiene el controller 00124 $controller = Dispatcher::get_controller(); 00125 00126 // Valor por defecto 00127 $value = NULL; 00128 00129 // Si tiene el formato form.field 00130 if(isset($formField[1])) { 00131 00132 // Si existe un valor cargado 00133 if(isset($controller->{$formField[0]})) { 00134 $form = $controller->{$formField[0]}; 00135 if (is_object($form) && isset($form->{$formField[1]})) { 00136 $value = $form->{$formField[1]}; 00137 } elseif (is_array($form) && isset($form[$formField[1]])) { 00138 $value = $form[$formField[1]]; 00139 } 00140 } elseif(isset($_POST[$formField[0]][$formField[1]])) { 00141 $value = $_POST[$formField[0]][$formField[1]]; 00142 } 00143 00144 } else { // Formato de campo comun 00145 00146 // Si existe un valor cargado 00147 if(isset($controller->$field)) { 00148 $value = $controller->$field; 00149 } elseif(isset($_POST[$field])) { 00150 $value = $_POST[$field]; 00151 } 00152 00153 } 00154 00155 // Retorna el valor de campo 00156 return $value; 00157 } 00158 00166 public static function input ($attrs = NULL, $content = NULL) 00167 { 00168 if(is_array($attrs)) { 00169 $attrs = Tag::getAttrs($attrs); 00170 } 00171 if (is_null($content)) { 00172 return "<input $attrs/>"; 00173 } 00174 return "<input $attrs>$content</input>"; 00175 } 00176 00185 public static function open ($action = NULL, $method = 'post', $attrs = NULL) 00186 { 00187 if (is_array($attrs)) { 00188 $attrs = Tag::getAttrs($attrs); 00189 } 00190 if ($action) { 00191 $action = PUBLIC_PATH . $action; 00192 } else { 00193 $action = PUBLIC_PATH . ltrim(Router::get('route'), '/'); 00194 } 00195 return "<form action=\"$action\" method=\"$method\" $attrs>"; 00196 } 00197 00205 public static function openMultipart ($action = NULL, $attrs = NULL) 00206 { 00207 self::$_multipart = TRUE; 00208 if (is_array($attrs)) { 00209 $attrs = Tag::getAttrs($attrs); 00210 } 00211 if ($action) { 00212 $action = PUBLIC_PATH . $action; 00213 } else { 00214 $action = PUBLIC_PATH . substr(Router::get('route'), 1); 00215 } 00216 return "<form action=\"$action\" method=\"post\" enctype=\"multipart/form-data\" $attrs>"; 00217 } 00218 00224 public static function close () 00225 { 00226 self::$_multipart = FALSE; 00227 return '</form>'; 00228 } 00229 00237 public static function submit ($text, $attrs = NULL) 00238 { 00239 if (is_array($attrs)) { 00240 $attrs = Tag::getAttrs($attrs); 00241 } 00242 return "<input type=\"submit\" value=\"$text\" $attrs />"; 00243 } 00244 00252 public static function reset ($text, $attrs = NULL) 00253 { 00254 if (is_array($attrs)) { 00255 $attrs = Tag::getAttrs($attrs); 00256 } 00257 return "<input type=\"reset\" value=\"$text\" $attrs />"; 00258 } 00259 00267 public static function button ($text, $attrs = NULL) 00268 { 00269 if (is_array($attrs)) { 00270 $attrs = Tag::getAttrs($attrs); 00271 } 00272 return "<input type=\"button\" value=\"$text\" $attrs />"; 00273 } 00274 00283 public static function label($text, $field, $attrs = NULL) 00284 { 00285 if (is_array($attrs)) { 00286 $attrs = Tag::getAttrs($attrs); 00287 } 00288 return "<label for=\"$field\" $attrs>$text</label>"; 00289 } 00290 00299 public static function text($field, $attrs = NULL, $value = NULL) 00300 { 00301 if(is_array($attrs)) { 00302 $attrs = Tag::getAttrs($attrs); 00303 } 00304 00305 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00306 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00307 00308 return "<input id=\"$id\" name=\"$name\" type=\"text\" value=\"$value\" $attrs/>"; 00309 } 00310 00320 public static function select($field, $data, $attrs = NULL, $value = NULL) 00321 { 00322 if(is_array($attrs)){ 00323 $attrs = Tag::getAttrs($attrs); 00324 } 00325 00326 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00327 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00328 00329 $options = ''; 00330 foreach($data as $k => $v) { 00331 $k = htmlspecialchars($k, ENT_COMPAT, APP_CHARSET); 00332 $options .= "<option value=\"$k\""; 00333 if($k == $value) { 00334 $options .= ' selected="selected"'; 00335 } 00336 $options .= '>' . htmlspecialchars($v, ENT_COMPAT, APP_CHARSET) . '</option>'; 00337 } 00338 00339 return "<select id=\"$id\" name=\"$name\" $attrs>$options</select>"; 00340 } 00341 00351 public static function check($field, $checkValue, $attrs = NULL, $checked = NULL) 00352 { 00353 if(is_array($attrs)) { 00354 $attrs = Tag::getAttrs($attrs); 00355 } 00356 00357 // Obtiene name y id para el campo y los carga en el scope 00358 extract(self::_getFieldData($field, $checked === NULL), EXTR_OVERWRITE); 00359 00360 if($checked || ($checked === NULL && $checkValue == $value)) { 00361 $checked = 'checked="checked"'; 00362 } 00363 00364 return "<input id=\"$id\" name=\"$name\" type=\"checkbox\" value=\"$checkValue\" $attrs $checked/>"; 00365 } 00366 00376 public static function radio ($field, $radioValue, $attrs = NULL, $checked = NULL) 00377 { 00378 if(is_array($attrs)){ 00379 $attrs = Tag::getAttrs($attrs); 00380 } 00381 00382 // Obtiene name y id para el campo y los carga en el scope 00383 extract(self::_getFieldData($field, $checked === NULL), EXTR_OVERWRITE); 00384 00385 if($checked || ($checked === NULL && $radioValue == $value)) { 00386 $checked = 'checked="checked"'; 00387 } 00388 00389 // contador de campos radio 00390 if(isset(self::$_radios[$field])) { 00391 self::$_radios[$field]++; 00392 } else { 00393 self::$_radios[$field] = 0; 00394 } 00395 $id .= self::$_radios[$field]; 00396 00397 return "<input id=\"$id\" name=\"$name\" type=\"radio\" value=\"$radioValue\" $attrs $checked/>"; 00398 } 00399 00407 public static function submitImage ($img, $attrs = NULL) 00408 { 00409 if (is_array($attrs)) { 00410 $attrs = Tag::getAttrs($attrs); 00411 } 00412 return "<input type=\"image\" src=\"".PUBLIC_PATH."img/$img\" $attrs/>"; 00413 } 00414 00423 public static function hidden ($field, $attrs = NULL, $value = NULL) 00424 { 00425 if(is_array($attrs)) { 00426 $attrs = Tag::getAttrs($attrs); 00427 } 00428 00429 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00430 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00431 00432 return "<input id=\"$id\" name=\"$name\" type=\"hidden\" value=\"$value\" $attrs/>"; 00433 } 00434 00442 public static function pass($field, $attrs = NULL, $value = NULL) 00443 { 00444 if(is_array($attrs)) { 00445 $attrs = Tag::getAttrs($attrs); 00446 } 00447 00448 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00449 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00450 00451 return "<input id=\"$id\" name=\"$name\" type=\"password\" value=\"$value\" $attrs/>"; 00452 } 00453 00465 public static function dbSelect($field, $show = NULL, $data = NULL, $blank = 'Seleccione', $attrs = NULL, $value = NULL) 00466 { 00467 if(is_array($attrs)) { 00468 $attrs = Tag::getAttrs($attrs); 00469 } 00470 00471 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00472 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00473 00474 // Si no se envia un campo por defecto, no se crea el tag option 00475 if($blank != NULL){ 00476 $options = '<option value="">' . htmlspecialchars($blank, ENT_COMPAT, APP_CHARSET) . '</option>'; 00477 }else{ 00478 $options = ''; 00479 } 00480 00481 //por defecto el modelo de modelo(_id) 00482 if($data === NULL){ 00483 $model_asoc = explode('.', $field, 2); 00484 $model_asoc = substr(end($model_asoc), 0, -3);//se elimina el _id 00485 $model_asoc = Load::model($model_asoc); 00486 $pk = $model_asoc->primary_key[0]; 00487 00488 if(! $show){ 00489 //por defecto el primer campo no pk 00490 $show = $model_asoc->non_primary[0]; 00491 } 00492 00493 $data = $model_asoc->find("columns: $pk,$show","order: $show asc");//mejor usar array 00494 } else { 00495 $model_asoc = Load::model($data[0]); 00496 $pk = $model_asoc->primary_key[0]; 00497 00498 // Verifica si existe el argumento 00499 if(isset($data[2])) { 00500 $data = $model_asoc->$data[1]($data[2]); 00501 } else { 00502 $data = $model_asoc->$data[1](); 00503 } 00504 } 00505 00506 foreach($data as $p) { 00507 $options .= "<option value=\"{$p->$pk}\""; 00508 // Si los valores seleccionados pertenecen a un text multiple se seleccionan todos 00509 if(is_array($value)){ 00510 foreach($value as $t){ 00511 if($p->$pk == $t){ 00512 $options .= ' selected="selected"'; 00513 } 00514 } 00515 }else{ 00516 if($p->$pk == $value) { 00517 $options .= ' selected="selected"'; 00518 } 00519 } 00520 $options .= '>' . htmlspecialchars($p->$show, ENT_COMPAT, APP_CHARSET) . '</option>'; 00521 } 00522 00523 return "<select id=\"$id\" name=\"$name\" $attrs>$options</select>".PHP_EOL; 00524 } 00525 00533 public static function file($field, $attrs = NULL) 00534 { 00535 // aviso al programador 00536 if(!self::$_multipart){ 00537 Flash::error('Para poder subir ficheros, debe abrir el form con Form::openMultipart()'); 00538 } 00539 00540 if(is_array($attrs)) { 00541 $attrs = Tag::getAttrs($attrs); 00542 } 00543 00544 // Obtiene name y id, y los carga en el scope 00545 extract(self::_getFieldData($field, false), EXTR_OVERWRITE); 00546 00547 return "<input id=\"$id\" name=\"$name\" type=\"file\" $attrs/>"; 00548 } 00549 00558 public static function textarea($field, $attrs = NULL, $value = NULL) 00559 { 00560 if(is_array($attrs)) { 00561 $attrs = Tag::getAttrs($attrs); 00562 } 00563 00564 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00565 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00566 00567 return "<textarea id=\"$id\" name=\"$name\" $attrs>$value</textarea>"; 00568 } 00569 00579 public static function date($field, $class = NULL, $attrs = NULL, $value = NULL) 00580 { 00581 if(is_array($attrs)) { 00582 $attrs = Tag::getAttrs($attrs); 00583 } 00584 00585 // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope 00586 extract(self::_getFieldData($field, $value === NULL), EXTR_OVERWRITE); 00587 00588 return "<input id=\"$id\" name=\"$name\" class=\"js-datepicker $class\" type=\"date\" value=\"$value\" $attrs/>"; 00589 } 00590 }