Passed
Push — master ( c98abf...91bafd )
by Jonathan
19:18
created

CreateGroupModule::createFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.8333
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use Illuminate\Database\Schema\Blueprint;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Schema\Blueprint was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Illuminate\Database\Migrations\Migration;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Migrations\Migration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Database\Migrations\Traits\TablePrefixTrait;
7
use Uccello\Core\Models\Module;
8
use Uccello\Core\Models\Domain;
9
use Uccello\Core\Models\Tab;
10
use Uccello\Core\Models\Block;
11
use Uccello\Core\Models\Field;
12
use Uccello\Core\Models\Filter;
13
use Uccello\Core\Models\Menu;
14
use Uccello\Core\Models\Relatedlist;
15
use Uccello\Core\Models\Link;
16
17
// TODO...
18
19
class CreateGroupModule extends Migration
20
{
21
    use TablePrefixTrait;
22
23
    /**
24
     * Run the migrations.
25
     *
26
     * @return void
27
     */
28
    public function up()
29
    {
30
        $module = $this->createModule();
31
        $this->createTables();
32
        $this->activateModuleOnDomains($module);
33
        $this->updateMenu();
34
        $this->createTabsBlocksFields($module);
35
        $this->createFilters($module);
36
        $this->createRelatedLists($module);
37
    }
38
39
    /**
40
     * Reverse the migrations.
41
     *
42
     * @return void
43
     */
44
    public function down()
45
    {
46
        // Drop table
47
        Schema::dropIfExists($this->tablePrefix . 'uccello_groups');
48
        Schema::dropIfExists($this->tablePrefix . 'uccello_rl_groups_groups');
49
        Schema::dropIfExists($this->tablePrefix . 'uccello_rl_groups_users');
50
51
        // Delete module
52
        Module::where('name', 'group')->forceDelete();
53
    }
54
55
    protected function createTables()
56
    {
57
        // Module Table
58
        Schema::create($this->tablePrefix . 'uccello_groups', function (Blueprint $table) {
59
            $table->increments('id');
60
            $table->string('name')->nullable();
61
            $table->text('description')->nullable();
62
            $table->timestamps();
63
            $table->softDeletes();
64
65
            // $table->foreign('domain_id')->references('id')->on('uccello_domains');
66
        });
67
68
        // Related List Table: Groups-Groups
69
        Schema::create($this->tablePrefix . 'uccello_rl_groups_groups', function (Blueprint $table) {
70
            $table->increments('id');
71
            $table->unsignedInteger('parent_id');
72
            $table->unsignedInteger('children_id');
73
            $table->timestamps();
74
75
            // Foreign keys
76
            $table->foreign('parent_id')
77
                ->references('id')->on($this->tablePrefix . 'uccello_groups')
78
                ->onDelete('cascade');
79
80
            $table->foreign('children_id')
81
                ->references('id')->on($this->tablePrefix . 'uccello_groups')
82
                ->onDelete('cascade');
83
84
            // Unique keys
85
            $table->unique(['parent_id', 'children_id']);
86
        });
87
88
        // Related List Table: Groups-Users
89
        Schema::create($this->tablePrefix . 'uccello_rl_groups_users', function (Blueprint $table) {
90
            $table->increments('id');
91
            $table->unsignedInteger('group_id');
92
            $table->unsignedInteger('user_id');
93
            $table->timestamps();
94
95
            // Foreign keys
96
            $table->foreign('group_id')
97
                ->references('id')->on($this->tablePrefix . 'uccello_groups')
98
                ->onDelete('cascade');
99
100
            $table->foreign('user_id')
101
                ->references('id')->on($this->tablePrefix . 'users')
102
                ->onDelete('cascade');
103
104
            // Unique keys
105
            $table->unique(['group_id', 'user_id']);
106
        });
107
    }
108
109
    protected function createModule()
110
    {
111
        $module = new Module();
112
        $module->name = 'group';
113
        $module->icon = 'group';
0 ignored issues
show
Bug Best Practice introduced by
The property icon does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        $module->model_class = 'Uccello\Core\Models\Group';
115
        $module->data = [ "package" => "uccello/uccello", "admin" => true, "mandatory" => true ];
116
        $module->save();
117
118
        return $module;
119
    }
120
121
    protected function createTabsBlocksFields($module)
122
    {
123
        // Main tab
124
        $tab = new Tab();
125
        $tab->label = 'tab.main';
126
        $tab->icon = null;
127
        $tab->sequence = 0;
128
        $tab->module_id = $module->id;
129
        $tab->save();
130
131
        // General block
132
        $block = new Block();
133
        $block->label = 'block.general';
134
        $block->icon = 'group';
135
        $block->sequence = 0;
136
        $block->tab_id = $tab->id;
137
        $block->module_id = $module->id;
138
        $block->save();
139
140
        // Name
141
        $field = new Field();
142
        $field->name = 'name';
143
        $field->uitype_id = uitype('text')->id;
144
        $field->displaytype_id = displaytype('everywhere')->id;
145
        $field->data = null;
146
        $field->sequence = 0;
147
        $field->block_id = $block->id;
148
        $field->module_id = $module->id;
149
        $field->save();
150
151
        // Description
152
        $field = new Field();
153
        $field->name = 'description';
154
        $field->uitype_id = uitype('textarea')->id;
155
        $field->displaytype_id = displaytype('everywhere')->id;
156
        $field->data = [ 'large' => true ];
157
        $field->sequence = 1;
158
        $field->block_id = $block->id;
159
        $field->module_id = $module->id;
160
        $field->save();
161
    }
162
163
    protected function createFilters($module)
164
    {
165
        $filter = new Filter();
166
        $filter->module_id = $module->id;
167
        $filter->domain_id = null;
168
        $filter->user_id = null;
169
        $filter->name = 'filter.all';
170
        $filter->type = 'list';
171
        $filter->columns = [ 'name', 'description'];
172
        $filter->conditions = null;
173
        $filter->order_by = null;
174
        $filter->is_default = true;
175
        $filter->is_public = false;
176
        $filter->data = [ 'readonly' => true ];
177
        $filter->save();
178
    }
179
180
    protected function activateModuleOnDomains($module)
181
    {
182
        $domains = Domain::all();
183
        foreach ($domains as $domain) {
184
            $domain->modules()->attach($module);
185
        }
186
    }
187
188
    protected function createRelatedLists($module)
189
    {
190
        // Related List groups
191
        //$relatedModule = Module::where('name', $this->tablePrefix . 'group')->first();
192
193
        Relatedlist::create([
194
            'module_id' => $module->id,
195
            'related_module_id' => $module->id,
196
            'tab_id' => null,
197
            'related_field_id' => null,
198
            'label' => 'relatedlist.childrenGroups',
199
            'type' => 'n-n',
200
            'method' => 'getRelatedList',
201
            'data' => [
202
                "relationName" => "childrenGroups",
203
                "actions" => ["select", "add"]
204
            ],
205
            'sequence' => 0
206
        ]);
207
208
209
        // Related List users
210
        $relatedModule = Module::where('name', 'user')->first();
211
212
        Relatedlist::create([
213
            'module_id' => $module->id,
214
            'related_module_id' => $relatedModule->id,
215
            'tab_id' => null,
216
            'related_field_id' => null,
217
            'label' => 'relatedlist.users',
218
            'type' => 'n-n',
219
            'method' => 'getRelatedList',
220
            'data' => ["actions" => ["select", "add"]],
221
            'sequence' => 0
222
        ]);
223
    }
224
225
    protected function updateMenu()
226
    {
227
        // Add default admin menu
228
        // TODO: Translate
229
        $menu = Menu::find(1);
230
        $menu->data = json_decode('[
231
            {
232
                "color":"grey",
233
                "nochildren":false,
234
                "icon":"settings",
235
                "translation":"Dashboard",
236
                "label":"menu.dashboard",
237
                "type":"module",
238
                "route":"uccello.settings.dashboard",
239
                "module":"settings",
240
                "id":1
241
            },
242
            {
243
                "color":"green",
244
                "nochildren":false,
245
                "icon":"lock",
246
                "label":"menu.security",
247
                "type":"group",
248
                "id":2,
249
                "children":[
250
                    {
251
                        "color":"grey",
252
                        "nochildren":false,
253
                        "icon":"person",
254
                        "translation":"Users",
255
                        "label":"user",
256
                        "type":"module",
257
                        "route":"uccello.list",
258
                        "module":"user",
259
                        "id":3
260
                    },
261
                    {
262
                        "color":"grey",
263
                        "nochildren":false,
264
                        "icon":"group",
265
                        "translation":"Group",
266
                        "label":"group",
267
                        "type":"module",
268
                        "route":"uccello.list",
269
                        "module":"group",
270
                        "id":4
271
                    },
272
                    {
273
                        "color":"grey",
274
                        "nochildren":false,
275
                        "icon":"domain",
276
                        "translation":"Domains",
277
                        "label":"domain",
278
                        "type":"module",
279
                        "route":"uccello.list",
280
                        "module":"domain",
281
                        "id":5
282
                    },
283
                    {
284
                        "color":"grey",
285
                        "nochildren":false,
286
                        "icon":"lock",
287
                        "translation":"Roles",
288
                        "label":"role",
289
                        "type":"module",
290
                        "route":"uccello.list",
291
                        "module":"role",
292
                        "id":6
293
                    },
294
                    {
295
                        "color":"grey",
296
                        "nochildren":false,
297
                        "icon":"lock",
298
                        "translation":"Profiles",
299
                        "label":"profile",
300
                        "type":"module",
301
                        "route":"uccello.list",
302
                        "module":"profile",
303
                        "id":7
304
                    }
305
                ]
306
            },
307
            {
308
                "color":"green",
309
                "nochildren":false,
310
                "icon":"settings",
311
                "label":"menu.settings",
312
                "type":"group",
313
                "id":7,
314
                "children":[
315
                    {
316
                        "color":"grey",
317
                        "nochildren":false,
318
                        "icon":"extension",
319
                        "translation":"Module manager",
320
                        "label":"module_manager.link",
321
                        "type":"module",
322
                        "route":"uccello.settings.module.manager",
323
                        "module":"settings",
324
                        "id":8
325
                    },
326
                    {
327
                        "color":"grey",
328
                        "nochildren":false,
329
                        "icon":"menu",
330
                        "translation":"Menu manager",
331
                        "label":"menu_manager.link",
332
                        "type":"module",
333
                        "route":"uccello.settings.menu.manager",
334
                        "module":"settings",
335
                        "id":9
336
                    }
337
                ]
338
            }
339
        ]');
340
        $menu->save();
341
    }
342
343
}
344