/** * Save product to database */ public function save() { $data = ['item_id' => $this->item_id, 'shop_id' => $this->shop_id] + $this->data; if (!App\Product::where('shop_id', $this->shop_id)->where('item_id', $this->item_id)->update($this->data) && !App\Product::insert($data)) { return false; } $this->id = App\Product::where('shop_id', $this->shop_id)->where('item_id', $this->item_id)->first()->id; if (!$this->id) { return false; } foreach ($this->images as $image) { if (!App\Image::where('url', $image['url'])->update($image) && !App\Image::insert($image + ['product_id' => $this->id])) { return false; } } /*if( isset( $this->params ) ) { foreach( $this->params as $param ) { if( !App\Param::where( 'param', $param['param'] )->update( $param ) && !App\Param::insert( $param + [ 'product_id' => $this->id ] ) ) return false; } }*/ return true; }
public function uploadProducts($xml) { /** * Formating XML structure of sent file */ $doc = new \DOMDocument(); $doc->loadXML($xml); $elements = $doc->getElementsByTagName('SHOPITEM'); $data = array('IMGURLS' => [], 'SHOP_ID' => 1); $item_groups = array(); /** * Getting a data of every shop item in feed */ foreach ($elements as $node) { foreach ($node->childNodes as $child) { if (trim($child->nodeValue) != '') { switch ($child->nodeName) { case 'ITEM_ID': case 'ITEMGROUP_ID': case 'PRODUCTNAME': case 'PRODUCT': case 'DESCRIPTION': case 'MANUFACTURER': case 'URL': case 'PRICE_VAT': case 'GIFT': $data[$child->nodeName] = $child->nodeValue; break; /*case 'PARAM': $main = $child->getElementsByTagName( 'PARAM_NAME' )->item(0)->nodeValue; $value = $child->getElementsByTagName( 'VAL' )->item(0)->nodeValue; $data[ $child->nodeName ][] = [ 'param' => $main, 'value' => $value ]; break;*/ /*case 'PARAM': $main = $child->getElementsByTagName( 'PARAM_NAME' )->item(0)->nodeValue; $value = $child->getElementsByTagName( 'VAL' )->item(0)->nodeValue; $data[ $child->nodeName ][] = [ 'param' => $main, 'value' => $value ]; break;*/ case 'IMGURL': case 'IMGURL_ALTERNATIVE': if (count($data['IMGURLS'] < 2)) { $url = $child->childNodes->item(0)->nodeValue; $m = $child->nodeName == 'IMGURL' ? true : false; $data['IMGURLS'][] = ['url' => $url, 'main' => $m]; } break; case 'CATEGORYTEXT': $data['CATEGORYTEXT'] = Section::parse(str_replace('Heureka.cz | ', '', $child->nodeValue)); break; default: continue; } } } /** * IF#1 * Checks if the product has variations */ if (isset($data['ITEMGROUP_ID'])) { /** * IF#2 * Checks if it is the very first occurence of item group */ if (isset($item_groups[$data['ITEMGROUP_ID']])) { /** * IF#3 * If there is already one product of this item group in DB * scripts increments number of variations it has by one. */ if (!$item_groups[$data['ITEMGROUP_ID']]['id'] === false) { $id = App\Product::where('shop_id', $data['SHOP_ID'])->where('item_id', $item_groups[$data['ITEMGROUP_ID']]['id'])->value('id'); App\Product::where('shop_id', $data['SHOP_ID'])->where('item_id', $item_groups[$data['ITEMGROUP_ID']]['id'])->increment('variations'); /** * IF#4 * Saving new product picture to the DB. */ if (isset($data['IMGURLS']) && count($data['IMGURLS']) > 0) { /** * IF#5 * We don't want to have multiple rows with same url in our DB */ if (App\Image::select('id')->where('product_id', $id)->where('url', $data['IMGURLS'][0]['url'])->count() == 0) { App\Image::insert(['product_id' => $id, 'url' => $data['IMGURLS'][0]['url'], 'main' => 0]); } } /** * Since there is not a single product of this * item group in db, we have to create one. */ } else { /** * IF#6 * If there is not a single avaible image, we increment group variations. */ if (count($data['IMGURLS']) < 1) { $item_groups[$data['ITEMGROUP_ID']]['count']++; } else { $data['VARIATIONS'] = $item_groups[$data['ITEMGROUP_ID']]['count']; $item_groups[$data['ITEMGROUP_ID']]['id'] = $data['ITEM_ID']; $this->saveProduct($data); } } } else { /** * If there was no variation for this item group ID found before * script creates a note in item_group array. */ $set = count($data['IMGURLS']) > 0 ? $data['ITEM_ID'] : false; $item_groups[$data['ITEMGROUP_ID']] = ['count' => 0, 'id' => $set]; /** * IF#7 * Script won't save product unless it has at least one image avaible. */ if (!$set === false) { $this->saveProduct($data); } } } else { /** * IF#8 * Products without variations and with avaible images are stored. */ if (count($data['IMGURLS']) > 0) { $this->saveProduct($data); } } $data = array('IMGURLS' => [], 'SHOP_ID' => 1); } }