Implement CSV file upload for creating parameters
This commit is contained in:
@@ -76,6 +76,54 @@ class Parameters extends Admin_Controller {
|
||||
$this->load->view('admin/parameters/create', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows admins to upload a CSV file to create a bunch of parameters.
|
||||
*
|
||||
* @param string $programId
|
||||
*/
|
||||
public function upload_csv($programId = '') {
|
||||
$program = $this->program->getByID($programId);
|
||||
|
||||
if (empty($programId) || !isset($program['id'])) {
|
||||
show_404();
|
||||
}
|
||||
|
||||
if (count($_FILES) > 0) {
|
||||
if ($_FILES['csv_file']['type'] == 'text/csv') {
|
||||
$row = 1;
|
||||
$inserted = 0;
|
||||
$handler = fopen($_FILES['csv_file']['tmp_name'], "r");
|
||||
|
||||
while (($parameter = fgetcsv($handler, 1000, ',')) !== false) {
|
||||
if ($row > 1) {
|
||||
$data = array(
|
||||
'program_id' => $program['id'],
|
||||
'name' => $parameter[0],
|
||||
'readable' => $parameter[1],
|
||||
'unit' => $parameter[2],
|
||||
'type' => $parameter[3],
|
||||
'default_value' => $parameter[4],
|
||||
'description' => $parameter[5],
|
||||
);
|
||||
|
||||
if ($this->parameter->create($data)) {
|
||||
$inserted++;
|
||||
}
|
||||
}
|
||||
$row++;
|
||||
}
|
||||
|
||||
if ($inserted > 0) {
|
||||
$this->messages->add(sprintf(_('%s parameters were successfully created.'), $inserted), 'success');
|
||||
}
|
||||
} else {
|
||||
$this->messages->add(_('No CSV file specified.'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
redirect('admin/programs/edit/' . $program['id'], 303);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows admins to edit a parameter.
|
||||
*
|
||||
|
||||
@@ -20,7 +20,7 @@ $(document).ready(function() {
|
||||
</div>
|
||||
|
||||
<div class="box">
|
||||
<form name="createUser" method="post" action="<?=site_url('admin/programs/edit/' . $program['id'])?>">
|
||||
<form name="editProgram" method="post" action="<?=site_url('admin/programs/edit/' . $program['id'])?>">
|
||||
<h3><?=_('Required information');?></h3>
|
||||
<ul>
|
||||
<li>
|
||||
@@ -33,41 +33,56 @@ $(document).ready(function() {
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
<a class="button save" href="javascript:void(0);" onclick="$('form[name=createUser]').submit();"><?=_('Save');?></a>
|
||||
<a class="button save" href="javascript:void(0);" onclick="$('form[name=editProgram]').submit();"><?=_('Save');?></a>
|
||||
<a class="button cancel" href="<?=site_url('admin/programs');?>"><?=_('Cancel');?></a>
|
||||
</p>
|
||||
</form>
|
||||
|
||||
<h3><?=_('Parameters');?></h3>
|
||||
<table class="tableList sortable" id="parameters">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"> </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>
|
||||
<h3><?=_('Parameters');?></h3>
|
||||
<table class="tableList sortable" id="parameters">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"> </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/parameters/edit/' . $parameter['id'], _('Edit'));?> | <a href="javascript:deleteConfirm('<?=site_url('admin/parameters/delete/' . $parameter['id']);?>');"><?=_('Delete');?></a></td>
|
||||
</tr>
|
||||
<tr id="<?=$parameter['id'];?>">
|
||||
<td class="drag_handle"></td>
|
||||
<td><?=$parameter['readable'];?></td>
|
||||
<td><?=$parameter['unit'];?></td>
|
||||
<td><?=$parameter['type'];?></td>
|
||||
<td><?=anchor('admin/parameters/edit/' . $parameter['id'], _('Edit'));?> | <a href="javascript:deleteConfirm('<?=site_url('admin/parameters/delete/' . $parameter['id']);?>');"><?=_('Delete');?></a></td>
|
||||
</tr>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>
|
||||
<a class="button add" href="<?=site_url('admin/parameters/create/' . $program['id']);?>"><?=_('Add new parameter');?></a>
|
||||
</p>
|
||||
</form>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>
|
||||
<a class="button add" href="<?=site_url('admin/parameters/create/' . $program['id']);?>"><?=_('Add new parameter');?></a>
|
||||
</p>
|
||||
|
||||
<form name="uploadCSV" method="post" action="<?=site_url('admin/parameters/upload_csv/' . $program['id'])?>" enctype="multipart/form-data">
|
||||
<ul>
|
||||
<li>
|
||||
<?=form_label(_('CVS file'), 'csv_file');?>
|
||||
<div>
|
||||
<input type="file" name="csv_file" id="csv_file" class="file" />
|
||||
<?=form_error('csv_file')?>
|
||||
</div>
|
||||
<label class="note">You can upload a CVS file, containing a bunch of parameters. The rows of the file must be in the following format: <em>parameter name, human-readable name, unit, type, default value, description</em>. The first row is reserved for headlines.</label>
|
||||
</li>
|
||||
</ul>
|
||||
<p>
|
||||
<a class="button upload" href="javascript:void(0);" onclick="$('form[name=uploadCSV]').submit();"><?=_('Upload');?></a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user