From bf40c38f13e2944fb1b4802a65b92bb7d08603e0 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Thu, 8 Sep 2011 05:20:00 +0200 Subject: [PATCH] Implement project shares --- application/controllers/projects.php | 28 +++++++- application/models/project.php | 11 +++- application/models/share.php | 94 +++++++++++++++++++++++++++ application/views/projects/detail.php | 41 +++++++----- application/views/projects/shares.php | 58 +++++++++++++++++ assets/css/form.css | 12 +++- assets/css/style.css | 12 ++++ 7 files changed, 233 insertions(+), 23 deletions(-) create mode 100644 application/models/share.php create mode 100644 application/views/projects/shares.php diff --git a/application/controllers/projects.php b/application/controllers/projects.php index d54fdf7..c2b45c2 100644 --- a/application/controllers/projects.php +++ b/application/controllers/projects.php @@ -35,6 +35,8 @@ class Projects extends CI_Controller { parent::__construct(); $this->load->library('form_validation'); $this->load->model('experiment'); + $this->load->model('job'); + $this->load->model('share'); } /** @@ -127,19 +129,41 @@ class Projects extends CI_Controller { } $this->load->helper('typography'); - $this->load->model('job'); $data['project'] = $project; $data['experiments'] = $this->experiment->getByProjectId($id); $data['jobs'] = $this->job->getRecent($id); + $data['shares'] = $this->share->getByProjectId($id); $this->load->view('projects/detail', $data); } + /** + * Allows users to share their projects. + * + * @param string $id + */ + public function share($id) { + $project = $this->project->getById($id); + if (!$project) { + show_404(); + } + + if (!$this->_checkAccess($id)) { + // check if the user has access + show_error(_("Sorry, you don't have access to this project."), 403); + } + + $data['project'] = $project; + $data['shares'] = $this->share->getByProjectId($id); + + $this->load->view('projects/shares', $data); + } + /** * Allows users to delete a project. * - * @param integer $projectId + * @param string $id */ public function delete($id) { if (!$this->_checkAccess($id)) { // check if the user has access diff --git a/application/models/project.php b/application/models/project.php index ece859e..6fe74ab 100644 --- a/application/models/project.php +++ b/application/models/project.php @@ -95,9 +95,19 @@ class Project extends CI_Model { * @return array All accessible projects */ public function getAccessible($userId) { + $shares = array(); + $query = $this->db->get_where('shares', array('user_id' => $userId)); + foreach ($query->result_array() as $share) { + $shares[] = $share['project_id']; + } + $this->db->where('public', 1); $this->db->or_where('owner', $userId); + if (count($shares) > 0) { + $this->db->or_where_in('projects.id', $shares); + } + return $this->getAll(); } @@ -147,7 +157,6 @@ class Project extends CI_Model { * @param type $needle The needle to look for in the haystack. */ public function search($needle) { - // get matching projects that are public $query = $this->db->query("SELECT * FROM `projects` WHERE `public`='1' AND `name` LIKE ".$this->db->escape('%'.$needle.'%')); $public_results = $query->result_array(); diff --git a/application/models/share.php b/application/models/share.php new file mode 100644 index 0000000..e1961fd --- /dev/null +++ b/application/models/share.php @@ -0,0 +1,94 @@ + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** +* Model for shares. +* +* @author Eike Foken +*/ +class Share extends CI_Model { + + /** + * Calls the parent constructor. + */ + public function __construct() { + parent::__construct(); + } + + /** + * Gets all share for a specific project. + * + * @param string $projectId + * @return array + */ + public function getByProjectId($projectId) { + $this->db->select('shares.*, 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(); + } + + /** + * Creates a share. + * + * @param array $data + * @return boolean + */ + public function create($data) { + $this->db->insert('shares', $data); + return $this->db->affected_rows() == 1; + } + + /** + * Updates a share. + * + * @param array $data + * @return boolean + */ + public function update($data) { + if (!isset($data['project_id']) || !isset($data['user_id'])) { + return false; + } + + $this->db->update('shares', $data, array('project_id' => $data['project_id'], 'user_id' => $data['user_id'])); + return $this->db->affected_rows() > 0; + } + + /** + * Deletes a share. + * + * @param array $data + * @return boolean + */ + public function delete($data) { + if (!isset($data['project_id']) || !isset($data['user_id'])) { + return false; + } + + $this->db->delete('shares', array('project_id' => $data['project_id'], 'user_id' => $data['user_id'])); + return $this->db->affected_rows() > 0; + } +} + +/* End of file share.php */ +/* Location: ./application/controllers/share.php */ diff --git a/application/views/projects/detail.php b/application/views/projects/detail.php index 06394d5..296e773 100644 --- a/application/views/projects/detail.php +++ b/application/views/projects/detail.php @@ -3,7 +3,11 @@
-

+

+ »« + + +

@@ -14,26 +18,27 @@ - -

+ +

- + +

@@ -71,7 +76,7 @@
-

+

diff --git a/application/views/projects/shares.php b/application/views/projects/shares.php new file mode 100644 index 0000000..1dc346b --- /dev/null +++ b/application/views/projects/shares.php @@ -0,0 +1,58 @@ +load->view('header');?> + +
+ +
+

»«

+
+ +
+ + + + + + + + + + + + + + + + + +
+ +
+
    +
  • +
    + + + +
    +
  • +
+
+ +

+
+ +
+ +load->view('footer');?> diff --git a/assets/css/form.css b/assets/css/form.css index 4ab4f73..4c9c9e5 100644 --- a/assets/css/form.css +++ b/assets/css/form.css @@ -45,6 +45,11 @@ input.button { font-size: 12px; } +input.submit { + padding: 0 4px; + font-size: 12px; +} + select.drop { font-size: 12px; margin: 5px 5px 0 0; @@ -75,11 +80,14 @@ p.success { color: #008000; } -p.error, span.req { +p.error, +span.req { color: #d8122d; font-weight: normal; } -p.success strong, p.error strong, p.req strong { +p.success strong, +p.error strong, +p.req strong { text-transform: uppercase; } diff --git a/assets/css/style.css b/assets/css/style.css index a78e6f6..fe736b4 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -447,6 +447,11 @@ a.delete { padding-left: 30px; } +a.edit { + background: url(../images/icons/pencil.png) 10px center no-repeat #f3f3f3; + padding-left: 30px; +} + a.flag { background: url(../images/icons/flag.png) 10px center no-repeat #f3f3f3; padding-left: 30px; @@ -505,6 +510,13 @@ a.down-big { padding: 10px 15px; } +a.share { + background: url(../images/icons/hand-share.png) 6px center no-repeat; + padding-left: 28px; + font-size: 12px; + font-weight: normal; +} + /* Tabs */ ul.tabs {