Skip to content

Commit

Permalink
Merge branch '65182-add-widgets' into 'master'
Browse files Browse the repository at this point in the history
65182 add widgets

See merge request chameleon-system-oss/chameleon-shop!61
  • Loading branch information
pixeljunkie committed Jan 15, 2025
2 parents 9c97b7a + 082b2d0 commit 06ec2c9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
protected readonly string $defaultTimeframe,
protected readonly ColorGeneratorServiceInterface $colorGeneratorService
) {
parent::__construct($dashboardCacheService);
parent::__construct($dashboardCacheService, $translator);
}

public function getTitle(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
</trans-unit>
<trans-unit id="chameleon_system_ecommerce_stats.widgets.reload_button_label">
<source>chameleon_system_ecommerce_stats.widgets.reload_button_label</source>
<target>Neuladen</target>
<target>Aktualisieren</target>
</trans-unit>
</body>
</file>
Expand Down
34 changes: 31 additions & 3 deletions src/ShopBundle/Dashboard/Widgets/LastOrdersDashboardWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

namespace ChameleonSystem\ShopBundle\Dashboard\Widgets;

use ChameleonSystem\CmsDashboardBundle\Bridge\Chameleon\Attribute\ExposeAsApi;
use ChameleonSystem\CmsDashboardBundle\Bridge\Chameleon\Dashboard\Widgets\DashboardWidget;
use ChameleonSystem\CmsDashboardBundle\Bridge\Chameleon\Service\DashboardCacheService;
use ChameleonSystem\CmsDashboardBundle\DataModel\WidgetDropdownItemDataModel;
use ChameleonSystem\ShopBundle\Dashboard\DataModel\LastOrdersItemDataModel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\Translation\TranslatorInterface;

class LastOrdersDashboardWidget extends DashboardWidget
{
private const LAST_ORDER_SYSTEM_NAME = 'lastOrders';
private const LAST_ORDER_SYSTEM_NAME = 'last-orders';

public function __construct(
protected readonly DashboardCacheService $dashboardCacheService,
protected readonly \ViewRenderer $renderer,
protected readonly TranslatorInterface $translator)
{
parent::__construct($dashboardCacheService);
parent::__construct($dashboardCacheService, $translator);
}

public function getTitle(): string
Expand All @@ -27,7 +29,13 @@ public function getTitle(): string

public function getDropdownItems(): array
{
return [new WidgetDropdownItemDataModel('LastOrdersDashboardWidget', 'Alle Bestellungen', '/cms?pagedef=tablemanager&id=268')];
$reloadItem = new WidgetDropdownItemDataModel('lastOrdersDashboardWidgetReload', $this->translator->trans('chameleon_system_shop.widget.reload_button_label'), '');
$reloadItem->addDataAttribute('data-service-alias', 'widget-'.$this->getChartId());

return [
new WidgetDropdownItemDataModel('lastOrdersDashboardWidgetAllOrders', $this->translator->trans('chameleon_system_shop.widget.last_orders_all_orders'), '/cms?pagedef=tablemanager&id=268'),
$reloadItem
];
}

public function getChartId(): string
Expand All @@ -44,6 +52,17 @@ protected function generateBodyHtml(): string
return $this->renderer->Render('Dashboard/Widgets/last-orders.html.twig');
}

#[ExposeAsApi(description: 'Call this method dynamically via API:/cms/api/dashboard/widget/{widgetServiceId}/getStatsDataAsJson')]
public function getWidgetHtmlAsJson(): JsonResponse
{
$data = [
'htmlTable' => $this->getBodyHtml(true),
'dateTime' => date('d.m.Y H:i'),
];

return new JsonResponse(json_encode($data));
}

private function getLastOrders(): array
{
$orderData = [];
Expand Down Expand Up @@ -89,4 +108,13 @@ private function getDetailUrl(\TdbShopOrder $order): string
{
return '/cms?pagedef=tableeditor&tableid=268&id='.$order->id;
}

public function getFooterIncludes(): array
{
$includes = parent::getFooterIncludes();
$includes[] = '<script type="text/javascript" src="/bundles/chameleonsystemshop/js/dashboard.js"></script>';

return $includes;
}

}
2 changes: 2 additions & 0 deletions src/ShopBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@
<argument type="service" id="chameleon_system_view_renderer.view_renderer" />
<argument type="service" id="chameleon_system_core.translator" />
</service>

<service id="widget-last-orders" alias="chameleon_system_shop.dashboard_widgets.last_orders_dashboard_widget" public="true" />
</services>

</container>
60 changes: 60 additions & 0 deletions src/ShopBundle/Resources/public/js/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
document.addEventListener("DOMContentLoaded", function () {
// Attach event listener to the button
const button = document.querySelector("#lastOrdersDashboardWidgetReload");

if (button) {
button.addEventListener("click", function (event) {
event.preventDefault();

// URL and target information
const serviceAlias = this.getAttribute("data-service-alias");
const reloadUrl = `/cms/api/dashboard/widget/${serviceAlias}/getWidgetHtmlAsJson`;

// Fetch data
fetch(reloadUrl, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP-Error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
// Parse the JSON string if needed
const parsedData = typeof data === "string" ? JSON.parse(data) : data;
const { htmlTable, dateTime } = parsedData;

console.log(dateTime); // Verify the content

// Update the target container
const targetDiv = document.querySelector("#widget-last-orders .card-body");
if (targetDiv) {
// Animation: hide old HTML
targetDiv.style.opacity = 0;

setTimeout(() => {
// Insert new HTML
targetDiv.innerHTML = htmlTable;

// Animation: fade in new HTML
targetDiv.style.transition = "opacity 0.5s";
targetDiv.style.opacity = 1;
}, 300);
}

// Optionally update timestamp
const footerElement = document.querySelector(`#widget-last-orders .card-footer .widget-timestamp`);
if (footerElement) {
footerElement.textContent = dateTime;
}
})
.catch(error => {
console.error("Error loading the widget data:", error);
});
});
}
});
8 changes: 8 additions & 0 deletions src/ShopBundle/Resources/translations/admin.de.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@
<source>chameleon_system_shop.widget.last_orders_not_found</source>
<target>Keine Bestellungen gefunden</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.last_orders_all_orders">
<source>chameleon_system_shop.widget.last_orders_all_orders</source>
<target>Alle Bestellungen</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.reload_button_label">
<source>chameleon_system_shop.widget.reload_button_label</source>
<target>Aktualisieren</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.order_number">
<source>chameleon_system_shop.widget.order_number</source>
<target>Bestellnummer</target>
Expand Down
8 changes: 8 additions & 0 deletions src/ShopBundle/Resources/translations/admin.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@
<source>chameleon_system_shop.widget.last_orders_not_found</source>
<target>No orders found</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.last_orders_all_orders">
<source>chameleon_system_shop.widget.last_orders_all_orders</source>
<target>All Orders</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.reload_button_label">
<source>chameleon_system_shop.widget.reload_button_label</source>
<target>Reload</target>
</trans-unit>
<trans-unit id="chameleon_system_shop.widget.order_number">
<source>chameleon_system_shop.widget.order_number</source>
<target>Order Number</target>
Expand Down

0 comments on commit 06ec2c9

Please sign in to comment.