CreateGroupModule::createTabsBlocksFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 40
rs 9.424
1
<?php
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Database\Schema\Blueprint;
5
use Uccello\Core\Database\Migrations\Migration;
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 . 'groups');
48
        Schema::dropIfExists($this->tablePrefix . 'rl_groups_groups');
49
        Schema::dropIfExists($this->tablePrefix . '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 . '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
66
        // Related List Table: Groups-Groups
67
        Schema::create($this->tablePrefix . 'rl_groups_groups', function (Blueprint $table) {
68
            $table->increments('id');
69
            $table->unsignedInteger('parent_id');
70
            $table->unsignedInteger('children_id');
71
            $table->timestamps();
72
73
            // Foreign keys
74
            $table->foreign('parent_id')
75
                ->references('id')->on($this->tablePrefix . 'groups')
76
                ->onDelete('cascade');
77
78
            $table->foreign('children_id')
79
                ->references('id')->on($this->tablePrefix . 'groups')
80
                ->onDelete('cascade');
81
82
            // Unique keys
83
            $table->unique(['parent_id', 'children_id']);
84
        });
85
86
        // Related List Table: Groups-Users
87
        Schema::create($this->tablePrefix . 'rl_groups_users', function (Blueprint $table) {
88
            $table->increments('id');
89
            $table->unsignedInteger('group_id');
90
            $table->unsignedInteger('user_id');
91
            $table->timestamps();
92
93
            // Foreign keys
94
            $table->foreign('group_id')
95
                ->references('id')->on($this->tablePrefix . 'groups')
96
                ->onDelete('cascade');
97
98
            $table->foreign('user_id')
99
                ->references('id')->on('users')
100
                ->onDelete('cascade');
101
102
            // Unique keys
103
            $table->unique(['group_id', 'user_id']);
104
        });
105
    }
106
107
    protected function createModule()
108
    {
109
        $module = new Module();
110
        $module->name = 'group';
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
111
        $module->icon = 'group';
0 ignored issues
show
Bug introduced by
The property icon does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
112
        $module->model_class = 'Uccello\Core\Models\Group';
0 ignored issues
show
Bug introduced by
The property model_class does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
113
        $module->data = [ "package" => "uccello/uccello", "admin" => true, "mandatory" => true ];
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
114
        $module->save();
115
116
        return $module;
117
    }
118
119
    protected function createTabsBlocksFields($module)
120
    {
121
        // Main tab
122
        $tab = new Tab();
123
        $tab->label = 'tab.main';
0 ignored issues
show
Bug introduced by
The property label does not seem to exist on Uccello\Core\Models\Tab. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
124
        $tab->icon = null;
0 ignored issues
show
Bug introduced by
The property icon does not seem to exist on Uccello\Core\Models\Tab. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
125
        $tab->sequence = 0;
0 ignored issues
show
Bug introduced by
The property sequence does not seem to exist on Uccello\Core\Models\Tab. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
126
        $tab->module_id = $module->id;
0 ignored issues
show
Bug introduced by
The property module_id does not exist on Uccello\Core\Models\Tab. Did you mean module?
Loading history...
127
        $tab->save();
128
129
        // General block
130
        $block = new Block();
131
        $block->label = 'block.general';
0 ignored issues
show
Bug introduced by
The property label does not seem to exist on Uccello\Core\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
132
        $block->icon = 'group';
0 ignored issues
show
Bug introduced by
The property icon does not seem to exist on Uccello\Core\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
133
        $block->sequence = 0;
0 ignored issues
show
Bug introduced by
The property sequence does not seem to exist on Uccello\Core\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
134
        $block->tab_id = $tab->id;
0 ignored issues
show
Bug introduced by
The property tab_id does not seem to exist on Uccello\Core\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
135
        $block->module_id = $module->id;
0 ignored issues
show
Bug introduced by
The property module_id does not seem to exist on Uccello\Core\Models\Block. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
136
        $block->save();
137
138
        // Name
139
        $field = new Field();
140
        $field->name = 'name';
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
141
        $field->uitype_id = uitype('text')->id;
0 ignored issues
show
Bug introduced by
The property uitype_id does not exist on Uccello\Core\Models\Field. Did you mean uitype?
Loading history...
142
        $field->displaytype_id = displaytype('everywhere')->id;
0 ignored issues
show
Bug introduced by
The property displaytype_id does not exist on Uccello\Core\Models\Field. Did you mean displaytype?
Loading history...
143
        $field->data = [ 'rules' => 'required' ];
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
144
        $field->sequence = 0;
0 ignored issues
show
Bug introduced by
The property sequence does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
145
        $field->block_id = $block->id;
0 ignored issues
show
Bug introduced by
The property block_id does not exist on Uccello\Core\Models\Field. Did you mean block?
Loading history...
146
        $field->module_id = $module->id;
0 ignored issues
show
Bug introduced by
The property module_id does not exist on Uccello\Core\Models\Field. Did you mean module?
Loading history...
147
        $field->save();
148
149
        // Description
150
        $field = new Field();
151
        $field->name = 'description';
152
        $field->uitype_id = uitype('textarea')->id;
153
        $field->displaytype_id = displaytype('everywhere')->id;
154
        $field->data = null;
155
        $field->sequence = 1;
156
        $field->block_id = $block->id;
157
        $field->module_id = $module->id;
158
        $field->save();
159
    }
160
161
    protected function createFilters($module)
