| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace crocodicstudio\crudbooster\Modules\PrivilegeModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use crocodicstudio\crudbooster\controllers\CBController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use crocodicstudio\crudbooster\controllers\FormValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use crocodicstudio\crudbooster\helpers\CRUDBooster; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Support\Facades\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class AdminPrivilegesController extends CBController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * AdminPrivilegesController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->table = 'cms_privileges'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->primaryKey = 'id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->titleField = "name"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function cbInit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->setButtons(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->makeCols(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->makeForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getAdd() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->cbLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $id = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'page_title' => 'Add Data', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'page_menu' => Route::getCurrentRoute()->getActionName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $data['moduls'] = $this->table('cms_moduls') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->where('is_protected', 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->select('cms_moduls.*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 DB::raw("(select is_visible from cms_privileges_roles where id_cms_moduls = cms_moduls.id and id_cms_privileges = '$id') as is_visible"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 DB::raw("(select is_create  from cms_privileges_roles where id_cms_moduls = cms_moduls.id and id_cms_privileges = '$id') as is_create"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 DB::raw("(select is_read    from cms_privileges_roles where id_cms_moduls = cms_moduls.id and id_cms_privileges = '$id') as is_read"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 DB::raw("(select is_edit    from cms_privileges_roles where id_cms_moduls = cms_moduls.id and id_cms_privileges = '$id') as is_edit"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 DB::raw("(select is_delete  from cms_privileges_roles where id_cms_moduls  = cms_moduls.id and id_cms_privileges = '$id') as is_delete")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->orderby('name', 'asc') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return view('CbPrivilege::privileges', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function postAddSave() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->cbInit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         app(FormValidator::class)->validate(null, $this->form, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->inputAssignment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->table()->insert($this->arr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $id = $this->arr[$this->primaryKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->setTheme(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         foreach (Request::input('privileges', []) as $moduleId => $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $arrs = array_get_keys($data, ['is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete'], 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $arrs['id_cms_privileges'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $arrs['id_cms_moduls'] = $moduleId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->table('cms_privileges_roles')->insert($arrs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             //$module = DB::table('cms_moduls')->where('id', $moduleId)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->refreshSessionRoles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_add_data_success'), 'success'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     public function getEdit($id) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $this->cbLoader(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $role = $this->findRow($id)->first() ?: new \stdClass(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $page_title = cbTrans('edit_data_page_title', ['module' => 'Privilege', 'name' => $role->name]); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         return view('CbPrivilege::privileges', compact('role', 'page_title', 'id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function postEditSave($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->cbInit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $row = CRUDBooster::first($this->table, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         app(FormValidator::class)->validate($id, $this->form, $this->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->inputAssignment($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->findRow($id)->update($this->arr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         foreach (Request::input('privileges', []) as $moduleId => $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             //Check Menu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $arrs = array_get_keys($data, ['is_visible', 'is_create', 'is_read', 'is_edit', 'is_delete',], 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->savePermissions($id, $moduleId, $arrs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($id == CRUDBooster::myPrivilegeId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $this->refreshSessionRoles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $this->setTheme(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_update_data_success', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'module' => 'Privilege', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'title' => $row->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ]), 'success'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function getDelete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->cbLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $row = $this->findRow($id)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->findRow($id)->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->table('cms_privileges_roles')->where('id_cms_privileges', $row->id)->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         CRUDBooster::redirect(CRUDBooster::mainpath(), cbTrans('alert_delete_data_success'), 'success'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     private function refreshSessionRoles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $roles = $this->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(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         session()->put('admin_privileges_roles', $roles); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param $moduleId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param $arrs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     private function savePermissions($id, $moduleId, $arrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $currentPermission = $this->table('cms_privileges_roles')->where('id_cms_moduls', $moduleId)->where('id_cms_privileges', $id)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ($currentPermission) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return $this->table('cms_privileges_roles')->where('id', $currentPermission->id)->update($arrs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $arrs['id'] = $this->table('cms_privileges_roles')->max('id') + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $arrs['id_cms_privileges'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $arrs['id_cms_moduls'] = $moduleId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->table('cms_privileges_roles')->insert($arrs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return $arrs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     private function setTheme() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         session()->put('theme_color', $this->arr['theme_color']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     private function setButtons() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $this->buttonImport = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $this->buttonExport = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->button_action_style = 'button_icon'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $this->buttonDetail = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $this->buttonBulkAction = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     private function makeForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         $this->form = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $this->form[] = ['label' => 'Name', 'name' => 'name', 'required' => true]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         $this->form[] = ['label' => 'Is Superadmin', 'name' => 'is_superadmin', 'required' => true]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $this->form[] = ['label' => 'Theme Color', 'name' => 'theme_color', 'required' => true]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     private function makeCols() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $this->col = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $this->col[] = ['label' => 'ID', 'name' => 'id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $this->col[] = ['label' => 'Name', 'name' => 'name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         $this->col[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             'label' => 'Superadmin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             'name' => 'is_superadmin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             'callback' => function ($row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 return ($row->is_superadmin) ? "<span class='label label-success'>Superadmin</span>" : "<span class='label label-default'>Standard</span>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  |