From d39b5c9ede3c74128e8a95e9927cac6f021b767b Mon Sep 17 00:00:00 2001
From: Laurent Laffont <llaffont@afi-sa.fr>
Date: Tue, 20 Sep 2022 08:52:55 +0000
Subject: [PATCH] Add admin var for editor

---
 library/Class/AdminVar.php                    | 11 ++++
 .../Plugin/Manager/ModeleFusion.php           | 19 -------
 library/ZendAfi/Form/Decorator/CodeMirror.php | 45 ++++++++++++++++
 library/ZendAfi/Form/Element/CodeMirror.php   | 24 +++++++++
 library/ZendAfi/Form/ModeleFusion.php         |  4 +-
 .../admin/controllers/PrintControllerTest.php | 51 ++++++++++++++++++-
 6 files changed, 133 insertions(+), 21 deletions(-)
 create mode 100644 library/ZendAfi/Form/Decorator/CodeMirror.php
 create mode 100644 library/ZendAfi/Form/Element/CodeMirror.php

diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index b5c87acd51b..b29306a8820 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -143,6 +143,7 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
        'activity' => $this->_getActivityVars(),
        'pellicule' => $this->_getPelliculeVars(),
        'rgpd' => (new Class_AdminVar_Cookies())->meta(),
+       'print' => $this->_getPrintModelVars()
       ];
   }
 
@@ -450,6 +451,16 @@ Pour vous désabonner de la lettre d\'information, merci de cliquer sur le lien
   }
 
 
