]> git.agnieray.net Git - galette.git/commitdiff
Handle RTL on PDF; closes #1430
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 31 May 2020 09:58:48 +0000 (11:58 +0200)
committerJohan Cwiklinski <johan@x-tnd.be>
Mon, 1 Jun 2020 05:48:11 +0000 (07:48 +0200)
galette/includes/fields_defs/pdfmodels_fields.php
galette/lib/Galette/Core/I18n.php
galette/lib/Galette/IO/Pdf.php
galette/lib/Galette/IO/PdfAttendanceSheet.php
galette/lib/Galette/IO/PdfGroups.php
galette/lib/Galette/IO/PdfMembersCards.php
galette/lib/Galette/IO/PdfMembersLabels.php

index 7d49daae490f65ca49a3bc42ba55b75f346658fc..42c47d1f8a77cf95f699d5796887e9f18b36fa20 100644 (file)
@@ -81,6 +81,16 @@ strong#asso_name {
 td#pdf_logo {
     text-align: right;
     width: 25%;
+}
+
+div[dir=\'rtl\'] td#pdf_logo {
+    text-align: left;
+    width: 0%;
+}
+
+div[dir=rtl] td#pdf_assoname {
+    width: 100%;
+    margin-left: 25%;
 }',
         'model_parent'  => null
     ),
index 6e89c9f562a8aeae89b3dced1bf0907ac4e3e06e..3b963a7b16c7f4400e398f45c7adb14aaaaa5ee2 100644 (file)
@@ -28,7 +28,7 @@
  * @package   Galette
  *
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2007-2018 The Galette Team
+ * @copyright 2007-2020 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @version   SVN: $Id$
  * @link      http://galette.tuxfamily.org
@@ -46,7 +46,7 @@ use Analog\Analog;
  * @name      i18n
  * @package   Galette
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2007-2014 The Galette Team
+ * @copyright 2007-2020 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @link      http://galette.tuxfamily.org
  * @since     Available since 0.7dev - 2007-07-06
@@ -64,6 +64,14 @@ class I18n
     private $dir = 'lang/';
     private $path;
 
+    private $rtl_langs = [
+        'ar',
+        'az',
+        'fa',
+        'he',
+        'ur'
+    ];
+
     /**
      * Default constructor.
      * Initialize default language and set environment variables
@@ -296,4 +304,17 @@ class I18n
         $this->langs = $langs;
         return $this->langs;
     }
+
+    /**
+     * Is current language RTL?
+     *
+     * @return boolean
+     */
+    public function isRTL()
+    {
+        return in_array(
+            $this->getAbbrev(),
+            $this->rtl_langs
+        );
+    }
 }
index 02f7e488f3f6c4a9bb5b61683e65fbe693de5846..e36620cf883cb4a3b04651663b050a4fa366d0bb 100644 (file)
@@ -50,11 +50,6 @@ use Analog\Analog;
  */
 require_once GALETTE_CONFIG_PATH . 'galette_tcpdf_config.php';
 
-/**
- *  Require TCPDF class
- */
-require_once GALETTE_TCPDF_PATH . '/tcpdf.php';
-
 /**
  * PDF class for galette
  *
@@ -77,6 +72,7 @@ class Pdf extends \TCPDF
     const FONT_SIZE = 10;
 
     protected $preferences;
+    protected $i18n;
     private $model;
     private $paginated = false;
     protected $filename;
@@ -89,7 +85,10 @@ class Pdf extends \TCPDF
      */
     public function __construct(Preferences $prefs, $model = null)
     {
+        global $i18n;
+
         $this->preferences = $prefs;
+        $this->i18n = $i18n;
         parent::__construct('P', 'mm', 'A4', true, 'UTF-8');
         //set some values
         $this->SetCreator(PDF_CREATOR);
@@ -106,6 +105,10 @@ class Pdf extends \TCPDF
             $name . ' (using Galette ' . GALETTE_VERSION . ')'
         );
 
+        if ($this->i18n->isRTL()) {
+            $this->setRTL(true);
+        }
+
         if ($model !== null) {
             if ($model instanceof PdfModel) {
                 $this->model = $model;
@@ -284,7 +287,7 @@ class Pdf extends \TCPDF
                     '/' . $this->getAliasNbPages(),
                     0,
                     1,
-                    'R'
+                    ($this->i18n->isRTL() ? 'L' : 'R')
                 );
             }
         }
@@ -304,7 +307,7 @@ class Pdf extends \TCPDF
             if (trim($this->model->hstyles) !== '') {
                 $html .= "<style>\n" . $this->model->hstyles . "\n</style>\n\n";
             }
-            $html .= $this->model->hheader;
+            $html .= "<div dir=\"".($this->i18n->isRTL() ? 'rtl' : 'ltr')."\">" . $this->model->hheader . "</div>";
             $this->writeHtml($html, true, false, true, false, '');
 
             if ($title !== null) {
@@ -370,17 +373,21 @@ class Pdf extends \TCPDF
                 6,
                 $this->preferences->pref_nom,
                 0,
-                'L'
+                ($this->i18n->isRTL() ? 'R' : 'L')
             );
             $this->SetFont(self::FONT, 'B', self::FONT_SIZE + 2);
 
             if ($title !== null) {
-                $this->Cell(0, 6, $title, 0, 1, 'L', 0);
+                $this->Cell(0, 6, $title, 0, 1, ($this->i18n->isRTL() ? 'R' : 'L'), 0);
             }
             $yend = $this->getY();//store position at the end of the text
 
             $this->SetY($ystart);
