Begin implementing programs management

This commit is contained in:
Eike Foken
2011-08-15 01:03:03 +02:00
parent 639f59378e
commit 020bd13058
9 changed files with 271 additions and 25 deletions

View File

@@ -117,6 +117,19 @@ $config['users/edit'] = array(
), ),
); );
/**
* Rules for editing programs.
*
* @var array
*/
$config['programs/edit'] = array(
array(
'field' => 'name',
'label' => _('Name of the program'),
'rules' => 'required|max_length[100]|trim',
),
);
/** /**
* Rules for the settings page. * Rules for the settings page.
* *

View File

@@ -0,0 +1,50 @@
<?php
/**
*
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Programs extends CI_Controller {
/**
* Calls the parent constructor.
*/
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('program');
}
/**
* Shows a list of all available programs.
*/
public function index() {
$data['programs'] = $this->program->getAll();
$this->load->view('admin/programs/list', $data);
}
/**
* Allows admins to edit a program.
*
* @param string $id
*/
public function edit($id = '') {
$program = $this->program->getByID($id);
if (!isset($program) || !is_array($program)){
show_404();
}
if ($this->form_validation->run('programs/edit') === true) {
if ($this->program->update($this->input->post('name'), $id)) {
$this->messages->add(sprintf(_("The program '%s' has been updated successfully"), $this->input->post('name')), 'success');
redirect('admin/programs', 303);
}
}
$data['program'] = $program;
$data['parameters'] = $this->program->getParameters($program['id']);
$this->load->view('admin/programs/edit', $data);
}
}

View File

@@ -55,4 +55,15 @@ class Ajax extends CI_Controller {
$data['description'] = $this->input->post('content'); $data['description'] = $this->input->post('content');
$this->project->update($this->session->userdata('active_project'), $data); $this->project->update($this->session->userdata('active_project'), $data);
} }
/**
* Sorts a programs parameters.
*
* @param string $id
*/
public function sort_parameters($programId) {
$this->load->model('program');
$this->program->sortParameters($this->input->post('parameters'), $programId);
}
} }

View File

@@ -1,5 +1,4 @@
<?php <?php
/* /*
* Copyright (c) 2011 Karsten Heiken <karsten@disposed.de> * Copyright (c) 2011 Karsten Heiken <karsten@disposed.de>
* *
@@ -24,29 +23,58 @@
/** /**
* Programs are used to do the actual calculation of a trial. * Programs are used to do the actual calculation of a trial.
* *
* @author Karsten Heiken <karsten@disposed.de> * @author Karsten Heiken <karsten@disposed.de>
* @author Eike Foken <kontakt@eikefoken.de>
*/ */
class Program extends CI_Model { class Program extends CI_Model {
/** /**
* Create a new program. * Calls the parent constructor.
*
* @param array $data the data of the new program
* @return bool was the insert successful
*/ */
public function create($data) { public function __construct() {
// TODO: stub parent::__construct();
return FALSE;
} }
/** /**
* Delete a program. * Creates a new program.
* @param string the program id to delete *
* @return bool was the deletion successful * @param string $name The name of the new program
* @return string|boolean Returns the ID of the new program, or FALSE if
* the insert was unsuccessful.
*/ */
public function delete($program_id) { public function create($name) {
return $this->db->delete('programs', array('id' => $program_id)); $this->load->helper('hash');
do { // generate unique hash
$id = random_hash('16');
} while ($this->db->where('id', $id)->from('programs')->count_all_results() > 0);
$this->db->insert('programs', array('id' => $id, 'name' => $name));
return $this->db->affected_rows() > 0 ? $id : false;
}
/**
* Updates a program.
*
* @param string $name The new name of the program
* @param string $id The ID of the program to update
* @return boolean Returns TRUE if the update was successful
*/
public function update($name, $id) {
$this->db->update('programs', array('name' => $name), array('id' => $id));
return $this->db->affected_rows() > 0;
}
/**
* Deletes a program.
*
* @param string $programId The program ID to delete
* @return boolean Returns TRUE if the deletion was successful
*/
public function delete($programId) {
return $this->db->delete('programs', array('id' => $programId));
} }
/** /**
@@ -59,13 +87,13 @@ class Program extends CI_Model {
} }
/** /**
* Get a specific program. * Gets a specific program.
* *
* @param string $prg_id The id of the program to get from the database * @param string $id The id of the program to get from the database
* @return array Declarative array with all available information of the program. * @return array Declarative array with all available information of the program.
*/ */
public function getById($prg_id) { public function getById($id) {
return $this->db->get_where('programs', array('id' => $prg_id))->row_array(); return $this->db->get_where('programs', array('id' => $id))->row_array();
} }
/** /**
@@ -87,13 +115,23 @@ class Program extends CI_Model {
* ) * )
* ) * )
* *
* @param type $program_id The program for which we want to get the parameters. * @param string $id The program for which we want to get the parameters.
* @return array The parameters * @return array The parameters
*/ */
public function getParameters($program_id) { public function getParameters($id) {
$query = $this->db->select('id, fieldname, readable, unit, description, type') $query = $this->db->order_by('order ASC')
->get_where('configuration_fields', array('program_id' => $program_id)); ->get_where('parameters', array('program_id' => $id));
return $query->result_array(); return $query->result_array();
} }
}
/**
*
* @param string $programId
*/
public function sortParameters($order, $programId) {
foreach ($order as $key => $value) {
$this->db->update('parameters', array('order' => $key), array('id' => $value));
}
}
}