+  protected function _getPrintModelVars() : array {
+    return [
+            'PRINT_MODELS_EDITOR' => Class_AdminVar_Meta::newCombo($this->_('Éditeur utilisé pour les modèles d\'impression'),
+                                        ['options' => ['selectOptions' => ['label' => $this->_('Éditeur'),
+                                                                           'multioptions' => ['CKEDITOR' => 'CKEditor',
+                                                                                              'CODE_MIRROR' => 'CodeMirror']]]])
+    ];
+  }
+
+
   protected function _getCommentVars() {
     return [
             'AVIS_MAX_SAISIE' => Class_AdminVar_Meta::newDefault($this->_('Nombre de caractères maximum autorisé à saisir dans les avis.')),
diff --git a/library/ZendAfi/Controller/Plugin/Manager/ModeleFusion.php b/library/ZendAfi/Controller/Plugin/Manager/ModeleFusion.php
index eda6588bd95..a041ee88115 100644
--- a/library/ZendAfi/Controller/Plugin/Manager/ModeleFusion.php
+++ b/library/ZendAfi/Controller/Plugin/Manager/ModeleFusion.php
@@ -38,24 +38,5 @@ class ZendAfi_Controller_Plugin_Manager_ModeleFusion extends ZendAfi_Controller_
     Class_ModeleFusion::generateDefaults($this->_view);
     $this->_redirectToReferer();
   }
-
-
-  protected function _postEditAction($model) {
-    Class_ScriptLoader::getInstance()
-      ->loadCodeMirror()
-      ->addAdminScript('codemirror-4.4/mode/css/css')
-      ->addAdminScript('codemirror-4.4/mode/xml/xml')
-      ->addAdminScript('codemirror-4.4/mode/htmlmixed/htmlmixed')
-      ->addInlineStyle('.CodeMirror-wrap { height: 500px }')
-      ->addJQueryReady("
-         CodeMirror.fromTextArea($('textarea[name=\'contenu\']')[0],
-              {
-                lineNumbers:true,
-                tabSize:2,
-                matchBrackets:true,
-                lineWrapping:true,
-                mode: 'htmlmixed'
-              });");
-  }
 }
 ?>
diff --git a/library/ZendAfi/Form/Decorator/CodeMirror.php b/library/ZendAfi/Form/Decorator/CodeMirror.php
new file mode 100644
index 00000000000..40db2cc2174
--- /dev/null
+++ b/library/ZendAfi/Form/Decorator/CodeMirror.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class ZendAfi_Form_Decorator_CodeMirror extends Zend_Form_Decorator_Abstract {
+  public function render($content) {
+    Class_ScriptLoader::getInstance()
+      ->loadCodeMirror()
+      ->addAdminScript('codemirror-4.4/mode/css/css')
+      ->addAdminScript('codemirror-4.4/mode/xml/xml')
+      ->addAdminScript('codemirror-4.4/mode/htmlmixed/htmlmixed')
+      ->addInlineStyle('.CodeMirror-wrap { height: 500px }')
+      ->addJQueryReady("
+         CodeMirror.fromTextArea($('textarea[name=\'" . $this->_element->getName() . "\']')[0],
+              {
+                lineNumbers:true,
+                tabSize:2,
+                matchBrackets:true,
+                lineWrapping:true,
+                mode: 'htmlmixed'
+              });");
+
+    return $content . $this->_element->getView()->tag('textarea',
+                                                      $this->_element->getValue(),
+                                                      ['name' => $this->_element->getName()]);
+  }
+}
diff --git a/library/ZendAfi/Form/Element/CodeMirror.php b/library/ZendAfi/Form/Element/CodeMirror.php
new file mode 100644
index 00000000000..88933377969
--- /dev/null
+++ b/library/ZendAfi/Form/Element/CodeMirror.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+class ZendAfi_Form_Element_CodeMirror extends ZendAfi_Form_Element_XhtmlOnViewHelper {
+  protected $_decorator_name = 'CodeMirror';
+}
diff --git a/library/ZendAfi/Form/ModeleFusion.php b/library/ZendAfi/Form/ModeleFusion.php
index 46ccb968bb6..c5be0c3ad1b 100644
--- a/library/ZendAfi/Form/ModeleFusion.php
+++ b/library/ZendAfi/Form/ModeleFusion.php
@@ -44,7 +44,9 @@ class ZendAfi_Form_ModeleFusion extends ZendAfi_Form {
                       'profil_ids',
                       ['label' => $form->_('Afficher pour les profils (separé par des ;)'),
                       ])
-         ->addElement('textarea',
+         ->addElement(Class_AdminVar::get('PRINT_MODELS_EDITOR') === 'CODE_MIRROR'
+                      ? 'codeMirror'
+                      : 'ckeditor',
                       'contenu',
                       [
                        'label' => '',
diff --git a/tests/application/modules/admin/controllers/PrintControllerTest.php b/tests/application/modules/admin/controllers/PrintControllerTest.php
index 6773ea9f4ab..8f0cd7e011f 100644
--- a/tests/application/modules/admin/controllers/PrintControllerTest.php
+++ b/tests/application/modules/admin/controllers/PrintControllerTest.php
@@ -48,6 +48,11 @@ class PrintControllerIndexTest extends Admin_AbstractControllerTestCase {
   public function generateLinkShouldBePresent() {
     $this->assertXPathContentContains('//button[contains(@onclick, "/admin/print/generate")]', 'Générer');
   }
+
+  /** @test */
+  public function pageShouldContainsLinkToAdminPrintConfigForAdminVars() {
+    $this->assertXPath('//a[contains(@href, "admin/print/config")]');
+  }
 }
 
 
@@ -170,7 +175,7 @@ class PrintControllerPostActionTest extends AbstractControllerTestCase {
 
 
 
-class PrintControllerAdminEditTest extends AbstractControllerTestCase {
+abstract class PrintControllerAdminEditTestCase extends AbstractControllerTestCase {
   public function setUp() {
     parent::setUp();
     $this->fixture(Class_ModeleFusion::class,
@@ -179,7 +184,16 @@ class PrintControllerAdminEditTest extends AbstractControllerTestCase {
                         'contenu' => '<p> {notices.each[<img src="{url_vignette}"/>  {titre_principal} <div>{article.contenu}</div>
 ]}</p>',
                         'type' => 'Notice_List']);
+  }
+}
 
+
+
+
+class PrintControllerAdminEditWithCodeMirrorTest extends PrintControllerAdminEditTestCase {
+  public function setUp() {
+    parent::setUp();
+    Class_AdminVar::set('PRINT_MODELS_EDITOR', 'CODE_MIRROR');
     $this->dispatch('/admin/print/edit/id/1');
   }
 
@@ -195,4 +209,39 @@ class PrintControllerAdminEditTest extends AbstractControllerTestCase {
   public function pageShouldLoadCodeMirror() {
     $this->assertXPathContentContains('//script', 'CodeMirror.fromTextArea($(\'textarea[name=\\\'contenu\\\'');
   }
+
+
+  /** @test */
+  public function pageShouldNotLoadCKEditor() {
+    $this->assertNotXPathContentContains('//script', 'CKEDITOR');
+  }
+}
+
+
+
+
+class PrintControllerAdminEditWithCKEditorTest extends PrintControllerAdminEditTestCase {
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/admin/print/edit/id/1');
+  }
+
+
+  /** @test */
+  public function textAreaShouldContainsContentNoticesEach() {
+    $this->assertXPathContentContains('//textarea[@name="contenu"]',
+                                      'notices.each[');
+  }
+
+
+  /** @test */
+  public function pageShouldNotLoadCodeMirror() {
+    $this->assertNotXPathContentContains('//script', 'CodeMirror');
+  }
+
+
+  /** @test */
+  public function pageShouldLoadCKEditor() {
+    $this->assertXPathContentContains('//script', 'CKEDITOR');
+  }
 }
-- 
GitLab