Passed
Push — master ( e0b6af...ab23d3 )
by Ferry
11:54
created

ControllerSetting::addSubModule()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 1
nop 7
dl 0
loc 10
rs 9.9666
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\models\AddActionButtonModel;
15
use crocodicstudio\crudbooster\models\IndexActionButtonModel;
16
17
trait ControllerSetting
18
{
19
20
    private $data = [];
21
22
    private function defaultData() {
23
        $this->setSearchForm(true);
24
        $this->setButtonDelete(true);
25
        $this->setButtonEdit(true);
26
        $this->setButtonAdd(true);
27
        $this->setButtonCancel(true);
28
        $this->setButtonAddMore(true);
29
        $this->setButtonDetail(true);
30
        $this->setButtonSave(true);
31
        $this->setButtonLimitPage(true);
32
        $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

32
        $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...
33
        $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

33
        $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...
34
        $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

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

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