129 lines
4.4 KiB
PHP
129 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace KupShop\IncomakerBundle\Admin\Tabs;
|
|
|
|
use KupShop\AdminBundle\Admin\WindowTab;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class IncomakerWindowTab extends WindowTab
|
|
{
|
|
protected $title = 'incomaker';
|
|
protected $template = 'window/settings.incomaker.tpl';
|
|
|
|
/** @var LoggerInterface */
|
|
protected $logger;
|
|
|
|
/** @required */
|
|
public function setLogger(LoggerInterface $logger)
|
|
{
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public static function getTypes()
|
|
{
|
|
return [
|
|
'settings' => 1,
|
|
];
|
|
}
|
|
|
|
public function getLabel()
|
|
{
|
|
return 'Incomaker';
|
|
}
|
|
|
|
public function handleUpdate()
|
|
{
|
|
$incomaker = getVal('incomaker');
|
|
$tags = $incomaker['tags'] ?? [];
|
|
$products_related = $incomaker['products_related'] ?? null;
|
|
$related_type = $incomaker['related_type'] ?? [];
|
|
$defaultDiscountTag = $incomaker['default_discount_tag'];
|
|
$account = $incomaker['account_uuid'] ?? '';
|
|
$plugin = $incomaker['plugin_uuid'] ?? '';
|
|
if (!empty($incomaker['oss_prices'])) {
|
|
$oss_prices = [0 => null];
|
|
foreach ($incomaker['oss_prices'] as $key => $item) {
|
|
if (!empty($item['delete']) || !$key) {
|
|
continue;
|
|
}
|
|
if (empty(array_filter($item))) {
|
|
continue;
|
|
}
|
|
$id = implode('_', $item);
|
|
$oss_prices[$id] = $item;
|
|
}
|
|
$oss_prices = array_values($oss_prices);
|
|
unset($oss_prices[0]);
|
|
}
|
|
$settings = \Settings::getDefault();
|
|
$incomaker = $settings->incomaker ?? [];
|
|
$incomaker['tags'] = $tags;
|
|
if ($products_related) {
|
|
$incomaker['products_related'] = $products_related;
|
|
if (findModule(\Modules::PRODUCTS_RELATED, \Modules::SUB_TYPES)) {
|
|
$incomaker['related_type'] = $related_type;
|
|
}
|
|
}
|
|
$incomaker['default_discount_tag'] = $defaultDiscountTag;
|
|
if (findModule(\Modules::OSS_VATS)) {
|
|
$incomaker['oss_prices'] = $oss_prices ?? null;
|
|
}
|
|
$incomaker['account_uuid'] = $account;
|
|
$incomaker['plugin_uuid'] = $plugin;
|
|
$settings->saveValue('incomaker', $incomaker);
|
|
}
|
|
|
|
public function handleIncomakerPlugin()
|
|
{
|
|
try {
|
|
$incomaker = getVal('incomaker');
|
|
unset($incomaker['api_key']);
|
|
$url = findModule(\Modules::INCOMAKER, 'url') ?? 'https://api.incomaker.com';
|
|
$url .= '/commons/v2/plugin/?'.http_build_query($incomaker);
|
|
|
|
$curl = curl_init();
|
|
curl_setopt_array(
|
|
$curl,
|
|
[
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_CUSTOMREQUEST => 'GET',
|
|
CURLOPT_HTTPHEADER => [
|
|
'content-type: application/json',
|
|
],
|
|
]
|
|
);
|
|
$this->logger->notice('Incomaker plugin: request', ['request_url' => $url]);
|
|
$response = curl_exec($curl);
|
|
$error = curl_error($curl);
|
|
curl_close($curl);
|
|
$this->logger->notice('Incomaker plugin: response', ['response' => $response, 'error' => $error]);
|
|
|
|
if ($error) {
|
|
throw new \Exception('curl error: '.$error);
|
|
}
|
|
|
|
$response_arr = json_decode($response, true);
|
|
$api_key = $response_arr['apiKey'] ?? null;
|
|
$accountId = $response_arr['accountId'] ?? null;
|
|
$pluginId = $response_arr['pluginId'] ?? null;
|
|
if (!$api_key || !$accountId || !$pluginId) {
|
|
throw new \Exception('Incomaker response: '.$response);
|
|
}
|
|
|
|
$data = [
|
|
'api_key' => $api_key, 'url' => $incomaker['url'], 'serverPalpUrl' => $incomaker['serverPalpUrl'],
|
|
'timeZone' => $incomaker['timeZone'], 'pluginVersion' => $incomaker['pluginVersion'],
|
|
'account_uuid' => $accountId, 'plugin_uuid' => $pluginId,
|
|
];
|
|
$dbcfg = \Settings::getDefault();
|
|
$dbcfg->saveValue('incomaker', $data);
|
|
\Settings::clearCache();
|
|
|
|
$this->window->returnOK();
|
|
} catch (\Throwable $e) {
|
|
$this->window->returnError("Chyba při registraci pluginu: {$e->getMessage()}");
|
|
}
|
|
}
|
|
}
|