Implement public projects

This commit is contained in:
Eike Foken
2011-09-08 03:21:54 +02:00
parent 7b329f4501
commit d7ed30e1ba
6 changed files with 124 additions and 22 deletions

View File

@@ -232,6 +232,11 @@ $config['projects/create'] = array(
'label' => _('Default configuration'),
'rules' => 'file_allowed_type[calc]',
),
array(
'field' => 'public',
'label' => _('Make the project public'),
'rules' => 'integer',
),
);
/**

View File

@@ -41,9 +41,13 @@ class Projects extends CI_Controller {
* Shows a list of all projects.
*/
public function index() {
$tpl['projects'] = $this->project->getAll();
if ($this->access->isAdmin()) {
$data['projects'] = $this->project->getAll();
} else {
$data['projects'] = $this->project->getAccessible($this->session->userdata('user_id'));
}
$this->load->view('projects/list', $tpl);
$this->load->view('projects/list', $data);
}
/**
@@ -55,6 +59,7 @@ class Projects extends CI_Controller {
$data = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description'),
'public' => $this->input->post('public'),
);
$data['project_id'] = $this->project->create($data);
@@ -112,15 +117,18 @@ class Projects extends CI_Controller {
* @param integer $id The ID of the project to show
*/
public function detail($id) {
$this->load->helper('typography');
$this->load->model('job');
$project = $this->project->getById($id);
if (!$project) {
$this->messages->add(_('The project could not be loaded.'), 'error');
redirect('projects', 303);
show_404();
}
if (!$this->_checkAccess($id)) { // check if the user has access
show_error(_("Sorry, you don't have access to this project."), 403);
}
$this->load->helper('typography');
$this->load->model('job');
$data['project'] = $project;
$data['experiments'] = $this->experiment->getByProjectId($id);
$data['jobs'] = $this->job->getRecent($id);
@@ -134,10 +142,26 @@ class Projects extends CI_Controller {
* @param integer $projectId
*/
public function delete($id) {
if (!$this->_checkAccess($id)) { // check if the user has access
show_error(_("Sorry, you don't have access to this project."), 403);
}
if ($this->project->delete($id)) {
$this->messages->add(_('The project was deleted.'), 'success');
}
redirect('projects', 303);
}
/**
* Checks if users have access to a project.
*
* @param string $projectId
*/
private function _checkAccess($projectId) {
$project = $this->project->getById($projectId);
return $this->access->isAdmin() || $project['public'] == 1;
}
}
/* End of file projects.php */
/* Location: ./application/controllers/projects.php */

View File

@@ -0,0 +1,47 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/*
* Copyright (c) 2011 Eike Foken <kontakt@eikefoken.de>
*
* 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.
*/
/**
* Extends CI's form helpers.
*
* @package ScattPort
* @subpackage Helpers
* @author Eike Foken <kontakt@eikefoken.de>
*/
if (!function_exists('form_yesno')) {
/**
* Shows a yes-no selection.
*
* @param string $name
* @param mixed $selected
* @param string $extra
* @return string
*/
function form_yesno($name = '', $selected = array(), $extra = '') {
return form_dropdown($name, array('0' => _('No'), '1' => _('Yes')), $selected, $extra);
}
}
/* End of file MY_form_helper.php */
/* Location: ./application/helpers/MY_form_helper.php */

View File

@@ -79,30 +79,42 @@ class Project extends CI_Model {
}
/**
* Get all publicly available projects.
* Gets all publicly available projects.
*
* @return array All public projects.
* @return array All public projects
*/
private function getPublic() {
$query = $this->db->where(array('public' => '1'))
->order_by('name', 'asc')
->get('projects');
public function getPublic() {
$query = $this->db->order_by('name ASC')->get_where('projects', array('public' => 1));
return $this->_addShortNames($query->result_array());
}
/**
* Gets all accessible projects for a user.
*
* @param string $userId
* @return array All accessible projects
*/
public function getAccessible($userId) {
$this->db->where('public', 1);
$this->db->or_where('owner', $userId);
return $this->getAll();
}
/**
* Get a specific project from the database.
*
* @param type $project_id The project to get.
*/
public function getById($project_id) {
$result = $this->db->get_where('projects', array('id' => $project_id))->row_array();
$this->db->where('id', $project_id)->update('projects', array(
'lastaccess' => mysql_now(),
));
public function getById($projectId) {
$result = $this->db->get_where('projects', array('id' => $projectId))->row_array();
$this->db->where('id', $projectId)->update('projects', array('lastaccess' => mysql_now()));
if ($result) {
return $this->_addShortName($result);
} else {
return false;
}
}
/**

View File

@@ -110,9 +110,9 @@
<ul>
<?php
$projects = $this->project->getOwn();
foreach($projects as $project):
foreach ($projects as $project):
?>
<li><a href="<?=site_url('projects/detail/'.$project['id']);?>"><?=$project['mediumname'];?></a></li>
<li><a href="<?=site_url('projects/detail/' . $project['id']);?>"><?=$project['mediumname'];?></a></li>
<?php
endforeach;
?>
@@ -128,6 +128,14 @@
<a href="javascript:void(0);"><?=_('Public projects');?></a>
<ul>
<li><a href="#">Beispielprojekt</a></li>
<?php
$projects = $this->project->getPublic();
foreach ($projects as $project):
?>
<li><a href="<?=site_url('projects/detail/' . $project['id']);?>"><?=$project['mediumname'];?></a></li>
<?php
endforeach;
?>
</ul>
</li>
</ul>

View File

@@ -44,6 +44,12 @@
</div>
<label class="note"><?=_('Upload a configuration that is used as a default for new experiments.<br/>This configuration can be changed for every experiment.');?></label>
</li>
<li>
<?=form_label(_('Make the project public?'), 'public');?>
<div>
<?=form_yesno('public', set_value('public'), 'id="public" class="drop"');?>
</div>
</li>
<li>
<a href="#" onclick="document.forms.createproject.submit()" class="button"><?=_('Save');?></a>
</li>