_STATUS_IN_STOCK; } } elseif (isset($mapped_data['is in stock']) && $mapped_data['is in stock'] !== '') { if (filter_var($mapped_data['is in stock'], FILTER_VALIDATE_BOOLEAN)) { $product['stock_status'] = ContentProduct::STOCK_STATUS_IN_STOCK; } else { $product['stock_status'] = ContentProduct::STOCK_STATUS_OUT_OF_STOCK; } } if (isset($mapped_data['gtin']) && TextHelper::isEan($mapped_data['gtin'])) { $product['ean'] = $mapped_data['gtin']; } else { $product['ean'] = ''; } if (!empty($mapped_data['direct link'])) { $product['orig_url'] = $mapped_data['direct link']; } elseif ($orig_url = TextHelper::findOriginalUrl($mapped_data['affiliate link'])) { $product['orig_url'] = $orig_url; } else { $product['orig_url'] = $mapped_data['affiliate link']; } $product['product'] = serialize($data); return $product; } public function doRequest($keyword, $query_params = array(), $is_autoupdate = false) { $this->maybeImportProducts(); if ($is_autoupdate) $limit = $this->config('entries_per_page_update'); else $limit = $this->config('entries_per_page'); $options = array(); if (!empty($query_params['price_min'])) $options['price_min'] = (float) $query_params['price_min']; if (!empty($query_params['price_min'])) $options['price_max'] = (float) $query_params['price_max']; if (TextHelper::isEan($keyword)) $results = $this->product_model->searchByEan($keyword, $limit, $options); elseif (filter_var($keyword, FILTER_VALIDATE_URL)) $results = $this->product_model->searchByUrl($keyword, $this->config('partial_url_match'), $limit); else { $options['search_type'] = $this->config('search_type'); $results = $this->product_model->searchByKeyword($keyword, $limit, $options); } if (!$results) return array(); return $this->prepareResults($results); } public function doRequestItems(array $items) { $this->maybeImportProducts(); $deeplink = $this->config('deeplink'); foreach ($items as $key => $item) { // fix if (!$key) { unset($items[$key]); continue; } $product = $this->product_model->searchById($item['unique_id']); if (!$product) { if ($this->product_model->count()) { $items[$key]['stock_status'] = ContentProduct::STOCK_STATUS_OUT_OF_STOCK; } continue; } if (!$r = unserialize($product['product'])) { continue; } $r = $this->mapProduct($r); if (isset($r['availability'])) $items[$key]['availability'] = $r['availability']; if (isset($r['image ​​link'])) $items[$key]['img'] = $r['image ​​link']; if (isset($r['shipping cost'])) $items[$key]['shipping_cost'] = self::extractShippingCost($r['shipping cost']); else $items[$key]['shipping_cost'] = null; $items[$key]['stock_status'] = $product['stock_status']; if (!empty($r['sale price'])) { $items[$key]['price'] = (float) TextHelper::parsePriceAmount($r['sale price']); if (isset($r['price']) && (float) TextHelper::parsePriceAmount($r['price']) > $items[$key]['price']) { $items[$key]['priceOld'] = (float) TextHelper::parsePriceAmount($r['price']); } } else { $items[$key]['price'] = (float) TextHelper::parsePriceAmount($r['price']); $items[$key]['priceOld'] = 0; } $items[$key]['url'] = $r['affiliate link']; if ($deeplink) $items[$key]['url'] = LinkHandler::createAffUrl($items[$key]['orig_url'], $deeplink, $item); if (!empty($r['description']) && \apply_filters('cegg_feed_description_update', false)) $items[$key]['description'] = $r['description']; } return $items; } private function prepareResults($results) { $data = array(); $deeplink = $this->config('deeplink'); foreach ($results as $product) { if (!$r = unserialize($product['product'])) { continue; } $r = $this->mapProduct($r); $content = new ContentProduct; $content->unique_id = $r['id']; $content->title = $r['title']; $content->url = $r['affiliate link']; if (!empty($r['sale price'])) { $content->price = (float) TextHelper::parsePriceAmount($r['sale price']); if (isset($r['price']) && (float) TextHelper::parsePriceAmount($r['price']) > $content->price) { $content->priceOld = (float) TextHelper::parsePriceAmount($r['price']); } } else { $content->price = (float) TextHelper::parsePriceAmount($r['price']); } if ($content->price) { $content->price = round($content->price, 2); } if ($content->priceOld) { $content->priceOld = round($content->priceOld, 2); } if (isset($r['currency']) && strlen($r['currency'])) { $content->currencyCode = $r['currency']; } else { $content->currencyCode = $this->config('currency'); } if (!empty($r['description']) && $r['title'] != $r['description']) { $content->description = $r['description']; } if (!empty($r['short description'])) { $content->short_description = $r['short description']; } if (isset($r['shipping cost'])) $content->shipping_cost = self::extractShippingCost($r['shipping cost']); else $content->shipping_cost = null; if (isset($r['brand'])) { $content->manufacturer = $r['brand']; } if (isset($r['isbn'])) { $content->isbn = $r['isbn']; } if (isset($r['category'])) { $content->category = $r['category']; if (strstr($r['category'], '>')) { $content->categoryPath = explode('>', $content->category); $content->categoryPath = array_map('trim', $content->categoryPath); $content->category = end($content->categoryPath); } } if (isset($r['availability'])) { $content->availability = $r['availability']; } if (isset($r['image ​​link']) && filter_var($r['image ​​link'], FILTER_VALIDATE_URL)) { $content->img = $r['image ​​link']; } $content->orig_url = $product['orig_url']; $content->stock_status = $product['stock_status']; $content->ean = $product['ean']; if ($content->orig_url != $content->url) { $content->domain = TextHelper::getHostName($content->orig_url); } else { $content->domain = $this->config('domain'); } $content->merchant = \apply_filters('cegg_feed_merchant_name', '', $content->domain); if ($deeplink) { $content->url = LinkHandler::createAffUrl($content->orig_url, $deeplink, (array) $content); } $data[] = $content; } return $data; } public function renderSearchPanel() { $this->render('search_panel', array('module_id' => $this->getId())); } public function renderResults() { PluginAdmin::render('_metabox_results', array('module_id' => $this->getId())); } public function renderSearchResults() { PluginAdmin::render('_metabox_search_results', array('module_id' => $this->getId())); } public function renderUpdatePanel() { $this->render('update_panel', array('module_id' => $this->getId())); } public function viewDataPrepare($data) { if (!$deeplink = $this->config('deeplink')) { return parent::viewDataPrepare($data); } foreach ($data as $key => $d) { $data[$key]['url'] = LinkHandler::createAffUrl($d['orig_url'], $deeplink, $d); } return parent::viewDataPrepare($data); } public function mapProduct(array $data) { $mapped_data = array(); $mapping = $this->config('mapping'); foreach ($mapping as $field => $feed_field) { if (isset($data[$feed_field])) { $mapped_data[$field] = $data[$feed_field]; } } return $mapped_data; } public static function extractShippingCost($shipping_cost) { $shipping_cost = \apply_filters('cegg_shipping_cost_value', $shipping_cost); if (strstr($shipping_cost, ':') && strstr($shipping_cost, ',')) { $parts = explode(',', $shipping_cost); $shipping_cost = reset($parts); } elseif (strstr($shipping_cost, ':')) { $parts = explode(':', $shipping_cost); foreach ($parts as $p) { if (strstr($p, 'EUR') || strstr($p, 'USD')) { $shipping_cost = $p; break; } } } if ($shipping_cost == '') return ''; return (float) TextHelper::parsePriceAmount($shipping_cost); } }