Files
interblue.pl/modules/x13allegro/controllers/admin/AdminXAllegroAccountsController.php
2024-10-25 14:16:28 +02:00

440 lines
16 KiB
PHP

<?php
require_once (dirname(__FILE__) . '/../../x13allegro.php');
use x13allegro\Api\XAllegroApi;
use x13allegro\Api\DataProvider\AfterSaleServicesProvider;
use x13allegro\Api\DataProvider\MarketplacesProvider;
use x13allegro\Api\DataProvider\ResponsiblePersonsProvider;
use x13allegro\Api\Model\Marketplace\Enum\Marketplace;
use x13allegro\Exception\ModuleException;
final class AdminXAllegroAccountsController extends XAllegroController
{
/** @var XAllegroAccount */
public $object;
public function __construct()
{
$this->table = 'xallegro_account';
$this->identifier = 'id_xallegro_account';
$this->className = 'XAllegroAccount';
$this->multiple_fieldsets = true;
parent::__construct();
$this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, Tab::getIdFromClassName('AdminXAllegroAccounts'));
$this->fields_list = array(
'id_xallegro_account' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 50,
'class' => 'fixed-width-xs'
),
'username' => array(
'title' => $this->l('Nazwa użytkownika'),
'width' => 300
),
'default' => array(
'title' => $this->l('Domyślny'),
'active' => 'default',
'width' => 100,
'class' => 'fixed-width-sm',
'align' => 'center',
'type' => 'bool'
),
'active' => array(
'title' => $this->l('Aktywny'),
'active' => 'active',
'width' => 100,
'class' => 'fixed-width-sm',
'align' => 'center',
'type' => 'bool'
),
'sandbox' => array(
'title' => $this->l('Sandbox'),
'width' => 100,
'class' => 'fixed-width-sm',
'align' => 'center',
'type' => 'bool',
'icon' => array(
'0' => array('class' => 'icon-remove'),
'1' => array('class' => 'icon-check')
),
),
'base_marketplace' => array(
'title' => $this->l('Rynek bazowy'),
'width' => 100,
'class' => 'fixed-width-xl',
'callback' => 'printMarketplace',
'search' => false
),
'expire_refresh' => array(
'title' => $this->l('Ważność autoryzacji'),
'callback' => 'printExpire',
'search' => false
)
);
$this->tpl_folder = 'x_allegro_accounts/';
if (Tools::isSubmit('cancelAuthorization')) {
(new XAllegroAccount(Tools::getValue('id_xallegro_account')))->resetAuthorization(false);
}
}
public function initPageHeaderToolbar()
{
if (empty($this->display))
{
$this->page_header_toolbar_btn['allegro_current'] = array(
'href' => $this->context->link->getAdminLink('AdminXAllegroAccounts') . '&addxallegro_account',
'desc' => $this->l('Dodaj nowe konto'),
'icon' => 'process-icon-new'
);
}
parent::initPageHeaderToolbar();
}
public function renderList()
{
$this->addRowAction('xAuthorize');
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->specificConfirmDelete = $this->l('Usunąć wybrane konto?') . '\n\n' .
$this->l('Spowoduje to usunięcie wszystkich powiązań ofert do produktów dla wybranego konta!');
if (!Tools::usingSecureMode() && Configuration::get('PS_SSL_ENABLED'))
{
$href = 'https://' . Tools::safeOutput(Tools::getServerName()) . Tools::safeOutput($_SERVER['REQUEST_URI']);
$this->errors[] = '<b>' . $this->l('Uwaga! SSL jest włączony.') . '</b><br>' .
$this->l('Aby poprawnie autoryzować konto Allegro zaloguj się do trybu bezpiecznego (https://)') .
': <a href="' . $href . '">' . $href .'</a>';
}
return parent::renderList();
}
public function renderForm()
{
$this->fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Dane integracji konta Allegro'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Nazwa użytkownika'),
'desc' => $this->l('Uzupełnij jedną z poniższych wartości') . ':'
. '<br> - ' . $this->l('ID konta Allegro')
. '<br> - ' . $this->l('login konta Allegro')
. '<br> - ' . $this->l('email przypisany do konta Allegro'),
'name' => 'username',
'size' => 30,
'class' => 'fixed-width-xxl',
'required' => true
),
array(
'type' => 'switch',
'label' => $this->l('Sandbox'),
'name' => 'sandbox',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'sandbox_on',
'value' => 1,
'label' => $this->l('Tak')
),
array(
'id' => 'sandbox_off',
'value' => 0,
'label' => $this->l('Nie')
)
),
'desc' => $this->l('Portal testowy Allegro')
),
array(
'type' => 'switch',
'label' => $this->l('Domyślne konto'),
'name' => 'default',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'default_on',
'value' => 1,
'label' => $this->l('Tak')
),
array(
'id' => 'default_off',
'value' => 0,
'label' => $this->l('Nie')
)
),
'desc' => $this->l('Automatycznie wybierane podczas wystawiania przedmiotów')
),
array(
'type' => 'switch',
'label' => $this->l('Aktywne konto'),
'name' => 'active',
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Tak')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Nie')
)
),
'default_value' => 1
)
),
'submit' => array(
'title' => $this->l('Zapisz')
)
);
return parent::renderForm();
}
public function postProcess()
{
$allegroAccountId = (int)Tools::getValue($this->identifier);
$responsiblePerson = Tools::getValue('responsible_person');
if (Tools::isSubmit('submitAdd' . $this->table) && $allegroAccountId && !empty($responsiblePerson)) {
$configurationAccount = new XAllegroConfigurationAccount($allegroAccountId);
$configurationAccount->updateValue('RESPONSIBLE_PERSON_DEFAULT_ID', $responsiblePerson);
}
return parent::postProcess();
}
public function printExpire($id, $row)
{
$now = new DateTime();
$expire = new DateTime($row['expire_refresh']);
$interval = $now->diff($expire);
$days = (int)$interval->format('%R%a');
$hours = (int)$interval->format('%R%h');
if (($days < 1 && $hours < 1) || empty($row['access_token']) || empty($row['refresh_token'])) {
return '<span class="badge badge-danger">' . $this->l('autoryzuj konto') . '</span>';
}
else if ($days < 14) {
return '<span class="badge badge-warning">' . $this->l('niedługo wygaśnie') . '</span>';
}
return '<span class="badge badge-success">' . $this->l('zautoryzowane') . '</span>';
}
public function printMarketplace($id, $row)
{
if ($row['base_marketplace']) {
try {
return Marketplace::from($row['base_marketplace'])->getValueTranslated();
}
catch (\UnexpectedValueException $ex) {
return $this->l('Nieobsługiwany rynek') . ' (' . $row['base_marketplace'] . ')';
}
}
return '';
}
public function displayXAuthorizeLink($token = null, $id, $name = null)
{
$tpl = $this->context->smarty->createTemplate($this->module->getLocalPath() . 'views/templates/admin/' . $this->tpl_folder . 'helpers/list/action_authorize.tpl');
$tpl->assign(array(
'href' => $this->context->link->getAdminLink('AdminXAllegroAccounts') . '&authorize&id_xallegro_account=' . $id,
'action' => $this->l('Autoryzuj'),
'title' => $this->l('Autoryzacja użytkownika'),
'id_account' => $id
));
return $tpl->fetch();
}
public function ajaxProcessAuthorizeApplication()
{
$account = new XAllegroAccount(Tools::getValue('id_account'));
if (!Validate::isLoadedObject($account) || !$account->active) {
die(json_encode(array(
'success' => false,
'text' => $this->l('Autoryzacja konta Allegro: Konto nieaktywne.')
)));
}
try {
$result = (new XAllegroApi($account, false))->auth()->getDeviceCode();
$account->setDeviceCode($result);
$tpl = $this->context->smarty->createTemplate($this->module->getLocalPath() . 'views/templates/admin/' . $this->tpl_folder . 'helpers/list/action_authorize_modal.tpl');
$tpl->assign(array(
'accountAuthUrl' => $result->verification_uri_complete,
'redirectUrl' => $this->context->link->getAdminLink('AdminXAllegroAccounts'),
'configurationUrl' => $this->context->link->getAdminLink('AdminXAllegroConfiguration') . '#xallegro_configuration_fieldset_advanced_settings',
'id_account' => $account->id
));
$html = $tpl->fetch();
}
catch (Exception $ex) {
die(json_encode(array(
'success' => false,
'text' => (string)$ex
)));
}
die(json_encode(array(
'success' => true,
'html' => $html,
'interval' => $result->interval,
'username' => $account->username
)));
}
public function ajaxProcessAuthorizeApplicationCheck()
{
$account = new XAllegroAccount(Tools::getValue('id_account'));
if (!Validate::isLoadedObject($account) || !$account->active) {
die(json_encode([
'success' => false,
'text' => $this->l('Autoryzacja konta Allegro: Konto nieaktywne.')
]));
}
try {
$result = (new XAllegroApi($account, false))->auth()->authorizeDevice();
$clear = new StdClass();
$clear->user_code = null;
$clear->device_code = null;
$account = $account->setDeviceCode($clear)
->setRefreshToken($result);
$me = (new XAllegroApi($account))->account()->me();
$account = $account->setBaseMarketplace($me->baseMarketplace->id);
$accountIdentity = false;
if ($account->username == $me->id
|| $account->username == $me->login
|| $account->username == $me->email
) {
$accountIdentity = true;
}
// block authorization when typed username does not match Allegro account
if (!$accountIdentity) {
$account->resetAuthorization(false);
throw new ModuleException($this->l("Wpisana nazwa użytkownika ($account->username) nie jest tożsama ze zautoryzowanym kontem Allegro"));
}
// block authorization when Marketplace is not supported by module
//if (!Marketplace::isValid($me->baseMarketplace->id)) {
if ($me->baseMarketplace->id != XAllegroApi::MARKETPLACE_PL) {
$account->resetAuthorization(false);
throw new ModuleException($this->l("Nieobsługiwany rynek ({$me->baseMarketplace->id})"));
}
}
catch (Exception $ex) {
if ($ex->getCode() == 400) {
die(json_encode([
'success' => true,
'process' => true
]));
}
die(json_encode([
'success' => false,
'text' => (string)$ex
]));
}
$marketplacesProvider = new MarketplacesProvider($account->base_marketplace);
$configurationForm = false;
// if id_language is empty then authorization is performed for the first time
if (!$account->id_language) {
$emptyOption = [
'id' => '',
'name' => $this->l('-- wybierz --')
];
$afterSaleServices = [];
$responsiblePersons = [];
$responsiblePersons[] = $emptyOption;
$shopLanguages[] = $emptyOption;
foreach (Language::getLanguages() as $language) {
$marketplaceLanguage = $marketplacesProvider->getMarketplaceLanguage();
$shopLanguages[] = [
'id' => $language['id_lang'],
'name' => $language['name'],
'isMarketplaceLanguage' => (Validate::isLoadedObject($marketplaceLanguage) && $language['id_lang'] == $marketplaceLanguage->id)
];
}
try {
$api = new XAllegroApi($account);
$afterSaleServicesProvider = new AfterSaleServicesProvider($api);
$responsiblePersonsProvider = new ResponsiblePersonsProvider($api);
foreach ($afterSaleServicesProvider->getAllServices() as $afterSaleServiceGroup => $afterSaleService) {
$afterSaleServices[$afterSaleServiceGroup][] = $emptyOption;
foreach ($afterSaleService as $service) {
$afterSaleServices[$afterSaleServiceGroup][] = (array)$service;
}
}
foreach ($responsiblePersonsProvider->getResponsiblePersons() as $responsiblePerson) {
$responsiblePersons[] = [
'id' => $responsiblePerson->id,
'name' => $responsiblePerson->name
];
}
}
catch (Exception $ex) {}
$tpl = $this->context->smarty->createTemplate($this->module->getLocalPath() . 'views/templates/admin/' . $this->tpl_folder . 'helpers/list/action_authorize_configuration_modal.tpl');
$tpl->assign([
'formAction' => $this->context->link->getAdminLink('AdminXAllegroAccounts'),
'cancelAction' => $this->context->link->getAdminLink('AdminXAllegroAccounts') . '&id_xallegro_account=' . $account->id . '&cancelAuthorization',
'accountId' => $account->id,
'shopLanguages' => $shopLanguages,
'afterSaleServices' => $afterSaleServices,
'responsiblePersons' => $responsiblePersons
]);
$configurationForm = $tpl->fetch();
}
// return "process" false when done
die(json_encode([
'success' => true,
'process' => false,
'baseMarketplace' => $marketplacesProvider->getMarketplaceName(),
'configurationForm' => $configurationForm
]));
}
}