Completed
Pull Request — master (#21)
by Michal
02:30
created

MySqlItemForm::addFieldsToForm()   C

Complexity

Conditions 12
Paths 57

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 30
cp 0
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 24
nc 57
nop 1
crap 156

How to fix   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
namespace UniMan\Drivers\Mysql\Forms;
4
5
use UniMan\Core\Forms\ItemForm\ItemFormInterface;
6
use UniMan\Drivers\MySql\MySqlDataManager;
7
use Nette\Application\UI\Form;
8
use Nette\Forms\Controls\Checkbox;
9
use Nette\Utils\ArrayHash;
10
use PDO;
11
12
class MySqlItemForm implements ItemFormInterface
13
{
14
    private $pdo;
15
16
    private $dataManager;
17
18
    private $type;
19
20
    private $table;
21
22
    private $item;
23
24
    private $columns = [];
25
26
    public function __construct(PDO $pdo, MySqlDataManager $dataManager, $type, $table, $item)
27
    {
28
        $this->pdo = $pdo;
29
        $this->dataManager = $dataManager;
30
        $this->type = $type;
31
        $this->table = $table;
32
        $this->item = $item;
33
    }
34
35
    public function addFieldsToForm(Form $form)
36
    {
37
        $this->columns = $this->dataManager->getColumns($this->type, $this->table);
38
        foreach ($this->columns as $column => $definition) {
39
            if ($definition['Type'] === 'datetime') {
40
                $field = $form->addDateTimePicker($column, $column);
41
            } elseif ($definition['Type'] === 'date') {
42
                $field = $form->addDatePicker($column, $column);
43
            } elseif (strpos($definition['Type'], 'text') !== false) {
44
                $field = $form->addTextArea($column, $column, null, 7);
45
            } elseif ($definition['Type'] === 'tinyint(1)') {
46
                $field = $form->addCheckbox($column, $column);
47
            } else {
48
                $field = $form->addText($column, $column);
49
                if (strpos($definition['Type'], 'int') !== false) {
50
                    $field->addCondition(Form::FILLED)
51
                        ->addRule(Form::INTEGER, 'mysql.item_form.field.integer');
52
                }
53
            }
54
55
            if ($definition['Extra'] == 'auto_increment') {
56
                $field->setAttribute('placeholder', 'autoincrement');
57
            } elseif (!$field instanceof Checkbox && $definition['Null'] === 'NO') {
58
                $field->setRequired('mysql.item_form.field.required');
59
            }
60
        }
61
62
        if ($this->item) {
63
            $item = $this->dataManager->loadItem($this->type, $this->table, $this->item);
64
            if ($item) {
65
                $form->setDefaults($item);
66
            }
67
        }
68
    }
69
70
    public function submit(Form $form, ArrayHash $values)
71
    {
72
        $values = (array)$values;
73
        $keys = array_map(function ($key) {
74
            return '`' . $key . '`';
75
        }, array_keys($values));
76
        $vals = array_map(function ($key) {
77
            return ':' . $key;
78
        }, array_keys($values));
79
        if ($this->item) {
80
            $query = sprintf('UPDATE `%s` SET ', $this->table);
81
            $set = [];
82
            foreach ($values as $key => $value) {
83
                $set[] = '`' . $key . '` = :' . $key;
84
            }
85
            $query .= implode(', ', $set);
86
            $primaryColumns = $this->dataManager->getPrimaryColumns($this->type, $this->table);
87
            $query .= sprintf(' WHERE md5(concat(%s)) = "%s"', implode(', "|", ', $primaryColumns), $this->item);
88
        } else {
89
            $query = sprintf('INSERT INTO `%s` %s VALUES %s', $this->table, '(' . implode(', ', $keys) . ')', '(' . implode(', ', $vals) . ')');
90
        }
91
        $statement = $this->pdo->prepare($query);
92
        foreach ($values as $key => $value) {
93
            $value = $value === '' && $this->columns[$key]['Null'] ? null : $value;
94
            $statement->bindValue(':' . $key, $value);
95
        }
96
        $ret = $statement->execute();
97
        if (!$ret) {
98
            $form->addError($statement->errorInfo()[2]);
99
            return;
100
        }
101
        return $ret;
102
    }
103
}
104