-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMetadataTypesController.php
170 lines (141 loc) · 5.23 KB
/
MetadataTypesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Kanboard\Plugin\MetaMagik\Controller;
use Kanboard\Controller\BaseController;
use Kanboard\Plugin\MetaMagik\Model\MetadataTypeModel;
/**
* Class MetadataTypes.
*
* @author Craig Crosby
*
*/
class MetadataTypesController extends BaseController
{
/**
* Action to list and save Metadata types in the config section.
*/
public function config()
{
$errors = [];
$values = [];
if ($this->request->isPost()) {
$values = $this->request->getValues();
$validation_errors = $this->validateValues($values);
if (!$validation_errors) {
$values['human_name'] = $this->fixHumanName($values['human_name']);
$machine_name = $this->createMachineName($values['human_name']);
$beauty_name = $this->beautyName($values['human_name']);
$values['machine_name'] = $machine_name;
$values['beauty_name'] = $beauty_name;
$type_id = $this->db->table(MetadataTypeModel::TABLE)->persist($values);
if ($type_id) {
$this->flash->success(t('Metadata type created successfully.'));
} else {
$this->flash->failure(t('Error saving the metadata type. Retry.'));
}
} else {
$errors = $validation_errors;
$this->flash->failure(t('There are errors in your submission.'));
}
}
$metadataTypes = $this->metadataTypeModel->getAll();
$this->response->html($this->helper->layout->config('MetaMagik:config/metadata_types', [
'values' => $values,
'errors' => $errors,
'types' => $metadataTypes,
'title' => t('Settings').' > '.t('Custom Fields'),
]));
}
/**
* Validate form input values.
*
* @param array $values The data from the form
*
* @return array|bool
*/
private function validateValues($values)
{
$errors = [];
if (strpos($values['human_name'], ' ')) {
$errors['human_name'] = [t('Please, do not use spaces.')];
}
if (strpos($values['human_name'], '.')) {
$errors['human_name'] = [t('Please, do not use periods.')];
}
if ($values['human_name'] == '') {
$errors['human_name'] = [t('This cannot be empty.')];
}
if ($values['data_type'] == '') {
$errors['data_type'] = [t('You need to select an option.')];
}
if ($values['attached_to'] == '') {
$errors['attached_to'] = [t('You need to select an option.')];
}
return empty($errors) ? false : $errors;
}
/**
* Create a machine name of the metadata type
* from the friendly name inserted by the user.
*
* @param string $human_name The human name of the metadata type
*
* @return string
*/
private function createMachineName($human_name = '')
{
$machine_name = strtolower($human_name);
// Remove special characters
$machine_name = preg_replace('/[^a-z0-9_\s-]/', '', $machine_name);
// Cleanup multiple dashes or whitespaces
$machine_name = preg_replace('/[\s-]+/', ' ', $machine_name);
// Replace whitespaces with underscores
$machine_name = preg_replace('/[\s]/', '_', $machine_name);
return $machine_name;
}
/**
* Create a beautiful name of the metadata type
* from the human name inserted by the user.
*
* @param string $human_name The human name of the metadata type
*
* @return string
*/
private function beautyName($human_name = '')
{
// Replace underscores with whitespaces
$beauty_name = preg_replace('/_/', ' ', $human_name);
return $beauty_name;
}
private function fixHumanName($human_name = '')
{
// Remove special characters
$human_name = preg_replace('/[^A-Za-z0-9_\s-]/', '', $human_name);
// Cleanup multiple dashes or whitespaces
$human_name = preg_replace('/[\s-]+/', ' ', $human_name);
// Replace whitespaces with underscores
$human_name = preg_replace('/[\s]/', '_', $human_name);
return $human_name;
}
public function confirmTask()
{
$key = $this->request->getStringParam('key');
$this->response->html($this->template->render('metaMagik:config/remove', [
'key' => $key,
]));
}
public function removeTask()
{
$key = $this->request->getStringParam('key');
$this->metadataTypeModel->remove($key);
return $this->response->redirect($this->helper->url->to('MetadataTypesController', 'config', ['plugin' => 'metaMagik']));
}
public function movePosition()
{
$values = $this->request->getJson();
if (! empty($values) && $_SESSION['user']['role'] == 'app-admin') {
$result = $this->metadataTypeModel->changePosition($values['id'], $values['position'], $values['columnnumber']);
$this->response->json(array('result' => $result));
} else {
throw new AccessForbiddenException();
}
}
}