Ejemplo n.º 1
0
 public function modules($id, Request $request)
 {
     $user = \Session::get('user');
     if (!$user->can('项目模块管理')) {
         abort(401);
     }
     $project = Project::find($id);
     $connected_modules = $project->modules()->where('project_id', $project->id)->lists('id')->all();
     if (count($connected_modules)) {
         $project->modules()->detach($connected_modules);
     }
     $new_modules = $request->input('modules', []);
     foreach ($new_modules as $module_id) {
         $module = Module::find($module_id);
         $project->modules()->save($module);
     }
     $d1 = array_diff($new_modules, $connected_modules);
     $d2 = array_diff($connected_modules, $new_modules);
     if (count($d1)) {
         //新加的模块
         Clog::add($project, '添加模块', [implode(',', \App\Module::whereIn('id', $d1)->lists('name')->all())], Clog::LEVEL_WARNING);
         foreach ($d1 as $m) {
             $module = Module::find($m);
             \Log::notice(strtr('项目模块增加: 用户(%name[%id] 添加了项目(%project_name[%project_id] 的模块 (%module_name[%module_id])', ['%name' => $user->name, '%id' => $user->id, '%project_name' => $project->name, '%project_id' => $project->id, '%module_name' => $module->name, '%module_id' => $module->id]));
         }
     }
     if (count($d2)) {
         //删除的模块
         Clog::add($project, '删除模块', [implode(',', \App\Module::whereIn('id', $d2)->lists('name')->all())], Clog::LEVEL_WARNING);
         foreach ($d2 as $m) {
             $module = Module::find($m);
             \Log::notice(strtr('项目模块删除: 用户(%name[%id] 删除了项目(%project_name[%project_id] 的模块 (%module_name[%module_id])', ['%name' => $user->name, '%id' => $user->id, '%project_name' => $project->name, '%project_id' => $project->id, '%module_name' => $module->name, '%module_id' => $module->id]));
         }
     }
     return redirect()->back()->with('message_content', '模块设置成功!')->with('message_type', 'info')->with('tab', 'softwares');
 }