public function manageAction($id = false)
 {
     $this->permission('manage');
     $model = new \modules\pm\models\Pm_projects();
     $user = new \modules\users\models\Users();
     $team_project = new \modules\pm\models\Pm_teams_projects(false);
     $model->attributes = $this->input->post();
     if ($id) {
         $model->pm_project_id = $id;
     }
     if (!$id) {
         $model->created = date("Y-m-d H:i:s");
     }
     $users = [];
     foreach ($user->get() as $item) {
         $users[$item->user_id] = $item->fullname;
     }
     if ($id) {
         $model->pm_project_id = $id;
     }
     $team_project->_select = "pm_team_project_id, pm_team_id, pm_project_id";
     $teams = Form_helper::queryToDropdown('pm_teams', 'pm_team_id', 'title');
     if ($pid = $model->save()) {
         foreach ($this->input->post('pm_teams') as $teamproject) {
             $team_project->pm_team_id = $teamproject;
             $team_project->pm_project_id = $pid;
             $team_project->save();
         }
         Uri_helper::redirect("management/pm_projects");
     } else {
         return $this->render('pm_projects/manage', ['item' => $id ? $model->get() : null, 'team' => $teams, 'user' => $users, 'team_project' => $id ? $team_project->get() : null]);
     }
 }
 function indexAction($id = false)
 {
     if (!$id) {
         return Brightery::error404();
     }
     $this->permission('index');
     $projects = new \modules\pm\models\Pm_projects();
     $projects->_select = "pm_project_id,title,about,deadline," . " (SELECT users.fullname FROM users WHERE users.user_id " . "= pm_projects.pm_supervisor_id) AS supervisor";
     $projects->pm_project_id = $id;
     $issues = new \modules\pm\models\Pm_issues();
     $issues->_select = "pm_issue_id,title";
     $issues->pm_project_id = $id;
     $team_result = $this->database->query("SELECT title,pm_teams.`pm_team_id` AS id\n                FROM pm_teams_projects JOIN pm_teams\n                ON pm_teams_projects.`pm_team_id` = pm_teams.`pm_team_id`\n                where pm_teams_projects.pm_project_id= {$id} ")->result();
     $member_result = [];
     foreach ($team_result as $value) {
         $member_result[] = ['team' => $value->title, 'member' => $this->database->query("SELECT  fullname , title\n                             FROM pm_team_users JOIN users\n                             ON pm_team_users.`user_id` = users.`user_id`                                                        \n                             JOIN pm_roles\n                             ON pm_team_users.`pm_role_id` = pm_roles.`pm_role_id`\n                             WHERE pm_team_users.pm_team_id={$value->id}\n                         ")->result()];
     }
     return $this->render('pm_project_details/index', ['item' => $projects->get(), 'issues' => $issues->get(), 'team_results' => $team_result, 'member_results' => $member_result]);
 }