[eluser]bugboy[/eluser]
I have a form that allows the user to upload an image as well.
The image isn't a required element so can be left out but its there if the user wants to have one.
Now this is my controller I feel its very messy so i'm looking for advice on how i can best do it.
Code:
function add_news()
{
$error = '';
$image = '0';
$insert_me = FALSE;
$data['module'] = $this->_module;
$data['controller'] =$this->_controller;
$data['area']= $this->_area;
$data['page'] = $this->_controller.'/base';
$data['content'] = $this->_controller.'/add_news';
// validation area
$rules['title'] = "trim|required|xss_clean";
$rules['tag'] = "trim|required|xss_clean";
$rules['dscpn'] = "trim|required|xss_clean";
$rules['text'] = "trim|required|xss_clean";
$rules['keyword'] = "trim|xss_clean";
$this->validation->set_rules($rules);
$this->validation->run();
$fields['title'] = 'title';
$fields['tag'] = 'tag';
$fields['dscpn'] = 'description';
$fields['text'] = 'text';
$fields['keyword'] = 'keyword';
$fields['day'] = 'day';
$fields['month'] = 'month';
$fields['year'] = 'year';
$this->validation->set_fields($fields);
$data['heading'] = 'Add a News artical';
$tag = $this->input->post('tag');
if(!empty($tag)){
$checkTag = $this->admin->checkNewsTag($tag);
}else{
$checkTag = FALSE;
}
if ($this->validation->run() == FALSE || $checkTag == TRUE)
{
if($checkTag == TRUE){
$data['tag_message'] = '<div class="error">Sorry this tag is already in use</div>';
}
$data['image_error'] = $error;
$this->load->vars($data);
$this->load->view($this->_container);
}else{
//if validation run success for (input,texarea,radio,select,etc..) then check for upload validation below
if($_FILES['image']['name'] != ''){
if (!$this->upload->do_upload('image')){
//fail upload and set String validation error
$error = array($this->upload->display_errors());
$data['image_error'] = $error;
$this->load->vars($data);
$this->load->view($this->_container);
}else{
//upload done and work now resize me
$upload_data = $this->upload->data();
$image = $upload_data["file_name"];
// run resize and thumbnail
$this->image_misc->resizeImage($image, './uploaded_images/', 500, 500);
$this->image_misc->resizeImage($image, './uploaded_images/', 75, 50, TRUE);
$insert_me = TRUE;
}
}else{
$insert_me = TRUE;
}
}
if($insert_me == TRUE){
$date = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
$addUnderScore = str_replace(" ", "_", trim($this->input->post('tag')));
$insert = array (
'title' => $this->input->post('title'),
'keyword' => $this->input->post('keyword'),
'dscpn' => ascii_to_entities($this->input->post('dscpn')),
'tag' => $addUnderScore,
'text' => ascii_to_entities($this->input->post('text')),
'author' => $this->db_session->userdata('user_name'),
'image' => $image,
'date' => $date
);
$insert = $this->admin->addNews($insert);
redirect($this->_module.'/console', 'refresh');
}
}
It works a treat but any advice would be greatly appreciated