Skip to content

Commit

Permalink
export logs functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
GytisZum committed Sep 10, 2024
1 parent aeccb84 commit 0ad2305
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
107 changes: 104 additions & 3 deletions controllers/admin/AdminMollieLogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @codingStandardsIgnoreStart
*/

use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Adapter\Shop;
use Mollie\Config\Config;
use Mollie\Logger\LogFormatter;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function __construct()
$this->className = 'PrestaShopLogger';
$this->lang = false;
$this->noLink = true;
$this->allow_export = true;

parent::__construct();

Expand Down Expand Up @@ -96,16 +98,16 @@ public function __construct()

$this->_select .= '
REPLACE(a.`message`, "' . LogFormatter::MOLLIE_LOG_PREFIX . '", "") as message,
kpl.request, kpl.response, kpl.context
ml.request, ml.response, ml.context
';

$shopIdCheck = '';

if (VersionUtility::isPsVersionGreaterOrEqualTo('1.7.8.0')) {
$shopIdCheck = ' AND kpl.id_shop = a.id_shop';
$shopIdCheck = ' AND ml.id_shop = a.id_shop';
}

$this->_join .= ' JOIN ' . _DB_PREFIX_ . 'mol_logs kpl ON (kpl.id_log = a.id_log' . $shopIdCheck . ' AND a.object_type = "' . pSQL(Logger::LOG_OBJECT_TYPE) . '")';
$this->_join .= ' JOIN ' . _DB_PREFIX_ . 'mol_logs ml ON (ml.id_log = a.id_log' . $shopIdCheck . ' AND a.object_type = "' . pSQL(Logger::LOG_OBJECT_TYPE) . '")';
$this->_use_found_rows = false;
$this->list_no_link = true;
}
Expand Down Expand Up @@ -337,4 +339,103 @@ protected function ajaxResponse($value = null, $controller = null, $method = nul

exit;
}

public function processExport($textDelimiter = '"')
{
// clean buffer
if (ob_get_level() && ob_get_length() > 0) {
ob_clean();
}

header('Content-type: text/csv');
header('Content-Type: application/force-download; charset=UTF-8');
header('Cache-Control: no-store, no-cache');
header('Content-disposition: attachment; filename="' . $this->table . '_' . date('Y-m-d_His') . '.csv"');

$fd = fopen('php://output', 'wb');

/** @var $configuration */
$configuration = $this->module->getService(ConfigurationAdapter::class);

Check failure on line 358 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

PHPDoc tag @var has invalid value ($configuration): Unexpected token "$configuration", expected type at offset 10

Check failure on line 358 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

PHPDoc tag @var has invalid value ($configuration): Unexpected token "$configuration", expected type at offset 10

/** @var Context $context */
$context = $this->module->getService(Mollie\Adapter\Context::class);

$storeInfo = [
'PrestaShop Version' => _PS_VERSION_,
'PHP Version' => phpversion(),
'Module Version' => $this->module->version,
'MySQL Version' => \DB::getInstance()->getVersion(),

Check failure on line 367 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

Class Db referenced with incorrect case: DB.

Check failure on line 367 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

Class Db referenced with incorrect case: DB.
'Shop URL' => $context->getShopDomain(),

Check failure on line 368 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

Call to an undefined method Context::getShopDomain().

Check failure on line 368 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

Call to an undefined method Context::getShopDomain().
'Shop Name' => $context->getShopName(),

Check failure on line 369 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

Call to an undefined method Context::getShopName().

Check failure on line 369 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

Call to an undefined method Context::getShopName().
];

$moduleConfigurations = [
'Environment' => $configuration->get(Config::MOLLIE_ENVIRONMENT) ? "Production" : "Sandbox",
'Components' => $configuration->get(Config::MOLLIE_IFRAME),
'OCP' => $configuration->get(Config::MOLLIE_SINGLE_CLICK_PAYMENT),
'Locale Webshop' => $configuration->get(Config::MOLLIE_PAYMENTSCREEN_LOCALE),
'Subscriptions enabled' => $configuration->get(Config::MOLLIE_SUBSCRIPTION_ENABLED),
];

$psSettings = [
'Default country' => $configuration->get('PS_COUNTRY_DEFAULT'),
'Default currency' => $configuration->get('PS_CURRENCY_DEFAULT'),
'Default language' => $configuration->get('PS_LANG_DEFAULT'),
'Round mode' => $configuration->get('PS_PRICE_ROUND_MODE'),
'Round type' => $configuration->get('PS_ROUND_TYPE'),
'Current theme name' => $context->getShopThemeName(),

Check failure on line 386 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

Call to an undefined method Context::getShopThemeName().

Check failure on line 386 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

Call to an undefined method Context::getShopThemeName().
'PHP memory limit' => ini_get('memory_limit'),
];

$moduleConfigurationsInfo = "**Module configurations:**\n";
foreach ($moduleConfigurations as $key => $value) {
$moduleConfigurationsInfo .= "- $key: $value\n";
}

$psSettingsInfo = "**Prestashop settings:**\n";
foreach ($psSettings as $key => $value) {
$psSettingsInfo .= "- $key: $value\n";
}

fputcsv($fd, array_keys($storeInfo), ';', $textDelimiter);
fputcsv($fd, $storeInfo, ';', $textDelimiter);
fputcsv($fd, [], ';', $textDelimiter);

fputcsv($fd, [$moduleConfigurationsInfo], ';', $textDelimiter);
fputcsv($fd, [$psSettingsInfo], ';', $textDelimiter);

$query = new \DbQuery();

$query
->select('ml.id_log, l.severity, l.message, ml.request, ml.response, ml.context, ml.date_add')
->from('mol_logs', 'ml')
->leftJoin('log', 'l', 'ml.id_log = l.id_log')
->orderBy('ml.id_log DESC')
->limit(1000);

$result = \Db::getInstance()->executeS($query);

$firstRow = $result[0];
foreach ($firstRow as $key => $value) {
$headers[] = strtoupper($key);
}

$fd = fopen('php://output', 'wb');

fputcsv($fd, $headers, ';', $textDelimiter);

Check failure on line 425 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.6.8)

Variable $headers might not be defined.

Check failure on line 425 in controllers/admin/AdminMollieLogsController.php

View workflow job for this annotation

GitHub Actions / PHPStan (1.7.7.0)

Variable $headers might not be defined.

$content = !empty($result) ? $result : [];

foreach ($content as $row) {
$rowValues = [];
foreach ($row as $key => $value) {
$rowValues[] = $value;
}

fputcsv($fd, $rowValues, ';', $textDelimiter);
}

@fclose($fd);
die;
}
}
10 changes: 10 additions & 0 deletions src/Adapter/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public function getShopGroupId(): int
return (int) PrestashopContext::getContext()->shop->id_shop_group;
}

public function getShopThemeName(): string
{
return PrestashopContext::getContext()->shop->theme_name;
}

public function getShopName(): string
{
return PrestashopContext::getContext()->shop->name;
}

public function formatPrice(float $price, string $isoCode): string
{
$locale = PrestashopContext::getContext()->getCurrentLocale();
Expand Down

0 comments on commit 0ad2305

Please sign in to comment.