From 85ca38d50adecde2d780b4dd1cf54fd092d7c4bb Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Mon, 30 Jan 2017 17:25:43 +0100
Subject: [PATCH] hotline #54764 cosmozendification codif_annexe

---
 .../cosmo/controllers/AnnexeController.php    |   3 +-
 .../cosmo/views/scripts/annexe/add.phtml      |   3 +
 .../cosmo/views/scripts/annexe/edit.phtml     |   3 +
 .../cosmo/views/scripts/annexe/index.phtml    |  21 ++++
 .../controllers/AnnexeControllerTest.php      | 107 +++++++++++++++++-
 library/Class/CodifAnnexe.php                 |   9 +-
 library/Class/IntBib.php                      |   9 ++
 .../Controller/Plugin/Manager/Annexe.php      |  35 ++++++
 .../Plugin/ResourceDefinition/Abstract.php    |   3 +-
 library/ZendAfi/Form/Admin/Annexe.php         |  61 ++++++++++
 10 files changed, 248 insertions(+), 6 deletions(-)
 create mode 100644 cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/add.phtml
 create mode 100644 cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/edit.phtml
 create mode 100644 cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/index.phtml
 create mode 100644 library/ZendAfi/Controller/Plugin/Manager/Annexe.php
 create mode 100644 library/ZendAfi/Form/Admin/Annexe.php

diff --git a/cosmogramme/cosmozend/application/modules/cosmo/controllers/AnnexeController.php b/cosmogramme/cosmozend/application/modules/cosmo/controllers/AnnexeController.php
index d518d731498..e94a2b87774 100644
--- a/cosmogramme/cosmozend/application/modules/cosmo/controllers/AnnexeController.php
+++ b/cosmogramme/cosmozend/application/modules/cosmo/controllers/AnnexeController.php
@@ -22,7 +22,8 @@
 
 class Cosmo_AnnexeController extends ZendAfi_Controller_Action {
   public function getPlugins() {
-    return ['ZendAfi_Controller_Plugin_ResourceDefinition_Annexe'];
+    return ['ZendAfi_Controller_Plugin_ResourceDefinition_Annexe',
+            'ZendAfi_Controller_Plugin_Manager_Annexe'];
   }
 }
 ?>
\ No newline at end of file
diff --git a/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/add.phtml b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/add.phtml
new file mode 100644
index 00000000000..fb242e29dad
--- /dev/null
+++ b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/add.phtml
@@ -0,0 +1,3 @@
+<?php
+echo $this->renderForm($this->form);
+?>
diff --git a/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/edit.phtml b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/edit.phtml
new file mode 100644
index 00000000000..fb242e29dad
--- /dev/null
+++ b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/edit.phtml
@@ -0,0 +1,3 @@
+<?php
+echo $this->renderForm($this->form);
+?>
diff --git a/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/index.phtml b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/index.phtml
new file mode 100644
index 00000000000..b762356521e
--- /dev/null
+++ b/cosmogramme/cosmozend/application/modules/cosmo/views/scripts/annexe/index.phtml
@@ -0,0 +1,21 @@
+<?php
+echo $this->tagAnchor(['action' => 'add',
+                       'id' => null],
+                      $this->_('Ajouter une annexe'));
+echo $this->tagModelTable($this->annexes,
+                          [$this->_('Bibliothèque'),
+                           $this->_('code'),
+                           $this->_('libellé'),
+                           $this->_('Rejeter les exemplaires'),
+                           $this->_('Exclu du PEB')],
+                          ['library_label',
+                           'code',
+                           'libelle',
+                           'invisible',
+                           'no_pickup'],
+                          [function($model)
+                           {
+                             return $this->renderPluginsActions($model);
+                           }],
+                          'sections');
+?>
diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/AnnexeControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/AnnexeControllerTest.php
index fee00b8c083..cebf7614fe1 100644
--- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/AnnexeControllerTest.php
+++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/AnnexeControllerTest.php
@@ -20,7 +20,7 @@
  */
 
 
