Passed
Push — dev5 ( 3ab1f5...1f5b4d )
by Ron
06:22
created

UpdatesForVersion50::addSoftDeleteToTechTips()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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