From ba0e4cd4a33adb10f5de3aed00af948010e1ee5c Mon Sep 17 00:00:00 2001
From: efalcy <efalcy@afi-sa.fr>
Date: Tue, 27 Oct 2015 18:01:17 +0100
Subject: [PATCH] dev #25728 : RT refacto RenderRss

---
 VERSIONS_WIP/25728                            |   2 +-
 .../views/scripts/accueil/sitotheque.phtml    |   6 +-
 .../Systeme/ModulesAccueil/Sitotheque.php     |   2 +-
 .../Controller/Action/Helper/RenderRss.php    | 144 ++++++++++++------
 library/ZendAfi/View/Helper/Accueil/Sito.php  |   2 +-
 .../controllers/AccueilControllerTest.php     |   2 +-
 .../ProfilOptionsControllerTest.php           |   2 +-
 .../opac/controllers/SitoControllerTest.php   |   6 +-
 8 files changed, 110 insertions(+), 56 deletions(-)

diff --git a/VERSIONS_WIP/25728 b/VERSIONS_WIP/25728
index e86c6a5eb07..76c10edf0cc 100644
--- a/VERSIONS_WIP/25728
+++ b/VERSIONS_WIP/25728
@@ -1 +1 @@
- - ticket #25728 : [Bonus] Ajouter la possibilité de flux rss à la boite sitothèque
\ No newline at end of file
+ - ticket #25728 : Ajouter la possibilité de flux rss à la boite sitothèque
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/accueil/sitotheque.phtml b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
index b88abfa23fd..184e770125d 100644
--- a/application/modules/admin/views/scripts/accueil/sitotheque.phtml
+++ b/application/modules/admin/views/scripts/accueil/sitotheque.phtml
@@ -42,10 +42,10 @@ formSelectToggleVisibilityForElement("input[name=display_order]", "#option_nb_af
           <tr>
             <td class="droite">Proposer un fil Rss&nbsp;</td>
             <td class="gauche">
-              <?php echo $this->formCheckbox('rss_avis',
-                                             $this->preferences['rss_avis'],
+              <?php echo $this->formCheckbox('rss',
+                                             $this->preferences['rss'],
                                              null,
-                                             array('1', '0')) ?>
+                                             ['1', '0']) ?>
             </td>
           </tr>
         </table>
diff --git a/library/Class/Systeme/ModulesAccueil/Sitotheque.php b/library/Class/Systeme/ModulesAccueil/Sitotheque.php
index 2269f87750d..26fb68344d5 100644
--- a/library/Class/Systeme/ModulesAccueil/Sitotheque.php
+++ b/library/Class/Systeme/ModulesAccueil/Sitotheque.php
@@ -44,7 +44,7 @@ class Class_Systeme_ModulesAccueil_Sitotheque extends Class_Systeme_ModulesAccue
                                'id_categorie' => '', // Liste d'id_categorie séparés par des tirets
                                'id_items' => '', // Liste d'id_site séparés par des tirets
                                'nb_aff' => '2', // Nombre à afficher
-                               'rss_avis' => '0',
+                               'rss' => '0',
                                'group_by_categorie' => false, //grouper les sites par categorie sous forme de menu,
                                'display_order' => 'Random' //ordre d'affichage: Random ou Selection
   ];