-class AnnexeControllerTest extends CosmoControllerTestCase {
+abstract class AnnexeControllerTestCase extends CosmoControllerTestCase {
 
   public function setUp() {
     parent::setUp();
@@ -29,19 +29,120 @@ class AnnexeControllerTest extends CosmoControllerTestCase {
                    ['id' => 56,
                     'libelle' => 'Médiathèque Annecy']);
 
+    $this->fixture('Class_IntBib',
+                   ['id' => 56,
+                    'nom_court' => 'Médiathèque Annecy']);
+
+    $this->fixture('Class_IntBib',
+                   ['id' => 9999854,
+                    'nom_court' => 'Bibliothèque de Cran-Gevrier']);
+
     $this->fixture('Class_CodifAnnexe',
                    ['id' => 98,
                     'libelle' => 'Annexe Annecy',
                     'code' => 'ACY-01',
                     'id_bib' => 56]);
+  }
+}
 
-    $this->dispatch('/cosmo/annexe/index', true);
+
+
+
+class AnnexeControllerIndexTest extends AnnexeControllerTestCase {
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/cosmo/annexe', true);
   }
 
 
   /** @test */
   public function annexeAnnecyShouldBeInTable() {
-    $this->assertXPathContentContains('//div//td', 'Annexe Annecy');
+    $this->assertXPathContentContains('//table//td', 'Annexe Annecy');
+  }
+
+
+  /** @test */
+  public function annexeAnnecyCodeShouldBeInTable() {
+    $this->assertXPathContentContains('//table//td', 'ACY-01');
+  }
+
+
+  /** @test */
+  public function editAnnexeAnnecyLinkShouldBeInTable() {
+    $this->assertXPath('//table//td//a[contains(@href, "/cosmo/annexe/edit/id/98")]');
+  }
+
+
+  /** @test */
+  public function deleteAnnexeAnnecyLinkShouldBeInTable() {
+    $this->assertXPath('//table//td//a[contains(@href, "/cosmo/annexe/delete/id/98")]');
+  }
+
+
+  /** @test */
+  public function addAnnexeLinkShouldBePresent() {
+    $this->assertXPath('//a[contains(@href, "/cosmo/annexe/add")]');
   }
 }
+
+
+
+
+class AnnexeControllerAddActionTest extends AnnexeControllerTestCase {
+
+
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/cosmo/annexe/add', true);
+  }
+
+
+  /** @test */
+  public function shouldContainsSelectWithMediathequeAnnecy() {
+    $this->assertXPath('//form//select[@name="id_bib"]//option[2][@value="56"]');
+  }
+
+
+  /** @test */
+  public function shouldContainsCode() {
+    $this->assertXPath('//form//input[@type="text"][@name="code"]');
+  }
+
+
+  /** @test */
+  public function shouldContainsLibelle() {
+    $this->assertXPath('//form//input[@type="text"][@name="libelle"][@placeholder="** nouvelle annexe **"]');
+  }
+
+
+  /** @test */
+  public function checkboxRejectItemsShouldBePresent() {
+    $this->assertXPath('//form//input[@type="checkbox"][@name="invisible"]');
+  }
+
+
+  /** @test */
+  public function checkboxNoPickupShouldBePresent() {
+    $this->assertXPath('//form//input[@type="checkbox"][@name="no_pickup"]');
+  }
+}
+
+
+
+
+class AnnexeControllerEditActionTest extends AnnexeControllerTestCase {
+
+
+  public function setUp() {
+    parent::setUp();
+    $this->dispatch('/cosmo/annexe/edit/id/98', true);
+  }
+
+
+  /** @test */
+  public function shouldContainsSelectWithMediathequeAnnecy() {
+    $this->assertXPath('//form//select[@name="id_bib"]//option[2][@value="56"][@selected]');
+  }
+}
+
 ?>
