Skip to content

Commit

Permalink
Merge pull request #2653 from dpfaffenbauer/issue/2651
Browse files Browse the repository at this point in the history
[OrderBundle] use user locale for transition and state translations
  • Loading branch information
dpfaffenbauer authored Jun 21, 2024
2 parents 030fedd + 130482e commit 820258e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/CoreShop/Bundle/CoreBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

class OrderController extends BaseOrderController
{
protected function prepareSale(OrderInterface $order): array
protected function prepareSale(OrderInterface $order, string $locale = null): array
{
$serialized = parent::prepareSale($order);
$serialized = parent::prepareSale($order, $locale);

if ($order instanceof CoreOrderInterface) {
$serialized['carrier'] = $order->getCarrier() instanceof CarrierInterface ? $order->getCarrier()->getId() : null;
Expand All @@ -39,9 +39,9 @@ protected function prepareSale(OrderInterface $order): array
return $serialized;
}

protected function getDetails(OrderInterface $order): array
protected function getDetails(OrderInterface $order, ?string $locale = null): array
{
$serialized = parent::getDetails($order);
$serialized = parent::getDetails($order, $locale);

if ($order instanceof CoreOrderInterface) {
$serialized['shippingPayment'] = [
Expand Down
50 changes: 40 additions & 10 deletions src/CoreShop/Bundle/OrderBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public function getStatesAction(Request $request): Response
$transitions = [];
$worklows = $this->container->get('workflows');

/**
* @var \Pimcore\Security\User\User $user
*/
$user = $this->getUser();
$locale = $user->getUser()->getLanguage();

foreach ($identifiers as $identifier) {
$stateMachine = null;

Expand Down Expand Up @@ -110,6 +116,7 @@ public function getStatesAction(Request $request): Response
$identifier,
$place,
false,
$locale,
);
}

Expand Down Expand Up @@ -245,8 +252,13 @@ public function listAction(Request $request, OrderRepositoryInterface $orderRepo
$orders = $list->getData();
$jsonSales = [];

/**
* @var \Pimcore\Security\User\User $user
*/
$user = $this->getUser();

foreach ($orders as $order) {
$jsonSales[] = $this->prepareSale($order);
$jsonSales[] = $this->prepareSale($order, $user->getUser()->getLanguage());
}

return $this->viewHandler->handle([
Expand All @@ -268,7 +280,12 @@ public function detailAction(Request $request, OrderRepositoryInterface $orderRe
return $this->viewHandler->handle(['success' => false, 'message' => "Order with ID '$orderId' not found"]);
}

$jsonSale = $this->getDetails($order);
/**
* @var \Pimcore\Security\User\User $user
*/
$user = $this->getUser();

$jsonSale = $this->getDetails($order, $user->getUser()->getLanguage());

return $this->viewHandler->handle(['success' => true, 'sale' => $jsonSale]);
}
Expand Down Expand Up @@ -395,7 +412,7 @@ public function findOrderAction(Request $request, OrderRepositoryInterface $orde
return $this->viewHandler->handle(['success' => false]);
}

protected function prepareSale(OrderInterface $order): array
protected function prepareSale(OrderInterface $order, ?string $locale = null): array
{
$date = $order->getOrderDate()->getTimestamp();

Expand Down Expand Up @@ -424,21 +441,25 @@ protected function prepareSale(OrderInterface $order): array
'coreshop_order',
$order->getOrderState() ?? OrderStates::STATE_NEW,
false,
$locale,
),
'orderPaymentState' => $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_payment',
$order->getPaymentState(),
false,
$locale,
),
'orderShippingState' => $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_shipment',
$order->getShippingState(),
false,
$locale,
),
'orderInvoiceState' => $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_invoice',
$order->getInvoiceState(),
false,
$locale,
),
];

Expand Down Expand Up @@ -488,7 +509,7 @@ protected function prepareAddress(AddressInterface $address, string $type): arra
return $values;
}

protected function getDetails(OrderInterface $order): array
protected function getDetails(OrderInterface $order, ?string $locale = null): array
{
$jsonSale = $this->container->get('jms_serializer')->toArray($order);

Expand Down Expand Up @@ -564,21 +585,25 @@ protected function getDetails(OrderInterface $order): array
'coreshop_order',
$order->getOrderState() ?? OrderStates::STATE_NEW,
false,
$locale,
);
$jsonSale['orderPaymentState'] = $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_payment',
$order->getPaymentState() ?? OrderPaymentStates::STATE_NEW,
false,
$locale,
);
$jsonSale['orderShippingState'] = $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_shipment',
$order->getShippingState() ?? OrderShipmentStates::STATE_NEW,
false,
$locale,
);
$jsonSale['orderInvoiceState'] = $this->container->get(WorkflowStateInfoManagerInterface::class)->getStateInfo(
'coreshop_order_invoice',
$order->getInvoiceState() ?? OrderInvoiceStates::STATE_NEW,
false,
$locale,
);

$availableTransitions = $this->container->get(WorkflowStateInfoManagerInterface::class)->parseTransitions(
Expand All @@ -588,17 +613,18 @@ protected function getDetails(OrderInterface $order): array
'cancel',
],
false,
$locale,
);

$jsonSale['availableOrderTransitions'] = $availableTransitions;
$jsonSale['statesHistory'] = $this->getStatesHistory($order);

$invoices = $this->getInvoices($order);
$invoices = $this->getInvoices($order, $locale);

