1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 2/3/2019 |
6
|
|
|
* Time: 7:37 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\controllers\traits; |
10
|
|
|
|
11
|
|
|
use Closure; |
12
|
|
|
use crocodicstudio\crudbooster\controllers\partials\ButtonColor; |
13
|
|
|
use crocodicstudio\crudbooster\controllers\partials\SidebarStyle; |
14
|
|
|
use crocodicstudio\crudbooster\controllers\scaffolding\traits\ColumnsRegister; |
15
|
|
|
use crocodicstudio\crudbooster\models\AddActionButtonModel; |
16
|
|
|
use crocodicstudio\crudbooster\models\IndexActionButtonModel; |
17
|
|
|
use crocodicstudio\crudbooster\types\Hidden; |
18
|
|
|
use Illuminate\Support\Facades\Cache; |
19
|
|
|
use Illuminate\Support\Str; |
20
|
|
|
|
21
|
|
|
trait ControllerSetting |
22
|
|
|
{ |
23
|
|
|
private $data = []; |
24
|
|
|
|
25
|
|
|
private function defaultData() { |
26
|
|
|
$this->setLimit(10); |
27
|
|
|
$this->setSearchForm(true); |
28
|
|
|
$this->setButtonDelete(true); |
29
|
|
|
$this->setButtonEdit(true); |
30
|
|
|
$this->setButtonAdd(true); |
31
|
|
|
$this->setButtonCancel(true); |
32
|
|
|
$this->setButtonAddMore(true); |
33
|
|
|
$this->setButtonDetail(true); |
34
|
|
|
$this->setButtonSave(true); |
35
|
|
|
$this->setButtonLimitPage(true); |
36
|
|
|
$this->hideButtonDeleteWhen(function ($row) { return false; }); |
|
|
|
|
37
|
|
|
$this->hideButtonDetailWhen(function ($row) { return false; }); |
|
|
|
|
38
|
|
|
$this->hideButtonEditWhen(function ($row) { return false; }); |
|
|
|
|
39
|
|
|
$this->style(function () { return null; }); |
40
|
|
|
$this->javascript(function () { return null; }); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function appendAddForm(callable $html) { |
44
|
|
|
$this->data['append_add_form'] = $html; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function prependAddForm(callable $html) { |
48
|
|
|
$this->data['prepend_add_form'] = $html; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function appendEditForm(callable $html) { |
52
|
|
|
$this->data['append_edit_form'] = $html; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function prependEditForm(callable $html) { |
56
|
|
|
$this->data['prepend_edit_form'] = $html; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function style(callable $style) { |
60
|
|
|
$this->data['style'] = $style; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function javascript(callable $javascript) { |
64
|
|
|
$this->data['javascript'] = $javascript; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param callable $condition |
69
|
|
|
*/ |
70
|
|
|
public function hideButtonDetailWhen(callable $condition) { |
71
|
|
|
$this->data['hide_button_detail'] = $condition; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param callable $condition |
76
|
|
|
*/ |
77
|
|
|
public function hideButtonEditWhen(callable $condition) { |
78
|
|
|
$this->data['hide_button_edit'] = $condition; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param callable $condition |
83
|
|
|
*/ |
84
|
|
|
public function hideButtonDeleteWhen(callable $condition) { |
85
|
|
|
$this->data['hide_button_delete'] = $condition; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $enable boolean |
90
|
|
|
*/ |
91
|
|
|
public function setButtonLimitPage($enable) { |
92
|
|
|
$this->data['button_limit_page'] = $enable; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setPermalink($path) |
96
|
|
|
{ |
97
|
|
|
$this->data['permalink'] = $path; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function disableSoftDelete() |
101
|
|
|
{ |
102
|
|
|
$this->data['disable_soft_delete'] = true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Closure $callbackQuery |
107
|
|
|
*/ |
108
|
|
|
public function hookIndexQuery(Closure $callbackQuery) |
109
|
|
|
{ |
110
|
|
|
$this->data['hook_index_query'] = $callbackQuery; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Closure $callback |
115
|
|
|
*/ |
116
|
|
|
public function hookAfterInsert(Closure $callback) |
117
|
|
|
{ |
118
|
|
|
$this->data['hook_after_insert'] = $callback; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Closure $callback |
123
|
|
|
*/ |
124
|
|
|
public function hookBeforeInsert(Closure $callback) |
125
|
|
|
{ |
126
|
|
|
$this->data['hook_before_insert'] = $callback; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param Closure $callback |
131
|
|
|
*/ |
132
|
|
|
public function hookBeforeUpdate(Closure $callback) |
133
|
|
|
{ |
134
|
|
|
$this->data['hook_before_update'] = $callback; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param Closure $callback |
139
|
|
|
*/ |
140
|
|
|
public function hookAfterUpdate(Closure $callback) |
141
|
|
|
{ |
142
|
|
|
$this->data['hook_after_update'] = $callback; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Closure $callbackQuery |
147
|
|
|
*/ |
148
|
|
|
public function hookSearchQuery(Closure $callbackQuery) |
149
|
|
|
{ |
150
|
|
|
$this->data['hook_search_query'] = $callbackQuery; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $label |
155
|
|
|
* @param string $actionURL |
156
|
|
|
* @param string $fontAwesome_icon |
157
|
|
|
* @param ButtonColor $color |
158
|
|
|
* @param string $attributes |
159
|
|
|
*/ |
160
|
|
|
public function addIndexActionButton($label, $actionURL, $fontAwesome_icon=null, $color=null, $attributes = null) |
161
|
|
|
{ |
162
|
|
|
$color = ($color)?:ButtonColor::DARK_BLUE; |
163
|
|
|
|
164
|
|
|
$model = new IndexActionButtonModel(); |
165
|
|
|
$model->setLabel($label); |
166
|
|
|
$model->setIcon($fontAwesome_icon); |
167
|
|
|
$model->setColor($color); |
168
|
|
|
$model->setUrl($actionURL); |
169
|
|
|
$model->setAttributes($attributes); |
170
|
|
|
|
171
|
|
|
$this->data['index_action_button'][] = $model; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function setOrderBy($field, $sort = 'desc') |
175
|
|
|
{ |
176
|
|
|
$this->data['order_by'] = [$field, $sort]; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param boolean $enable |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function setSearchForm($enable) { |
185
|
|
|
$this->data['search_form'] = $enable; |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param boolean $button_save |
191
|
|
|
* @return ControllerSetting |
192
|
|
|
*/ |
193
|
|
|
public function setButtonSave($button_save) |
194
|
|
|
{ |
195
|
|
|
$this->data['button_save'] = $button_save; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param boolean $button_cancel |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function setButtonCancel($button_cancel) |
204
|
|
|
{ |
205
|
|
|
$this->data['button_cancel'] = $button_cancel; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $label |
211
|
|
|
* @param string $controllerName |
212
|
|
|
* @param string $foreignKey |
213
|
|
|
* @param callable|null $additionalInfo |
214
|
|
|
* @param callable|null $condition |
215
|
|
|
* @param string|null $font |
216
|
|
|
* @param ButtonColor|string $color |
217
|
|
|
*/ |
218
|
|
|
public function addSubModule($label, $controllerName, $foreignKey, callable $additionalInfo = null, callable $condition = null, $font = null, $color = null) { |
219
|
|
|
$parentPath = $this->getData("permalink"); |
|
|
|
|
220
|
|
|
$parentTitle = $this->getData("page_title"); |
221
|
|
|
$this->addActionButton($label,function($row) use ($label, $controllerName, $foreignKey, $additionalInfo, $parentPath, $parentTitle) { |
222
|
|
|
$actionParameters = [ |
223
|
|
|
"label"=>$label, |
224
|
|
|
"foreignKey"=>$foreignKey, |
225
|
|
|
"foreignValue"=>$row->primary_key, |
226
|
|
|
"parentPath"=>$parentPath, |
227
|
|
|
"parentTitle"=>$parentTitle |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
if(isset($additionalInfo) && is_callable($additionalInfo)) { |
231
|
|
|
$additionalInfo = call_user_func($additionalInfo, $row); |
232
|
|
|
if(is_array($additionalInfo)) { |
233
|
|
|
$actionParameters['info'] = $additionalInfo; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$actionHash = md5(serialize($actionParameters)); |
238
|
|
|
$actionHashToken = Cache::get("subModule".$actionHash); |
239
|
|
|
if(!$actionHashToken) { |
240
|
|
|
$actionHashToken = Str::random(5); |
241
|
|
|
Cache::forever("subModule".$actionHash, $actionHashToken); |
242
|
|
|
Cache::forever("subModule".$actionHashToken, $actionParameters); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return action(class_basename($controllerName)."@getSubModule",['subModuleKey'=>$actionHashToken])."?ref=".makeReferalUrl($parentTitle); |
246
|
|
|
}, $condition, $font, $color); |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param $label |
253
|
|
|
* @param callable|string $url_target |
254
|
|
|
* @param callable|string $condition_callback |
255
|
|
|
* @param $fontAwesome_icon |
256
|
|
|
* @param ButtonColor|string $color |
257
|
|
|
*/ |
258
|
|
|
public function addActionButton($label, $url_target, $condition_callback=null, $fontAwesome_icon=null, $color=null, $confirmation = false) |
259
|
|
|
{ |
260
|
|
|
$new = new AddActionButtonModel(); |
261
|
|
|
$new->setLabel($label); |
262
|
|
|
$new->setIcon($fontAwesome_icon?:"fa fa-bars"); |
263
|
|
|
$new->setColor($color?:"primary"); |
264
|
|
|
$new->setUrl($url_target); |
265
|
|
|
$new->setCondition($condition_callback); |
266
|
|
|
$new->setConfirmation($confirmation); |
267
|
|
|
$this->data['add_action_button'][] = $new; |
268
|
|
|
} |
269
|
|
|
/** |
270
|
|
|
* @param mixed $button_edit |
271
|
|
|
* @return ControllerSetting |
272
|
|
|
*/ |
273
|
|
|
public function setButtonEdit(bool $button_edit) |
274
|
|
|
{ |
275
|
|
|
$this->data['button_edit'] = $button_edit; |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param mixed $button_detail |
281
|
|
|
* @return ControllerSetting |
282
|
|
|
*/ |
283
|
|
|
public function setButtonDetail(bool $button_detail) |
284
|
|
|
{ |
285
|
|
|
$this->data['button_detail'] = $button_detail; |
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param mixed $button_delete |
291
|
|
|
* @return ControllerSetting |
292
|
|
|
*/ |
293
|
|
|
public function setButtonDelete(bool $button_delete) |
294
|
|
|
{ |
295
|
|
|
$this->data['button_delete'] = $button_delete; |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $alert_message |
303
|
|
|
* @return ControllerSetting |
304
|
|
|
*/ |
305
|
|
|
public function setAlertMessage($alert_message) |
306
|
|
|
{ |
307
|
|
|
$this->data['alert_message'] = $alert_message; |
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param string $html_or_view |
314
|
|
|
* @return ControllerSetting |
315
|
|
|
*/ |
316
|
|
|
public function setBeforeIndexTable($html_or_view) |
317
|
|
|
{ |
318
|
|
|
$this->data['before_index_table'] = $html_or_view; |
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param mixed $html_or_view |
324
|
|
|
* @return ControllerSetting |
325
|
|
|
*/ |
326
|
|
|
public function setAfterIndexTable($html_or_view) |
327
|
|
|
{ |
328
|
|
|
$this->data['after_index_table'] = $html_or_view; |
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param callable|string $html_or_view |
334
|
|
|
* @return ControllerSetting |
335
|
|
|
*/ |
336
|
|
|
public function setBeforeDetailForm($html_or_view) |
337
|
|
|
{ |
338
|
|
|
$this->data['before_detail_form'] = $html_or_view; |
339
|
|
|
return $this; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param callable|string $html_or_view |
344
|
|
|
* @return ControllerSetting |
345
|
|
|
*/ |
346
|
|
|
public function setAfterDetailForm($html_or_view) |
347
|
|
|
{ |
348
|
|
|
$this->data['after_detail_form'] = $html_or_view; |
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param mixed $page_title |
355
|
|
|
* @return ControllerSetting |
356
|
|
|
*/ |
357
|
|
|
public function setPageTitle($page_title) |
358
|
|
|
{ |
359
|
|
|
$this->data['page_title'] = $page_title; |
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param mixed $icon |
366
|
|
|
* @return ControllerSetting |
367
|
|
|
*/ |
368
|
|
|
public function setPageIcon($icon = "fa fa-table") |
369
|
|
|
{ |
370
|
|
|
$this->data['page_icon'] = $icon; |
371
|
|
|
return $this; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param mixed $table |
376
|
|
|
* @return ControllerSetting |
377
|
|
|
*/ |
378
|
|
|
public function setTable($table) |
379
|
|
|
{ |
380
|
|
|
$this->data['table'] = $table; |
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* To retrive current table name |
386
|
|
|
*/ |
387
|
|
|
public function table() { |
388
|
|
|
return $this->data['table']; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param int $limit |
393
|
|
|
* @return ControllerSetting |
394
|
|
|
*/ |
395
|
|
|
public function setLimit($limit = 20) |
396
|
|
|
{ |
397
|
|
|
$this->data['limit'] = $limit; |
398
|
|
|
return $this; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @param bool $button_add_more |
403
|
|
|
* @return ControllerSetting |
404
|
|
|
*/ |
405
|
|
|
public function setButtonAddMore(bool $button_add_more) |
406
|
|
|
{ |
407
|
|
|
$this->data['button_add_more'] = $button_add_more; |
408
|
|
|
return $this; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @param bool $button_add |
413
|
|
|
* @return ControllerSetting |
414
|
|
|
*/ |
415
|
|
|
public function setButtonAdd(bool $button_add) |
416
|
|
|
{ |
417
|
|
|
$this->data['button_add'] = $button_add; |
418
|
|
|
return $this; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param string $head_script |
423
|
|
|
* @return ControllerSetting |
424
|
|
|
*/ |
425
|
|
|
public function setHeadScript($head_script) |
426
|
|
|
{ |
427
|
|
|
$this->data['head_script'] = $head_script; |
428
|
|
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param string $bottom_view |
433
|
|
|
* @return ControllerSetting |
434
|
|
|
*/ |
435
|
|
|
public function setBottomView($bottom_view) |
436
|
|
|
{ |
437
|
|
|
$this->data['bottom_view'] = $bottom_view; |
438
|
|
|
return $this; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param SidebarStyle|string $sidebar_style |
443
|
|
|
* @return ControllerSetting |
444
|
|
|
*/ |
445
|
|
|
public function setSidebarStyle($sidebar_style) |
446
|
|
|
{ |
447
|
|
|
$this->data['sidebar_style'] = $sidebar_style; |
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.