KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
form.php
Ir a la documentación de este archivo.
1 <?php
27 class Form
28 {
35  protected static $_radios = array();
36 
37 
44  protected static $_multipart = FALSE;
45 
57  public static function getFieldData($field, $value = null, $filter = true)
58  {
59  // Obtiene considerando el patrón de formato form.field
60  $formField = explode('.', $field, 2);
61 
62  // Formato modelo.campo
63  if(isset($formField[1])) {
64  // Id de campo
65  $id = "{$formField[0]}_{$formField[1]}";
66  // Nombre de campo
67  $name = "{$formField[0]}[{$formField[1]}]";
68 
69  // Verifica en $_POST
70  if(isset($_POST[$formField[0]][$formField[1]])) {
71  $value = $_POST[$formField[0]][$formField[1]];
72  } elseif($value === null) {
73  // Autocarga de datos
74  $form = View::getVar($formField[0]);
75  if(is_array($form) && isset($form[$formField[1]])) {
76  $value = $form[$formField[1]];
77  } elseif(is_object($form) && isset($form->$formField[1])) {
78  $value = $form->{$formField[1]};
79  }
80  }
81  } else {
82  // Asignacion de Id y Nombre de campo
83  $id = $name = $field;
84 
85  // Verifica en $_POST
86  if(isset($_POST[$field])) {
87  $value = $_POST[$field];
88  } elseif($value === null) {
89  // Autocarga de datos
90  $value = View::getVar($field);
91  }
92  }
93 
94  // Filtrar caracteres especiales
95  if ($value !== null && $filter) {
96  $value = htmlspecialchars($value, ENT_COMPAT, APP_CHARSET);
97  }
98 
99  // Devuelve los datos
100  return array($id, $name, $value);
101  }
102 
114  public static function getFieldDataCheck($field, $checkValue, $checked = null)
115  {
116  // Obtiene considerando el patrón de formato form.field
117  $formField = explode('.', $field, 2);
118 
119  // Formato modelo.campo
120  if(isset($formField[1])) {
121  // Id de campo
122  $id = "{$formField[0]}_{$formField[1]}";
123  // Nombre de campo
124  $name = "{$formField[0]}[{$formField[1]}]";
125 
126  // Verifica en $_POST
127  if(isset($_POST[$formField[0]][$formField[1]])) {
128  $checked = $_POST[$formField[0]][$formField[1]] == $checkValue;
129  } elseif($checked === null) {
130  // Autocarga de datos
131  $form = View::getVar($formField[0]);
132  if(is_array($form)) {
133  $checked = isset($form[$formField[1]]) && $form[$formField[1]] == $checkValue;
134  } elseif(is_object($form)) {
135  $checked = isset($form->$formField[1]) && $form->$formField[1] == $checkValue;
136  }
137  }
138  } else {
139  // Asignacion de Id y Nombre de campo
140  $id = $name = $field;
141 
142  // Verifica en $_POST
143  if(isset($_POST[$field])) {
144  $checked = $_POST[$field] == $checkValue;
145  } elseif($checked === null) {
146  // Autocarga de datos
147  $checked = View::getVar($field) == $checkValue;
148  }
149  }
150 
151  // Devuelve los datos
152  return array($id, $name, $checked);
153  }
154 
162  public static function getFieldValue($field, $filter = true)
163  {
164  // Obtiene considerando el patrón de formato form.field
165  $formField = explode('.', $field, 2);
166 
167  $value = null;
168 
169  // Formato modelo.campo
170  if(isset($formField[1])) {
171  // Verifica en $_POST
172  if(isset($_POST[$formField[0]][$formField[1]])) {
173  $value = $_POST[$formField[0]][$formField[1]];
174  } else {
175  // Autocarga de datos
176  $form = View::getVar($formField[0]);
177  if(is_array($form) && isset($form[$formField[1]])) {
178  $value = $form[$formField[1]];
179  } elseif(is_object($form) && isset($form->$formField[1])) {
180  $value = $form->{$formField[1]};
181  }
182  }
183  } else {
184  // Verifica en $_POST
185  if(isset($_POST[$field])) {
186  $value = $_POST[$field];
187  } else {
188  // Autocarga de datos
189  $value = View::getVar($field);
190  }
191  }
192 
193  // Filtrar caracteres especiales
194  if ($value !== null && $filter) {
195  return htmlspecialchars($value, ENT_COMPAT, APP_CHARSET);
196  }
197 
198  // Devuelve valor
199  return $value;
200  }
201 
211  public static function input($type, $field,$attrs = NULL, $value=NULL)
212  {
213 
214  if (is_array($attrs)) {
215  $attrs = Tag::getAttrs($attrs);
216  }
217  // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope
218  list($id, $name, $value) = self::getFieldData($field, $value);
219  return "<input id=\"$id\" name=\"$name\" type=\"$type\" value=\"$value\" $attrs/>";
220  }
221 
230  public static function open($action = NULL, $method = 'post', $attrs = NULL)
231  {
232  if (is_array($attrs)) {
233  $attrs = Tag::getAttrs($attrs);
234  }
235  if ($action) {
236  $action = PUBLIC_PATH . $action;
237  } else {
238  $action = PUBLIC_PATH . ltrim(Router::get('route'), '/');
239  }
240  return "<form action=\"$action\" method=\"$method\" $attrs>";
241  }
242 
250  public static function openMultipart($action = NULL, $attrs = NULL)
251  {
252  self::$_multipart = TRUE;
253  if (is_array($attrs)) {
254  $attrs = Tag::getAttrs($attrs);
255  }
256  if ($action) {
257  $action = PUBLIC_PATH . $action;
258  } else {
259  $action = PUBLIC_PATH . ltrim(Router::get('route'), '/');
260  }
261  return "<form action=\"$action\" method=\"post\" enctype=\"multipart/form-data\" $attrs>";
262  }
263 
269  public static function close()
270  {
271  self::$_multipart = FALSE;
272  return '</form>';
273  }
274 
282  public static function submit($text, $attrs = NULL)
283  {
284  if (is_array($attrs)) {
285  $attrs = Tag::getAttrs($attrs);
286  }
287  return "<input type=\"submit\" value=\"$text\" $attrs />";
288  }
289 
297  public static function reset($text, $attrs = NULL)
298  {
299  if (is_array($attrs)) {
300  $attrs = Tag::getAttrs($attrs);
301  }
302  return "<input type=\"reset\" value=\"$text\" $attrs />";
303  }
304 
312  public static function button($text, $attrs = NULL)
313  {
314  if (is_array($attrs)) {
315  $attrs = Tag::getAttrs($attrs);
316  }
317  return "<input type=\"button\" value=\"$text\" $attrs />";
318  }
319 
328  public static function label($text, $field, $attrs = NULL)
329  {
330  if (is_array($attrs)) {
331  $attrs = Tag::getAttrs($attrs);
332  }
333  return "<label for=\"$field\" $attrs>$text</label>";
334  }
335 
344  public static function text($field, $attrs = NULL, $value = NULL)
345  {
346  return self::input('text', $field, $attrs, $value);
347  }
348 
358  public static function select($field, $data, $attrs = NULL, $value = NULL)
359  {
360  if (is_array($attrs)) {
361  $attrs = Tag::getAttrs($attrs);
362  }
363 
364  // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope
365  list($id, $name, $value) = self::getFieldData($field, $value);
366 
367  $options = '';
368  foreach ($data as $k => $v) {
369  $k = htmlspecialchars($k, ENT_COMPAT, APP_CHARSET);
370  $options .= "<option value=\"$k\"";
371  // Si es array $value para select multiple se seleccionan todos
372  if (is_array($value)) {
373  if (in_array($k, $value)) {
374  $options .= ' selected="selected"';
375  }
376  } else {
377  if ($k == $value) {
378  $options .= ' selected="selected"';
379  }
380  }
381  $options .= '>' . htmlspecialchars($v, ENT_COMPAT, APP_CHARSET) . '</option>';
382  }
383 
384  return "<select id=\"$id\" name=\"$name\" $attrs>$options</select>";
385  }
386 
396  public static function check($field, $checkValue, $attrs = NULL, $checked = NULL)
397  {
398  if (is_array($attrs)) {
399  $attrs = Tag::getAttrs($attrs);
400  }
401 
402  // Obtiene name y id para el campo y los carga en el scope
403  list($id, $name, $checked) = self::getFieldDataCheck($field, $checkValue, $checked);
404 
405  if ($checked) {
406  $checked = 'checked="checked"';
407  }
408 
409  return "<input id=\"$id\" name=\"$name\" type=\"checkbox\" value=\"$checkValue\" $attrs $checked/>";
410  }
411 
421  public static function radio($field, $radioValue, $attrs = NULL, $checked = NULL)
422  {
423  if (is_array($attrs)) {
424  $attrs = Tag::getAttrs($attrs);
425  }
426 
427  // Obtiene name y id para el campo y los carga en el scope
428  list($id, $name, $checked) = self::getFieldDataCheck($field, $radioValue, $checked);
429 
430  if ($checked) {
431  $checked = 'checked="checked"';
432  }
433 
434  // contador de campos radio
435  if (isset(self::$_radios[$field])) {
436  self::$_radios[$field]++;
437  } else {
438  self::$_radios[$field] = 0;
439  }
440  $id .= self::$_radios[$field];
441 
442  return "<input id=\"$id\" name=\"$name\" type=\"radio\" value=\"$radioValue\" $attrs $checked/>";
443  }
444 
452  public static function submitImage($img, $attrs = NULL)
453  {
454  if (is_array($attrs)) {
455  $attrs = Tag::getAttrs($attrs);
456  }
457  return "<input type=\"image\" src=\"" . PUBLIC_PATH . "img/$img\" $attrs/>";
458  }
459 
468  public static function hidden($field, $attrs = NULL, $value = NULL)
469  {
470  return self::input('hidden', $field, $attrs, $value);
471  }
472 
480  public static function pass($field, $attrs = NULL, $value = NULL)
481  {
482  return self::input('password',$field, $attrs, $value);
483  }
484 
496  public static function dbSelect($field, $show = NULL, $data = NULL, $blank = 'Seleccione', $attrs = NULL, $value = NULL)
497  {
498  if (is_array($attrs)) {
499  $attrs = Tag::getAttrs($attrs);
500  }
501 
502  // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope
503  list($id, $name, $value) = self::getFieldData($field, $value);
504 
505  // Si no se envía un campo por defecto, no se crea el tag option
506  if ($blank != NULL) {
507  $options = '<option value="">' . htmlspecialchars($blank, ENT_COMPAT, APP_CHARSET) . '</option>';
508  } else {
509  $options = '';
510  }
511 
512  //por defecto el modelo de modelo(_id)
513  if ($data === NULL) {
514  $model_asoc = explode('.', $field, 2);
515  $model_asoc = substr(end($model_asoc), 0, -3); //se elimina el _id
516  $model_asoc = Load::model($model_asoc);
517  $pk = $model_asoc->primary_key[0];
518 
519  if (!$show) {
520  //por defecto el primer campo no pk
521  $show = $model_asoc->non_primary[0];
522  }
523 
524  $data = $model_asoc->find("columns: $pk,$show", "order: $show asc"); //mejor usar array
525  } else {
526  $model_asoc = Load::model($data[0]);
527  $pk = $model_asoc->primary_key[0];
528 
529  // Verifica si existe el parámetro
530  if (isset($data[2])) {
531  $data = $model_asoc->$data[1]($data[2]);
532  } else {
533  $data = $model_asoc->$data[1]();
534  }
535  }
536 
537  foreach ($data as $p) {
538  $options .= "<option value=\"{$p->$pk}\"";
539  // Si es array $value para select multiple se seleccionan todos
540  if (is_array($value)) {
541  if (in_array($p->$pk, $value)) {
542  $options .= ' selected="selected"';
543  }
544  } else {
545  if ($p->$pk == $value) {
546  $options .= ' selected="selected"';
547  }
548  }
549  $options .= '>' . htmlspecialchars($p->$show, ENT_COMPAT, APP_CHARSET) . '</option>';
550  }
551 
552  return "<select id=\"$id\" name=\"$name\" $attrs>$options</select>" . PHP_EOL;
553  }
554 
562  public static function file($field, $attrs = NULL)
563  {
564  // aviso al programador
565  if (!self::$_multipart) {
566  Flash::error('Para poder subir ficheros, debe abrir el form con Form::openMultipart()');
567  }
568 
569  if (is_array($attrs)) {
570  $attrs = Tag::getAttrs($attrs);
571  }
572 
573  // Obtiene name y id, y los carga en el scope
574  list($id, $name, $value) = self::getFieldData($field, FALSE);
575  return "<input id=\"$id\" name=\"$name\" type=\"file\" $attrs/>";
576  }
577 
586  public static function textarea($field, $attrs = NULL, $value = NULL)
587  {
588  if (is_array($attrs)) {
589  $attrs = Tag::getAttrs($attrs);
590  }
591 
592  // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope
593  list($id, $name, $value) = self::getFieldData($field, $value);
594 
595  return "<textarea id=\"$id\" name=\"$name\" $attrs>$value</textarea>";
596  }
597 
606  public static function date($field, $attrs = NULL, $value = NULL)
607  {
608  return self::input('date',$field, $attrs, $value);
609  }
610 
620  public static function datepicker($field, $class = NULL, $attrs = NULL, $value = NULL)
621  {
622  if (is_array($attrs)) {
623  $attrs = Tag::getAttrs($attrs);
624  }
625 
626  // Obtiene name, id y value (solo para autoload) para el campo y los carga en el scope
627  list($id, $name, $value) = self::getFieldData($field, $value);
628  return "<input id=\"$id\" name=\"$name\" class=\"js-datepicker $class\" type=\"text\" value=\"$value\" $attrs/>";
629 
630  }
631 
640  public static function time($field, $attrs = NULL, $value = NULL)
641  {
642  return self::input('time',$field, $attrs, $value);
643  }
644 
653  public static function datetime($field, $attrs = NULL, $value = NULL)
654  {
655  return self::input('datetime',$field, $attrs, $value);
656  }
657 
666  public static function number($field, $attrs = NULL, $value = NULL)
667  {
668  return self::input('number',$field, $attrs, $value);
669  }
670 
671 
680  public static function url($field, $attrs = NULL, $value = NULL)
681  {
682  return self::input('url',$field, $attrs, $value);
683  }
684 
693  public static function email($field, $attrs = NULL, $value = NULL)
694  {
695  return self::input('email',$field, $attrs, $value);
696  }
697 }