Passed
Push — master ( db0af9...5e9f02 )
by Ferry
03:57
created

ControllerSetting::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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->setSearchForm(true);
27
        $this->setButtonDelete(true);
28
        $this->setButtonEdit(true);
29
        $this->setButtonAdd(true);
30
        $this->setButtonCancel(true);
31
        $this->setButtonAddMore(true);
32
        $this->setButtonDetail(true);
33
        $this->setButtonSave(true);
34
        $this->setButtonLimitPage(true);
35
        $this->hideButtonDeleteWhen(function ($row) { return false; });
0 ignored issues
show
Unused Code introduced by
The parameter $row is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
        $this->hideButtonDeleteWhen(function (/** @scrutinizer ignore-unused */ $row) { return false; });

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
        $this->hideButtonDetailWhen(function ($row) { return false; });
0 ignored issues
show
Unused Code introduced by
The parameter $row is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
        $this->hideButtonDetailWhen(function (/** @scrutinizer ignore-unused */ $row) { return false; });

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        $this->hideButtonEditWhen(function ($row) { return false; });
0 ignored issues
show
Unused Code introduced by
The parameter $row is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
        $this->hideButtonEditWhen(function (/** @scrutinizer ignore-unused */ $row) { return false; });

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
        $this->style(function () { return null; });
39
        $this->javascript(function () { return null; });
40
    }
41
42
    public function style(callable $style) {
43
        $this->data['style'] = $style;
44
    }
45
46
    public function javascript(callable $javascript) {
47
        $this->data['javascript'] = $javascript;
48
    }
49
50
    /**
51
     * @param callable $condition
52
     */
53
    public function hideButtonDetailWhen(callable $condition) {
54
        $this->data['hide_button_detail'] = $condition;
55
    }
56
57
    /**
58
     * @param callable $condition
59
     */
60
    public function hideButtonEditWhen(callable $condition) {
61
        $this->data['hide_button_edit'] = $condition;
62
    }
63
64
    /**
65
     * @param callable $condition
66
     */
67
    public function hideButtonDeleteWhen(callable $condition) {
68
        $this->data['hide_button_delete'] = $condition;
69
    }
70
71
    /**
72
     * @param $enable boolean
73
     */
74
    public function setButtonLimitPage($enable) {
75
        $this->data['button_limit_page'] = $enable;
76
    }
77
78
    public function setPermalink($path)
79
    {
80
        $this->data['permalink'] = $path;
81
    }
82
83
    public function disableSoftDelete()
84
    {
85
        $this->data['disable_soft_delete'] = true;
86
    }
87
88
    /**
89
     * @param Closure $callbackQuery
90
     */
91
    public function hookIndexQuery(Closure $callbackQuery)
92
    {
93
        $this->data['hook_index_query'] = $callbackQuery;
94
    }
95
96
    /**
97
     * @param Closure $callback
98
     */
99
    public function hookAfterInsert(Closure $callback)
100
    {
101
        $this->data['hook_after_insert'] = $callback;
102
    }
103
104
    /**
105
     * @param Closure $callback
106
     */
107
    public function hookBeforeInsert(Closure $callback)
108
    {
109
        $this->data['hook_before_insert'] = $callback;
110
    }
111
112
    /**
113
     * @param Closure $callback
114
     */
115
    public function hookBeforeUpdate(Closure $callback)
116
    {
117
        $this->data['hook_before_update'] = $callback;
118
    }
119
120
    /**
121
     * @param Closure $callback
122
     */
123
    public function hookAfterUpdate(Closure $callback)
124
    {
125
        $this->data['hook_after_update'] = $callback;
126
    }
127
128
    /**
129
     * @param Closure $callbackQuery
130
     */
131
    public function hookSearchQuery(Closure $callbackQuery)
132
    {
133
        $this->data['hook_search_query'] = $callbackQuery;
134
    }
135
136
    /**
137
     * @param string $label
138
     * @param string $actionURL
139
     * @param string $fontAwesome_icon
140
     * @param ButtonColor $color
141
     * @param string $attributes
142
     */
143
    public function addIndexActionButton($label, $actionURL, $fontAwesome_icon=null, $color=null, $attributes = null)
