$this->load->model('tool/image');
$this->load->library('amazonus');
$this->load->library('log');
$log = new Log('amazonus_product.log');
$result = array();
if(isset($this->request->get['xml'])) {
$request = array('template' => $this->request->get['xml'], 'version' => 2);
$response = $this->openbay->amazonus->callWithResponse("productv2/GetTemplateXml", $request);
if ($response) {
$template = $this->openbay->amazonus->parseCategoryTemplate($response);
if ($template) {
$variation = isset($this->request->get['var']) ? $this->request->get['var'] : '';
if (isset($this->request->get['product_id'])) {
$template['fields'] = $this->fillDefaultValues($this->request->get['product_id'], $template['fields'], $variation);
} elseif (isset($this->request->get['edit_id'])) {
$template['fields'] = $this->fillSavedValues($this->request->get['edit_id'], $template['fields'], $variation);
}
foreach($template['fields'] as $key => $field) {
if($field['accepted']['type'] == 'image') {
$template['fields'][$key]['thumb'] = $this->model_tool_image->resize(str_replace(HTTPS_CATALOG . 'image/', '', $field['value']), 100, 100);
if(empty($field['thumb'])) {
$template['fields'][$key]['thumb'] = '';
}
}
}
$result = array(
"category" => $template['category'],
"fields" => $template['fields'],
"tabs" => $template['tabs']
);
} else {
$json_decoded = json_decode($response);
if ($json_decoded) {
$result = $json_decoded;
} else {
$result = array('status' => 'error');
$log->write("admin/openbay/amazon_product/parseTemplateAjax failed to parse template response: " . $response);
}
}
} else {
$log->write("admin/openbay/amazonus_product/parseTemplateAjax failed calling productv2/GetTemplateXml with params: " . print_r($request, true));
}
}
$this->response->setOutput(json_encode($result));