-            $x = 190 - $wlogo; //right align
+            if ($this->i18n->isRTL()) {
+                $x = $this->getX();
+            } else {
+                $x = 190 - $wlogo; //right align
+            }
             $this->Image($logofile, $x, $this->GetY(), $wlogo, $hlogo);
             $this->y += $hlogo + 3;
             //if position after logo is < than position after text,
index 1de10b98006f81786439745862705245b196c843..5745f67dc620266d7a532bf758d662190d784eae 100644 (file)
@@ -191,7 +191,7 @@ class PdfAttendanceSheet extends Pdf
         $mcount = 0;
         foreach ($members as $m) {
             $mcount++;
-            $this->Cell(10, 16, $mcount, 'LTB', 0, 'R');
+            $this->Cell(10, 16, $mcount, ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0, 'R');
 
             if ($m->hasPicture() && $this->wimages) {
                 $p = $m->picture->getPath();
@@ -216,16 +216,20 @@ class PdfAttendanceSheet extends Pdf
 
                 $y = $this->getY() + 1;
                 $x = $this->getX() + 1;
-                $this->Cell($wlogo+2, 16, '', 'LTB', 0);
-                $this->Image($p, $x, $y, $wlogo, $hlogo);
+                $ximg = $x;
+                if ($this->i18n->isRTL()) {
+                    $ximg = $this->getPageWidth() - $x - $wlogo;
+                }
+                $this->Cell($wlogo+2, 16, '', ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0);
+                $this->Image($p, $ximg, $y, $wlogo, $hlogo);
             } else {
                 $x = $this->getX() + 1;
-                $this->Cell(1, 16, '', 'LTB', 0);
+                $this->Cell(1, 16, '', ($this->i18n->isRTL() ? 'R' : 'L') . 'TB', 0);
             }
 
             $xs = $this->getX() - $x + 1;
-            $this->Cell(100 - $xs, 16, $m->sname, 'RTB', 0, 'L');
-            $this->Cell(80, 16, '', 1, 1, 'L');
+            $this->Cell(100 - $xs, 16, $m->sname, ($this->i18n->isRTL() ? 'L' : 'R') . 'TB', 0, ($this->i18n->isRTL() ? 'R' : 'L'));
+            $this->Cell(80, 16, '', 1, 1, ($this->i18n->isRTL() ? 'R' : 'L'));
         }
         $this->Cell(190, 0, '', 'T');
     }
index ce5b3c61146ed55dda25be30dd093f21af71bc00..3981f1d1a6c16b316dbb2e20bd9d840528b58883 100644 (file)
@@ -170,7 +170,7 @@ class PdfGroups extends Pdf
                     _T("Managers:") . ' ' . implode(', ', $managers),
                     0,
                     1,
-                    'R'
+                    ($this->i18n->isRTL() ? 'L' : 'R')
                 );
             }
             $this->ln(3);
@@ -187,10 +187,11 @@ class PdfGroups extends Pdf
             $members = $group->getMembers();
 
             foreach ($members as $m) {
-                $this->Cell(80, 7, $m->sname, 1, 0, 'L');
-                $this->Cell(50, 7, $m->email, 1, 0, 'L');
-                $this->Cell(30, 7, $m->phone, 1, 0, 'L');
-                $this->Cell(30, 7, $m->gsm, 1, 1, 'L');
+                $align = ($this->i18n->isRTL() ? 'R' : 'L');
+                $this->Cell(80, 7, $m->sname, 1, 0, $align);
+                $this->Cell(50, 7, $m->email, 1, 0, $align);
+                $this->Cell(30, 7, $m->phone, 1, 0, $align);
+                $this->Cell(30, 7, $m->gsm, 1, 1, $align);
             }
             $this->Cell(190, 0, '', 'T');
             $first = false;
index 5a4c934af70a323d04fdbfd381509b7da419d689..46442ed39824b5b27aa3f0555789ba7c8294cf07 100644 (file)
@@ -90,6 +90,7 @@ class PdfMembersCards extends Pdf
     public function __construct(Preferences $prefs)
     {
         parent::__construct($prefs);
+        $this->setRTL(false);
         $this->filename = __('cards') . '.pdf';
         $this->init();
     }
index 5a15a9897d5971c434d22ab7282b8e599b9bbc1f..31a179ef9a7467ddcb9a6101833757a85e24a005 100644 (file)
@@ -70,6 +70,7 @@ class PdfMembersLabels extends Pdf
     public function __construct(Preferences $prefs)
     {
         parent::__construct($prefs);
+        $this->setRTL(false);
         $this->filename = __('labels_print_filename') . '.pdf';
         $this->init();
     }