Completed
Push — 1.0 ( f095f8...1b624d )
by joanhey
10s
created

ModelForm::create()   D

Complexity

Conditions 33
Paths 186

Size

Total Lines 74
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 33
eloc 50
c 2
b 0
f 0
nc 186
nop 2
dl 0
loc 74
rs 4.8726

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 - 2016 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   KumbiaPHP
23
 * @package    Helpers
24
 */
25
class ModelForm
26
{
27
28
    /**
29
     * Genera un form de un modelo (objeto) automáticamente
30
     *
31
     * @var object
32
     */
33
    public static function create($model, $action = NULL)
34
    {
35
36
        $model_name = Util::smallcase(get_class($model));
37
        if (!$action)
38
            $action = ltrim(Router::get('route'), '/');
39
40
        echo '<form action="', PUBLIC_PATH . $action, '" method="post" id="', $model_name, '" class="scaffold">' , PHP_EOL;
41
        $pk = $model->primary_key[0];
42
        echo '<input id="', $model_name, '_', $pk, '" name="', $model_name, '[', $pk, ']" class="id" value="', $model->$pk , '" type="hidden">' , PHP_EOL;
43
44
        $fields = array_diff($model->fields, $model->_at, $model->_in, $model->primary_key);
45
46
        foreach ($fields as $field) {
47
48
            $tipo = trim(preg_replace('/(\(.*\))/', '', $model->_data_type[$field])); //TODO: recoger tamaño y otros valores
49
            $alias = $model->get_alias($field);
50
            $formId = $model_name . '_' . $field;
51
            $formName = $model_name . '[' . $field . ']';
52
53
            if (in_array($field, $model->not_null)) {
54
                echo "<label for=\"$formId\" class=\"required\">$alias *</label>" , PHP_EOL;
55
            } else
56
                echo "<label for=\"$formId\">$alias</label>" , PHP_EOL;
57
58
            switch ($tipo) {
59
                case 'tinyint': case 'smallint': case 'mediumint':
60
                case 'integer': case 'int': case 'bigint':
61
                case 'float': case 'double': case 'precision':
62
                case 'real': case 'decimal': case 'numeric':
63
                case 'year': case 'day': case 'int unsigned': // Números
64
65
                    if (strripos($field, '_id', -3)) {
66
                        echo Form::dbSelect($model_name . '.' . $field, NULL, NULL, 'Seleccione', NULL, $model->$field);
67
                        break;
68
                    } else {
69
                        echo "<input id=\"$formId\" type=\"number\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
70
                        break;
71
                    }
72
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
                case 'datetime': case 'timestamp':
77
                    echo "<input id=\"$formId\" type=\"datetime\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
78
79
                    //echo '<script type="text/javascript" src="/javascript/kumbia/jscalendar/calendar.js"></script>
80
                    //<script type="text/javascript" src="/javascript/kumbia/jscalendar/calendar-setup.js"></script>
81
                    //<script type="text/javascript" src="/javascript/kumbia/jscalendar/calendar-es.js"></script>'.PHP_EOL;
82
                    //echo date_field_tag("$formId");
83
                    break;
84
85
                case 'enum': case 'set': case 'bool':
86
                    $enumList = explode(",", str_replace("'", "", substr($model->_data_type[$field], 5, (strlen($model->_data_type[$field])-6))));
87
                    echo "<select id=\"$formId\" class=\"select\" name=\"$formName\" >", PHP_EOL;
88
                    foreach($enumList as $value)
89
                        echo "<option value=\"{$value}\">$value</option>", PHP_EOL;
90
                    echo '</select>', PHP_EOL;
91
                    break;
92
93
                case 'text': case 'mediumtext': case 'longtext':
94
                case 'blob': case 'mediumblob': case 'longblob': // Usar textarea
95
                    echo "<textarea id=\"$formId\" name=\"$formName\">{$model->$field}</textarea>" , PHP_EOL;
96
                    break;
97
98
                default: //text,tinytext,varchar, char,etc se comprobara su tamaño
99
                    echo "<input id=\"$formId\" type=\"text\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL;
100
                //break;
101
            }
102
        }
103
        //echo radio_field_tag("actuacion", array("U" => "una", "D" => "dos", "N" => "Nada"), "value: N");
104
        echo '<input type="submit" value="Enviar" />' , PHP_EOL;
105
        echo '</form>' , PHP_EOL;
106
    }
107
108
}
109