Test Setup Failed
Push — dev5 ( 73d054...92f243 )
by Ron
35:09
created

addDocumentationColumnToTechTips()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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