bootstrap = true; $this->table = 'empik_action'; $this->className = 'EmpikAction'; $this->lang = false; parent::__construct(); $this->empikClientFactory = $this->module->getService('empik.marketplace.factory.empikClientFactory'); $this->empikClient = $this->empikClientFactory->createClient(); $this->_select .= 'id_import AS id_import_log'; $this->_defaultOrderWay = 'DESC'; $this->fields_list = [ 'id_empik_action' => [ 'title' => $this->l('ID'), 'align' => 'center', 'class' => 'fixed-width-xs' ], 'action' => [ 'title' => $this->l('Action'), ], 'date_start' => [ 'title' => $this->l('Date start'), 'type' => 'datetime', ], 'date_end' => [ 'title' => $this->l('Date end'), 'type' => 'datetime', ], 'status' => [ 'title' => $this->l('Status'), ], 'id_import' => [ 'title' => $this->l('Import ID'), ], 'import_count' => [ 'title' => $this->l('Nb. products'), ], 'id_import_log' => [ 'title' => $this->l('Report'), 'class' => 'fixed-width-md', 'align' => 'center', 'callback' => 'displayDownloadReportButton', 'search' => false, 'orderby' => false, ], ]; } public function initToolbar() { $this->toolbar_btn = []; } public function initContent() { $this->cleanupActionLog(); parent::initContent(); $this->updateActionLog(); } public function init() { if (Tools::getValue('action') === 'downloadLog') { $this->getExportErrorLog(); } parent::init(); } public function displayDownloadReportButton($val, $row) { if (!$val || $row['status'] !== 'COMPLETE') { return ''; } $this->context->smarty->assign( [ 'href' => self::$currentIndex.'&token='.$this->token. '&'.$this->identifier.'='.$row['id_empik_action'].'&action=downloadLog', ] ); return $this->module->display($this->module->name, 'views/templates/admin/list_action_download.tpl'); } public function getExportErrorLog() { /** @var EmpikAction $obj */ $empikAction = $this->loadObject(); $importId = $empikAction->id_import; if (!$importId) { $this->errors[] = $this->l('No import ID for selected action'); return; } try { switch ($empikAction->action) { case EmpikAction::ACTION_PRODUCT_EXPORT: $response = $this->empikClient->getProductImportErrorReport($importId); break; case EmpikAction::ACTION_OFFER_EXPORT: $response = $this->empikClient->getOfferImportErrorReport($importId); break; default: return; } $this->contentOutput($response, sprintf('report_%s.csv', $importId)); } catch (Exception $e) { $this->errors[] = sprintf($this->l('No error report found for import: %s'), $importId); } } protected function cleanupActionLog() { $id = Db::getInstance()->getValue('SELECT MAX(id_empik_action) - '.(int)$this->logLimit.' FROM `'._DB_PREFIX_.'empik_action`'); Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'empik_action` WHERE id_empik_action <= ' . (int)$id); } protected function updateActionLog() { $this->getList(Context::getContext()->language->id); $endStatuses = ['COMPLETE', 'FAILED', 'API_ERROR']; foreach ($this->_list as $row) { $empikAction = new EmpikAction($row['id_empik_action']); if (!$empikAction->id_import || in_array($empikAction->status, $endStatuses)) { continue; } try { switch ($empikAction->action) { case EmpikAction::ACTION_PRODUCT_EXPORT: $response = $this->empikClient->getProductsImport($empikAction->id_import); $this->updateActionProductImport($response, $empikAction); break; case EmpikAction::ACTION_OFFER_EXPORT: $response = $this->empikClient->getOffersImport($empikAction->id_import); $this->updateActionOfferImport($response, $empikAction); break; } } catch (Exception $e) { $this->errors[] = $e->getMessage(); } } } /** * @param array $response * @param EmpikAction $action * @throws PrestaShopDatabaseException * @throws PrestaShopException */ protected function updateActionProductImport($response, $action) { $action->status = $response['import_status']; $action->import_count = $response['transform_lines_read']; $action->update(); } /** * @param array $response * @param EmpikAction $action * @throws PrestaShopDatabaseException * @throws PrestaShopException */ protected function updateActionOfferImport($response, $action) { $action->status = $response['status']; $action->import_count = $response['lines_read']; $action->update(); } /** * @param string $content * @param string $filename */ protected function contentOutput($content, $filename = 'error_report.csv') { if (ob_get_level() > 0) { ob_clean(); } header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-Length: ' . strlen($content)); echo $content; exit; } protected function l($string, $class = null, $addslashes = false, $htmlentities = true) { return Translate::getModuleTranslation($this->module, $string, get_class($this), null, $addslashes); } }