162
    {
163
        $filter = new Filter();
164
        $filter->module_id = $module->id;
0 ignored issues
show
Bug introduced by
The property module_id does not exist on Uccello\Core\Models\Filter. Did you mean module?
Loading history...
165
        $filter->domain_id = null;
0 ignored issues
show
Bug introduced by
The property domain_id does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
166
        $filter->user_id = null;
0 ignored issues
show
Bug introduced by
The property user_id does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
167
        $filter->name = 'filter.all';
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
168
        $filter->type = 'list';
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
169
        $filter->columns = [ 'name', 'description'];
0 ignored issues
show
Bug introduced by
The property columns does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
170
        $filter->conditions = null;
0 ignored issues
show
Bug introduced by
The property conditions does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
171
        $filter->order_by = null;
0 ignored issues
show
Bug introduced by
The property order_by does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
172
        $filter->is_default = true;
0 ignored issues
show
Bug introduced by
The property is_default does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
173
        $filter->is_public = false;
0 ignored issues
show
Bug introduced by
The property is_public does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
174
        $filter->data = [ 'readonly' => true ];
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Filter. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
175
        $filter->save();
176
    }
177
178
    protected function activateModuleOnDomains($module)
179
    {
180
        $domains = Domain::all();
181
        foreach ($domains as $domain) {
182
            $domain->modules()->attach($module);
183
        }
184
    }
185
186
    protected function createRelatedLists($module)
187
    {
188
        // Related List groups
189
        //$relatedModule = Module::where('name', $this->tablePrefix . 'group')->first();
190
191
        Relatedlist::create([
192
            'module_id' => $module->id,
193
            'related_module_id' => $module->id,
194
            'tab_id' => null,
195
            'related_field_id' => null,
196
            'label' => 'relatedlist.childrenGroups',
197
            'type' => 'n-n',
198
            'method' => 'getRelatedList',
199
            'data' => [
200
                "relationName" => "childrenGroups",
201
                "actions" => ["select", "add"]
202
            ],
203
            'sequence' => 0
204
        ]);
205
206
207
        // Related List users
208
        $relatedModule = Module::where('name', 'user')->first();
209
210
        Relatedlist::create([
211
            'module_id' => $module->id,
212
            'related_module_id' => $relatedModule->id,
213
            'tab_id' => null,
214
            'related_field_id' => null,
215
            'label' => 'relatedlist.users',
216
            'type' => 'n-n',
217
            'method' => 'getRelatedList',
218
            'data' => ["actions" => ["select", "add"]],
219
            'sequence' => 1
220
        ]);
221
    }
222
223
    protected function updateMenu()
224
    {
225
        // Add default admin menu
226
        // TODO: Translate
227
        $menu = Menu::find(1);
228
        $menu->data = json_decode('[
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Menu. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
229
            {
230
                "color":"grey",
231
                "nochildren":false,
232
                "icon":"settings",
233
                "translation":"Dashboard",
234
                "label":"menu.dashboard",
235
                "type":"module",
236
                "route":"uccello.settings.dashboard",
237
                "module":"settings",
238
                "id":1
239
            },
240
            {
241
                "color":"green",
242
                "nochildren":false,
243
                "icon":"lock",
244
                "label":"menu.security",
245
                "type":"group",
246
                "id":2,
247
                "children":[
248
                    {
249
                        "color":"grey",
250
                        "nochildren":false,
251
                        "icon":"person",
252
                        "translation":"Users",
253
                        "label":"user",
254
                        "type":"module",
255
                        "route":"uccello.list",
256
                        "module":"user",
257
                        "id":3
258
                    },
259
                    {
260
                        "color":"grey",
261
                        "nochildren":false,
262
                        "icon":"group",
263
                        "translation":"Group",
264
                        "label":"group",
265
                        "type":"module",
266
                        "route":"uccello.list",
267
                        "module":"group",
268
                        "id":4
269
                    },
270
                    {
271
                        "color":"grey",
272
                        "nochildren":false,
273
                        "icon":"domain",
274
                        "translation":"Domains",
275
                        "label":"domain",
276
                        "type":"module",
277
                        "route":"uccello.list",
278
                        "module":"domain",
279
                        "id":5
280
                    },
281
                    {
282
                        "color":"grey",
283
                        "nochildren":false,
284
                        "icon":"lock",
285
                        "translation":"Roles",
286
                        "label":"role",
287
                        "type":"module",
288
                        "route":"uccello.list",
289
                        "module":"role",
290
                        "id":6
291
                    },
292
                    {
293
                        "color":"grey",
294
                        "nochildren":false,
295
                        "icon":"lock",
296
                        "translation":"Profiles",
297
                        "label":"profile",
298
                        "type":"module",
299
                        "route":"uccello.list",
300
                        "module":"profile",
301
                        "id":7
302
                    }
303
                ]
304
            },
305
            {
306
                "color":"green",
307
                "nochildren":false,
308
                "icon":"settings",
309
                "label":"menu.settings",
310
                "type":"group",
311
                "id":7,
312
                "children":[
313
                    {
314
                        "color":"grey",
315
                        "nochildren":false,
316
                        "icon":"extension",
317
                        "translation":"Module manager",
318
                        "label":"module_manager.link",
319
                        "type":"module",
320
                        "route":"uccello.settings.module.manager",
321
                        "module":"settings",
322
                        "id":8
323
                    },
324
                    {
325
                        "color":"grey",
326
                        "nochildren":false,
327
                        "icon":"menu",
328
                        "translation":"Menu manager",
329
                        "label":"menu_manager.link",
330
                        "type":"module",
331
                        "route":"uccello.settings.menu.manager",
332
                        "module":"settings",
333
                        "id":9
334
                    }
335
                ]
336
            }
337
        ]');
338
        $menu->save();
339
    }
340
341
}
342