Passed
Push — master ( 816a06...aeb70e )
by Iman
04:44
created

Step1Handler::modulePathExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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 (ModulesRepo::modulePathExists($path)) {
27
                //todo: should be translated
28
                backWithMsg('Sorry the slug has already exists, please choose another !', 'warning');
29
            }
30
            $id = $this->registerNewModule($table_name, $path, $name, $icon);
31
            return redirect()->route("AdminModulesControllerGetStep2", ["id" => $id]);
32
        }
33
34
        $id = request('id');
35
        ModulesRepo::updateById($id, compact("name", "table_name", "icon", "path"));
36
37
        $row = ModulesRepo::find($id);
38
39
40
        if (file_exists(controller_path($row->controller))) {
41
            $response = FileManipulator::readCtrlContent(str_replace('.', '', $row->controller));
42
        }else{
43
            $response = file_get_contents(__DIR__.'Step1Handler.php/'.str_replace('.', '', $row->controller).'.php');
44
        }
45
46
        if (strpos($response, "# START COLUMNS") !== true) {
47
            // 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']);
48
        }
49
50
        return redirect()->route("AdminModulesControllerGetStep2", ["id" => $id]);
51
    }
52
53
    /**
54
     * @param $table_name
55
     * @param $path
56
     * @param $name
57
     * @param $icon
58
     * @return mixed
59
     */
60
    private function registerNewModule($table_name, $path, $name, $icon)
61
    {
62
        list($controller, $id) = $this->insertModule($table_name, $path, $name, $icon);
63
64
        //Insert Menu
65
        if ($controller && request('create_menu')) {
66
            $this->insertMenu($name, $icon, $controller);
67
        }
68
69
        $this->grantAllPermissions($id);
70
71
        //Refresh Session Roles
72
        $this->refreshSessionRoles();
73
74
        return $id;
75
    }
76
77
    /**
78
     * @param $name
79
     * @param $icon
80
     * @param $controller
81
     */
82
    private function insertMenu($name, $icon, $controller)
83
    {
84
        DB::table('cms_menus')->insert([
85
            'created_at' => YmdHis(),
86
            'name' => $name,
87
            'icon' => $icon,
88
            'path' => $controller.'GetIndex',
89
            'type' => 'Route',
90
            'is_active' => 1,
91
            'cms_privileges' => CRUDBooster::myPrivilegeId(),
92
            'sorting' => DB::table('cms_menus')->where('parent_id', 0)->max('sorting') + 1,
93
            'parent_id' => 0,
94
        ]);
95
    }
96
97
    /**
98
     * @param $table_name
99
     * @param $path
100
     * @param $name
101
     * @param $icon
102
     * @return array
103
     */
104
    private function insertModule($table_name, $path, $name, $icon)
105
    {
106
        $created_at = YmdHis();
107
108
        $controller = ControllerGenerator::generateController($table_name, $path);
109
        $id = \DB::table('cms_moduls')->insertGetId(compact("controller", "name", "table_name", "icon", "path", "created_at"));
110
111
        return [$controller, $id];
112
    }
113
114
    /**
115
     * @param $id
116
     */
117
    private function grantAllPermissions($id)
118
    {
119
        DB::table('cms_privileges_roles')->insert([
120
            'id_cms_moduls' => $id,
121
            'id_cms_privileges' => CRUDBooster::myPrivilegeId(),
122
            'is_visible' => 1,
123
            'is_create' => 1,
124
            'is_read' => 1,
125
            'is_edit' => 1,
126
            'is_delete' => 1,
127
        ]);
128
    }
129
130
    private function refreshSessionRoles()
131
    {
132
        $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();
133
134
        session()->put('admin_privileges_roles', $roles);
135
    }
136
}