[eluser]Bramme[/eluser]
Hey all,
I've got a question for the (maybe) more experienced CI user.
I'm looking for a best practice to make things more DRY:
I've got a little backend that handles news posting/editting. The posting and editting is done in two different methods but they look largely the same, seeing as they're both based on the same form (even the same view, I define the form action from within my controller), the procedure is highly similar etc... There's only a few minor differences.
Actually, here's the code:
Code:
function new_item()
{
$this->auth->restrict();
$this->load->library('validation');
//rules
$this->validation->set_rules($rules);
//fields
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<li>', '</li>');
$standard_values['poster'] = 'MickM';
$standard_values['type'] = 'news';
if ($this->validation->run() == TRUE)
{
foreach ($fields as $field => $name)
{
$insert[$field] = $this->input->post($field);
}
$timestamp = time();
$insert['datetime'] = unix_to_human($timestamp, TRUE, 'eu');
$insert['type'] = $this->input->post('type');
if($_FILES['thumb_url']['error'] != 4)
{
$upload_config['upload_path'] = './_assets/images/thumbs/';
$upload_config['allowed_types'] = 'gif|jpg|png';
$upload_config['max_size'] = '100';
$upload_config['max_width'] = '150';
$upload_config['max_height'] = '150';
$this->load->library('upload', $upload_config);
if ($this->upload->do_upload('thumb_url'))
{
$info = $this->upload->data();
$insert['thumb_url'] = '/_assets/images/thumbs/'.$info['file_name'];
} else {
echo $this->upload->display_errors();
die();
}
}
else
{
$insert['thumb_url'] = '/_assets/images/thumbs/m.gif';
}
if ( ! $this->db->insert('news', $insert))
{
send_notice('error', 'Something went wrong with the query', 'self');
}
else
{
send_notice('success', 'Item successfully added', 'self');
}
}
else
{
if ( ! empty($this->validation->error_string))
{
send_notice('validation_error', $this->validation->error_string);
$standard_values['poster'] = $this->input->post('poster');
$standard_values['type'] = $this->input->post('type');
$standard_values['title'] = $this->input->post('title');
$standard_values['content'] = $this->input->post('content');
}
}
$data['form'] = $this->table_editor->create_form($standard_values);
$data['action'] = '/admin/news/new_item';
$this->template->write_view('region_content', '_admin/news/new', $data);
$this->template->render();
}
I know it's rather lengthy, but it's just to give you an idea how similar it is!