144
    {
145
        $color = ($color)?:ButtonColor::DARK_BLUE;
146
147
        $model = new IndexActionButtonModel();
148
        $model->setLabel($label);
149
        $model->setIcon($fontAwesome_icon);
150
        $model->setColor($color);
151
        $model->setUrl($actionURL);
152
        $model->setAttributes($attributes);
153
154
        $this->data['index_action_button'][] = $model;
155
    }
156
157
    public function setOrderBy($field, $sort = 'desc')
158
    {
159
        $this->data['order_by'] = [$field, $sort];
160
        return $this;
161
    }
162
163
    /**
164
     * @param boolean $enable
165
     * @return $this
166
     */
167
    public function setSearchForm($enable) {
168
        $this->data['search_form'] = $enable;
169
        return $this;
170
    }
171
172
    /**
173
     * @param boolean $button_save
174
     * @return ControllerSetting
175
     */
176
    public function setButtonSave($button_save)
177
    {
178
        $this->data['button_save'] = $button_save;
179
        return $this;
180
    }
181
182
    /**
183
     * @param boolean $button_cancel
184
     * @return $this
185
     */
186
    public function setButtonCancel($button_cancel)
187
    {
188
        $this->data['button_cancel'] = $button_cancel;
189
        return $this;
190
    }
191
192
    /**
193
     * @param string $label
194
     * @param string $controllerName
195
     * @param string $foreignKey
196
     * @param callable|null $additionalInfo
197
     * @param callable|null $condition
198
     * @param string|null $font
199
     * @param ButtonColor|string $color
200
     */
201
    public function addSubModule($label, $controllerName, $foreignKey, callable $additionalInfo = null, callable $condition = null, $font = null, $color = null) {
202
        $parentPath = $this->getData("permalink");
0 ignored issues
show
Bug introduced by
It seems like getData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
        /** @scrutinizer ignore-call */ 
203
        $parentPath = $this->getData("permalink");
Loading history...
203
        $parentTitle = $this->getData("page_title");
204
        $this->addActionButton($label,function($row) use ($label, $controllerName, $foreignKey, $additionalInfo, $parentPath, $parentTitle) {
205
           $actionParameters = [
206
               "label"=>$label,
207
               "foreignKey"=>$foreignKey,
208
               "foreignValue"=>$row->primary_key,
209
               "parentPath"=>$parentPath,
210
               "parentTitle"=>$parentTitle
211
           ];
212
213
            if(isset($additionalInfo) && is_callable($additionalInfo)) {
214
                $additionalInfo = call_user_func($additionalInfo, $row);
215
                if(is_array($additionalInfo)) {
216
                    $actionParameters['info'] = $additionalInfo;
217
                }
218
            }
219
220
           $actionHash = md5(serialize($actionParameters));
221
           $actionHashToken = Cache::get("subModule".$actionHash);
222
           if(!$actionHashToken) {
223
               $actionHashToken = Str::random(5);
224
               Cache::forever("subModule".$actionHash, $actionHashToken);
225
               Cache::forever("subModule".$actionHashToken, $actionParameters);
226
           }
227
228
           return action(class_basename($controllerName)."@getSubModule",['subModuleKey'=>$actionHashToken])."?ref=".makeReferalUrl($parentTitle);
229
        }, $condition, $font, $color);
230
231
    }
232
233
234
    /**
235
     * @param $label
236
     * @param callable|string $url_target
237
     * @param callable|string $condition_callback
238
     * @param $fontAwesome_icon
239
     * @param ButtonColor|string $color
240
     */
241
    public function addActionButton($label, $url_target, $condition_callback=null, $fontAwesome_icon=null, $color=null, $confirmation = false)
242
    {
243
        $new = new AddActionButtonModel();
244
        $new->setLabel($label);
245
        $new->setIcon($fontAwesome_icon?:"fa fa-bars");
246
        $new->setColor($color?:"primary");
247
        $new->setUrl($url_target);
248
        $new->setCondition($condition_callback);
249
        $new->setConfirmation($confirmation);
250
        $this->data['add_action_button'][] = $new;
251
    }
