public function manageAction($id = false)
 {
     $this->permission('manage');
     $model = new \modules\pm\models\Pm_teams();
     $user = new \modules\users\models\Users();
     $team_users = new \modules\pm\models\Pm_team_users(false);
     $model->attributes = $this->Input->post();
     if ($id) {
         $model->pm_team_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;
     }
     $team_users->_select = "pm_team_users_id , pm_team_id, user_id,pm_role_id";
     $roles = Form_helper::queryToDropdown('pm_roles', 'pm_role_id', 'title');
     if ($tid = $model->save()) {
         if ($id) {
             $team_users->pm_team_id = $id;
             $team_users->delete();
         }
         foreach ($this->input->post('add_user_id') as $key => $member_role) {
             $team_users->user_id = $member_role;
             $team_users->pm_team_id = $tid;
             $team_users->pm_role_id = $this->input->post('add_pm_role_id.' . $key);
             $team_users->save();
         }
         Uri_helper::redirect('management/pm_teams');
     } else {
         return $this->render('pm_teams/manage', ['item' => $id ? $model->get() : null, 'team_users' => $id ? $team_users->get() : null, 'roles' => $roles, 'user' => $users]);
     }
 }
 public function indexAction($id = false)
 {
     if (!$id) {
         return Brightery::error404();
     }
     $this->permission('index');
     $users = new \modules\users\models\Users();
     $users->_select = "user_id,fullname,image,status,\n          (SELECT  usergroups.name FROM usergroups WHERE usergroups.usergroup_id = users.usergroup_id) AS usergroup\n       ";
     $users->user_id = $id;
     $teams = new \modules\pm\models\Pm_team_users();
     $teams->_select = "pm_team_users_id, user_id,\n          (SELECT  pm_teams.title FROM pm_teams WHERE pm_teams.pm_team_id = Pm_team_users.pm_team_id) AS team,\n          (SELECT  pm_roles.title FROM pm_roles WHERE pm_roles.pm_role_id = Pm_team_users.pm_role_id) AS role\n       ";
     $teams->user_id = $id;
     $project_count = $this->database->query("SELECT COUNT(*) AS projects\n               FROM (\n                   SELECT DISTINCT  `pm_projects`.`pm_project_id`\n            FROM   `brighterycms`.`pm_projects`\n                RIGHT JOIN  `brighterycms`.`pm_teams_projects`ON (`pm_teams_projects`.`pm_project_id` = `pm_projects`.`pm_project_id`)\n                INNER JOIN `brighterycms`.`pm_teams`  ON (`pm_teams_projects`.`pm_team_id` = `pm_teams`.`pm_team_id`)\n                INNER JOIN `brighterycms`.`pm_team_users` ON (`pm_team_users`.`pm_team_id` = `pm_teams`.`pm_team_id`)\n            WHERE (`pm_team_users`.`user_id` = {$id})\n            ) t;")->row();
     $issue_count = $this->database->query("SELECT COUNT(*) AS issues\n              FROM (\n                SELECT pm_history.pm_issue_id\n                 FROM\n                `brighterycms`.`pm_history`\n                INNER JOIN `brighterycms`.`pm_issues`\n                    ON (`pm_history`.`pm_issue_id` = `pm_issues`.`pm_issue_id`)\n                 WHERE (`pm_history`.`actions` ='assign' AND `pm_history`.`to_user_id` = {$id})\n              ) s;")->row();
     $assign = $this->database->query("SELECT pm_issues.title AS issue_title ,pm_issues.pm_issue_id, actions = 'assign' AS assign ,\n            DATETIME AS assigned_date , NAME AS priority , pm_priorities.color AS p_color , pm_issue_statues.`title` AS statues , pm_issue_statues.`color` AS s_color\n            FROM pm_issues JOIN  pm_history\n            ON pm_issues.`pm_issue_id`= pm_history.`pm_issue_id`\n            JOIN users\n            ON users.user_id = pm_history.to_user_id\n            JOIN pm_priorities\n            ON pm_priorities.`pm_priority_id` = pm_issues.`pm_priority_id`\n            JOIN pm_issue_statues\n            ON pm_issue_statues.`pm_issue_statues_id` = pm_issues.`pm_issue_statues_id`\n            WHERE pm_history.to_user_id = {$id}")->result();
     $actions = $this->database->query("SELECT *, 'Infraction' AS actions , (SELECT pm_issues.`title` FROM pm_issues WHERE pm_issues.`pm_issue_id` = pm_infractions.`pm_issue_id`) AS issue_title FROM `pm_infractions`\n            WHERE pm_infractions.`user_id` = {$id}\n            UNION\n            SELECT *, 'Reputation' AS actions , (SELECT pm_issues.`title` FROM pm_issues WHERE pm_issues.`pm_issue_id` = pm_reputations.`pm_issue_id`) AS issue_title FROM `pm_reputations`\n            WHERE pm_reputations.`user_id` = {$id}")->result();
     return $this->render('pm_user_profile/index', ['item' => $users->get(), 'teams' => $teams->get(), 'assign' => $assign, 'actions' => $actions, 'project_count' => $project_count, 'issue_count' => $issue_count]);
 }