From eaa7d818d72ed13f69b3ab9efad86dcc20a3ddb1 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Thu, 27 Feb 2014 18:56:10 +0100
Subject: [PATCH] Fix #12295 : regression in record view configuration

---
 .../admin/controllers/ModulesController.php   | 370 +++++++-----------
 .../controllers/ModulesControllerTest.php     |  91 +++++
 2 files changed, 232 insertions(+), 229 deletions(-)

diff --git a/application/modules/admin/controllers/ModulesController.php b/application/modules/admin/controllers/ModulesController.php
index 7c78419060b..78f4cd28889 100644
--- a/application/modules/admin/controllers/ModulesController.php
+++ b/application/modules/admin/controllers/ModulesController.php
@@ -18,10 +18,7 @@
  * along with AFI-OPAC 2.0; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
-//////////////////////////////////////////////////////////////////////////////////////////
-// OPAC3 - Propriétés des modules gérés par les controllers
-//
-//////////////////////////////////////////////////////////////////////////////////////////
+
 class Admin_ModulesController extends ZendAfi_Controller_Action {
 
 	protected 
@@ -33,14 +30,13 @@ class Admin_ModulesController extends ZendAfi_Controller_Action {
 
 	public function init() {
 		parent::init();
-
-		if ($this->_request->getActionName()!='kiosque-change-data') {
+		if ($this->_request->getActionName() != 'kiosque-change-data') {
 			$this->render = $this->getHelper('ViewRenderer');
 			$this->render->setLayoutScript('subModal.phtml');
 		}
 
 		$this->initParams();
-		if(array_key_exists('type_module',$this->_request->getParams()))
+		if (array_key_exists('type_module', $this->_request->getParams()))
 			$this->initViewParams();
 			
 	}
@@ -48,24 +44,25 @@ class Admin_ModulesController extends ZendAfi_Controller_Action {
 	
 	public function preDispatch() {
 		parent::preDispatch();
-		if($this->_request->getActionName()!='kiosque-change-data' &&	
-			 $this->_request->getActionName()!='search-result')
+		if ($this->_request->getActionName()!='kiosque-change-data' &&	
+				$this->_request->getActionName()!='search-result')
 			Zend_Layout::startMvc();
 	}
 	
 
 	protected function initParams() {
 		$this->profil = Class_Profil::getCurrentProfil();
-		$this->preferences = $this->profil->getCfgModulesPreferences($this->_getParam('type_module'),
-																													 $this->_getParam('action1'),
-																													 $this->_getParam('action2'));
+		$this->preferences = $this->profil
+			->getCfgModulesPreferences($this->_getParam('type_module'),
+																 $this->_getParam('action1'),
+																 $this->_getParam('action2'));
 	}
 
 
 	protected function initViewParams() {
-		$cls_module=new Class_Systeme_ModulesAppli();
-		$this->view->titre_module=$cls_module->getLibelleModule($this->_getParam('type_module'),
-																														$this->_getParam('action1'));
+		$cls_module = new Class_Systeme_ModulesAppli();
+		$this->view->titre_module = $cls_module->getLibelleModule($this->_getParam('type_module'),
+																															$this->_getParam('action1'));
 
 		$this->view->preferences = $this->preferences;
 		$this->view->url=$this->_request->getRequestUri();
@@ -78,169 +75,84 @@ class Admin_ModulesController extends ZendAfi_Controller_Action {
 	}
 
 	
-	function indexAction() {
-		if ($this->_request->isPost()) {
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/index_all.phtml');
+	protected function _simpleAction($script) {
+		if ($this->_request->isPost())
+			return $this->updateEtRetour($this->_request->getPost());
+		$this->render->renderScript('modules/' . $script . '.phtml');
 	}
+	
 
+	public function indexAction() {
+		return $this->_simpleAction('index_all');
+	}
 
-	function abonneAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-	
-		$this->render->renderScript('modules/abonne_all.phtml');
+
+	public function abonneAction() {
+		return $this->_simpleAction('abonne_all');
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés auth (login)
-//------------------------------------------------------------------------------------------------------
-	function authAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/auth_'.$this->_getParam('action1').'.phtml');
+
+	public function authAction() {
+		return $this->_simpleAction('auth_'.$this->_getParam('action1'));
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés cms
-//------------------------------------------------------------------------------------------------------
-	function cmsAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		if ($this->_getParam('action1') == "articleviewbydate")
-			$this->render->renderScript('modules/cms_'.$this->_getParam('action1').'.phtml');
-		else
-			$this->render->renderScript('modules/cms_all.phtml');
+
+	public function cmsAction() {
+		return $this->_simpleAction(($this->_getParam('action1') == 'articleviewbydate') 
+																? 'cms_articleviewbydate' : 'cms_all');
 	}
 
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés rss
-//------------------------------------------------------------------------------------------------------
-	function rssAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/rss_all.phtml');
+	public function rssAction() {
+		return $this->_simpleAction('rss_all');
 	}
 
 
-	function bibAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/bib_all.phtml');
+	public function bibAction() {
+		return $this->_simpleAction('bib_all');
 	}
 
-	//------------------------------------------------------------------------------------------------------
-	// Proprietés rss
-	//------------------------------------------------------------------------------------------------------
-	function sitoAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/sito_all.phtml');
+
+	public function sitoAction() {
+		return $this->_simpleAction('sito_all');
 	}
 
-	//------------------------------------------------------------------------------------------------------
-	// Proprietés blog (avis et critiques)
-	//------------------------------------------------------------------------------------------------------
-	function blogAction()
-	{
-		if ($this->_request->isPost())
-		{
-			$enreg=$_POST;
-			$this->updateEtRetour($enreg);
-		}
-		// Redirection sur le bon formulaire
-		$this->render->renderScript('modules/blog_all.phtml');
+
+	public function blogAction() {
+		return $this->_simpleAction('blog_all');
 	}
 
-	//------------------------------------------------------------------------------------------------------
-	// Catalogues
-	//------------------------------------------------------------------------------------------------------
-	function catalogueAction()
-	{
-		$this->view->titre="Domaines";
-		if ($this->_request->isPost())
-		{
-			$ret=$this->getPostListe();
-			if($ret != "ok") $this->retourErreur($ret);
-			else
-			{
-				$enreg=$_POST;
-				$this->updateEtRetour($enreg);
-			}
+
+	public function catalogueAction() {
+		$this->view->titre = 'Domaines';
+		if ($this->_request->isPost()) {
+			$ret = $this->getPostListe();
+			if ('ok' == $ret)
+				return $this->updateEtRetour($this->_request->getPost());
+			$this->retourErreur($ret);
 		}
-		// Redirection sur le bon formulaire
+
 		$this->render->renderScript('modules/recherche_resultat.phtml');
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés recherche
-//------------------------------------------------------------------------------------------------------
-	function rechercheAction() {
-		
-		$erreur='';
-		$enreg='';
-		if($this->_getParam('action1')=="resultat")	{
+
+	public function rechercheAction() {
+		if ('resultat' == $this->_getParam('action1'))
 			return $this->_forward('search-result');
-		}
-		
-		if($this->_getParam('action1')==='viewnotice')
-			$this->view->type_doc=$this->_getParam('action2');
-		$this->view->titre="Recherche";
-		
-		// Retour du formulaire
-		if($this->_request->isPost())
-		{
-			if($this->_getParam('action1')=="viewnotice") 
-				$enreg=$this->getPostNotice();
-			
-			if($erreur) 
-				$this->retourErreur($erreur);
-			else{
-				if(!$enreg) 
-					$enreg=$_POST;
-				$this->updateEtRetour($enreg);
-			}
-		}
 
-		// viewnotice -> Consolidation des onglets (si de nouveaux ont ete ajoutes)
-		if ($this->_getParam('action1')=="viewnotice") 
+		if ($this->_request->isPost())
+			return $this->updateEtRetour((('viewnotice' == $this->_getParam('action1')) 
+																		? $this->getPostNotice() 
+																		: $this->_request->getPost()));
+
+		$this->view->titre = 'Recherche';
+		
+		if ('viewnotice' == $this->_getParam('action1')) {
+			$this->view->type_doc = $this->_getParam('action2');
 			$this->view->preferences = $this->noticeConsolidationOnglets($this->preferences);
+		}
 
-		// Redirection sur le bon formulaire
-	  $this->render->renderScript('modules/recherche_'.$this->_getParam('action1').'.phtml');
+	  $this->render->renderScript('modules/recherche_' . $this->_getParam('action1') . '.phtml');
 	}
 
 
@@ -249,71 +161,68 @@ class Admin_ModulesController extends ZendAfi_Controller_Action {
  		
 		$form = 
 			ZendAfi_Form_Configuration_SearchResult::newConfigurationWith(
-				array_merge(Class_Profil::getCurrentProfil()->getConfigurationOf('recherche',
-																																				 'resultat',
-																																				 'simple'),
-										$post_values
-										));
-		if ($this->_request->isPost() && $form->isValid($post_values)) {
+				array_merge(Class_Profil::getCurrentProfil()
+										->getConfigurationOf('recherche', 'resultat', 'simple'),
+										$post_values));
+
+		if ($this->_request->isPost() && $form->isValid($post_values))
 			return $this->updateEtRetour($post_values);
-		}
 
-		$this->view->form= $form;
-		
+		$this->view->form = $form;
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Controle de saisie pour la liste résultat de recherches et catalogues
-//------------------------------------------------------------------------------------------------------
-	protected function getPostListe()
-	{
-		$erreur = 'ok';
+
+	protected function getPostListe() {		
+		$liste_nb_par_page = (int)$liste_nb_par_page;
+		$facettes_nombre = (int)$facettes_nombre;
+		$tags_nombre = (int)$tags_nombre;
+
+		if ($liste_nb_par_page < 3 or $liste_nb_par_page > 50) 
+			return "Le nombre de notices par page doit être compris entre 3 et 50";
+		
+		if(!trim($liste_codes)) 
+			return "Indiquez au moins 1 champ à afficher pour la liste";
 		
-		$liste_nb_par_page=(int)$liste_nb_par_page;
-		$facettes_nombre=(int)$facettes_nombre;
-		$tags_nombre=(int)$tags_nombre;
-
-		if($liste_nb_par_page < 3 or $liste_nb_par_page > 50) $erreur="Le nombre de notices par page doit être compris entre 3 et 50";
-		elseif(!trim($liste_codes)) $erreur="Indiquez au moins 1 champ à afficher pour la liste";
-		elseif($facettes_actif == 1 and $facettes_nombre < 2 or $facettes_nombre > 10) $erreur="Le nombre de facettes doit être compris entre 2 et 10";
-		elseif($facettes_actif == 1 and !trim($facettes_codes)) $erreur="Indiquez au moins 1 facette à afficher";
-		elseif($tags_actif == 1 and $tags_nombre < 5 or $tags_nombre > 1000) $erreur="Le nombre de tags doit être compris entre 5 et 1000";
-		elseif($tags_actif == 1 and !trim($tags_codes)) $erreur="Indiquez au moins 1 type de tag à afficher";
-
-	  return $erreur;
+		if($facettes_actif == 1 and $facettes_nombre < 2 or $facettes_nombre > 10) 
+			return "Le nombre de facettes doit être compris entre 2 et 10";
+		
+		if($facettes_actif == 1 and !trim($facettes_codes)) 
+			return "Indiquez au moins 1 facette à afficher";
+		
+		if($tags_actif == 1 and $tags_nombre < 5 or $tags_nombre > 1000) 
+			return "Le nombre de tags doit être compris entre 5 et 1000";
+		
+		if($tags_actif == 1 and !trim($tags_codes)) 
+			return "Indiquez au moins 1 type de tag à afficher";
+
+	  return 'ok';
 	}
 
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés notice ajax
-//------------------------------------------------------------------------------------------------------
-	function noticeajaxAction()	{
-		$this->view->type_doc=$this->action2;
+	public function noticeajaxAction() {
+		$this->view->type_doc = $this->action2;
 		$this->noticeConsolidationOnglets();
-		// Retour du formulaire
-		if($this->_request->isPost()) {
+
+		if ($this->_request->isPost()) {
 			$enreg=$this->getPostNotice();
-			$this->updateEtRetour($enreg);
+			return $this->updateEtRetour($enreg);
 		}
-		// On utilise le meme formulaire que pour recherche/viewnotice
+
 		$this->render->renderScript('modules/recherche_viewnotice.phtml');
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés des notices
-//------------------------------------------------------------------------------------------------------
-	protected function noticeConsolidationOnglets($preferences)
-	{
+
+	protected function noticeConsolidationOnglets($preferences) {
 		if (!array_key_exists("onglets", $preferences))
 			$preferences["onglets"] = array();
 
-		$onglets=Class_Codification::getInstance()->getNomOnglet("");
+		$onglets = Class_Codification::getInstance()->getNomOnglet('');
 		foreach($onglets as $key => $valeur) {
 			if(!array_key_exists($key, $preferences["onglets"])) {
-				$preferences["onglets"][$key]["titre"]="";
-				$preferences["onglets"][$key]["aff"]=0;
-				$preferences["onglets"][$key]["ordre"]=100;
-				$preferences["onglets"][$key]["largeur"]=0;
+				$preferences["onglets"][$key]["titre"] = "";
+				$preferences["onglets"][$key]["aff"] = 0;
+				$preferences["onglets"][$key]["ordre"] = 100;
+				$preferences["onglets"][$key]["largeur"] = 0;
 			} 
 		}
 
@@ -325,55 +234,58 @@ class Admin_ModulesController extends ZendAfi_Controller_Action {
 		return $preferences;
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Proprietés des notices
-//------------------------------------------------------------------------------------------------------
-	protected function getPostNotice()
-	{
-		// Récup et controle des valeurs du formulaire
-		foreach($_POST as $clef => $valeur)
-		{
-			$pos=strPos($clef,"_");
-			if($pos === false)  {
+
+	protected function getPostNotice() {
+		foreach($this->_request->getPost() as $clef => $valeur) {
+			if (false === ($pos = strpos($clef, '_'))) {
 				$enreg[$clef] = $valeur;
 				continue;
 			}
-			$type=substr($clef,0,$pos);
-			$champ=substr($clef,($pos+1));
-			if($champ == "ordre") {$valeur=(int)$valeur; if($valeur < 1 or $valeur > 100) $valeur="1";}
-			if($champ == "largeur") {$valeur=(int)$valeur; if($valeur < 5 or $valeur > 50) $valeur="0";}
-			if($clef == "liste_codes") $enreg["entete"]=$valeur;
-			else $enreg["onglets"][$type][$champ]=$valeur;
+
+			if (in_array($clef, ['liste_codes', 'champs_codes'])) {
+				$enreg['entete'] = $valeur;
+				continue;
+			}
+
+			$type = substr($clef, 0, $pos);
+			$champ = substr($clef, $pos + 1);
+
+			if ($champ == 'ordre') {
+				$valeur = (int)$valeur; 
+				if($valeur < 1 or $valeur > 100) 
+					$valeur = '1';
+			}
+
+			if ($champ == 'largeur') {
+				$valeur = (int)$valeur; 
+				if($valeur < 5 or $valeur > 50) 
+					$valeur = '0';
+			}
+
+			$enreg['onglets'][$type][$champ] = $valeur;
 		}
+
 		return $enreg;
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Validation et retour config admin de la page d'accueil
-//------------------------------------------------------------------------------------------------------
-	protected function updateEtRetour($enreg)
-	{
+
+	protected function updateEtRetour($enreg) {
 		$cfg_modules = $this->profil->getCfgModulesAsArray();
 		$cfg_modules
 			[$this->_getParam('type_module')]
-			[$this->_getParam('action1').$this->_getParam('action2')] = $enreg;
+			[$this->_getParam('action1') . $this->_getParam('action2')] = $enreg;
 
 		$this->profil
 			->setCfgModules($cfg_modules)
 			->save();
-		$this->view->reload="SITE";
-
-		// Execute le script de retour
+		$this->view->reload = 'SITE';
 		$this->render->renderScript('modules/_retour.phtml');
 	}
 
-//------------------------------------------------------------------------------------------------------
-// Retour au formulaire pour erreurs
-//------------------------------------------------------------------------------------------------------
-	protected function retourErreur($erreur)
-	{
-		$this->view->erreur=$erreur;
-		$this->view->preferences=$_POST;
+
+	protected function retourErreur($erreur) {
+		$this->view->erreur = $erreur;
+		$this->view->preferences = $this->_request->getPost();
 	}
 
 	
diff --git a/tests/application/modules/admin/controllers/ModulesControllerTest.php b/tests/application/modules/admin/controllers/ModulesControllerTest.php
index e183276f84b..58e94fc6e68 100644
--- a/tests/application/modules/admin/controllers/ModulesControllerTest.php
+++ b/tests/application/modules/admin/controllers/ModulesControllerTest.php
@@ -56,6 +56,97 @@ class ModulesControllerRechercheTest extends Admin_AbstractControllerTestCase {
 
 
 
+class ModulesControllerRecherchePostTest extends Admin_AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->postDispatch('/admin/modules/recherche?config=site&type_module=recherche&id_profil=2&action1=viewnotice&action2=1',
+												['boite' => 'testing_box_of_the_doom', 
+												 'champs_codes' => 'ECNA',
+												 'detail_aff' => '1',
+												 'detail_ordre' => '1',
+												 'detail_titre' => 'Notice détaillée',
+												 'detail_largeur' => '0',
+												 'avis_aff' => '1',
+												 'avis_ordre' => '3',
+												 'avis_titre' => 'Avis',
+												 'avis_largeur' => '0',
+												 'exemplaires_aff' => '1',
+												 'exemplaires_ordre' => '2',
+												 'exemplaires_titre' => 'Exemplaires',
+												 'exemplaires_largeur' => '0',
+												 'resume_aff' => '0',
+												 'resume_ordre' => '1',
+												 'resume_titre' => 'Résumés, analyses',
+												 'resume_largeur' => '0',
+												 'tags_aff' => '3',
+												 'tags_ordre' => '3',
+												 'tags_titre' => 'Tags',
+												 'tags_largeur' => '0',
+												 'biographie_aff' => '3',
+												 'biographie_ordre' => '2',
+												 'biographie_titre' => 'L\'auteur',
+												 'biographie_largeur' => '0',
+												 'similaires_aff' => '3',
+												 'similaires_ordre' => '1',
+												 'similaires_titre' => 'Notices similaires',
+												 'similaires_largeur' => '0',
+												 'bibliographie_aff' => '0',
+												 'bibliographie_ordre' => '7',
+												 'bibliographie_titre' => 'Bibliographies',
+												 'bibliographie_largeur' => '0',
+												 'morceaux_aff' => '0',
+												 'morceaux_ordre' => '9',
+												 'morceaux_titre' => 'Morceaux',
+												 'morceaux_largeur' => '0',
+												 'bandeAnnonce_aff' => '0',
+												 'bandeAnnonce_ordre' => '10',
+												 'bandeAnnonce_titre' => 'Bande-annonce',
+												 'bandeAnnonce_largeur' => '0',
+												 'photos_aff' => '0',
+												 'photos_ordre' => '11',
+												 'photos_titre' => 'Photos',
+												 'photos_largeur' => '0',
+												 'videos_aff' => '3',
+												 'videos_ordre' => '12',
+												 'videos_titre' => 'Archives INA',
+												 'videos_largeur' => '0',
+												 'resnumeriques_aff' => '0',
+												 'resnumeriques_ordre' => '100',
+												 'resnumeriques_titre' => 'Ressources numériques',
+												 'resnumeriques_largeur' => '0',
+												 'series_aff' => '0',
+												 'series_ordre' => '100',
+												 'series_titre' => 'Documents de la même série',
+												 'series_largeur' => '0',
+												 'babeltheque_aff' => '0',
+												 'babeltheque_ordre' => '100',
+												 'babeltheque_titre' => 'Babelthèque',
+												 'babeltheque_largeur' => '0',
+												 'frbr_aff' => '0',
+												 'frbr_ordre' => '100',
+												 'frbr_titre' => 'Notices liées',
+												 'frbr_largeur' => '0']);
+
+		$this->viewnotice_pref = Class_Profil::getCurrentProfil()
+			->getCfgModulesAsArray()['recherche']['viewnotice1'];
+	}
+
+
+	/** @test */
+	public function boiteShouldBeTestingBox() {
+		$this->assertEquals('testing_box_of_the_doom', $this->viewnotice_pref['boite']);
+	}
+
+
+	/** @test */
+	public function enteteShouldBeECNA() {
+		$this->assertEquals('ECNA', $this->viewnotice_pref['entete']);
+	}
+}
+
+
+
+
 class ModulesControllerVariousConfigTest extends Admin_AbstractControllerTestCase {
 	public function setUp() {
 		parent::setUp();
-- 
GitLab