Passed
Push — master ( 64d788...8a11c6 )
by Iman
04:13
created

Step1Handler::insertMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
use crocodicstudio\crudbooster\helpers\CRUDBooster;
6
use Illuminate\Support\Facades\DB;
7
8
class Step1Handler
9
{
10
    public function showForm($id)
11
    {
12
        $tables_list = CRUDBooster::listCbTables();
13
        $row = CRUDBooster::first('cms_moduls', ['id' => $id]);
14
15
        return view("CbModulesGen::step1", compact("tables_list", "row", "id"));
16
    }
17
18
    public function handleFormSubmit()
19
    {
20
        $name = request('name');
21
        $table_name = request('table');
22
        $icon = request('icon');
23
        $path = request('path');
24
25
        if (! request('id')) {
26
            if (DB::table('cms_moduls')->where('path', $path)->where('deleted_at', null)->count()) {
27
                backWithMsg('Sorry the slug has already exists, please choose another !', 'warning');
28
            }
29
            $id = $this->registerNewModule($table_name, $path, $name, $icon);
30
            return redirect()->route("AdminModulesControllerGetStep2", ["id" => $id]);
31
        }
32
33
        $id = request('id');
34
        \DB::table('cms_moduls')->where('id', $id)->update(compact("name", "table_name", "icon", "path"));
35
36
        $row = ModulesRepo::find($id);
37
38
39
        if (file_exists(controller_path($row->controller))) {
40
            $response = FileManipulator::readCtrlContent(str_replace('.', '', $row->controller));
41
        }else{
42
            $response = file_get_contents(__DIR__.'Step1Handler.php/'.str_replace('.', '', $row->controller).'.php');
43
        }
44
45
        if (strpos($response, "# START COLUMNS") !== true) {
46
            // return redirect()->back()->with(['message'=>'Sorry, is not possible to edit the module with Module Generator Tool. Prefix and or Suffix tag is missing !','message_type'=>'warning']);
47
        }
48
49
        return redirect()->route("AdminModulesControllerGetStep2", ["id" => $id]);
50
    }
51
52
    /**
53
     * @param $table_name
54
     * @param $path
55
     * @param $name
56
     * @param $icon
57
     * @return mixed
58
     */
59
    private function registerNewModule($table_name, $path, $name, $icon)
60
    {
61
        list($controller, $id) = $this->insertModule($table_name, $path, $name, $icon);
62
63
        //Insert Menu
64
        if ($controller && request('create_menu')) {
65
            $this->insertMenu($name, $icon, $controller);
66
        }
67
68
        $this->grantAllPermissions($id);
69
70
        //Refresh Session Roles
71
        $this->refreshSessionRoles();
72
73
        return $id;
74
    }
75
76
    /**
77
     * @param $name
78
     * @param $icon
79
     * @param $controller
80
     */
81
    private function insertMenu($name, $icon, $controller)
82
    {
83
        DB::table('cms_menus')->insert([
84
            'created_at' => YmdHis(),
85
            'name' => $name,
86
            'icon' => $icon,
87
            'path' => $controller.'GetIndex',
88
            'type' => 'Route',
89
            'is_active' => 1,
90
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
91
            'sorting' => DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1,
92
            'parent_id' => 0,
93
        ]);
94
    }
95
96
    /**
97
     * @param $table_name
98
     * @param $path
99
     * @param $name
100
     * @param $icon
101
     * @return array
102
     */
103
    private function insertModule($table_name, $path, $name, $icon)
104
    {
105
        $created_at = YmdHis();
106
107
        $controller = ControllerGenerator::generateController($table_name, $path);
108
        $id = \DB::table('cms_moduls')->insertGetId(compact("controller", "name", "table_name", "icon", "path", "created_at"));
109
110
        return [$controller, $id];
111
    }
112
113
    /**
114
     * @param $id
115
     */
116
    private function grantAllPermissions($id)
117
    {
118
        DB::table('cms_privileges_roles')->insert([
119
            'id_cms_moduls' => $id,
120
            'id_cms_privileges' => CRUDBooster::myPrivilegeId(),
121
            'is_visible' => 1,
122
            'is_create' => 1,
123
            'is_read' => 1,
124
            'is_edit' => 1,
125
            'is_delete' => 1,
126
        ]);
127
    }
128
129
    private function refreshSessionRoles()
130
    {
131
        $roles = DB::table('cms_privileges_roles')->where('id_cms_privileges', CRUDBooster::myPrivilegeId())->join('cms_moduls', 'cms_moduls.id', '=', 'id_cms_moduls')->select('cms_moduls.name', 'cms_moduls.path', 'is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete')->get();
132
133
        session()->put('admin_privileges_roles', $roles);
134
    }
135
}