View File

@@ -0,0 +1,78 @@
<?php $this->load->view('header');?>
<script>
$(document).ready(function() {
$('#parameters').tableDnD({
onDrop: function(table, row) {
/*var rows = table.tBodies[0].rows;
for (var i = 0; i < rows.length; i++) {
$(rows[i]).children().find('input[type=hidden]').val(i + 1);
}*/
$.post(SITE_URL + 'ajax/sort_parameters/<?=$program['id'];?>', $.tableDnD.serialize());
},
dragHandle: 'drag_handle'
});
});
</script>
<div id="content">
<div id="debug"></div>
<div class="title">
<h2><?php printf(_("Edit program '%s'"), $program['name']);?></h2>
</div>
<div class="box">
<form name="createUser" method="post" action="<?=site_url('admin/programs/edit/' . $program['id'])?>">
<h3><?=_('Required information');?></h3>
<ul>
<li>
<?=form_label(_('Name of the program'), 'name');?>
<span class="req">*</span>
<div>
<input type="text" name="name" id="name" class="medium text" value="<?=set_value('name', $program['name']);?>" />
<?=form_error('name')?>
</div>
</li>
</ul>
<p>
<a class="button save" href="javascript:void(0);" onclick="$('form[name=createUser]').submit();"><?=_('Save');?></a>
<a class="button cancel" href="<?=site_url('admin/programs');?>"><?=_('Cancel');?></a>
</p>
<h3><?=_('Parameters');?></h3>
<table class="tableList sortable" id="parameters">
<thead>
<tr>
<th scope="col">&nbsp;</th>
<th scope="col"><?=_('Readable name');?></th>
<th scope="col"><?=_('Unit');?></th>
<th scope="col"><?=_('Type');?></th>
<th scope="col"><?=_('Actions');?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($parameters as $parameter):
?>
<tr id="<?=$parameter['id'];?>">
<td class="drag_handle"></td>
<td><?=$parameter['readable'];?></td>
<td><?=$parameter['unit'];?></td>
<td><?=$parameter['type'];?></td>
<td><?=anchor('admin/programs/edit_parameter/' . $parameter['id'], _('Edit'));?> | <a href="javascript:deleteConfirm('<?=site_url('admin/programs/delete_parameter/' . $parameter['id']);?>');"><?=_('Delete');?></a></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<p>
<a class="button add" href="<?=site_url('admin/programs/add_parameter');?>"><?=_('Add new parameter');?></a>
</p>
</form>
</div>
</div>
<?php $this->load->view('footer');?>

View File

@@ -0,0 +1,36 @@
<?php $this->load->view('header');?>
<div id="content">
<div class="title">
<h2><?=_('Programs');?></h2>
</div>
<div class="box">
<h3><?=_('Available programs');?></h3>
<table class="tableList paginated">
<thead>
<tr>
<th scope="col"><?=_('Name');?></th>
<th scope="col"><?=_('Actions');?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($programs as $program):
?>
<tr>
<td><?=$program['name'];?></td>
<td><?=anchor('admin/programs/edit/' . $program['id'], _('Edit'));?> | <a href="javascript:deleteConfirm('<?=site_url('admin/programs/delete/' . $program['id']);?>');"><?=_('Delete');?></a></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<p><a class="button add" href="<?=site_url('admin/users/create')?>"><?=_('Create new user')?></a>
</div>
</div>
<?php $this->load->view('footer');?>

View File

@@ -59,7 +59,7 @@ p.success {
color: #008000; color: #008000;
} }
p.error, p.req { p.error, span.req {
color: #d8122d; color: #d8122d;
font-weight: normal; font-weight: normal;
} }

View File

@@ -48,3 +48,12 @@ table tbody tr:nth-child(even) {
table tbody tr:nth-child(odd) { table tbody tr:nth-child(odd) {
background: #f6f6f6; background: #f6f6f6;
} }
td.drag_handle {
width: 16px;
}
td.drag_handle-show {
background: url(../images/icons/arrow-resize-090.png) center center no-repeat;
cursor: move;
}

View File

@@ -163,7 +163,18 @@ $(document).ready(function() {
table.trigger('repaginate'); table.trigger('repaginate');
}); });
$('.sortable').tableDnD(); /*
* Sortable tables
*/
$('.sortable').tableDnD({
dragHandle: 'drag_handle'
});
$('.sortable tr').hover(function() {
$(this).find('td.drag_handle').addClass('drag_handle-show');
}, function() {
$(this).find('td.drag_handle').removeClass('drag_handle-show');
});
/* /*
* In-place editor * In-place editor