Ejemplo n.º 1
0
 public function store($data)
 {
     $row =& $this->getTable();
     if (!$row->bind($data)) {
         $this->setError($this->_db->getErrorMsg());
         return false;
     }
     // Storing image name in the database
     $option = JRequest::getVar('option');
     $filename = "";
     // Get File name, tmp_name
     $file = JRequest::getVar('category_full_image', array(), 'files', 'array');
     if (count($file) > 0) {
         // Make the filename unique
         $filename = JPath::clean(time() . '_' . $file['name']);
         $filename = str_replace(" ", "_", $filename);
     }
     if (isset($data['image_delete'])) {
         unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/thumb/' . $data['old_image']);
         unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $data['old_image']);
         $query = "UPDATE " . $this->_table_prefix . "category set category_thumb_image = '',category_full_image = ''  where category_id =" . $row->category_id;
         $this->_db->setQuery($query);
         $this->_db->query();
     }
     if (count($_FILES) > 0 && $_FILES['category_full_image']['name'] != "") {
         $newwidth = THUMB_WIDTH;
         $newheight = THUMB_HEIGHT;
         $row->category_full_image = $filename;
         $row->category_thumb_image = $filename;
         // Get extension of the file
         $filetype = JFile::getExt($file['name']);
         $src = $file['tmp_name'];
         // Specific path of the file
         $dest = REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $filename;
         JFile::upload($src, $dest);
     } else {
         if (isset($data['category_image']) && $data['category_image'] != null) {
             $image_split = explode('/', $data['category_image']);
             // Make the filename unique
             $filename = JPath::clean(time() . '_' . $image_split[count($image_split) - 1]);
             $row->category_full_image = $filename;
             $row->category_thumb_image = $filename;
             // Image Upload
             $newwidth = THUMB_WIDTH;
             $newheight = THUMB_HEIGHT;
             $src = JPATH_ROOT . '/' . $data['category_image'];
             $dest = REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $filename;
             copy($src, $dest);
         }
     }
     // Get File name, tmp_name
     $backfile = JRequest::getVar('category_back_full_image', '', 'files', 'array');
     if (isset($data['image_back_delete'])) {
         unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/thumb/' . $data['old_back_image']);
         unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $data['old_back_image']);
         $query = "UPDATE " . $this->_table_prefix . "category set category_back_full_image = ''  where category_id =" . $row->category_id;
         $this->_db->setQuery($query);
         $this->_db->query();
     }
     if (count($backfile) > 0 && $backfile['name'] != "") {
         // Make the filename unique
         $filename = JPath::clean(time() . '_' . $backfile['name']);
         $row->category_back_full_image = $filename;
         // Get extension of the file
         $filetype = JFile::getExt($backfile['name']);
         $src = $backfile['tmp_name'];
         // Specific path of the file
         $dest = REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $filename;
         JFile::upload($src, $dest);
     }
     // Upload back image end
     if (!$row->store()) {
         $this->setError($this->_db->getErrorMsg());
         return false;
     }
     if (!$data['category_id']) {
         $newcatid = $this->_db->insertid();
         if (isset($_POST['category_parent_id'])) {
             $parentcat = $_POST['category_parent_id'];
         } else {
             $parentcat = $data['category_parent_id'];
         }
         $query = 'INSERT INTO ' . $this->_table_prefix . 'category_xref(category_parent_id,category_child_id) VALUES ("' . $parentcat . '","' . $newcatid . '");';
         $this->_db->setQuery($query);
         $this->_db->query();
     } else {
         $newcatid = $data['category_id'];
         if (isset($_POST['category_parent_id'])) {
             $parentcat = $_POST['category_parent_id'];
         } else {
             $parentcat = $data['category_parent_id'];
         }
         $query = 'UPDATE ' . $this->_table_prefix . 'category_xref SET category_parent_id= "' . $parentcat . '"  WHERE category_child_id = "' . $newcatid . '" ';
         $this->_db->setQuery($query);
         $this->_db->query();
         // Sheking for the image at the updation time
         if ($_FILES['category_full_image']['name'] != "") {
             @unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/thumb/' . $_POST['old_image']);
             @unlink(REDSHOP_FRONT_IMAGES_RELPATH . 'category/' . $_POST['old_image']);
         }
     }
     // Extra Field Data Saved
     $field = new extra_field();
     $field->extra_field_save($data, 2, $newcatid);
     // Start Accessory Product
     if (count($data['product_accessory']) > 0 && is_array($data['product_accessory'])) {
         $data['product_accessory'] = array_merge(array(), $data['product_accessory']);
         $product_category = new product_category();
         $product_list = $product_category->getCategoryProductList($newcatid);
         for ($p = 0; $p < count($product_list); $p++) {
             $product_id = $product_list[$p]->id;
             for ($a = 0; $a < count($data['product_accessory']); $a++) {
                 $acc = $data['product_accessory'][$a];
                 $accessory_id = $product_category->CheckAccessoryExists($product_id, $acc['child_product_id']);
                 if ($product_id != $acc['child_product_id']) {
                     $accdetail =& $this->getTable('accessory_detail');
                     $accdetail->accessory_id = $accessory_id;
                     $accdetail->category_id = $newcatid;
                     $accdetail->product_id = $product_id;
                     $accdetail->child_product_id = $acc['child_product_id'];
                     $accdetail->accessory_price = $acc['accessory_price'];
                     $accdetail->oprand = $acc['oprand'];
                     $accdetail->ordering = $acc['ordering'];
                     $accdetail->setdefault_selected = isset($acc['setdefault_selected']) && $acc['setdefault_selected'] == 1 ? 1 : 0;
                     if (!$accdetail->store()) {
                         $this->setError($this->_db->getErrorMsg());
                         return false;
                     }
                 }
             }
         }
     }
     // End Accessory Product insert
     return $row;
 }