diff --git a/application/controllers/projects.php b/application/controllers/projects.php index ceac588..54c41c3 100644 --- a/application/controllers/projects.php +++ b/application/controllers/projects.php @@ -143,7 +143,7 @@ class Projects extends CI_Controller { * * @param string $id */ - public function share($id) { + public function shares($id) { $project = $this->project->getById($id); if (!$project) { show_404(); @@ -154,6 +154,28 @@ class Projects extends CI_Controller { show_error(_("Sorry, you don't have access to this project."), 403); } + // add a new share + if ($this->input->get('action') == 'add') { + $data = array( + 'user_id' => $this->input->post('user_id'), + 'project_id' => $project['id'], + 'can_edit' => $this->input->post('rights'), + ); + if ($this->share->create($data)) { + redirect('projects/shares/' . $project['id'], 303); + } + } else if ($this->input->get('action') == 'update') { // update all shares + foreach ($_POST['rights'] as $userId => $value) { + $this->share->update(array('project_id' => $project['id'], 'user_id' => $userId, 'can_edit' => $value)); + } + redirect('projects/shares/' . $project['id'], 303); + } else if ($this->input->get('action') == 'delete') { // delete a share + if ($this->share->delete(array('user_id' => $this->input->get('user_id'), 'project_id' => $project['id']))) { + redirect('projects/shares/' . $project['id'], 303); + } + } + + $data = array(); // empty data array $data['project'] = $project; $data['shares'] = $this->share->getByProjectId($id); diff --git a/application/models/project.php b/application/models/project.php index c10e25d..aa4311f 100644 --- a/application/models/project.php +++ b/application/models/project.php @@ -135,7 +135,7 @@ class Project extends CI_Model { * Get all projects from the database. */ public function getAll() { - $result = $this->db->select('projects.*, users.firstname AS `firstname`, users.lastname AS `lastname`') + $result = $this->db->select('projects.*, users.username, users.firstname, users.lastname') ->join('users', 'users.id = projects.owner', 'left') ->get('projects')->result_array(); diff --git a/application/models/share.php b/application/models/share.php index 4d81d90..59ae6cc 100644 --- a/application/models/share.php +++ b/application/models/share.php @@ -53,7 +53,7 @@ class Share extends CI_Model { * @return array */ public function getByProjectId($projectId) { - $this->db->select('shares.*, users.firstname, users.lastname'); + $this->db->select('shares.*, users.username, users.firstname, users.lastname'); $this->db->join('users', 'users.id = shares.user_id', 'left'); return $this->db->get_where('shares', array('project_id' => $projectId))->result_array(); @@ -79,7 +79,16 @@ class Share extends CI_Model { * @return boolean */ public function create($data) { - $this->db->insert('shares', $data); + if (!isset($data['project_id']) || !isset($data['user_id'])) { + return false; + } + + $this->db->query('REPLACE INTO `shares` (`project_id`, `user_id`, `can_edit`) VALUES (' + . $this->db->escape($data['project_id']) . ', ' + . $this->db->escape($data['user_id']) . ', ' + . $this->db->escape($data['can_edit']) . ')'); + + //$this->db->insert('shares', $data); return $this->db->affected_rows() == 1; } diff --git a/application/views/projects/detail.php b/application/views/projects/detail.php index 49b71f0..3019cc8 100644 --- a/application/views/projects/detail.php +++ b/application/views/projects/detail.php @@ -6,7 +6,7 @@

» - +

diff --git a/application/views/projects/shares.php b/application/views/projects/shares.php index 27273e4..83cfc25 100644 --- a/application/views/projects/shares.php +++ b/application/views/projects/shares.php @@ -7,34 +7,38 @@
- - - - - - - - - + +
+ + + + + + + + - - - - - + + + + + - -
+ + + -
+

+ +

+
- -