Passed
Push — master ( 61cc62...67a7e6 )
by Iman
04:30
created

Step1Handler::grantAllPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
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 crocodicstudio\crudbooster\Modules\PrivilegeModule\PrivilegeRepo;
7
use Illuminate\Support\Facades\DB;
8
9
class Step1Handler
10
{
11
    public function showForm($id)
12
    {
13
        $row = ModulesRepo::find($id);
14
15
        return view("CbModulesGen::step1", compact("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, $moduleId) = $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
        (new PrivilegeRepo())->grantAllPermissions($moduleId);
70
71
        //Refresh Session Roles
72
        CRUDBooster::refreshSessionRoles();
73
74
        return $moduleId;
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' => auth('cbAdmin')->user()->id_cms_privileges,
0 ignored issues
show
Bug introduced by
Accessing id_cms_privileges on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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_modules')->insertGetId(compact("controller", "name", "table_name", "icon", "path", "created_at"));
110
111
        return [$controller, $id];
112
    }
113
}