Fix trial creation

This commit is contained in:
Eike Foken
2011-09-08 00:10:52 +02:00
parent 7b98b416ee
commit 33e116ed60
3 changed files with 68 additions and 55 deletions

View File

@@ -255,6 +255,11 @@ $config['trials/create'] = array(
'label' => _('Program'),
'rules' => 'required|alpha_numeric|trim',
),
array(
'field' => '3dmodel',
'label' => _('3D model'),
'rules' => 'file_allowed_type[obj]',
),
);
/* End of file form_validation.php */

View File

@@ -35,7 +35,6 @@ class Trials extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('upload');
$this->load->model('parameter');
$this->load->model('program');
$this->load->model('project');
@@ -63,55 +62,65 @@ class Trials extends CI_Controller {
$parameters[$program['id']] = $this->parameter->getAll($program['id']);
}
if ($this->form_validation->run('trials/create') === true) {
$config = array(
'upload_path' => '/tmp',
'allowed_types' => '*',
'max_size' => 64*1024, // set maximum file size to 64 megabytes
'file_name' => 'default',
);
$this->upload->initialize($config);
if (is_null($project['default_model'])) {
$this->load->config('form_validation');
foreach ($this->config->item('trials/create') as $rule) { // restore old rules
$this->form_validation->set_rules($rule['field'], $rule['label'], $rule['rules']);
}
if (!$this->upload->do_upload('3dmodel')) {
$this->messages->add(_('There was an error while uploading the selected 3d model.'), 'error');
} else {
$data = array(
$this->form_validation->set_rules('3dmodel', _('3d model'), 'file_required|file_allowed_type[obj]');
}
// run form validation
if ($this->form_validation->run('trials/create') === true) {
$data = array(
'name' => $this->input->post('name'),
'description' => $this->input->post('description'),
'program_id' => $this->input->post('program_id'),
'project_id' => $projectId,
'creator_id' => $this->session->userdata('user_id'),
);
$data['trial_id'] = $this->trial->create($data);
if (isset($data['trial_id'])) {
$this->load->helper('directory');
$trialPath = FCPATH . 'uploads/' . $projectId . '/' . $data['trial_id'] . '/';
mkdirs($trialPath);
$config = array(
'upload_path' => $trialPath,
'allowed_types' => '*',
'overwrite' => true,
'file_name' => 'default',
);
$this->load->library('upload', $config);
$trialId = $this->trial->create($data);
if ($trialId) {
foreach ($_POST as $key => $value) {
if (preg_match('/^param-[0-9a-z]+/', $key) && !empty($value)) {
$param['parameter_id'] = substr($key, 6, 16);
$param['value'] = $this->input->post($key);
$this->trial->addParameter($param, $trialId);
}
if ($_FILES['3dmodel']['tmp_name'] != '') {
if ($this->upload->do_upload('3dmodel')) {
$model = $this->upload->data();
} else {
$this->messages->add(_('The selected model could not be uploaded.'), 'error');
}
$this->load->helper('directory');
$trialPath = FCPATH . 'uploads/' . $projectId . '/' . $trialId . '/';
mkdirs($trialPath);
$model = $this->upload->data();
if (!copy($model['full_path'], $trialPath . $model['file_name'])) {
$this->messages->add(_('The selected 3d model could not be copied to trial path.'), 'error');
}
$program = $this->program->getById($data['program_id']);
$this->load->library('program_runner', array('program_driver' => $program['driver']));
$this->program_runner->createJob($trialId);
//redirect('trials/detail/' . $trialId, 'refresh');
redirect('projects/detail/' . $projectId, 303);
} else {
$this->messages->add(_('The trial could not be created.'), 'error');
}
// save parameters to db
foreach ($_POST as $key => $value) {
if (preg_match('/^param-[0-9a-z]+/', $key) && !empty($value)) {
$param['parameter_id'] = substr($key, 6, 16);
$param['value'] = $this->input->post($key);
$this->trial->addParameter($param, $data['trial_id']);
}
}
// TODO: Don't start jobs automatically
$program = $this->program->getById($data['program_id']);
$this->load->library('program_runner', array('program_driver' => $program['driver']));
$this->program_runner->createJob($data['trial_id']);
redirect('/projects/detail/' . $projectId, 303);
} else {
$this->messages->add(_('The trial could not be created.'), 'error');
}
}

View File

@@ -21,29 +21,28 @@
</li>
<li>
<?=form_label(_('Description'), 'description');?>
<span class="req">*</span>
<span class="req">*</span><br />
<label class="note"><?=_('A description is useful if you want to share this trial with co-workers.');?></label>
<div>
<textarea name="description" id="description" rows="6" cols="60" class="textarea"><?=set_value('description');?></textarea>
<?=form_error('description');?>
</div>
<label class="note"><?=_('A description is useful if you want to share this trial with co-workers.');?></label>
</li>
<li>
<h4><?=_('3D model');?></h4>
<?
if (file_exists(FCPATH.'uploads/'.$project['id'].'/default.txt')):
<?php
if (!is_null($project['default_model'])):
?>
<div class="notice">
<strong><?=_('There is a default model set for this project.');?></strong><br />
<?=_('If you want to use a different model for this trial, you can upload it here.');?>
</div>
<?
<?php
endif;
?>
<div>
<input type="file" class="file" name="3dmodel" value="<?=set_value('3dmodel');?>" />
<?=form_error('3dmodel');?>
<?=$this->upload->display_errors('<span class="error">', '</span>');?>
</div>
</li>
</ul>
@@ -52,30 +51,30 @@
<div class="box">
<h3><?=_('Application-specific parameters');?></h3>
<?
if (isset($defaultconfig)):
<?php
if (!is_null($project['default_config'])):
?>
<div class="notice">
<strong><?=_('There is a default configuration set for this project.');?></strong><br />
<?=_('This form contains the default values. You can adjust them for this trial.');?><br />
<?=_('The default configuration will not be modified.');?>
</div>
<?
<?php
endif;
?>
<h4><?=_('Application to use for the computation');?></h4>
<input type="hidden" name="program_id" id="program_id" value="<?=set_value('program_id');?>" />
<?=form_error('program_id');?>
<p>
<?
<?php
foreach ($programs as $program):
?>
<a class="button" href="javascript:void(0);" onclick="$('.program-parameters').hide();$('#<?=$program['id'];?>-params').show();$('.button').removeClass('locked');$(this).addClass('locked');$('input[name=program_id]').val('<?=$program['id'];?>');return false;"><?=$program['name'];?></a>
<?
<?php
endforeach;
?>
</p>
<?
<?php
foreach ($programs as $program):
?>
@@ -91,7 +90,7 @@
</tr>
</thead>
<tbody>
<?
<?php
foreach ($parameters[$program['id']] as $param):
?>
<tr>
@@ -111,14 +110,14 @@
</td>
<td><?=$param['unit'];?></td>
</tr>
<?
<?php
endforeach;
?>
</tbody>
</table>
</p>
</div>
<?
<?php
endforeach;
?>
<p>