Code:
class MProducts extends Model{
function MProducts(){
parent::Model();
}
...
...
/**
* This will add product
*/
function addProduct(){
$data = $this->_uploadFile();
$this->db->insert('omc_products', $data);
...
...
}
/**
* This will edit the product.
*/
function updateProduct(){
$data = $this->_uploadFile();
$this->db->where('id', $_POST['id']);
$this->db->update('omc_products', $data);
$this->db->where('product_id', $_POST['id']);
$this->db->delete('omc_products_colors');
$this->db->where('product_id', $_POST['id']);
$this->db->delete('omc_products_sizes');
...
...
}
/**
* This function will upload image and thumbnail and return $data['image']
* and $data['thumbnail'] and other $_POST details data
*
*/
function _uploadFile(){
$data = array(
'name' => db_clean($_POST['name']),
'shortdesc' => db_clean($_POST['shortdesc']),
'longdesc' => db_clean($_POST['longdesc'],5000),
'status' => db_clean($_POST['status'],8),
'class' => db_clean($_POST['class'],30),
'grouping' => db_clean($_POST['grouping'],16),
'category_id' => id_clean($_POST['category_id']),
'featured' => db_clean($_POST['featured'],20),
'price' => db_clean($_POST['price'],16),
'other_feature' => db_clean($_POST['other_feature'],20)
);
$catname = array();
$category_id = $data['category_id'];
$catname = $this->MCats->getCategoryNamebyProduct($category_id);
foreach ($catname as $key => $name){
$foldername = strtolower($name);
$foldername = str_replace(" ", "_", $foldername);
}
if ($_FILES){
$config['upload_path'] = './images/'.$foldername.'/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (strlen($_FILES['image']['name'])){
if(!$this->upload->do_upload('image')){
$this->upload->display_errors();
exit();
}
$image = $this->upload->data();
if ($image['file_name']){
$data['image'] = "images/".$foldername."/".$image['file_name'];
}
}
$config['upload_path'] = './images/'.$foldername.'/thumbnails/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = true;
$config['max_width'] = '0';
$config['max_height'] = '0';
//initialize otherwise thumb will take the first one
$this->upload->initialize($config);
if (strlen($_FILES['thumbnail']['name'])){
if(!$this->upload->do_upload('thumbnail')){
$this->upload->display_errors();
exit();
}
$thumb = $this->upload->data();
if ($thumb['file_name']){
$data['thumbnail'] = "images/".$foldername."/thumbnails/".$thumb['file_name'];
}
}
}
return $data;
}