|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Mysql\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Application\UI\Form; |
|
6
|
|
|
use Nette\Forms\Controls\Checkbox; |
|
7
|
|
|
use Nette\Forms\Controls\TextArea; |
|
8
|
|
|
use Nette\Utils\ArrayHash; |
|
9
|
|
|
use PDO; |
|
10
|
|
|
use UniMan\Core\Forms\ItemForm\ItemFormInterface; |
|
11
|
|
|
use UniMan\Drivers\MySql\MySqlDataManager; |
|
12
|
|
|
|
|
13
|
|
|
class MySqlItemForm implements ItemFormInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $pdo; |
|
16
|
|
|
|
|
17
|
|
|
private $dataManager; |
|
18
|
|
|
|
|
19
|
|
|
private $type; |
|
20
|
|
|
|
|
21
|
|
|
private $table; |
|
22
|
|
|
|
|
23
|
|
|
private $item; |
|
24
|
|
|
|
|
25
|
|
|
private $columns = []; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(PDO $pdo, MySqlDataManager $dataManager, $type, $table, $item) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->pdo = $pdo; |
|
30
|
|
|
$this->dataManager = $dataManager; |
|
31
|
|
|
$this->type = $type; |
|
32
|
|
|
$this->table = $table; |
|
33
|
|
|
$this->item = $item; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addFieldsToForm(Form $form) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->columns = $this->dataManager->getColumns($this->type, $this->table); |
|
39
|
|
|
foreach ($this->columns as $column => $definition) { |
|
40
|
|
|
if (isset($definition['key_info']['REFERENCED_TABLE_NAME'])) { |
|
41
|
|
|
$items = []; |
|
42
|
|
|
foreach ($this->dataManager->items($this->type, $definition['key_info']['REFERENCED_TABLE_NAME'], 1, PHP_INT_MAX) as $item) { |
|
43
|
|
|
$items[$item[$definition['key_info']['REFERENCED_COLUMN_NAME']]] = implode(' ', $item); |
|
44
|
|
|
} |
|
45
|
|
|
$field = $form->addSelect($column, $column, $items); |
|
46
|
|
|
$field->setAttribute('class', 'js-select2'); |
|
|
|
|
|
|
47
|
|
|
$field->setPrompt(''); |
|
48
|
|
|
} elseif ($definition['Type'] === 'datetime') { |
|
49
|
|
|
$field = $form->addDateTimePicker($column, $column); |
|
50
|
|
|
} elseif ($definition['Type'] === 'date') { |
|
51
|
|
|
$field = $form->addDatePicker($column, $column); |
|
52
|
|
|
} elseif (strpos($definition['Type'], 'text') !== false) { |
|
53
|
|
|
$field = $form->addTextArea($column, $column, null, 7); |
|
54
|
|
|
} elseif ($definition['Type'] === 'tinyint(1)') { |
|
55
|
|
|
$field = $form->addCheckbox($column, $column); |
|
56
|
|
|
} else { |
|
57
|
|
|
$field = $form->addText($column, $column); |
|
58
|
|
|
if (strpos($definition['Type'], 'int') !== false) { |
|
59
|
|
|
$field->addCondition(Form::FILLED) |
|
60
|
|
|
->addRule(Form::INTEGER, 'mysql.item_form.field.integer'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($definition['Extra'] == 'auto_increment') { |
|
65
|
|
|
$field->setAttribute('placeholder', 'autoincrement'); |
|
66
|
|
|
} elseif (!$field instanceof Checkbox && !$field instanceof TextArea && $definition['Null'] === 'NO') { |
|
67
|
|
|
$field->setRequired('mysql.item_form.field.required'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->item) { |
|
72
|
|
|
$item = $this->dataManager->loadItem($this->type, $this->table, $this->item); |
|
73
|
|
|
if ($item) { |
|
74
|
|
|
$form->setDefaults($item); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function submit(Form $form, ArrayHash $values) |
|
80
|
|
|
{ |
|
81
|
|
|
$values = (array)$values; |
|
82
|
|
|
$keys = array_map(function ($key) { |
|
83
|
|
|
return '`' . $key . '`'; |
|
84
|
|
|
}, array_keys($values)); |
|
85
|
|
|
$vals = array_map(function ($key) { |
|
86
|
|
|
return ':' . $key; |
|
87
|
|
|
}, array_keys($values)); |
|
88
|
|
|
if ($this->item) { |
|
89
|
|
|
$query = sprintf('UPDATE `%s` SET ', $this->table); |
|
90
|
|
|
$set = []; |
|
91
|
|
|
foreach ($values as $key => $value) { |
|
92
|
|
|
$set[] = '`' . $key . '` = :' . $key; |
|
93
|
|
|
} |
|
94
|
|
|
$query .= implode(', ', $set); |
|
95
|
|
|
$primaryColumns = $this->dataManager->getPrimaryColumns($this->type, $this->table); |
|
96
|
|
|
$query .= sprintf(' WHERE md5(concat(%s)) = "%s"', implode(', "|", ', $primaryColumns), $this->item); |
|
97
|
|
|
} else { |
|
98
|
|
|
$query = sprintf('INSERT INTO `%s` %s VALUES %s', $this->table, '(' . implode(', ', $keys) . ')', '(' . implode(', ', $vals) . ')'); |
|
99
|
|
|
} |
|
100
|
|
|
$statement = $this->pdo->prepare($query); |
|
101
|
|
|
foreach ($values as $key => $value) { |
|
102
|
|
|
$value = $value === '' && $this->columns[$key]['Null'] ? null : $value; |
|
103
|
|
|
$statement->bindValue(':' . $key, $value); |
|
104
|
|
|
} |
|
105
|
|
|
$ret = $statement->execute(); |
|
106
|
|
|
if (!$ret) { |
|
107
|
|
|
$form->addError($statement->errorInfo()[2]); |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
return $ret; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: