Total Complexity | 33 |
Total Lines | 228 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | class CommonDataSettings extends ModuleView |
||
16 | { |
||
17 | protected $label = 'Common Data'; |
||
18 | |||
19 | protected $form; |
||
20 | protected $parentId; |
||
21 | |||
22 | protected $buttons = []; |
||
23 | |||
24 | public static function access() |
||
25 | { |
||
26 | return Auth::user()->can('modify system settings'); |
||
27 | } |
||
28 | |||
29 | public function body() |
||
30 | { |
||
31 | ActionBar::addButton('back')->link(url('view/system')); |
||
32 | |||
33 | $this->parentId = $this->stickyGet('parentId')?: null; |
||
34 | |||
35 | $this->displayBreadCrumb(); |
||
36 | |||
37 | $this->displayGrid(); |
||
38 | } |
||
39 | |||
40 | public function displayBreadCrumb() |
||
41 | { |
||
42 | $breadcrumb = $this->add('BreadCrumb'); |
||
43 | |||
44 | $breadcrumb->addCrumb(__('Common Data Root'), '?parentId=0'); |
||
1 ignored issue
–
show
|
|||
45 | |||
46 | foreach (CommonData::ancestorsAndSelf($this->parentId) as $node) { |
||
47 | $label = $node->key; |
||
48 | $link = '?parentId=' . $node->id; |
||
49 | |||
50 | $breadcrumb->addCrumb($label, $link); |
||
51 | } |
||
52 | |||
53 | $breadcrumb->popTitle(); |
||
1 ignored issue
–
show
|
|||
54 | |||
55 | return $this; |
||
56 | } |
||
57 | |||
58 | public function displayGrid() |
||
59 | { |
||
60 | $this->grid = $this->add([ |
||
61 | 'CRUD', |
||
62 | 'itemCreate' => ActionBar::addButton('add'), |
||
63 | 'formCreate' => $this->getNodeForm(), |
||
64 | 'formUpdate' => $this->getNodeForm(), |
||
65 | 'fieldsCreate' => ['key', 'value'], |
||
66 | 'fieldsUpdate' => ['key', 'value'], |
||
67 | 'notifyDefault' => ['jsNotify', 'content' => __('Data is saved!'), 'color' => 'green'], |
||
68 | 'canUpdate' => false, //use custom update routine |
||
69 | 'canDelete' => false, //use custom delete routine |
||
70 | 'paginator' => false, |
||
71 | 'menu' => false, |
||
72 | ]); |
||
73 | |||
74 | $this->grid->table->addClass('selectable')->addStyle('cursor', 'pointer'); |
||
75 | |||
76 | $this->grid->setModel($model = $this->getModel()); |
||
77 | |||
78 | if ($model->action('count')->getOne() ) { |
||
79 | $this->grid->on('click', 'td', new jsExpression( |
||
80 | 'document.location=\'?parentId=\'+[]', |
||
81 | [(new jQuery())->closest('tr')->data('id')] |
||
1 ignored issue
–
show
|
|||
82 | )); |
||
83 | } |
||
84 | |||
85 | $this->grid->addDragHandler()->onReorder(function ($order) { |
||
1 ignored issue
–
show
|
|||
86 | $result = true; |
||
87 | foreach ($this->nodes()->get() as $node) { |
||
88 | $node->position = array_search($node->id, $order); |
||
89 | |||
90 | $result &= $node->save(); |
||
91 | } |
||
92 | |||
93 | $notifier = $result? $this->notify(__('Items reordered!')): $this->notifyError(__('Error saving order!')); |
||
94 | |||
95 | return $this->grid->jsSave($notifier); |
||
96 | }); |
||
97 | |||
98 | $this->addContolButton('update', $this->getUpdateModal(), 'edit'); |
||
99 | |||
100 | $this->addContolButton('delete', function ($jschain, $id) { |
||
101 | CommonData::find($id)->delete(); |
||
102 | |||
103 | return $jschain->closest('tr')->transition('fade left'); |
||
104 | }, 'red trash'); |
||
105 | |||
106 | $this->grid->addColumn('actions', ['Multiformat', function($row, $column) { |
||
107 | return [['Template', $this->getControlButtonsHtml($row)]]; |
||
108 | }, 'caption' => ' ']); |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | public function getControlButtonsHtml($row) |
||
114 | { |
||
115 | if ($row['readonly']) return ''; |
||
116 | |||
117 | $ret = ''; |
||
118 | foreach ($this->buttons as $button) { |
||
119 | $ret .= $button->getHtml(); |
||
120 | } |
||
121 | |||
122 | return $ret;; |
||
123 | } |
||
124 | |||
125 | public function getUpdateModal() |
||
161 | } |
||
162 | |||
163 | public function addContolButton($name, $callback, $icon = null) |
||
164 | { |
||
165 | $class = "button_$name"; |
||
166 | |||
167 | $this->grid->on('click', ".$class", $callback, [$this->grid->table->jsRow()->data('id')]); |
||
168 | |||
169 | $this->buttons[$name] = new \atk4\ui\Button($icon? compact('icon'): $name); |
||
170 | |||
171 | $this->buttons[$name]->app = $this->app; |
||
172 | |||
173 | $this->buttons[$name]->addClass("compact $class"); |
||
174 | } |
||
175 | |||
176 | public function nodes() |
||
177 | { |
||
178 | return CommonData::where('parent_id', $this->parentId); |
||
179 | } |
||
180 | |||
181 | public function getModel() |
||
182 | { |
||
183 | $rows = []; |
||
184 | foreach ($this->nodes()->orderBy('position')->get() as $node) { |
||
185 | $rows[] = [ |
||
186 | 'id' => $node->id, |
||
187 | 'position' => $node->position, |
||
188 | 'key' => $node->key, |
||
189 | 'value' => $node->value, |
||
190 | 'readonly' => $node->readonly |
||
191 | ]; |
||
192 | } |
||
193 | |||
194 | $rowsEmpty = []; |
||
195 | |||
196 | $model = new \atk4\data\Model($rows? new \atk4\data\Persistence_Static($rows): new \atk4\data\Persistence_Array($rowsEmpty)); |
||
197 | |||
198 | $captions = [ |
||
199 | 'position' => __('Position'), |
||
200 | 'key' => __('Key'), |
||
201 | 'value' => __('Value'), |
||
202 | 'readonly' => __('Read Only') |
||
203 | ]; |
||
204 | |||
205 | foreach ($captions as $key => $caption) { |
||
206 | $field = $rows? $model->hasField($key): $model->addField($key); |
||
207 | |||
208 | if ($key === 'readonly') { |
||
209 | $field->setDefaults(['ui' => ['visible' => false]]); |
||
210 | |||
211 | continue; |
||
212 | } |
||
213 | |||
214 | $field->setDefaults(compact('caption')); |
||
215 | } |
||
216 | |||
217 | return $model; |
||
218 | } |
||
219 | |||
220 | public function getNodeForm() |
||
243 | } |
||
244 | } |
||
245 |