\ No newline at end of file
diff --git a/library/Class/CodifAnnexe.php b/library/Class/CodifAnnexe.php
index 72bd14a53ed..fba38d0489d 100644
--- a/library/Class/CodifAnnexe.php
+++ b/library/Class/CodifAnnexe.php
@@ -28,7 +28,9 @@ class Class_CodifAnnexe extends Storm_Model_Abstract {
     $_table_name = 'codif_annexe',
     $_table_primary = 'id_annexe',
     $_default_attribute_values = ['libelle' => '',
-                                  'id_bib' => 0];
+                                  'id_bib' => 0,
+                                  'invisible' => 0,
+                                  'no_pickup' => 0];
 
 
   protected $_belongs_to = [
@@ -56,6 +58,11 @@ class Class_CodifAnnexe extends Storm_Model_Abstract {
   }
 
 
+  public function getLibraryLabel() {
+    return $this->getIntBib()->getLabel();
+  }
+
+
   public function isVisible() {
     return (1 != $this->getInvisible());
   }
diff --git a/library/Class/IntBib.php b/library/Class/IntBib.php
index c8891cfb7eb..941daaba7a7 100644
--- a/library/Class/IntBib.php
+++ b/library/Class/IntBib.php
@@ -20,6 +20,15 @@
  */
 
 class IntBibLoader extends Storm_Model_Loader {
+  public function findAllForCombo() {
+    $combo = [];
+    foreach(Class_IntBib::findAll() as $library)
+      $combo[$library->getId()] = $library->getLabel();
+    asort($combo, SORT_STRING | SORT_FLAG_CASE);
+    return $combo;
+  }
+
+
   public function findAllWithWebServices() {
     return Class_IntBib::findAllBy(['comm_sigb' => Class_IntBib::allCommSigbCodes()]);
   }
diff --git a/library/ZendAfi/Controller/Plugin/Manager/Annexe.php b/library/ZendAfi/Controller/Plugin/Manager/Annexe.php
new file mode 100644
index 00000000000..dfdc80eb7b4
--- /dev/null
+++ b/library/ZendAfi/Controller/Plugin/Manager/Annexe.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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_Controller_Plugin_Manager_Annexe extends ZendAfi_Controller_Plugin_Manager_Manager {
+  public function getActions($model) {
+    return [['url' => ['action' => 'edit',
+                       'id' => $model->getId()],
+             'icon' => 'edit',
+             'label' => $this->_('Modifier l\'annexe %s', $model->getLibelle())],
+            ['url' => ['action' => 'delete',
+                       'id' => $model->getId()],
+             'icon' => 'delete',
+             'label' => $this->_('Supprimer l\'annexe %s', $model->getLibelle())]];
+  }
+}
+?>
\ No newline at end of file
diff --git a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
index f6572732a08..d757272d377 100644
--- a/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
+++ b/library/ZendAfi/Controller/Plugin/ResourceDefinition/Abstract.php
@@ -32,6 +32,7 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen
   public function init() {
     parent::init();
     $this->_attribs = $this->getDefinitions();
+    $this->_controller->setResourceDefinition($this);
   }
 
 
@@ -344,7 +345,7 @@ abstract class ZendAfi_Controller_Plugin_ResourceDefinition_Abstract extends Zen
                             return $this->getModelActions();
                           })
       ;
-    $this->_controller->setResourceDefinition($this);
+
     return $this;
   }
 
diff --git a/library/ZendAfi/Form/Admin/Annexe.php b/library/ZendAfi/Form/Admin/Annexe.php
new file mode 100644
index 00000000000..3949d593954
--- /dev/null
+++ b/library/ZendAfi/Form/Admin/Annexe.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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_Admin_Annexe extends ZendAfi_Form {
+  public function init() {
+    parent::init();
+
+    $this
+      ->addElement('select',
+                   'id_bib',
+                   ['label' => $this->_('Bibliothèque'),
+                    'multiOptions' => Class_IntBib::findAllForCombo()])
+
+      ->addElement('text',
+                   'code',
+                   ['label' => $this->_('Code'),
+                    'required' => true,
+                    'allowEmpty' => false])
+
+      ->addElement('text',
+                   'libelle',
+                   ['label' => $this->_('Libellé'),
+                    'required' => true,
+                    'allowEmpty' => false,
+                    'placeholder' => $this->_('** nouvelle annexe **')])
+
+      ->addElement('checkbox',
+                   'invisible',
+                   ['label' => $this->_('Rejeter les exemplaires')])
+
+      ->addElement('checkbox',
+                   'no_pickup',
+                   ['label' => $this->_('Exclu du PEB')])
+
+
+      ->addUniqDisplayGroup('annexe',
+                            ['legend' => $this->_('Formulaire d\'annexe')]);
+  }
+
+
+}
+?>
\ No newline at end of file
-- 
GitLab