Passed
Push — dev5 ( 6e89ff...3685b3 )
by Ron
05:58
created

UpdatesForVersion50::migrateSystemDocumentation()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 29
rs 9.584
1
<?php
2
3
use App\User;
4
use App\TechTips;
5
use App\Customers;
6
use App\FileLinks;
7
use App\TechTipFiles;
8
use App\FileLinkFiles;
9
use App\TechTipSystems;
10
use App\UserPermissions;
11
use App\PhoneNumberTypes;
12
use Illuminate\Support\Facades\Schema;
13
use Illuminate\Database\Schema\Blueprint;
14
use Illuminate\Database\Migrations\Migration;
15
16
class UpdatesForVersion50 extends Migration
17
{
18
    /**
19
     * Database changes for version 5.0
20
     *
21
     * @return void
22
     */
23
    public function up()
24
    {
25
        //  See the function itself for a description of the database changes
26
        //  DB Modifications
27
        $this->updatePhoneIcons();
28
        $this->modifySystemDataTableNames();
29
        $this->migrateSystemDocumentation();
30
31
        //  DB Adds
32
        $this->addSoftDeleteToCustomerSystems();
33
        $this->addPasswordExpiresColumn();
34
        $this->addIsInstallerToUsers();
35
        $this->addHiddenColumn();
36
        $this->addColumnsToFileLinksTable();
37
        $this->addNotesColumnToFileLinkFiles();
38
        // $this->addDocumentationColumnToTechTips();
39
40
        //  Remove Unneeded Tables
41
        $this->removeNavBarView();
42
        $this->removeUserRolesTables();
43
        $this->removeFileLinkInstructionsTable();
44
        $this->removeFileLinkNotesTable();
45
        $this->removeSystemFilesTables();
46
        $this->removeActiveFromCustomers();
47
48
    }
49
50
    /**
51
     * Reverse the migrations.
52
     *
53
     * @return void
54
     */
55
    public function down()
56
    {
57
        return 'Unable to Process.  Please Downgrade app and load backup.';
58
    }
59
60
    /*
61
    *   Database Modifications
62
    */
63
    //  Update the icon class to change from font awesome, to theymify icons
64
    private function updatePhoneIcons()
65
    {
66
        $newIcons = [
67
            ['description' => 'Home', 'icon_class' => 'ti-home'],
68
            ['description' => 'Work', 'icon_class' => 'ti-briefcase'],
69
            ['description' => 'Mobile', 'icon_class' => 'ti-mobile'],
70
        ];
71
72
        foreach($newIcons as $new)
73
        {
74
            PhoneNumberTypes::where('description', $new['description'])->update(['icon_class' => $new['icon_class']]);
75
        }
76
    }
77
78
    /*
79
    *   New Columns and Tables
80
    */
81
    //  Added the ability to set an expiration date for user passwords - will force them to change after this expires
82
    private function addPasswordExpiresColumn()
83
    {
84
        if(!Schema::hasColumn('users', 'password_expires')) {
85
            Schema::table('users', function(Blueprint $table) {
86
                $table->timestamp('password_expires')
87
                    ->nullable()
88
                    ->after('active');
89
            });
90
        }
91
    }
92
93
    //  Add the is installer column to the users table
94
    private function addIsInstallerToUsers()
95
    {
96
        if(!Schema::hasColumn('users', 'is_installer'))
97
        {
98
            //  Add the is installer column
99
            Schema::table('users', function(Blueprint $table) {
100
                $table->boolean('is_installer')->default(0)->after('active');
101
            });
102
            //  Make user id 1 the installer user
103
            User::find(1)->update(['is_installer' => 1]);
104
            //  default permissions to be used for remaining users
105
            $defaultPermissions =
106
            [
107
                'user_id'             => null,
108
                'manage_users'        => 0,
109
                'run_reports'         => 0,
110
                'add_customer'        => 1,
111
                'deactivate_customer' => 0,
112
                'use_file_links'      => 1,
113
                'create_tech_tip'     => 1,
114
                'edit_tech_tip'       => 0,
115
                'delete_tech_tip'     => 0,
116
                'create_category'     => 0,
117
                'modify_category'     => 0
118
            ];
119
120
            $userRoles = DB::select('SELECT * FROM `user_role` LEFT JOIN `roles` ON `user_role`.`role_id` = `roles`.`role_id`');
121
            $permissions = UserPermissions::all();
122
123
            //  Cycle thorugh all users and assign proper permissions
124
            foreach($userRoles as $user)
125
            {
126
                $userPerm = $defaultPermissions;
127
                if(!$permissions->contains($user->user_id))
128
                {
129
                    switch($user->name)
130
                    {
131
                        case 'Installer':
132
                            User::find($user->user_id)->update(['is_installer' => 1]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
133
                        case 'Admin':
134
                            $userPerm['manage_users']        = 1;
135
                            $userPerm['deactivate_customer'] = 1;
136
                            $userPerm['edit_tech_tip']       = 1;
137
                            $userPerm['delete_tech_tip']     = 1;
138
                            $userPerm['create_category']     = 1;
139
                            $userPerm['modify_category']     = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
140
                        case 'Report':
141
                            $userPerm['run_reports']         = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
142
                        default:
143
                            $userPerm['user_id']             = $user->user_id;
144
                    }
145
                    UserPermissions::create($userPerm);
146
                }
147
            }
148
        }
149
    }
150
151
    //  Added a 'hidden' attribute to system customer data types to allow passwords to not be viewed unless clicked or focus
152
    private function addHiddenColumn()
153
    {
154
        if(!Schema::hasColumn('system_data_field_types', 'hidden')) {
155
            Schema::table('system_cust_data_types', function(Blueprint $table) {
156
                $table->boolean('hidden')
157
                    ->default(0)
158
                    ->after('name');
159
            });
160
        }
161
    }
162
163
    //  Update the File links table - add cust_id and note column
164
    private function addColumnsToFileLinksTable()
165
    {
166
        if(!Schema::hasColumn('file_links', 'cust_id')) {
167
            Schema::table('file_links', function(Blueprint $table) {
168
                $table->integer('cust_id')
169
                    ->unsigned()
170
                    ->nullable()
171
                    ->after('user_id');
172
                $table->foreign('cust_id')->references('cust_id')->on('customers')->onUpdate('cascade')->onDelete('cascade');
173
            });
174
        }
175
        if(!Schema::hasColumn('file_links', 'note')) {
176
            Schema::table('file_links', function(Blueprint $table) {
177
                $table->longText('note')
178
                    ->nullable()
179
                    ->after('link_name');
180
            });
181
            //  Migrate the instructions from the old table to the new column
182
            $instructions = DB::select('SELECT * FROM `file_link_instructions`');
183
            foreach($instructions as $ins) {
184
                FileLinks::find($ins->link_id)->update([
185
                    'note' => $ins->instruction
186
                ]);
187
            }
188
        }
189
    }
190
191
    //  Add Notes column to the file links files table
192
    private function addNotesColumnToFileLinkFiles()
193
    {
194
        if(!Schema::hasColumn('file_link_files', 'note')) {
195
            Schema::table('file_link_files', function(Blueprint $table) {
196
                $table->longText('note')
197
                    ->nullable()
198
                    ->after('upload');
199
            });
200
            //  Migrate the existing notes to the new table
201
            $notes = DB::select('SELECT * FROM `file_link_notes`');
202
            foreach($notes as $note) {
203
                FileLinkFiles::where('file_id', $note->file_id)->update([
204
                    'note' => $note->note
205
                ]);
206
            }
207
        }
208
    }
209
210
    //  Add the documentation column to the tech tips table
211
    private function migrateSystemDocumentation()
212
    {
213
        if(!Schema::hasColumn('tech_tips', 'tip_type_id')) {
214
            Schema::table('tech_tips', function(Blueprint $table) {
215
                $table->bigInteger('tip_type_id')->unsigned()->after('public')->default(1);
216
                $table->foreign('tip_type_id')->references('tip_type_id')->on('tech_tip_types')->onUpdate('cascade');
217
            });
218
219
            //  Move all of the system files over to the tech tips table
220
            $sysFiles = DB::select('SELECT * FROM `system_files`');
221
            foreach($sysFiles as $sysFile) {
222
                $newTip = TechTips::create([
223
                    'user_id'       => $sysFile->user_id,
224
                    'public'        => 0,
225
                    'tip_type_id'   => 2,
226
                    'subject'       => $sysFile->name,
227
                    'description'   => empty($sysFile->description) ? $sysFile->name : $sysFile->description,
228
                    'created_at'    => $sysFile->created_at,
229
                    'updated_at'    => $sysFile->updated_at
230
                ]);
231
232
                $tipId = $newTip->tip_id;
233
                TechTipFiles::create([
234
                    'tip_id'  => $tipId,
235
                    'file_id' => $sysFile->file_id
236
                ]);
237
                TechTipSystems::create([
238
                    'tip_id' => $tipId,
239
                    'sys_id' => $sysFile->sys_id
240
                ]);
241
            }
242
        }
243
    }
244
245
    /*
246
    *   Database Cleanup
247
    */
248
    //  Remove the NavBar view if it exists - We no longer need it to sort out the system types
249
    private function removeNavBarView()
250
    {
251
        DB::statement('DROP VIEW IF EXISTS `navbar_view`');
252
    }
253
254
    //  Remove the active column from the customers table
255
    private function removeActiveFromCustomers()
256
    {
257
        if(Schema::hasColumn('customers', 'active'))
258
        {
259
            //  Migrate the existing disabled customers first
260
            $deactivatedCustomers = Customers::where('active', 0)->get();
261
            foreach($deactivatedCustomers as $cust)
262
            {
263
                Customers::find($cust->cust_id)->delete();
264
            }
265
266
            Schema::table('customers', function(Blueprint $table) {
267
                $table->dropColumn('active');
268
            });
269
        }
270
    }
271
272
    //  Remove the user roles tables
273
    private function removeUserRolesTables()
274
    {
275
        if(Schema::hasTable('user_role'))
276
        {
277
            Schema::table('user_role', function(Blueprint $table) {
278
                $table->dropForeign(['user_id']);
279
                $table->dropForeign(['role_id']);
280
            });
281
            Schema::dropIfExists('user_role');
282
            Schema::dropIfExists('roles');
283
        }
284
    }
285
286
    //  Remove the File Link Instructions Table
287
    private function removeFileLinkInstructionsTable()
288
    {
289
        if(Schema::hasTable('file_link_instructions'))
290
        {
291
            //  Remove the foreign key to allow for dropping the table
292
            Schema::table('file_link_instructions', function(Blueprint $table) {
293
                $table->dropForeign(['link_id']);
294
            });
295
            Schema::dropIfExists('file_link_instructions');
296
        }
297
    }
298
299
    //  Remove the file link files note table
300
    private function removeFileLinkNotesTable()
301
    {
302
        if(Schema::hasTable('file_link_notes'))
303
        {
304
            Schema::table('file_link_notes', function(Blueprint $table) {
305
                $table->dropForeign(['link_id']);
306
                $table->dropForeign(['file_id']);
307
            });
308
            Schema::dropIfExists('file_link_notes');
309
        }
310
    }
311
312
    //  Remove the system files and system file types table
313
    private function removeSystemFilesTables()
314
    {
315
        if(Schema::hasTable('system_files')) {
316
            Schema::table('system_files', function(Blueprint $table) {
317
                $table->dropForeign(['sys_id']);
318
                $table->dropForeign(['type_id']);
319
                $table->dropForeign(['file_id']);
320
                $table->dropForeign(['user_id']);
321
            });
322
            Schema::dropIfExists('system_files');
323
            Schema::dropIfExists('system_file_types');
324
        }
325
    }
326
327
    //  Rename the tables that hold customer system information to make more sense
328
    private function modifySystemDataTableNames()
329
    {
330
        if(Schema::hasTable('customer_system_fields'))
331
        {
332
            Schema::dropIfExists('customer_system_data');
333
            Schema::rename('customer_system_fields', 'customer_system_data');
334
        }
335
        if(Schema::hasTable('system_cust_data_fields'))
336
        {
337
            Schema::dropIfExists('system_data_fields');
338
            Schema::rename('system_cust_data_fields', 'system_data_fields');
339
        }
340
        if(Schema::hasTable('system_cust_data_types'))
341
        {
342
            Schema::dropIfExists('system_data_field_types');
343
            Schema::rename('system_cust_data_types', 'system_data_field_types');
344
        }
345
    }
346
347
    //  Add soft deletes to customer systems table to prevent accidental deletes
348
    private function addSoftDeleteToCustomerSystems()
349
    {
350
        if(!Schema::hasColumn('customer_systems', 'deleted_at'))
351
        {
352
            Schema::table('customer_systems', function(Blueprint $table) {
353
                $table->softDeletes()->after('sys_id');
354
            });
355
        }
356
    }
357
}
358