Esempio n. 1
0
 function render()
 {
     $listModuleFile = array();
     $listIgnores = array('dashboard', 'home', 'chat', 'search', '.', '..', '.DS_Store', '.svn');
     $primaryArray = array('Create', 'Read', 'Edit', 'Delete');
     $listFiles = array_diff(scandir($this->modulePath), $listIgnores);
     // GET LIST FILE IN FOLDER
     if (!empty($listFiles)) {
         foreach ($listFiles as $file) {
             $file = str_replace("_backend", "", $file);
             $fileName = ucwords(str_replace("_", " ", $file));
             $fileSlug = str_replace("_", "-", $file);
             $listModuleFile[$fileSlug] = $fileName;
         }
     }
     // GET LIST FILE IN DATABASE
     $listModuleStore = Modules::get()->toArray();
     $listModuleStore = array_column($listModuleStore, 'name', 'slug');
     // NEW MODULE
     $diffInsert = array_diff($listModuleFile, $listModuleStore);
     $insertData = array();
     if (!empty($diffInsert)) {
         foreach ($diffInsert as $k => $v) {
             $insertData = array('slug' => $k, 'name' => $v, 'status' => 1);
             if ($item = Modules::create($insertData)) {
                 if ($item->save()) {
                     // Insert Menu
                     $menuInsert = array('status' => 0, 'name' => $item->name, 'module_id' => $item->id, 'slug' => $item->slug . '/show-list');
                     Menus::create($menuInsert);
                     // Create Primary Permission
                     foreach ($primaryArray as $p) {
                         $primaryInsert = array('name' => $item->name . " " . $p, 'slug' => $item->slug . "-" . strtolower($p), 'module_id' => $item->id, 'action' => strtolower($p));
                         Permission::create($primaryInsert);
                     }
                 }
             }
         }
     }
     // REMOVE MODULE
     $diffRemove = array_diff($listModuleStore, $listModuleFile);
     if (!empty($diffRemove)) {
         foreach ($diffRemove as $k => $v) {
             $item = Modules::where(array('slug' => $k, 'name' => $v))->first();
             $deleteID = $item->id;
             if ($item->delete()) {
                 Menus::where('module_id', $deleteID)->delete();
                 Permission::where('module_id', $deleteID)->delete();
             }
         }
     }
 }