$this->load->model('openbay/ebay_profile');
$this->load->model('openbay/ebay');
$this->load->model('openbay/ebay_template');
$this->load->model('catalog/product');
$this->load->model('tool/image');
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
if ($this->checkConfig() == true) {
$post = $this->request->post;
$data = array();
//load all of the listing defaults and assign to correct variable names
$profile_shipping = $this->model_openbay_ebay_profile->get($post['shipping_profile']);
$profile_return = $this->model_openbay_ebay_profile->get($post['return_profile']);
$profile_template = $this->model_openbay_ebay_profile->get($post['theme_profile']);
$profile_generic = $this->model_openbay_ebay_profile->get($post['generic_profile']);
$payments = $this->model_openbay_ebay->getPaymentTypes();
$paymentsAccepted = $this->config->get('ebay_payment_types');
$product_info = $this->model_catalog_product->getProduct($post['product_id']);
$query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, p.sort_order FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$post['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
$data['product_info'] = $query->row;
$data['description'] = $product_info['description'];
$data['name'] = $post['title'];
$data['sub_name'] = '';
$data['bestoffer'] = 0;
$data['finalCat'] = $post['finalCat'];
$data['price'][0] = $post['price'];
$data['qty'][0] = $post['qty'];
$data['product_id'] = $post['product_id'];
$data['feat'] = $post['feat'];
$data['featother'] = $post['featother'];
if(!empty($product_info['sku'])){
$data['sku'] = $product_info['sku'];
}
$data['auction_duration'] = $post['duration'];
$data['condition'] = (isset($post['condition']) && $post['condition'] != 0 ? $post['condition'] : '');
$data['auction_type'] = 'FixedPriceItem';
$data['catalog_epid'] = (isset($post['catalog_epid']) && $post['catalog_epid'] != 0 ? $post['catalog_epid'] : '');
$data['payment_immediate'] = $this->config->get('payment_immediate');
$data['paypal_email'] = $this->config->get('field_payment_paypal_address');
$data['payment_instruction']= $this->config->get('field_payment_instruction');
if(isset($profile_return['data']['returns_accepted'])) {
$data['returns_accepted'] = $profile_return['data']['returns_accepted'];
}
if(isset($profile_return['data']['returns_policy'])) {
$data['return_policy'] = $profile_return['data']['returns_policy'];
}
if(isset($profile_return['data']['returns_option'])) {
$data['returns_option'] = $profile_return['data']['returns_option'];
}
if(isset($profile_return['data']['returns_within'])) {
$data['returns_within'] = $profile_return['data']['returns_within'];
}
if(isset($profile_return['data']['returns_shipping'])) {
$data['returns_shipping'] = $profile_return['data']['returns_shipping'];
}
if(isset($profile_return['data']['returns_restocking_fee'])) {
$data['returns_restocking_fee'] = $profile_return['data']['returns_restocking_fee'];
}
$data['location'] = $profile_shipping['data']['location'];
$data['postcode'] = $profile_shipping['data']['postcode'];
$data['dispatch_time'] = $profile_shipping['data']['dispatch_time'];
if(isset($profile_shipping['data']['country'])) {
$data['country'] = $profile_shipping['data']['country'];
}
$data['get_it_fast'] = (isset($profile_shipping['data']['get_it_fast']) ? $profile_shipping['data']['get_it_fast'] : 0);
if(isset($profile_template['data']['ebay_template_id'])) {
$template = $this->model_openbay_ebay_template->get($profile_template['data']['ebay_template_id']);
$data['template_html'] = (isset($template['html']) ? base64_encode($template['html']) : '');
$data['template'] = $profile_template['data']['ebay_template_id'];
}else{
$data['template_html'] = '';
$data['template'] = '';
}
$data['gallery_plus'] = $profile_template['data']['ebay_gallery_plus'];
$data['gallery_super'] = $profile_template['data']['ebay_supersize'];
$data['private_listing'] = $profile_generic['data']['private_listing'];
//product attributes - this is just a direct pass through used with the template tag
$data['attributes'] = base64_encode(json_encode($this->model_openbay_ebay->getProductAttributes($post['product_id'])));
$data['payments'] = array();
foreach($payments as $payment) {
if($paymentsAccepted[$payment['ebay_name']] == 1) {
$data['payments'][$payment['ebay_name']] = 1;
}
}
$data['main_image'] = 0;
$data['img'] = array();
$product_images = $this->model_catalog_product->getProductImages($post['product_id']);
$product_info['product_images'] = array();
if (!empty($product_info['image'])) {
$data['img'][] = $product_info['image'];
}
if(isset($profile_template['data']['ebay_img_ebay']) && $profile_template['data']['ebay_img_ebay'] == 1) {
foreach ($product_images as $product_image) {
if ($product_image['image'] && file_exists(DIR_IMAGE . $product_image['image'])) {
$data['img'][] = $product_image['image'];
}
}
}
if(isset($profile_template['data']['ebay_img_template']) && $profile_template['data']['ebay_img_template'] == 1) {
$tmpGalArray = array();
$tmpThumbArray = array();
//if the user has not set the exclude default image, add it to the array for theme images.
$keyOffset = 0;
if(!isset($profile_template['data']['default_img_exclude']) || $profile_template['data']['default_img_exclude'] != 1) {
$tmpGalArray[0] = $this->model_tool_image->resize($product_info['image'], $profile_template['data']['ebay_gallery_width'], $profile_template['data']['ebay_gallery_height']);
$tmpThumbArray[0] = $this->model_tool_image->resize($product_info['image'], $profile_template['data']['ebay_thumb_width'], $profile_template['data']['ebay_thumb_height']);
$keyOffset = 1;
}
//loop through the product images and add them.
foreach ($product_images as $k => $v) {
$tmpGalArray[$k+$keyOffset] = $this->model_tool_image->resize($v['image'], $profile_template['data']['ebay_gallery_width'], $profile_template['data']['ebay_gallery_height']);
$tmpThumbArray[$k+$keyOffset] = $this->model_tool_image->resize($v['image'], $profile_template['data']['ebay_thumb_width'], $profile_template['data']['ebay_thumb_height']);
}
$data['img_tpl'] = $tmpGalArray;
$data['img_tpl_thumb'] = $tmpThumbArray;
}
$data = array_merge($data, $profile_shipping['data']);
$verifyResponse = $this->model_openbay_ebay->ebayAddItem($data, 'no');
$json = array(
'errors' => $verifyResponse['data']['Errors'],
'fees' => $verifyResponse['data']['Fees'],
'itemid' => (string)$verifyResponse['data']['ItemID'],
'preview' => (string)$verifyResponse['data']['link'],
'i' => $this->request->get['i'],
'ack' => (string)$verifyResponse['data']['Ack'],
);
$this->response->setOutput(json_encode($json));
}
} else {
$this->redirect($this->url->link('extension/openbay/itemList&token=' . $this->session->data['token']));
}