]> BookStack Code Mirror - bookstack/blob - database/migrations/2017_01_01_130541_create_comments_table.php
Merge branch 'Abijeet-master'
[bookstack] / database / migrations / 2017_01_01_130541_create_comments_table.php
1 <?php
2
3 use Illuminate\Support\Facades\Schema;
4 use Illuminate\Database\Schema\Blueprint;
5 use Illuminate\Database\Migrations\Migration;
6
7 class CreateCommentsTable extends Migration
8 {
9     /**
10      * Run the migrations.
11      *
12      * @return void
13      */
14     public function up()
15     {
16         if (Schema::hasTable('comments')) {
17             return;
18         }
19         Schema::create('comments', function (Blueprint $table) {
20             $table->increments('id')->unsigned();
21             $table->integer('page_id')->unsigned();
22             $table->longText('text')->nullable();
23             $table->longText('html')->nullable();
24             $table->integer('parent_id')->unsigned()->nullable();
25             $table->integer('created_by')->unsigned();
26             $table->integer('updated_by')->unsigned()->nullable();
27             $table->index(['page_id', 'parent_id']);
28             $table->timestamps();
29
30             // Get roles with permissions we need to change
31             $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id;
32
33             // Create & attach new entity permissions
34             $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
35             $entity = 'Comment';
36             foreach ($ops as $op) {
37                 $permissionId = DB::table('role_permissions')->insertGetId([
38                     'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
39                     'display_name' => $op . ' ' . $entity . 's',
40                     'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
41                     'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
42                 ]);
43                 DB::table('permission_role')->insert([
44                     'role_id' => $adminRoleId,
45                     'permission_id' => $permissionId
46                 ]);
47             }
48
49             // Get roles with permissions we need to change
50             /*
51             $editorRole = DB::table('roles')->where('name', '=', 'editor')->first();
52             if (!empty($editorRole)) {
53                 $editorRoleId = $editorRole->id;
54                 // Create & attach new entity permissions
55                 $ops = ['Create All', 'Create Own', 'Update Own', 'Delete Own'];
56                 $entity = 'Comment';
57                 foreach ($ops as $op) {
58                     $permissionId = DB::table('role_permissions')->insertGetId([
59                         'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
60                         'display_name' => $op . ' ' . $entity . 's',
61                         'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
62                         'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
63                     ]);
64                     DB::table('permission_role')->insert([
65                         'role_id' => $editorRoleId,
66                         'permission_id' => $permissionId
67                     ]);
68                 }
69             }
70
71             // Get roles with permissions we need to change
72             $viewerRole = DB::table('roles')->where('name', '=', 'viewer')->first();
73             if (!empty($viewerRole)) {
74                 $viewerRoleId = $viewerRole->id;
75                 // Create & attach new entity permissions
76                 $ops = ['Create All'];
77                 $entity = 'Comment';
78                 foreach ($ops as $op) {
79                     $permissionId = DB::table('role_permissions')->insertGetId([
80                         'name' => strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op)),
81                         'display_name' => $op . ' ' . $entity . 's',
82                         'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
83                         'updated_at' => \Carbon\Carbon::now()->toDateTimeString()
84                     ]);
85                     DB::table('permission_role')->insert([
86                         'role_id' => $viewerRoleId,
87                         'permission_id' => $permissionId
88                     ]);
89                 }
90             }
91             */
92
93         });
94     }
95
96     /**
97      * Reverse the migrations.
98      *
99      * @return void
100      */
101     public function down()
102     {
103         Schema::dropIfExists('comments');
104         // Create & attach new entity permissions
105         $ops = ['Create All', 'Create Own', 'Update All', 'Update Own', 'Delete All', 'Delete Own'];
106         $entity = 'Comment';
107         foreach ($ops as $op) {
108             $permName = strtolower($entity) . '-' . strtolower(str_replace(' ', '-', $op));
109             DB::table('role_permissions')->where('name', '=', $permName)->delete();
110         }
111     }
112 }