$jsonSale['editable'] = $this->container->get(OrderEditPossibleInterface::class)->isOrderEditable($order);
$jsonSale['invoices'] = $invoices;
$jsonSale['payments'] = $this->getPayments($order);
$jsonSale['shipments'] = $this->getShipments($order);
$jsonSale['payments'] = $this->getPayments($order, $locale);
$jsonSale['shipments'] = $this->getShipments($order, $locale);
$jsonSale['paymentCreationAllowed'] = !in_array(
$order->getOrderState(),
[OrderStates::STATE_CANCELLED, OrderStates::STATE_COMPLETE],
Expand Down Expand Up @@ -638,7 +664,7 @@ protected function getMailCorrespondence(OrderInterface $order): array
return $list;
}

protected function getInvoices(OrderInterface $order): array
protected function getInvoices(OrderInterface $order, ?string $locale = null): array
{
$invoices = $this->container->get('coreshop.repository.order_invoice')->getDocuments($order);
$invoiceArray = [];
Expand All @@ -660,6 +686,7 @@ protected function getInvoices(OrderInterface $order): array
'coreshop_invoice',
$invoice->getState(),
false,
$locale,
);
$data['transitions'] = $availableTransitions;

Expand All @@ -669,7 +696,7 @@ protected function getInvoices(OrderInterface $order): array
return $invoiceArray;
}

protected function getShipments(OrderInterface $order): array
protected function getShipments(OrderInterface $order, ?string $locale = null): array
{
$shipments = $this->container->get('coreshop.repository.order_shipment')->getDocuments($order);
$shipmentArray = [];
Expand All @@ -692,6 +719,7 @@ protected function getShipments(OrderInterface $order): array
'coreshop_shipment',
$shipment->getState(),
false,
$locale,
);
$data['transitions'] = $availableTransitions;

Expand Down Expand Up @@ -800,7 +828,7 @@ protected function getStatesHistory(OrderInterface $order): array
return $statesHistory;
}

protected function getPayments(OrderInterface $order): array
protected function getPayments(OrderInterface $order, ?string $locale = null): array
{
$payments = $this->container->get('coreshop.repository.payment')->findForPayable($order);
$return = [];
Expand Down Expand Up @@ -830,6 +858,7 @@ protected function getPayments(OrderInterface $order): array
'refund',
],
false,
$locale,
);

$return[] = [
Expand All @@ -843,6 +872,7 @@ protected function getPayments(OrderInterface $order): array
'coreshop_payment',
$payment->getState(),
false,
$locale,
),
'transitions' => $availableTransitions,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ public function __construct(
) {
}

public function getStateInfo(string $workflowName, string $value, bool $forFrontend = true): array
public function getStateInfo(string $workflowName, string $value, bool $forFrontend = true, ?string $locale = null): array
{
$transPrefix = $forFrontend ? 'coreshop.ui.workflow.state.' : 'coreshop_workflow_state_';
$transValue = $transPrefix . $workflowName . ($forFrontend ? '.' : '_') . $value;

$color = $this->stateColors[$workflowName]['place_colors'][$value] ?? '#f6f1de';

return [
'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'),
'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin', $locale),
'state' => $value,
'color' => $color,
];
}

public function parseTransitions(object $subject, string $workflowName, array $transitions = [], bool $forFrontend = true): array
public function parseTransitions(object $subject, string $workflowName, array $transitions = [], bool $forFrontend = true, ?string $locale = null): array
{
$event = new WorkflowTransitionEvent($transitions, $workflowName);
$this->eventDispatcher->dispatch($event, 'coreshop.workflow.valid_transitions');
Expand All @@ -59,21 +59,21 @@ public function parseTransitions(object $subject, string $workflowName, array $t
$workflow = $this->stateMachineManager->get($subject, $workflowName);
foreach ($event->getAllowedTransitions() as $transition) {
if ($workflow->can($subject, $transition)) {
$valid[] = $this->getTransitionInfo($workflowName, $transition, $forFrontend);
$valid[] = $this->getTransitionInfo($workflowName, $transition, $forFrontend, $locale);
}
}

return $valid;
}

public function getTransitionInfo(string $workflowName, string $transition, bool $forFrontend = true): array
public function getTransitionInfo(string $workflowName, string $transition, bool $forFrontend = true, ?string $locale = null): array
{
$transPrefix = $forFrontend ? 'coreshop.ui.workflow.transition.' : 'coreshop_workflow_transition_';
$transValue = $transPrefix . $workflowName . ($forFrontend ? '.' : '_') . $transition;
$color = $this->stateColors[$workflowName]['transition_colors'][$transition] ?? '#999999';

return [
'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin'),
'label' => $this->translator->trans($transValue, [], $forFrontend ? null : 'admin', $locale),
'transition' => $transition,
'color' => $color,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface WorkflowStateInfoManagerInterface
*/
public function getStateHistory(DataObject\Concrete $object): array;

public function getStateInfo(string $workflowName, string $value, bool $forFrontend = true): array;
public function getStateInfo(string $workflowName, string $value, bool $forFrontend = true, ?string $locale = null): array;

public function parseTransitions(object $subject, string $workflowName, array $transitions = [], bool $forFrontend = true);
public function parseTransitions(object $subject, string $workflowName, array $transitions = [], bool $forFrontend = true, ?string $locale = null);
}

0 comments on commit 820258e

Please sign in to comment.