252
    /**
253
     * @param mixed $button_edit
254
     * @param null $condition_callback
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $condition_callback is correct as it would always require null to be passed?
Loading history...
255
     * @return ControllerSetting
256
     */
257
    public function setButtonEdit($button_edit, $condition_callback = null)
258
    {
259
        $this->data['button_edit'] = $button_edit;
260
        $this->data['button_edit_callback'] = $condition_callback;
261
        return $this;
262
    }
263
264
    /**
265
     * @param mixed $button_detail
266
     * @param null $condition_callback
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $condition_callback is correct as it would always require null to be passed?
Loading history...
267
     * @return ControllerSetting
268
     */
269
    public function setButtonDetail($button_detail, $condition_callback = null)
270
    {
271
        $this->data['button_detail'] = $button_detail;
272
        $this->data['button_detail_callback'] = $condition_callback;
273
        return $this;
274
    }
275
276
    /**
277
     * @param mixed $button_delete
278
     * @param null $condition_callback
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $condition_callback is correct as it would always require null to be passed?
Loading history...
279
     * @return ControllerSetting
280
     */
281
    public function setButtonDelete($button_delete, $condition_callback = null)
282
    {
283
        $this->data['button_delete'] = $button_delete;
284
        $this->data['button_delete_callback'] = $condition_callback;
285
        return $this;
286
    }
287
288
289
290
    /**
291
     * @param string $alert_message
292
     * @return ControllerSetting
293
     */
294
    public function setAlertMessage($alert_message)
295
    {
296
        $this->data['alert_message'] = $alert_message;
297
        return $this;
298
    }
299
300
301
    /**
302
     * @param string $html_or_view
303
     * @return ControllerSetting
304
     */
305
    public function setBeforeIndexTable($html_or_view)
306
    {
307
        $this->data['before_index_table'] = $html_or_view;
308
        return $this;
309
    }
310
311
    /**
312
     * @param mixed $html_or_view
313
     * @return ControllerSetting
314
     */
315
    public function setAfterIndexTable($html_or_view)
316
    {
317
        $this->data['after_index_table'] = $html_or_view;
318
        return $this;
319
    }
320
321
    /**
322
     * @param callable|string $html_or_view
323
     * @return ControllerSetting
324
     */
325
    public function setBeforeDetailForm($html_or_view)
326
    {
327
        $this->data['before_detail_form'] = $html_or_view;
328
        return $this;
329
    }
330
331
    /**
332
     * @param callable|string $html_or_view
333
     * @return ControllerSetting
334
     */
335
    public function setAfterDetailForm($html_or_view)
336
    {
337
        $this->data['after_detail_form'] = $html_or_view;
338
        return $this;
339
    }
340
341
342
    /**
343
     * @param mixed $page_title
344
     * @return ControllerSetting
345
     */
346
    public function setPageTitle($page_title)
347
    {
348
        $this->data['page_title'] = $page_title;
349
        return $this;
350
    }
351
352
353
    /**
354
     * @param mixed $icon
355
     * @return ControllerSetting
356
     */
357
    public function setPageIcon($icon = "fa fa-table")
358
    {
359
        $this->data['page_icon'] = $icon;
360
        return $this;
361
    }
362
363
    /**
364
     * @param mixed $table
365
     * @return ControllerSetting
366
     */
367
    public function setTable($table)
368
    {
369
        $this->data['table'] = $table;
370
        return $this;
371
    }
372
373
    /**
374
     * To retrive current table name
375
     */
376
    public function table() {
377
        return $this->data['table'];
378
    }
379
380
    /**
381
     * @param int $limit
382
     * @return ControllerSetting
383
     */
384
    public function setLimit($limit)
385
    {
386
        $this->data['limit'] = $limit;
387
        return $this;
388
    }
389
390
    /**
391
     * @param bool $button_filter
392
     * @return ControllerSetting
393
     */
394
    public function setButtonFilter($button_filter)
395
    {
396
        $this->data['button_filter'] = $button_filter;
397
        return $this;
398
    }
399
400
401
    /**
402
     * @param bool $button_add_more
403
     * @return ControllerSetting
404
     */
405
    public function setButtonAddMore($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($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
}