diff --git a/library/ZendAfi/Controller/Action/Helper/RenderRss.php b/library/ZendAfi/Controller/Action/Helper/RenderRss.php
index 9c2ff35c182..ac1676a9251 100644
--- a/library/ZendAfi/Controller/Action/Helper/RenderRss.php
+++ b/library/ZendAfi/Controller/Action/Helper/RenderRss.php
@@ -21,18 +21,30 @@
 
 
 class ZendAfi_Controller_Action_Helper_RenderRss extends Zend_Controller_Action_Helper_Abstract {
-  protected $_type, $_view;
 
-  public function renderRss($id_profil, $id_module, $view, $type) {
-    $this->_type = $type;
-    $this->_view = $view;
+  protected function _newStrategy($named) {
+    $class_name = 'ZendAfi_Controller_Action_Helper_RenderRss' . ucfirst($named);
+    return new $class_name();
+  }
+
+  public function direct($id_profil, $id_module, $view, $type) {
+    $this->_newStrategy($type)->render($id_profil,$id_module,$view,$this->getActionController());
+  }
+}
 
+
+abstract class ZendAfi_Controller_Action_Helper_RenderRssAbstract {
+  protected $action, $_view;
+
+  public function render($id_profil, $id_module, $view,$action) {
+    $this->_view = $view;
+    $this->action=$action;
     $datas = [];
     $data_rss =
       [
        'title'       => 'Flux indisponible',
-       'link'        => ($this->getActionController()->getRequest()->getScheme() . '://'
-                         . $this->getActionController()->getRequest()->getServer('HTTP_HOST')),
+       'link'        => ($this->action->getRequest()->getScheme() . '://'
+                         . $this->action->getRequest()->getServer('HTTP_HOST')),
        'charset'     => 'utf-8',
        'description' => '',
        'lastUpdate'  => time()
@@ -40,7 +52,7 @@ class ZendAfi_Controller_Action_Helper_RenderRss extends Zend_Controller_Action_
 
     $profil = Class_Profil::find($id_profil);
     if ($profil != null) {
-      $preferences  = $profil->getModuleAccueilPreferences($id_module, ($this->_type == 'SITO') ? 'SITO' : 'NEWS');
+      $preferences  = $profil->getModuleAccueilPreferences($id_module, $this->getType());
       $datas = $this->_getObjectDatas($id_module, $profil, $preferences);
       $data_rss = array_merge(
                               $data_rss,
@@ -54,6 +66,20 @@ class ZendAfi_Controller_Action_Helper_RenderRss extends Zend_Controller_Action_
     $this->_renderRssFeed($datas, $data_rss);
   }
 
+  protected function getDescriptionForElement($data) {
+    return  html_entity_decode(Class_CmsUrlTransformer::imgUrlRelativeToAbsolute($this->_view->tagArticleEvent($data)
+                                                                                 . $data->getFullContent()));
+  }
+
+  protected function getUrlLink($data) {
+    return $this->action->getRequest()->getScheme() . '://'
+      . $this->action->getRequest()->getServer('HTTP_HOST')
+      . $this->_view->url($data->getUrl());
+  }
+
+  protected function lastUpdate($data) {
+    return strtotime($data->getPubDate());
+  }
 
   protected function _renderRssFeed($datas, $rss_array) {
     $entries = [];
@@ -61,62 +87,90 @@ class ZendAfi_Controller_Action_Helper_RenderRss extends Zend_Controller_Action_
       $entries[] =
         [
          'title'       => html_entity_decode($data->getTitre()),
-         'link'        => (($this->_type == 'SITO')
-                           ? $data->getUrl()
-                           : $this->getActionController()->getRequest()->getScheme() . '://'
-                           . $this->getActionController()->getRequest()->getServer('HTTP_HOST')
-                           . $this->_view->url($data->getUrl())),
-         'lastUpdate'  => (($this->_type == 'SITO')
-                           ? strtotime($data->getDateMaj())
-                           : strtotime($data->getPubDate())),
-         'description' => (($this->_type == 'SITO')
-                           ? html_entity_decode($data->getDescription())
-                           : html_entity_decode(Class_CmsUrlTransformer::imgUrlRelativeToAbsolute($this->_view->tagArticleEvent($data)
-                                                                                                  . $data->getFullContent())))
+         'link'        => $this->getUrlLink($data),
+         'lastUpdate'  => $this->lastUpdate($data),
+
+         'description' => $this->getDescriptionForElement($data)
         ];
     }
     $rss_array['entries'] = $entries;
     $feed = Zend_Feed::importArray($rss_array, 'rss');
 
-    $this->getActionController()->getHelper('ViewRenderer')->setNoRender();
-    $this->getActionController()->getResponse()->setHeader('Content-Type', 'application/rss+xml;charset=utf-8') ;
-    $this->getActionController()->getResponse()->setBody($feed->saveXML());
+    $this->action->getHelper('ViewRenderer')->setNoRender();
+    $this->action->getResponse()->setHeader('Content-Type', 'application/rss+xml;charset=utf-8') ;
+    $this->action->getResponse()->setBody($feed->saveXML());
   }
 
+  abstract protected function _getObjectDatas($id_module, $profil, $preferences);
+
+  protected function _getDescription() {
+    return '';
+  }
+
+  protected function _getLink($profil, $id_module) {
+    return '';
+  }
+
+  protected function getType() {
+    return 'NEWS';
+  }
+}
+
+
+class  ZendAfi_Controller_Action_Helper_RenderRssSito extends ZendAfi_Controller_Action_Helper_RenderRssAbstract {
+
+  protected function getType() {
+    return 'SITO';
+  }
+
+  protected function getDescriptionForElement($data) {
+    return  html_entity_decode($data->getDescription());
+  }
+
+  protected function lastUpdate($data) {
+    return strtotime($data->getDateMaj());
+  }
+
+  protected function getUrlLink($data) {
+    return $data->getUrl();
+  }
 
   protected function _getObjectDatas($id_module, $profil, $preferences) {
-    if ($this->_type == 'SITO')
-      return Class_Sitotheque::getSitesFromIdsAndCategories(
-                                                            explode('-', $preferences['id_items']),
-                                                            explode('-', $preferences['id_categorie']));;
-    if ($this->_type == 'NEWS')
-      return Class_Calendar::getAllnextEvents($id_module, $profil);
-    if ($this->_type == 'ARTICLE') {
-      $articles = Class_Article::getArticlesByPreferences($preferences);
-      return Class_Article::filterByLocaleAndWorkflow($articles);
-    }
+    return Class_Sitotheque::getSitesFromIdsAndCategories(
+                                                          explode('-', $preferences['id_items']),
+                                                          explode('-', $preferences['id_categorie']));
   }
+}
 
 
-  protected function _getDescription() {
-    if ($this->_type == 'NEWS')
-      return 'Agenda: ';
-    if ($this->_type == 'ARTICLE')
-      return 'Articles: ';
-    return '';
+class  ZendAfi_Controller_Action_Helper_RenderRssNews extends ZendAfi_Controller_Action_Helper_RenderRssAbstract {
+
+  protected function _getObjectDatas($id_module, $profil, $preferences) {
+    return Class_Calendar::getAllnextEvents($id_module, $profil);
   }
 
+  protected function _getDescription() {
+    return 'Agenda: ';
+  }
 
   protected function _getLink($profil, $id_module) {
-    if ($this->_type == 'NEWS')
-      return $profil->urlForModule('cms', 'articleviewbydate', $id_module);
-    if ($this->_type == 'ARTICLE')
-      return $profil->urlForModule('cms', 'viewselection', $id_module);
-    return '';
+    return $profil->urlForModule('cms', 'articleviewbydate', $id_module);
+  }
+}
+
+
+class  ZendAfi_Controller_Action_Helper_RenderRssArticle extends ZendAfi_Controller_Action_Helper_RenderRssAbstract {
+
+  protected function _getObjectDatas($id_module, $profil, $preferences) {
+    $articles = Class_Article::getArticlesByPreferences($preferences);
+    return Class_Article::filterByLocaleAndWorkflow($articles);
   }
 
+  protected function _getDescription() {
+    return 'Articles: ';
+  }
 
-  public function direct($id_module, $id_profil, $view, $type) {
-    $this->renderRss($id_module, $id_profil, $view, $type);
+  protected function _getLink($profil, $id_module) {
+    return $profil->urlForModule('cms', 'viewselection', $id_module);
   }
 }
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/Accueil/Sito.php b/library/ZendAfi/View/Helper/Accueil/Sito.php
index bad7ef60ec7..e541de0328e 100644
--- a/library/ZendAfi/View/Helper/Accueil/Sito.php
+++ b/library/ZendAfi/View/Helper/Accueil/Sito.php
@@ -47,7 +47,7 @@ class ZendAfi_View_Helper_Accueil_Sito extends ZendAfi_View_Helper_Accueil_Base
     $contenu = '';
     $nb_aff = $this->getPreference('nb_aff');
 
-    if ($this->preferences['rss_avis'])
+    if ($this->preferences['rss'])
       $this->rss_interne = $this->_getRSSurl('sito', 'sito-rss');
 
     $box_title = $this->getPreference('titre') ?
diff --git a/tests/application/modules/admin/controllers/AccueilControllerTest.php b/tests/application/modules/admin/controllers/AccueilControllerTest.php
index ef773048f31..455316597f4 100644
--- a/tests/application/modules/admin/controllers/AccueilControllerTest.php
+++ b/tests/application/modules/admin/controllers/AccueilControllerTest.php
@@ -318,7 +318,7 @@ class AccueilControllerConfigSitothequeDefaultsTest extends Admin_AbstractContro
 
 /** @test */
   public function rssFlowPreferenceShouldBePresent() {
-    $this->assertXPath('//input[@type="checkbox"][@name="rss_avis"]', $this->_response->getBody());
+    $this->assertXPath('//input[@type="checkbox"][@name="rss"]', $this->_response->getBody());
   }
 }
 
diff --git a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
index 1c641ba10e4..02420da50b2 100644
--- a/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
+++ b/tests/application/modules/opac/controllers/ProfilOptionsControllerTest.php
@@ -1311,7 +1311,7 @@ class ProfilOptionsControllerPagesJeuxWithSitotheque extends ProfilOptionsContro
 
                                                    '10' => ['division' => '2',
                                                             'type_module' => 'SITO',
-                                                            'preferences' => ['rss_avis' => 1,
+                                                            'preferences' => ['rss' => 1,
                                                                               'id_categorie' => '2',
                                                                               'group_by_categorie' => true]]],
                                      'options' =>   []]);
diff --git a/tests/application/modules/opac/controllers/SitoControllerTest.php b/tests/application/modules/opac/controllers/SitoControllerTest.php
index f850867a9e7..f833fdb5ad2 100644
--- a/tests/application/modules/opac/controllers/SitoControllerTest.php
+++ b/tests/application/modules/opac/controllers/SitoControllerTest.php
@@ -106,7 +106,7 @@ abstract class SitoControllerViewCategoryTestCase extends SitoControllerTestCase
                                              'preferences' => ['id_categorie' => '12',
                                                                'type_aff' => '3',
                                                                'id_items' => '280-281',
-                                                               'rss_avis' => 1]]],
+                                                               'rss' => 1]]],
                        'options' => []]);
   }
 }
@@ -336,7 +336,7 @@ class SitoControllerRssWithProfileTest extends SitoControllerViewCategoryTestCas
                                              'preferences' => ['id_categorie' => '19',
                                                                'type_aff' => '3',
                                                                'id_items' => '281',
-                                                               'rss_avis' => 1]]],
+                                                               'rss' => 1]]],
                        'options' => []]);
 
     $this->dispatch('sito/sito-rss?id_module=1&id_profil=2');
@@ -344,7 +344,7 @@ class SitoControllerRssWithProfileTest extends SitoControllerViewCategoryTestCas
 
 /** @test */
   public function rssShouldOnlyContainsTwoSitos() {
-    $this->assertXPathCount('//item', 2);
+    $this->assertXPathCount('//item', 2,$this->_response->getBody());
   }
 
 
-- 
GitLab