Example #1
0
 private function saveProduct($is_new = true)
 {
     if ($is_new == true && $this->req['product']['name'] == "" || $this->req['product']['id'] == "" && $is_new == false) {
         return false;
     }
     $valid = true;
     // add checks here
     if ($valid == true) {
         $file = new FileTransfer();
         $json_template = json_decode($this->req['product']['templatefile'], true);
         $images = "";
         if (isset($json_template['objects'][0])) {
             foreach ($json_template['objects'] as $k => $v) {
                 if ($v['type'] == "image" && strpos($v['src'], "/temp/") != false) {
                     $new_url = $file->move($v['src']);
                     if ($new_url != "") {
                         $this->req['product']['templatefile'] = str_replace($v['src'], $new_url, $this->req['product']['templatefile']);
                     }
                 }
                 if ($v['type'] == "image") {
                     $images = $images . "##" . $v['src'];
                 }
             }
         }
         if ($json_template['overlayImage']['type'] == "image") {
             $new_url = $file->move($json_template['overlayImage']['src']);
             if ($new_url != "") {
                 $this->req['product']['templatefile'] = str_replace($json_template['overlayImage']['src'], $new_url, $this->req['product']['templatefile']);
             }
         }
         if ($is_new == true) {
             $this->productModel = new ProductModel();
         } else {
             $temp = $this->productModel->fetchProductById($this->req['product']['id']);
             $this->productModel = $temp[0];
             $str_old_template = $this->productModel->getTemplateFile();
             $json_old_template = json_decode($str_old_template, true);
             if (isset($json_old_template['objects'][0])) {
                 foreach ($json_old_template['objects'] as $k => $v) {
                     if ($v['type'] == "image" && strpos($images, $v['src']) == false) {
                         $file->delete($v['src']);
                     }
                 }
             }
             $this->productModel->setProductId($this->req['product']['id']);
         }
         $this->productModel->setTitle($this->req['product']['name']);
         $this->productModel->setSku($this->req['product']['sku']);
         $this->productModel->setModel($this->req['product']['model']);
         $this->productModel->setDescription($this->req['product']['description']);
         $this->productModel->setInStock($this->req['product']['instock']);
         $this->productModel->setInsertTime(date('Y-m-d H:i:s'));
         $this->productModel->setIsActive($this->req['product']['status']);
         $this->productModel->setMetaDescription($this->req['product']['meta_description']);
         $this->productModel->setMetaKeywords($this->req['product']['meta_keywords']);
         $this->productModel->setPrice($this->req['product']['price']);
         $this->productModel->setQty($this->req['product']['qty']);
         $this->productModel->setSummary($this->req['product']['summary']);
         if (strlen($this->req['product']['templatefile']) < 40) {
             $this->req['product']['templatefile'] = "";
         }
         $this->productModel->setTemplateFile($this->req['product']['templatefile']);
         $this->productModel->setUpdateTime(date('Y-m-d H:i:s'));
         $this->productModel->setCategories("," . implode(",", $this->req['product']['categories']) . ",");
         $this->productModel->setSalePrice($this->req['product']['sprice']);
         $this->productModel->setSoldQty($this->req['product']['sold_qty']);
         $this->productModel->setPreference($this->req['product']['pref']);
         $fileurl = $file->upload('fileupload1');
         $fileurl = $file->move($fileurl);
         if ($is_new == true) {
             $this->productModel->setImageFile($fileurl);
             $this->productModel->insert();
         } else {
             if ($fileurl != "") {
                 $file->delete($this->productModel->getImageFile());
                 $this->productModel->setImageFile($fileurl);
             }
             $this->productModel->update();
         }
         if (is_array($this->req['product']['atr']) && count($this->req['product']['atr']) > 0) {
             $ahc = new AttributeHelper();
             $ahc->saveProductAttributesValue($this->req['product']['atr'], $this->productModel->getProductId());
         }
         if ($this->productModel->isSuccess() == 1) {
             return true;
         }
     }
     return false;
 }