Completed
Push — 1.0 ( a989c5...9ec0ba )
by joanhey
01:33 queued 14s
created

ModelForm::create()   C

Complexity

Conditions 33
Paths 186

Size

Total Lines 70
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 50
nc 186
nop 2
dl 0
loc 70
rs 5.0419
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @copyright  Copyright (c) 2005 - 2017 KumbiaPHP Team (http://www.kumbiaphp.com)
16
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
17
 */
18
19
/**
20
 * Helper para crear Formularios de un modelo automáticamente.
21
 *
22
 * @category   Helpers
23
 */
24
class ModelForm
25
{
26
    /**
27
     * Genera un form de un modelo (objeto) automáticamente.
28
     *
29
     * @param object $model
30
     * @param string $action
31
     */
32
    public static function create($model, $action = '')
33
    {
34
        $model_name = get_class($model);
35
        if (!$action) {
36
            $action = ltrim(Router::get('route'), '/');
37
        }
38
39
        echo '<form action="', PUBLIC_PATH.$action, '" method="post" id="', $model_name, '" class="scaffold">' , PHP_EOL;
40
        $pk = $model->primary_key[0];
41
        echo '<input id="', $model_name, '_', $pk, '" name="', $model_name, '[', $pk, ']" class="id" value="', $model->$pk , '" type="hidden">' , PHP_EOL;
42
43
        $fields = array_diff($model->fields, $model->_at, $model->_in, $model->primary_key);
44
45
        foreach ($fields as $field) {
46
            $tipo = trim(preg_replace('/(\(.*\))/', '', $model->_data_type[$field])); //TODO: recoger tamaño y otros valores
47
            $alias = $model->get_alias($field);
48
            $formId = $model_name.'_'.$field;
49
            $formName = $model_name.'['.$field.']';
50
51
            if (in_array($field, $model->not_null)) {
52
                echo "<label for=\"$formId\" class=\"required\">$alias *</label>" , PHP_EOL;
53
            } else {
54
                echo "<label for=\"$formId\">$alias</label>" , PHP_EOL;
55
            }
56
57
            switch ($tipo) {
58
                case 'tinyint': case 'smallint': case 'mediumint':
59
                case 'integer': case 'int': case 'bigint':
60
                case 'float': case 'double': case 'precision':
61
                case 'real': case 'decimal': case 'numeric':
62
                case 'year': case 'day': case 'int unsigned': // Números
63
64
                    if (strripos($field, '_id', -3)) {
65
                        echo Form::dbSelect($model_name.'.'.$field, null, null, 'Seleccione', null, $model->$field);
66
                        break;
67
                    } else {
68
                        echo "<input id=\"$formId\" type=\"number\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
69
                        break;
70
                    }
71
72
                    // no break
73
                case 'date': // Usar el js de datetime
74
                    echo "<input id=\"$formId\" type=\"date\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
75
                    break;
76
77
                case 'datetime': case 'timestamp':
78
                    echo "<input id=\"$formId\" type=\"datetime\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
79
                    break;
80
81
                case 'enum': case 'set': case 'bool':
82
                    $enumList = explode(',', str_replace("'", '', substr($model->_data_type[$field], 5, (strlen($model->_data_type[$field]) - 6))));
83
                    echo "<select id=\"$formId\" class=\"select\" name=\"$formName\" >", PHP_EOL;
84
                    foreach ($enumList as $value) {
85
                        echo "<option value=\"{$value}\">$value</option>", PHP_EOL;
86
                    }
87
                    echo '</select>', PHP_EOL;
88
                    break;
89
90
                case 'text': case 'mediumtext': case 'longtext': // Usar textarea
91
                case 'blob': case 'mediumblob': case 'longblob':
92
                    echo "<textarea id=\"$formId\" name=\"$formName\">{$model->$field}</textarea>" , PHP_EOL;
93
                    break;
94
95
                default: //text,tinytext,varchar, char,etc se comprobara su tamaño
96
                    echo "<input id=\"$formId\" type=\"text\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
97
            }
98
        }
99
        echo '<input type="submit" value="Enviar" />' , PHP_EOL;
100
        echo '</form>' , PHP_EOL;
101
    }
102
}
103