function edit($id = FALSE) { //load block submit helper and append in the head $this->template->append_metadata(block_submit_button()); //get the $id and sanitize $id = $this->uri->segment(4) ? $this->uri->segment(4) : $this->input->post('id', TRUE); $id = $id != 0 ? filter_var($id, FILTER_VALIDATE_INT) : NULL; //search the categories and send to the view $this->load->model('category'); //create control variables $this->template->title('web_edit_product'); $this->template->set('updType', 'edit'); $this->template->set('page', $this->uri->segment(6) ? $this->uri->segment(6) : $this->input->post('page', TRUE)); $this->template->set('parent_id', $this->uri->segment(5) ? $this->uri->segment(5) : $this->input->post('parent_id', TRUE)); $this->template->set('categories', Category::get_formatted_combo()); //variables for check the upload $form_data_aux = array(); $files_to_delete = array(); //redirect if it´s no correct if (!$id) { $this->session->set_flashdata('message', array('type' => 'warning', 'text' => lang('web_object_not_exist'))); redirect('products/'); } //Rules for validation $this->set_rules(); if ($this->form_validation->run() == FALSE) { //search the item to show in edit form $this->template->set('product', Product::find_by_id($id)); //load the view and the layout $this->template->build('products/create'); } else { $data['product'] = Product::find($this->input->post('id', TRUE)); $this->template->set('product', $data['product']); foreach ($_FILES as $index => $value) { if ($value['name'] != '') { //initializing the upload library $this->load->library('upload'); $this->upload->initialize($this->set_upload_options('products')); //upload the image if (!$this->upload->do_upload($index)) { $this->template->set('upload_error', $this->upload->display_errors("<span class='error'>", "</span>")); //load the view and the layout $this->template->build('products/create'); return FALSE; } else { //create an array to send to image_lib library to create the thumbnail $info_upload = $this->upload->data(); //Save the name an array to save on BD before $form_data_aux[$index] = $info_upload["file_name"]; //Save the name of old files to delete array_push($files_to_delete, $data['product']->{$index}); //Load and initializing the imagelib library to create the thumbnail $this->load->library('image_lib'); $this->image_lib->initialize($this->set_thumbnail_options($info_upload, 'products')); //create the thumbnail if (!$this->image_lib->resize()) { $this->template->set('upload_error', $this->image_lib->display_errors("<span class='error'>", "</span>")); //load the view and the layout $this->template->build('products/create'); return FALSE; } } } } // build array for the model $form_data = array('name' => $this->input->post('name', TRUE), 'description' => $this->input->post('description', TRUE), 'active' => $this->input->post('active', TRUE), 'option' => $this->input->post('option', TRUE), 'category_id' => $this->input->post('category_id', TRUE), 'id' => $this->input->post('id', TRUE)); //add the aux form data to the form data array to save $form_data = array_merge($form_data_aux, $form_data); //find the item to update $product = Product::find($this->input->post('id', TRUE)); //save the old image to delete $old_image = $product->image; // run insert model to write data to db $product->update_attributes($form_data); if ($product->is_valid()) { $this->session->set_flashdata('message', array('type' => 'success', 'text' => lang('web_edit_success'))); //delete the old images foreach ($files_to_delete as $index) { if (is_file(FCPATH . 'public/uploads/products/img/' . $index)) { unlink(FCPATH . 'public/uploads/products/img/' . $index); } if (is_file(FCPATH . 'public/uploads/products/img/thumbs/' . $index)) { unlink(FCPATH . 'public/uploads/products/img/thumbs/' . $index); } } } if ($product->is_invalid()) { $this->session->set_flashdata('message', array('type' => 'error', 'text' => $product->errors->full_messages())); } if ($this->input->post('parent_id')) { redirect('admin/products/product_list/' . $this->input->post('category_id', TRUE) . '/' . $this->input->post('page', TRUE)); } else { redirect('admin/products/' . $this->input->post('page', TRUE)); } } }