ModelForm   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 9.76
c 0
b 0
f 0
wmc 33
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
F create() 0 69 33
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   KumbiaPHP
11
 * @package    Helpers
12
 *
13
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Helper para crear Formularios de un modelo automáticamente.
19
 *
20
 * @category   KumbiaPHP
21
 * @package    Helpers
22
 */
23
class ModelForm
24
{
25
    /**
26
     * Genera un form de un modelo (objeto) automáticamente.
27
     *
28
     * @param object $model
29
     * @param string $action
30
     */
31
    public static function create($model, $action = '')
32
    {
33
        $model_name = get_class($model);
34
        if (!$action) {
35
            $action = ltrim(Router::get('route'), '/');
36
        }
37
38
        echo '<form action="', PUBLIC_PATH.$action, '" method="post" id="', $model_name, '" class="scaffold">' , PHP_EOL;
39
        $pk = $model->primary_key[0];
40
        echo '<input id="', $model_name, '_', $pk, '" name="', $model_name, '[', $pk, ']" class="id" value="', $model->$pk , '" type="hidden">' , PHP_EOL;
41
42
        $fields = array_diff($model->fields, $model->_at, $model->_in, $model->primary_key);
43
44
        foreach ($fields as $field) {
45
            $tipo = trim(preg_replace('/(\(.*\))/', '', $model->_data_type[$field])); //TODO: recoger tamaño y otros valores
46
            $alias = $model->get_alias($field);
47
            $formId = $model_name.'_'.$field;
48
            $formName = $model_name.'['.$field.']';
49
50
            if (in_array($field, $model->not_null)) {
51
                echo "<label for=\"$formId\" class=\"required\">$alias *</label>" , PHP_EOL;
52
            } else {
53
                echo "<label for=\"$formId\">$alias</label>" , PHP_EOL;
54
            }
55
56
            switch ($tipo) {
57
                case 'tinyint': case 'smallint': case 'mediumint':
58
                case 'integer': case 'int': case 'bigint':
59
                case 'float': case 'double': case 'precision':
60
                case 'real': case 'decimal': case 'numeric':
61
                case 'year': case 'day': case 'int unsigned': // Números
62
63
                    if (strripos($field, '_id', -3)) {
64
                        echo Form::dbSelect($model_name.'.'.$field, null, null, 'Seleccione', '', $model->$field);
65
                        break;
66
                    }
67
68
                    echo "<input id=\"$formId\" type=\"number\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
69
                    break;
70
71
                case 'date': // Usar el js de datetime
72
                    echo "<input id=\"$formId\" type=\"date\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
73
                    break;
74
75
                case 'datetime': case 'timestamp':
76
                    echo "<input id=\"$formId\" type=\"datetime\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
77
                    break;
78
79
                case 'enum': case 'set': case 'bool':
80
                    $enumList = explode(',', str_replace("'", '', substr($model->_data_type[$field], 5, (strlen($model->_data_type[$field]) - 6))));
81
                    echo "<select id=\"$formId\" class=\"select\" name=\"$formName\" >", PHP_EOL;
82
                    foreach ($enumList as $value) {
83
                        echo "<option value=\"{$value}\">$value</option>", PHP_EOL;
84
                    }
85
                    echo '</select>', PHP_EOL;
86
                    break;
87
88
                case 'text': case 'mediumtext': case 'longtext': // Usar textarea
89
                case 'blob': case 'mediumblob': case 'longblob':
90
                    echo "<textarea id=\"$formId\" name=\"$formName\">{$model->$field}</textarea>" , PHP_EOL;
91
                    break;
92
93
                default: //text,tinytext,varchar, char,etc se comprobara su tamaño
94
                    echo "<input id=\"$formId\" type=\"text\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
95
            }
96
        }
97
        echo '<input type="submit" value="Enviar" />' , PHP_EOL;
98
        echo '</form>' , PHP_EOL;
99
    }
100
}
101