shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

etsy_product.php (27631B)


      1 <?php
      2 class ControllerExtensionOpenbayEtsyProduct extends Controller {
      3 	private $error;
      4 
      5 	public function create() {
      6 		$this->load->model('catalog/product');
      7 		$this->load->model('tool/image');
      8 
      9 		$data = $this->load->language('extension/openbay/etsy_create');
     10 
     11 		$this->document->setTitle($this->language->get('heading_title'));
     12 		$this->document->addScript('view/javascript/openbay/js/faq.js');
     13 
     14 		$data['action']   = $this->url->link('extension/openbay/etsy_product/create', 'user_token=' . $this->session->data['user_token'], true);
     15 		$data['cancel']   = $this->url->link('marketplace/openbay/items', 'user_token=' . $this->session->data['user_token'], true);
     16 		$data['user_token']    = $this->session->data['user_token'];
     17 
     18 		$data['breadcrumbs'] = array();
     19 		$data['breadcrumbs'][] = array(
     20 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
     21 			'text' => $this->language->get('text_home'),
     22 		);
     23 		$data['breadcrumbs'][] = array(
     24 			'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
     25 			'text' => $this->language->get('text_openbay'),
     26 		);
     27 		$data['breadcrumbs'][] = array(
     28 			'href' => $this->url->link('extension/openbay/etsy', 'user_token=' . $this->session->data['user_token'], true),
     29 			'text' => $this->language->get('text_etsy'),
     30 		);
     31 		$data['breadcrumbs'][] = array(
     32 			'href' => $this->url->link('extension/openbay/etsy_product/create', 'user_token=' . $this->session->data['user_token'], true),
     33 			'text' => $this->language->get('heading_title'),
     34 		);
     35 
     36 		$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
     37 
     38 		$this->load->model('tool/image');
     39 
     40 		if (!empty($product_info) && is_file(DIR_IMAGE . $product_info['image'])) {
     41 			$product_info['image_url'] = $this->model_tool_image->resize($product_info['image'], 800, 800);
     42 			$product_info['thumb'] = $this->model_tool_image->resize($product_info['image'], 100, 100);
     43 		} else {
     44 			$product_info['image_url'] = '';
     45 			$product_info['thumb'] = $this->model_tool_image->resize('no_image.png', 100, 100);
     46 		}
     47 
     48 		// Images
     49 		if (isset($this->request->get['product_id'])) {
     50 			$product_images = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
     51 		} else {
     52 			$product_images = array();
     53 		}
     54 
     55 		$data['product_images'] = array();
     56 
     57 		foreach ($product_images as $product_image) {
     58 			if (is_file(DIR_IMAGE . $product_image['image'])) {
     59 				$image = $product_image['image'];
     60 			} else {
     61 				$image = '';
     62 			}
     63 
     64 			$product_info['product_images'][] = array(
     65 				'image_url'  => $this->model_tool_image->resize($image, 800, 800),
     66 				'thumb'      => $this->model_tool_image->resize($image, 100, 100),
     67 				'sort_order' => $product_image['sort_order']
     68 			);
     69 		}
     70 
     71 		$data['product'] = $product_info;
     72 		$data['product']['description_raw'] = trim(strip_tags(html_entity_decode($data['product']['description'], ENT_QUOTES, 'UTF-8')));
     73 
     74 		$setting = array();
     75 
     76 		$setting['who_made'] = $this->openbay->etsy->getSetting('who_made');
     77 		if (is_array($setting['who_made'])) {
     78 			ksort($setting['who_made']);
     79 		}
     80 
     81 		$setting['when_made'] = $this->openbay->etsy->getSetting('when_made');
     82 		if (is_array($setting['when_made'])) {
     83 			ksort($setting['when_made']);
     84 		}
     85 
     86 		$setting['recipient'] = $this->openbay->etsy->getSetting('recipient');
     87 		if (is_array($setting['recipient'])) {
     88 			ksort($setting['recipient']);
     89 		}
     90 
     91 		$setting['occasion'] = $this->openbay->etsy->getSetting('occasion');
     92 		if (is_array($setting['occasion'])) {
     93 			ksort($setting['occasion']);
     94 		}
     95 
     96 		$setting['state'] = array('active', 'draft');
     97 
     98 		$data['setting'] = $setting;
     99 
    100 		if ($product_info['quantity'] > 999) {
    101 			$this->error['warning'] = sprintf($this->language->get('error_stock_max'), $product_info['quantity']);
    102 		}
    103 
    104 		if (isset($this->error['warning'])) {
    105 			$data['error_warning'] = $this->error['warning'];
    106 		} else {
    107 			$data['error_warning'] = '';
    108 		}
    109 
    110 		if ($this->openbay->addonLoad('openstock') && $product_info['has_option'] == 1) {
    111 			$data['error_variant'] = $this->language->get('error_variant');
    112 		} else {
    113 			$data['error_variant'] = '';
    114 		}
    115 
    116 		$data['header'] = $this->load->controller('common/header');
    117 		$data['column_left'] = $this->load->controller('common/column_left');
    118 		$data['footer'] = $this->load->controller('common/footer');
    119 
    120 		$this->response->setOutput($this->load->view('extension/openbay/etsy_create', $data));
    121 	}
    122 
    123 	public function createSubmit() {
    124 		$this->load->language('extension/openbay/etsy_create');
    125 		$this->load->model('extension/openbay/etsy_product');
    126 
    127 		$data = $this->request->post;
    128 
    129 		// validation
    130 		if (!isset($data['title']) || empty($data['title']) || strlen($data['title']) > 255) {
    131 			if (strlen($data['title']) > 255) {
    132 				$this->error['title'] = $this->language->get('error_title_length');
    133 			} else {
    134 				$this->error['title'] = $this->language->get('error_title_missing');
    135 			}
    136 		}
    137 
    138 		if (!isset($data['description']) || empty($data['description'])) {
    139 			$this->error['title'] = $this->language->get('error_desc_missing');
    140 		}
    141 
    142 		if (!isset($data['price']) || empty($data['price'])) {
    143 			$this->error['price'] = $this->language->get('error_price_missing');
    144 		}
    145 
    146 		if (!isset($data['taxonomy_id']) || empty($data['taxonomy_id']) || $data['taxonomy_id'] == 0) {
    147 			$this->error['taxonomy_id'] = $this->language->get('error_category');
    148 		}
    149 
    150 		if (isset($data['tags']) && count($data['tags']) > 13) {
    151 			$this->error['tags'] = $this->language->get('error_tags');
    152 		}
    153 
    154 		if (isset($data['materials']) && count($data['materials']) > 13) {
    155 			$this->error['materials'] = $this->language->get('error_materials');
    156 		}
    157 
    158 		if (isset($data['style_1']) && !empty($data['style_1'])) {
    159 			if (preg_match('/[^\p{L}\p{Nd}\p{Zs}]/u', $data['style_1']) == 1) {
    160 				$this->error['style_1'] = $this->language->get('error_style_1_tag');
    161 			}
    162 		}
    163 
    164 		if (isset($data['style_2']) && !empty($data['style_2'])) {
    165 			if (preg_match('/[^\p{L}\p{Nd}\p{Zs}]/u', $data['style_2']) == 1) {
    166 				$this->error['style_2'] = $this->language->get('error_style_2_tag');
    167 			}
    168 		}
    169 
    170 		if ($data['quantity'] > 999) {
    171 			$this->error['quantity'] = sprintf($this->language->get('error_stock_max'), $data['quantity']);
    172 		}
    173 
    174 		if (isset($data['product_image']) && count($data['product_image']) > 4) {
    175 			$this->error['images'] = sprintf($this->language->get('error_image_max'), count($data['product_image'])+1);
    176 		}
    177 
    178 		if (!$this->error) {
    179 			// process the request
    180 			$response = $this->openbay->etsy->call('v1/etsy/product/listing/create/', 'POST', $data);
    181 
    182 			$this->response->addHeader('Content-Type: application/json');
    183 
    184 			if (isset($response['data']['results'][0]['listing_id'])) {
    185 				$this->model_extension_openbay_etsy_product->addLink($data['product_id'], $response['data']['results'][0]['listing_id'], 1);
    186 			}
    187 
    188 			if (isset($response['data']['error'])) {
    189 				$this->response->setOutput(json_encode($response['data']));
    190 			} else {
    191 				$this->response->setOutput(json_encode($response['data']['results'][0]));
    192 			}
    193 		} else {
    194 			$this->response->setOutput(json_encode(array('error' => $this->error)));
    195 		}
    196 	}
    197 
    198 	public function edit() {
    199 		$data = $this->load->language('extension/openbay/etsy_edit');
    200 
    201 		$this->load->model('extension/openbay/etsy_product');
    202 		$this->load->model('tool/image');
    203 
    204 		$this->document->setTitle($this->language->get('heading_title'));
    205 		$this->document->addScript('view/javascript/openbay/js/faq.js');
    206 
    207 		$data['action']   = $this->url->link('extension/openbay/etsy_product/editSubmit', 'user_token=' . $this->session->data['user_token'], true);
    208 		$data['cancel']   = $this->url->link('marketplace/openbay/items', 'user_token=' . $this->session->data['user_token'], true);
    209 		$data['user_token']    = $this->session->data['user_token'];
    210 
    211 		$data['breadcrumbs'] = array();
    212 		$data['breadcrumbs'][] = array(
    213 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    214 			'text' => $this->language->get('text_home'),
    215 		);
    216 		$data['breadcrumbs'][] = array(
    217 			'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    218 			'text' => $this->language->get('text_openbay'),
    219 		);
    220 		$data['breadcrumbs'][] = array(
    221 			'href' => $this->url->link('extension/openbay/etsy', 'user_token=' . $this->session->data['user_token'], true),
    222 			'text' => $this->language->get('text_etsy'),
    223 		);
    224 		$data['breadcrumbs'][] = array(
    225 			'href' => $this->url->link('extension/openbay/etsy_product/edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=' . $this->request->get['product_id'], true),
    226 			'text' => $this->language->get('heading_title'),
    227 		);
    228 
    229 		$links = $this->openbay->etsy->getLinks($this->request->get['product_id'], 1, 1);
    230 
    231 		$data['listing'] = $this->openbay->etsy->getEtsyItem($links[0]['etsy_item_id']);
    232 
    233 		$data['etsy_item_id'] = $links[0]['etsy_item_id'];
    234 		$data['product_id'] = $this->request->get['product_id'];
    235 
    236 		$setting['state'] = array('active', 'inactive', 'draft');
    237 
    238 		$data['setting'] = $setting;
    239 
    240 		if ($data['listing']['state'] == 'edit') {
    241 			$data['listing']['state'] = 'inactive';
    242 		}
    243 
    244 		if (isset($this->error['warning'])) {
    245 			$data['error_warning'] = $this->error['warning'];
    246 		} else {
    247 			$data['error_warning'] = '';
    248 		}
    249 
    250 		$data['header'] = $this->load->controller('common/header');
    251 		$data['column_left'] = $this->load->controller('common/column_left');
    252 		$data['footer'] = $this->load->controller('common/footer');
    253 
    254 		$this->response->setOutput($this->load->view('extension/openbay/etsy_edit', $data));
    255 	}
    256 
    257 	public function editSubmit() {
    258 		$this->load->language('extension/openbay/etsy_edit');
    259 		$this->load->model('extension/openbay/etsy_product');
    260 
    261 		$data = $this->request->post;
    262 
    263 		// validation
    264 		if (!isset($data['title']) || empty($data['title']) || strlen($data['title']) > 255) {
    265 			if (strlen($data['title']) > 255) {
    266 				$this->error['title'] = $this->language->get('error_title_length');
    267 			} else {
    268 				$this->error['title'] = $this->language->get('error_title_missing');
    269 			}
    270 		}
    271 
    272 		if (!isset($data['description']) || empty($data['description'])) {
    273 			$this->error['title'] = $this->language->get('error_desc_missing');
    274 		}
    275 
    276 		if (!isset($data['price']) || empty($data['price'])) {
    277 			$this->error['price'] = $this->language->get('error_price_missing');
    278 		}
    279 
    280 		if (!isset($data['state']) || empty($data['state'])) {
    281 			$this->error['state'] = $this->language->get('error_state_missing');
    282 		}
    283 
    284 		if (!$this->error) {
    285 			// process the request
    286 			$response = $this->openbay->etsy->call('v1/etsy/product/listing/' . $data['etsy_item_id'] . '/update/', 'POST', $data);
    287 
    288 			$this->response->addHeader('Content-Type: application/json');
    289 
    290 			if (isset($response['data']['error'])) {
    291 				$this->response->setOutput(json_encode($response['data']));
    292 			} else {
    293 				$this->response->setOutput(json_encode($response['data']['results'][0]));
    294 			}
    295 		} else {
    296 			$this->response->setOutput(json_encode(array('error' => $this->error)));
    297 		}
    298 	}
    299 
    300 	public function addImage() {
    301 		$this->load->language('extension/openbay/etsy_create');
    302 
    303 		$data = $this->request->post;
    304 
    305 		if (!isset($data['image']) || empty($data['image'])) {
    306 			$this->error['image'] = $this->language->get('error_no_img_url');
    307 		}
    308 
    309 		if (!isset($data['listing_id']) || empty($data['listing_id'])) {
    310 			$this->error['listing_id'] = $this->language->get('error_no_listing_id');
    311 		}
    312 
    313 		if (!$this->error) {
    314 			$response = $this->openbay->etsy->call('v1/etsy/product/listing/' . (int)$data['listing_id'] . '/image/', 'POST', $data);
    315 
    316 			$this->response->addHeader('Content-Type: application/json');
    317 
    318 			if (isset($response['data']['error'])) {
    319 				$this->response->setOutput(json_encode($response['data']));
    320 			} else {
    321 				$this->response->setOutput(json_encode($response['data']['results'][0]));
    322 			}
    323 		}
    324 	}
    325 
    326 	public function getCategories() {
    327 		$categories = $this->cache->get('etsy_categories');
    328 
    329 		if (!$categories) {
    330 			$response = $this->openbay->etsy->call('v1/etsy/product/taxonomy/', 'GET');
    331 
    332 			if (isset($response['header_code']) && $response['header_code'] == 200) {
    333 				$categories = $this->formatCategories($response['data']);
    334 
    335 				$this->cache->set('etsy_categories', $categories);
    336 			}
    337 		}
    338 
    339 		$response = array();
    340 		$parent_categories = array();
    341 		$last_id = 0;
    342 
    343 		if (isset($this->request->get['id_path']) && $this->request->get['id_path'] != '' && $this->request->get['id_path'] != 0) {
    344 			$id_path_parts = explode(',', $this->request->get['id_path']);
    345 
    346 
    347 			foreach ($id_path_parts as $id_path) {
    348 				$parent_categories[] = $categories[$id_path]['name'];
    349 
    350 				$categories = $categories[$id_path]['children'];
    351 
    352 				$last_id = $id_path;
    353 			}
    354 		}
    355 
    356 		if (empty($categories)) {
    357 			$final_category = true;
    358 		} else {
    359 			foreach ($categories as $id => $category) {
    360 				$response[$id] = array(
    361 					'name' => $category['name'],
    362 					'id_path' => $category['id_path'],
    363 					'children_count' => (is_array($category['children']) ? count($category['children']) : 0),
    364 				);
    365 			}
    366 
    367 			$final_category = false;
    368 		}
    369 
    370 
    371 		$this->response->addHeader('Content-Type: application/json');
    372 		$this->response->setOutput(json_encode(array('data' => $response, 'parent_text' => implode(' > ', $parent_categories), 'final_category' => $final_category, 'last_id' => $last_id)));
    373 	}
    374 
    375 	private function formatCategories($category_data) {
    376 		$response = array();
    377 
    378 		foreach ($category_data as $category) {
    379 			$response[$category['id']] = array(
    380 				'name' => $category['name'],
    381 				'id_path' => implode(',', $category['full_path_taxonomy_ids']),
    382 				'children' => (isset($category['children']) && !empty($category['children']) ? $this->formatCategories($category['children']) : ''),
    383 			);
    384 		}
    385 
    386 		return $response;
    387 	}
    388 
    389 	public function addLink() {
    390 		$this->load->language('extension/openbay/etsy_links');
    391 		$this->load->model('extension/openbay/etsy_product');
    392 		$this->load->model('catalog/product');
    393 
    394 		$data = $this->request->post;
    395 
    396 		if (!isset($data['product_id'])) {
    397 			echo json_encode(array('error' => $this->language->get('error_product_id')));
    398 			die();
    399 		}
    400 
    401 		if (!isset($data['etsy_id'])) {
    402 			echo json_encode(array('error' => $this->language->get('error_etsy_id')));
    403 			die();
    404 		}
    405 
    406 		$links = $this->openbay->etsy->getLinks($data['product_id'], 1);
    407 
    408 		if ($links != false) {
    409 			echo json_encode(array('error' => $this->language->get('error_link_exists')));
    410 			die();
    411 		}
    412 
    413 		$product = $this->model_catalog_product->getProduct($data['product_id']);
    414 
    415 		if (!$product) {
    416 			echo json_encode(array('error' => $this->language->get('error_product')));
    417 			die();
    418 		}
    419 
    420 		if ($product['quantity'] <= 0) {
    421 			echo json_encode(array('error' => $this->language->get('error_stock')));
    422 			die();
    423 		}
    424 
    425 		// check the etsy item exists
    426 		$get_response = $this->openbay->etsy->getEtsyItem($data['etsy_id']);
    427 
    428 		if (isset($get_response['data']['error'])) {
    429 			echo json_encode(array('error' => $this->language->get('error_etsy') . $get_response['data']['error']));
    430 			die();
    431 		} else {
    432 			if ((int)$get_response['quantity'] != (int)$product['quantity']) {
    433 				// if the stock is different than the item being linked update the etsy stock level
    434 				$update_response = $this->openbay->etsy->updateListingStock($data['etsy_id'], $product['quantity'], $get_response['state']);
    435 
    436 				if (isset($update_response['data']['error'])) {
    437 					echo json_encode(array('error' => $this->language->get('error_etsy') . $update_response['data']['error']));
    438 					die();
    439 				}
    440 			}
    441 		}
    442 
    443 		$this->model_extension_openbay_etsy_product->addLink($data['product_id'], $data['etsy_id'], 1);
    444 
    445 		$this->response->addHeader('Content-Type: application/json');
    446 		$this->response->setOutput(json_encode(array('error' => false)));
    447 	}
    448 
    449 	public function deleteLink() {
    450 		$this->load->language('extension/openbay/etsy_links');
    451 
    452 		$data = $this->request->post;
    453 
    454 		if (!isset($data['etsy_link_id'])) {
    455 			echo json_encode(array('error' => $this->language->get('error_link_id')));
    456 			die();
    457 		}
    458 
    459 		$this->openbay->etsy->deleteLink($data['etsy_link_id']);
    460 
    461 		$this->response->addHeader('Content-Type: application/json');
    462 		$this->response->setOutput(json_encode(array('error' => false)));
    463 	}
    464 
    465 	public function links() {
    466 		$this->load->model('extension/openbay/etsy_product');
    467 
    468 		$data = $this->load->language('extension/openbay/etsy_links');
    469 
    470 		$data['cancel'] = $this->url->link('marketplace/openbay/items', 'user_token=' . $this->session->data['user_token'], true);
    471 
    472 		$this->document->setTitle($this->language->get('heading_title'));
    473 		$this->document->addScript('view/javascript/openbay/js/faq.js');
    474 
    475 		$data['breadcrumbs'] = array();
    476 
    477 		$data['breadcrumbs'][] = array(
    478 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    479 			'text' => $this->language->get('text_home'),
    480 		);
    481 
    482 		$data['breadcrumbs'][] = array(
    483 			'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    484 			'text' => $this->language->get('text_openbay'),
    485 		);
    486 
    487 		$data['breadcrumbs'][] = array(
    488 			'href' => $this->url->link('extension/openbay/etsy', 'user_token=' . $this->session->data['user_token'], true),
    489 			'text' => $this->language->get('text_etsy'),
    490 		);
    491 
    492 		$data['breadcrumbs'][] = array(
    493 			'href' => $this->url->link('extension/openbay/etsy_product/itemLinks', 'user_token=' . $this->session->data['user_token'], true),
    494 			'text' => $this->language->get('heading_title'),
    495 		);
    496 
    497 		$data['return']       = $this->url->link('extension/openbay/etsy', 'user_token=' . $this->session->data['user_token'], true);
    498 		//$data['edit_url']     = $this->url->link('extension/openbay/ebay/edit', 'user_token=' . $this->session->data['user_token'] . '&product_id=', true);
    499 		//$data['validation']   = $this->openbay->ebay->validate();
    500 		$data['user_token']        = $this->session->data['user_token'];
    501 
    502 		$total_linked = $this->model_extension_openbay_etsy_product->totalLinked();
    503 
    504 		if (isset($this->request->get['page'])){
    505 			$page = (int)$this->request->get['page'];
    506 		} else {
    507 			$page = 1;
    508 		}
    509 
    510 		$limit = $this->config->get('config_limit_admin');
    511 
    512 		$pagination = new Pagination();
    513 		$pagination->total = $total_linked;
    514 		$pagination->page = $page;
    515 		$pagination->limit = $limit;
    516 		$pagination->text = $this->language->get('text_pagination');
    517 		$pagination->url = $this->url->link('extension/openbay/etsy/itemLinks', 'user_token=' . $this->session->data['user_token'] . '&page={page}', true);
    518 
    519 		$data['pagination'] = $pagination->render();
    520 		$data['results'] = sprintf($this->language->get('text_pagination'), ($total_linked) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($total_linked - $limit)) ? $total_linked : ((($page - 1) * $limit) + $limit), $total_linked, ceil($total_linked / $limit));
    521 
    522 		$data['items'] = $this->model_extension_openbay_etsy_product->loadLinked($limit, $page);
    523 
    524 		$data['header'] = $this->load->controller('common/header');
    525 		$data['column_left'] = $this->load->controller('common/column_left');
    526 		$data['footer'] = $this->load->controller('common/footer');
    527 
    528 		$this->response->setOutput($this->load->view('extension/openbay/etsy_links', $data));
    529 	}
    530 
    531 	public function listings() {
    532 		$data = $this->load->language('extension/openbay/etsy_listings');
    533 
    534 		$this->document->setTitle($this->language->get('heading_title'));
    535 		$this->document->addScript('view/javascript/openbay/js/faq.js');
    536 
    537 		$this->load->model('extension/openbay/etsy_product');
    538 
    539 		$data['user_token'] = $this->session->data['user_token'];
    540 
    541 		$data['breadcrumbs'] = array();
    542 
    543 		$data['breadcrumbs'][] = array(
    544 			'text' => $this->language->get('text_home'),
    545 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
    546 		);
    547 
    548 		$data['breadcrumbs'][] = array(
    549 			'href' => $this->url->link('marketplace/openbay', 'user_token=' . $this->session->data['user_token'], true),
    550 			'text' => $this->language->get('text_openbay'),
    551 		);
    552 
    553 		$data['breadcrumbs'][] = array(
    554 			'href' => $this->url->link('extension/openbay/etsy', 'user_token=' . $this->session->data['user_token'], true),
    555 			'text' => $this->language->get('text_etsy'),
    556 		);
    557 
    558 		$data['breadcrumbs'][] = array(
    559 			'href' => $this->url->link('extension/openbay/etsy_product/itemLinks', 'user_token=' . $this->session->data['user_token'], true),
    560 			'text' => $this->language->get('heading_title'),
    561 		);
    562 
    563 		$filter = array();
    564 
    565 		if (!isset($this->request->get['status'])) {
    566 			$filter['status'] = 'active';
    567 		} else {
    568 			$filter['status'] = $this->request->get['status'];
    569 		}
    570 
    571 		if (!isset($this->request->get['page'])) {
    572 			$filter['page'] = 1;
    573 		} else {
    574 			$filter['page'] = $this->request->get['page'];
    575 		}
    576 
    577 		if (isset($this->request->get['keywords'])) {
    578 			$filter['keywords'] = $this->request->get['keywords'];
    579 		}
    580 
    581 		$filter['limit'] = (int)$this->config->get('config_limit_admin');
    582 
    583 		$data['filter'] = $filter;
    584 
    585 		$response = $this->openbay->etsy->call('v1/etsy/product/listing/all/?' . http_build_query($filter), 'GET');
    586 
    587 		unset($filter['page']);
    588 
    589 		if (isset($response['data']['error'])) {
    590 			$data['listings'] = array();
    591 			$data['pagination'] = '';
    592 			$data['results'] = '';
    593 			$this->error['warning'] = $this->language->get('error_etsy') . $response['data']['error'];
    594 		} else {
    595 			$listings = array();
    596 
    597 			foreach($response['data']['results'] as $listing) {
    598 				$product_link = $this->openbay->etsy->getLinkedProduct($listing['listing_id']);
    599 
    600 				$actions = array();
    601 
    602 				if ($filter['status'] == 'inactive') {
    603 					$actions[] = 'activate_item';
    604 				}
    605 
    606 				if ($filter['status'] == 'active') {
    607 					$actions[] = 'end_item';
    608 					$actions[] = 'deactivate_item';
    609 				}
    610 
    611 				if ($filter['status'] == 'active' && empty($product_link)) {
    612 					$actions[] = 'add_link';
    613 				}
    614 
    615 				if (!empty($product_link)) {
    616 					$actions[] = 'delete_link';
    617 				}
    618 
    619 				if ($product_link != false) {
    620 					$listings[] = array('link' => $product_link, 'listing' => $listing, 'actions' => $actions);
    621 				} else {
    622 					$listings[] = array('link' => '', 'listing' => $listing, 'actions' => $actions);
    623 				}
    624 			}
    625 
    626 			$data['listings'] = $listings;
    627 
    628 			$pagination = new Pagination();
    629 			$pagination->total = $response['data']['count'];
    630 			$pagination->page = $response['data']['pagination']['effective_page'];
    631 			$pagination->limit = $response['data']['pagination']['effective_limit'];
    632 			$pagination->url = $this->url->link('extension/openbay/etsy_product/listings', 'user_token=' . $this->session->data['user_token'] . '&page={page}&' . http_build_query($filter), true);
    633 
    634 			$data['pagination'] = $pagination->render();
    635 			$data['results'] = sprintf($this->language->get('text_pagination'), ($response['data']['count']) ? (($response['data']['pagination']['effective_page'] - 1) * $response['data']['pagination']['effective_limit']) + 1 : 0, ((($response['data']['pagination']['effective_page'] - 1) * $response['data']['pagination']['effective_limit']) > ($response['data']['count'] - $response['data']['pagination']['effective_limit'])) ? $response['data']['count'] : ((($response['data']['pagination']['effective_page'] - 1) * $response['data']['pagination']['effective_limit']) + $response['data']['pagination']['effective_limit']), $response['data']['count'], ceil($response['data']['count'] / $response['data']['pagination']['effective_limit']));
    636 		}
    637 
    638 		$data['success'] = '';
    639 
    640 		if (isset($this->request->get['item_ended'])) {
    641 			$data['success'] = $this->language->get('text_item_ended');
    642 		}
    643 
    644 		if (isset($this->request->get['item_activated'])) {
    645 			$data['success'] = $this->language->get('text_item_activated');
    646 		}
    647 
    648 		if (isset($this->request->get['item_deactivated'])) {
    649 			$data['success'] = $this->language->get('text_item_deactivated');
    650 		}
    651 
    652 		if (isset($this->request->get['link_added'])) {
    653 			$data['success'] = $this->language->get('text_link_added');
    654 		}
    655 
    656 		if (isset($this->request->get['link_deleted'])) {
    657 			$data['success'] = $this->language->get('text_link_deleted');
    658 		}
    659 
    660 		if (isset($this->error['warning'])) {
    661 			$data['error_warning'] = $this->error['warning'];
    662 		} else {
    663 			$data['error_warning'] = '';
    664 		}
    665 
    666 		$data['header'] = $this->load->controller('common/header');
    667 		$data['column_left'] = $this->load->controller('common/column_left');
    668 		$data['footer'] = $this->load->controller('common/footer');
    669 
    670 		$this->response->setOutput($this->load->view('extension/openbay/etsy_listings', $data));
    671 	}
    672 
    673 	public function endListing() {
    674 		$this->load->language('extension/openbay/etsy_links');
    675 
    676 		$data = $this->request->post;
    677 
    678 		if (!isset($data['etsy_item_id'])) {
    679 			echo json_encode(array('error' => $this->language->get('error_etsy_id')));
    680 			die();
    681 		}
    682 
    683 		$response = $this->openbay->etsy->call('v1/etsy/product/listing/' . (int)$data['etsy_item_id'] . '/delete/', 'POST', array());
    684 
    685 		if (isset($response['data']['error'])) {
    686 			echo json_encode(array('error' => $this->language->get('error_etsy') . $response['data']['error']));
    687 			die();
    688 		} else {
    689 			$linked_item = $this->openbay->etsy->getLinkedProduct($data['etsy_item_id']);
    690 
    691 			if ($linked_item != false) {
    692 				$this->openbay->etsy->deleteLink($linked_item['etsy_listing_id']);
    693 			}
    694 
    695 			$this->response->addHeader('Content-Type: application/json');
    696 			$this->response->setOutput(json_encode(array('error' => false)));
    697 		}
    698 	}
    699 
    700 	public function deactivateListing() {
    701 		$this->load->language('extension/openbay/etsy_links');
    702 
    703 		$data = $this->request->post;
    704 
    705 		if (!isset($data['etsy_item_id'])) {
    706 			echo json_encode(array('error' => $this->language->get('error_etsy_id')));
    707 			die();
    708 		}
    709 
    710 		$response = $this->openbay->etsy->call('v1/etsy/product/listing/' . (int)$data['etsy_item_id'] . '/inactive/', 'POST', array());
    711 
    712 		if (isset($response['data']['error'])) {
    713 			echo json_encode(array('error' => $this->language->get('error_etsy') . $response['data']['error']));
    714 			die();
    715 		} else {
    716 			$linked_item = $this->openbay->etsy->getLinkedProduct($data['etsy_item_id']);
    717 
    718 			if ($linked_item != false) {
    719 				$this->openbay->etsy->deleteLink($linked_item['etsy_listing_id']);
    720 			}
    721 
    722 			$this->response->addHeader('Content-Type: application/json');
    723 			$this->response->setOutput(json_encode(array('error' => false)));
    724 		}
    725 	}
    726 
    727 	public function activateListing() {
    728 		$this->load->language('extension/openbay/etsy_links');
    729 
    730 		$data = $this->request->post;
    731 
    732 		$this->response->addHeader('Content-Type: application/json');
    733 
    734 		if (!isset($data['etsy_item_id'])) {
    735 			echo json_encode(array('error' => $this->language->get('error_etsy_id')));
    736 			die();
    737 		}
    738 
    739 		$response = $this->openbay->etsy->call('v1/etsy/product/listing/' . (int)$data['etsy_item_id'] . '/active/', 'POST', array());
    740 
    741 		if (isset($response['data']['error'])) {
    742 			echo json_encode(array('error' => $this->language->get('error_etsy') . $response['data']['error']));
    743 			die();
    744 		} else {
    745 			$this->response->setOutput(json_encode(array('error' => false)));
    746 		}
    747 	}
    748 }