Skip to content

Commit 41ab016

Browse files
committed
Fix Arabic & Hebrew in invoices
Revert MC-13880 and bring back MAGETWO-95816 This reverts commit 1c0549e
1 parent cd77da6 commit 41ab016

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

app/code/Magento/Sales/Model/Order/Address/Renderer.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Customer\Model\Address\Config as AddressConfig;
1010
use Magento\Framework\Event\ManagerInterface as EventManager;
1111
use Magento\Sales\Model\Order\Address;
12+
use Magento\Framework\Stdlib\StringUtils;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
/**
1416
* Class Renderer used for formatting an order address
@@ -27,18 +29,26 @@ class Renderer
2729
*/
2830
protected $eventManager;
2931

32+
/**
33+
* @var StringUtils
34+
*/
35+
private $stringUtils;
36+
3037
/**
3138
* Constructor
3239
*
3340
* @param AddressConfig $addressConfig
3441
* @param EventManager $eventManager
42+
* @param StringUtils $stringUtils
3543
*/
3644
public function __construct(
3745
AddressConfig $addressConfig,
38-
EventManager $eventManager
46+
EventManager $eventManager,
47+
StringUtils $stringUtils = null
3948
) {
4049
$this->addressConfig = $addressConfig;
4150
$this->eventManager = $eventManager;
51+
$this->stringUtils = $stringUtils ?: ObjectManager::getInstance()->get(StringUtils::class);
4252
}
4353

4454
/**
@@ -58,4 +68,50 @@ public function format(Address $address, $type)
5868
$this->eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $address]);
5969
return $formatType->getRenderer()->renderArray($address->getData());
6070
}
71+
72+
/**
73+
* Detect an input string is Arabic
74+
*
75+
* @param string $subject
76+
* @return bool
77+
*/
78+
public function isArabic(string $subject): bool
79+
{
80+
return (preg_match('/\p{Arabic}/u', $subject) > 0);
81+
}
82+
83+
/**
84+
* Reverse text with Arabic characters
85+
*
86+
* @param string $string
87+
* @return string
88+
*/
89+
public function reverseArabicText($string)
90+
{
91+
$splitText = explode(' ', $string);
92+
for ($i = 0; $i < count($splitText); $i++) {
93+
if ($this->isArabic($splitText[$i])) {
94+
for ($j = $i + 1; $j < count($splitText); $j++) {
95+
$tmp = ($this->isArabic($splitText[$j]))
96+
? $this->stringUtils->strrev($splitText[$j]) : $splitText[$j];
97+
$splitText[$j] = ($this->isArabic($splitText[$i]))
98+
? $this->stringUtils->strrev($splitText[$i]) : $splitText[$i];
99+
$splitText[$i] = $tmp;
100+
}
101+
}
102+
}
103+
return implode(' ', $splitText);
104+
}
105+
106+
/**
107+
* Check and revert arabic text
108+
*
109+
* @param string $string
110+
* @return string
111+
*/
112+
public function processArabicText($string)
113+
{
114+
return ($this->isArabic($string))
115+
? $this->reverseArabicText($string) : $string;
116+
}
61117
}

app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
501501
if ($value !== '') {
502502
$text = [];
503503
foreach ($this->string->split($value, 45, true, true) as $_value) {
504-
$text[] = $_value;
504+
$text[] = ($this->addressRenderer->isArabic($_value))
505+
? $this->addressRenderer->reverseArabicText($_value) : $_value;
505506
}
506507
foreach ($text as $part) {
507508
$page->drawText(strip_tags(ltrim($part)), 35, $this->y, 'UTF-8');
@@ -518,7 +519,8 @@ protected function insertOrder(&$page, $obj, $putOrderId = true)
518519
if ($value !== '') {
519520
$text = [];
520521
foreach ($this->string->split($value, 45, true, true) as $_value) {
521-
$text[] = $_value;
522+
$text[] = ($this->addressRenderer->isArabic($_value))
523+
? $this->addressRenderer->reverseArabicText($_value) : $_value;
522524
}
523525
foreach ($text as $part) {
524526
$page->drawText(strip_tags(ltrim($part)), 285, $this->y, 'UTF-8');

app/code/Magento/Sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\Sales\Model\Order\Pdf\Items\Invoice;
99

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Sales\Model\Order\Address\Renderer;
12+
1013
/**
1114
* Sales Order Invoice Pdf default items renderer
1215
*/
@@ -19,6 +22,11 @@ class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
1922
*/
2023
protected $string;
2124

25+
/**
26+
* @var \Magento\Sales\Model\Order\Address\Renderer
27+
*/
28+
private $renderer;
29+
2230
/**
2331
* @param \Magento\Framework\Model\Context $context
2432
* @param \Magento\Framework\Registry $registry
@@ -29,6 +37,8 @@ class DefaultInvoice extends \Magento\Sales\Model\Order\Pdf\Items\AbstractItems
2937
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
3038
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
3139
* @param array $data
40+
* @param \Magento\Sales\Model\Order\Address\Renderer $renderer
41+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
3242
*/
3343
public function __construct(
3444
\Magento\Framework\Model\Context $context,
@@ -39,7 +49,8 @@ public function __construct(
3949
\Magento\Framework\Stdlib\StringUtils $string,
4050
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
4151
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
42-
array $data = []
52+
array $data = [],
53+
Renderer $renderer = null
4354
) {
4455
$this->string = $string;
4556
parent::__construct(
@@ -52,6 +63,7 @@ public function __construct(
5263
$resourceCollection,
5364
$data
5465
);
66+
$this->renderer = $renderer ?: ObjectManager::getInstance()->get(Renderer::class);
5567
}
5668

5769
/**
@@ -71,15 +83,15 @@ public function draw()
7183
$lines[0] = [
7284
[
7385
// phpcs:ignore Magento2.Functions.DiscouragedFunction
74-
'text' => $this->string->split(html_entity_decode($item->getName()), 35, true, true),
86+
'text' => $this->string->split($this->renderer->processArabicText(html_entity_decode($item->getName())), 35, true, true),
7587
'feed' => 35
7688
]
7789
];
7890

7991
// draw SKU
8092
$lines[0][] = [
8193
// phpcs:ignore Magento2.Functions.DiscouragedFunction
82-
'text' => $this->string->split(html_entity_decode($this->getSku($item)), 17),
94+
'text' => $this->string->split($this->renderer->processArabicText(html_entity_decode($this->getSku($item))), 17),
8395
'feed' => 290,
8496
'align' => 'right',
8597
];

0 commit comments

Comments
 (0)