Complete shares implementation

This commit is contained in:
Eike Foken
2011-09-09 00:27:56 +02:00
parent 4832e8eab0
commit a017aaff06
5 changed files with 60 additions and 26 deletions

View File

@@ -143,7 +143,7 @@ class Projects extends CI_Controller {
* *
* @param string $id * @param string $id
*/ */
public function share($id) { public function shares($id) {
$project = $this->project->getById($id); $project = $this->project->getById($id);
if (!$project) { if (!$project) {
show_404(); show_404();
@@ -154,6 +154,28 @@ class Projects extends CI_Controller {
show_error(_("Sorry, you don't have access to this project."), 403); 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['project'] = $project;
$data['shares'] = $this->share->getByProjectId($id); $data['shares'] = $this->share->getByProjectId($id);

View File

@@ -135,7 +135,7 @@ class Project extends CI_Model {
* Get all projects from the database. * Get all projects from the database.
*/ */
public function getAll() { 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') ->join('users', 'users.id = projects.owner', 'left')
->get('projects')->result_array(); ->get('projects')->result_array();

View File

@@ -53,7 +53,7 @@ class Share extends CI_Model {
* @return array * @return array
*/ */
public function getByProjectId($projectId) { 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'); $this->db->join('users', 'users.id = shares.user_id', 'left');
return $this->db->get_where('shares', array('project_id' => $projectId))->result_array(); return $this->db->get_where('shares', array('project_id' => $projectId))->result_array();
@@ -79,7 +79,16 @@ class Share extends CI_Model {
* @return boolean * @return boolean
*/ */
public function create($data) { 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; return $this->db->affected_rows() == 1;
} }

View File

@@ -6,7 +6,7 @@
<h2> <h2>
<?=anchor('projects', _('Projects'));?> &raquo; <?=$project['name'];?> <?=anchor('projects', _('Projects'));?> &raquo; <?=$project['name'];?>
<a class="share" href="<?=site_url('projects/share/' . $project['id']);?>"><?=_(sprintf('Shared with %s people', count($shares)));?></a> <a class="share" href="<?=site_url('projects/shares/' . $project['id']);?>"><?=_(sprintf('Shared with %s people', count($shares)));?></a>
</h2> </h2>
</div> </div>

View File

@@ -7,6 +7,7 @@
</div> </div>
<div class="box"> <div class="box">
<form method="post" name="updateShares" action="?action=update">
<table class="tableList"> <table class="tableList">
<thead> <thead>
<tr> <tr>
@@ -20,8 +21,8 @@
foreach ($shares as $share): foreach ($shares as $share):
?> ?>
<tr> <tr>
<td><a href="<?=site_url('users/profile/' . $share['user_id']);?>"><?=$share['firstname'];?> <?=$share['lastname'];?></a></td> <td><a href="<?=site_url('users/profile/' . $share['username']);?>"><?=$share['firstname'];?> <?=$share['lastname'];?></a></td>
<td><?=form_dropdown('rights', array('Can edit', 'Can view'), $share['can_edit'], 'class="drop"')?></td> <td><?=form_dropdown('rights[' . $share['user_id'] . ']', array('Can view', 'Can edit'), $share['can_edit'], 'class="drop"')?></td>
<td><a href="?action=delete&user_id=<?=$share['user_id'];?>"><?=_('Delete');?></a></td> <td><a href="?action=delete&user_id=<?=$share['user_id'];?>"><?=_('Delete');?></a></td>
</tr> </tr>
<?php <?php
@@ -29,12 +30,15 @@
?> ?>
</tbody> </tbody>
</table> </table>
</form>
<form method="post" name="addShare" action="<?=site_url('projects/share/' . $project['id']);?>"> <p><a class="button save" href="javascript:void(0);" onclick="$('form[name=updateShares]').submit();"><?=_('Save and back');?></a></p>
<h3><?=_('Share with someone');?></h3>
<form method="post" name="addShare" action="?action=add">
<ul> <ul>
<li> <li>
<div> <div>
<?=form_label(_('Add person:'), 'user_id');?>
<select name="user_id" id="user_id" class="drop"> <select name="user_id" id="user_id" class="drop">
<?php <?php
foreach ($this->user->getAll() as $user): foreach ($this->user->getAll() as $user):
@@ -44,13 +48,12 @@
endforeach; endforeach;
?> ?>
</select> </select>
<?=form_submit('', _('Share'), 'class="submit"');?> <?=form_dropdown('rights', array('Can view', 'Can edit'), $share['can_edit'], 'class="drop"')?>
<?=form_submit('add', _('Share'), 'class="submit"');?>
</div> </div>
</li> </li>
</ul> </ul>
</form> </form>
<p><a class="button save" href="javascript:history.back();"><?=_('Save and back');?></a></p>
</div> </div>
</div> </div>