From 020bd130589e4382b73c75a483025662efe442f1 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Mon, 15 Aug 2011 01:03:03 +0200 Subject: [PATCH] Begin implementing programs management --- application/config/form_validation.php | 13 ++++ application/controllers/admin/programs.php | 50 +++++++++++++ application/controllers/ajax.php | 11 +++ application/models/program.php | 84 ++++++++++++++++------ application/views/admin/programs/edit.php | 78 ++++++++++++++++++++ application/views/admin/programs/list.php | 36 ++++++++++ assets/css/form.css | 2 +- assets/css/table.css | 9 +++ assets/js/scattport.js | 13 +++- 9 files changed, 271 insertions(+), 25 deletions(-) create mode 100644 application/controllers/admin/programs.php create mode 100644 application/views/admin/programs/edit.php create mode 100644 application/views/admin/programs/list.php diff --git a/application/config/form_validation.php b/application/config/form_validation.php index 8f779ed..2a24c08 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -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. * diff --git a/application/controllers/admin/programs.php b/application/controllers/admin/programs.php new file mode 100644 index 0000000..c7703c0 --- /dev/null +++ b/application/controllers/admin/programs.php @@ -0,0 +1,50 @@ + + */ +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); + } +} \ No newline at end of file diff --git a/application/controllers/ajax.php b/application/controllers/ajax.php index 307bf0a..fdbe9bd 100644 --- a/application/controllers/ajax.php +++ b/application/controllers/ajax.php @@ -55,4 +55,15 @@ class Ajax extends CI_Controller { $data['description'] = $this->input->post('content'); $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); + } + } \ No newline at end of file diff --git a/application/models/program.php b/application/models/program.php index 324a012..2a1e9d8 100644 --- a/application/models/program.php +++ b/application/models/program.php @@ -1,5 +1,4 @@ * @@ -24,29 +23,58 @@ /** * Programs are used to do the actual calculation of a trial. - * + * * @author Karsten Heiken + * @author Eike Foken */ class Program extends CI_Model { /** - * Create a new program. - * - * @param array $data the data of the new program - * @return bool was the insert successful + * Calls the parent constructor. */ - public function create($data) { - // TODO: stub - return FALSE; + public function __construct() { + parent::__construct(); } /** - * Delete a program. - * @param string the program id to delete - * @return bool was the deletion successful + * Creates a new program. + * + * @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) { - return $this->db->delete('programs', array('id' => $program_id)); + public function create($name) { + $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. */ - public function getById($prg_id) { - return $this->db->get_where('programs', array('id' => $prg_id))->row_array(); + public function getById($id) { + 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 */ - public function getParameters($program_id) { - $query = $this->db->select('id, fieldname, readable, unit, description, type') - ->get_where('configuration_fields', array('program_id' => $program_id)); + public function getParameters($id) { + $query = $this->db->order_by('order ASC') + ->get_where('parameters', array('program_id' => $id)); return $query->result_array(); } -} \ No newline at end of file + + /** + * + * @param string $programId + */ + public function sortParameters($order, $programId) { + foreach ($order as $key => $value) { + $this->db->update('parameters', array('order' => $key), array('id' => $value)); + } + } +} diff --git a/application/views/admin/programs/edit.php b/application/views/admin/programs/edit.php new file mode 100644 index 0000000..a713d62 --- /dev/null +++ b/application/views/admin/programs/edit.php @@ -0,0 +1,78 @@ +load->view('header');?> + + + +
+ +
+ +
+

+
+ +
+
+

+
    +
  • + + * +
    + + +
    +
  • +
+

+ + +

+ +

+ + + + + + + + + + + + + + + + + + + + + +
 
|
+

+ +

+
+
+
+ +load->view('footer');?> \ No newline at end of file diff --git a/application/views/admin/programs/list.php b/application/views/admin/programs/list.php new file mode 100644 index 0000000..e4f091f --- /dev/null +++ b/application/views/admin/programs/list.php @@ -0,0 +1,36 @@ +load->view('header');?> + +
+ +
+

+
+ +
+

+ + + + + + + + + + + + + + + +
|
+ +

+

+
+ +load->view('footer');?> diff --git a/assets/css/form.css b/assets/css/form.css index 2a290a3..38cb201 100644 --- a/assets/css/form.css +++ b/assets/css/form.css @@ -59,7 +59,7 @@ p.success { color: #008000; } -p.error, p.req { +p.error, span.req { color: #d8122d; font-weight: normal; } diff --git a/assets/css/table.css b/assets/css/table.css index d948c4d..d4b3833 100644 --- a/assets/css/table.css +++ b/assets/css/table.css @@ -48,3 +48,12 @@ table tbody tr:nth-child(even) { table tbody tr:nth-child(odd) { 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; +} diff --git a/assets/js/scattport.js b/assets/js/scattport.js index 9b09d82..10e5923 100644 --- a/assets/js/scattport.js +++ b/assets/js/scattport.js @@ -163,7 +163,18 @@ $(document).ready(function() { 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