From 312255aa7fa95d1bb2a21c335c8eaa40be33d605 Mon Sep 17 00:00:00 2001 From: llaffont <llaffont@git-test.afi-sa.fr> Date: Wed, 13 Feb 2013 14:26:37 +0000 Subject: [PATCH] Refacto export panier. --- .../opac/controllers/PanierController.php | 46 ++++++-- .../opac/views/scripts/panier/index.phtml | 8 +- library/Class/Notice.php | 23 +++- library/Class/PanierNotice.php | 71 +++--------- .../opac/controllers/PanierControllerTest.php | 108 +++++++++++++++++- 5 files changed, 182 insertions(+), 74 deletions(-) diff --git a/application/modules/opac/controllers/PanierController.php b/application/modules/opac/controllers/PanierController.php index eb4ea820a6c..b2e142f642b 100644 --- a/application/modules/opac/controllers/PanierController.php +++ b/application/modules/opac/controllers/PanierController.php @@ -151,16 +151,44 @@ function indexAction() { // Mise a jour libelle panier //------------------------------------------------------------------------------------------------------ function majtitrepanierAction() { - $id_panier=$_SESSION["panier"]["panier_courant"]; - $this->panier->majTitre($this->_user->ID_USER,$id_panier, $_POST["new_libelle"]); - $this->_redirect('opac/panier?id_panier='.$id_panier); + $panier = Class_PanierNotice::find($this->_getParam('id_panier')); + + $old_libelle = $panier->getLibelle(); + + $panier + ->setLibelle($this->_request->getPost('new_libelle')) + ->save(); + + $this->_helper->notify($this->_('Panier "%s" renommé vers "%s"', + $old_libelle, + $panier->getLibelle())); + $this->_redirect('opac/panier/index/id_panier/'.$panier->getId()); } -//------------------------------------------------------------------------------------------------------ -// Export du panier aux 2 formats -//------------------------------------------------------------------------------------------------------ - function exportAction() { - $id_panier=$_SESSION["panier"]["panier_courant"]; - $this->view->panier=$this->panier->exportPaniers($this->_user->ID_USER,$id_panier); + + public function exportUnimarcAction() { + $panier = Class_PanierNotice::find($this->_getParam('id_panier')); + $this->exportData($panier, $panier->toUnimarcISO2709()); + } + + + public function exportListeAction() { + $panier = Class_PanierNotice::find($this->_getParam('id_panier')); + $this->exportData($panier, $panier->toTabbedList()); + } + + + public function exportData($panier, $data) { + $this->getHelper('ViewRenderer')->setNoRender(); + $response = $this->_response; + $response->clearAllHeaders(); + $response->setHeader('Content-Type', 'text/plain; name="' . $panier->getId(). '.txt"', true); + $response->setHeader('Content-Disposition', 'attachment; filename="' . $panier->getId(). '.txt"', true); + $response->setHeader('Content-Transfer-Encoding', 'base64', true); + $response->setHeader('Expires', '0'); + $response->setHeader('Cache-Control', 'no-cache, must-revalidate'); + $response->setHeader('Pragma', 'no-cache'); + $response->setHeader('Access-Control-Allow-Origin', '*'); + $response->setBody($data); } } \ No newline at end of file diff --git a/application/modules/opac/views/scripts/panier/index.phtml b/application/modules/opac/views/scripts/panier/index.phtml index 201c7affade..2c4d3ffcd6a 100644 --- a/application/modules/opac/views/scripts/panier/index.phtml +++ b/application/modules/opac/views/scripts/panier/index.phtml @@ -60,8 +60,12 @@ if($this->panier_courant) { print('<form method="post" action="'.$this->url(['action' => 'majtitrepanier', 'id_panier' => $this->panier_courant->getId()]).'">'.$this->_('Nouveau titre').'<input type="text" size="40" maxlength="40" id="new_libelle" name="new_libelle" value="'.htmlspecialchars($this->panier_courant->getLibelle()).'">'); print(' <input name="button" type="submit" class="submit" value=""/></form><br></div>'); print('<a href="#" onclick="document.getElementById(\'maj_titre\').style.display=\'block\';">» '.$this->_('Modifier le titre du panier').'</a>'); - if($nombre_notices > 0) - print('<a href="'.$this->url(['action' => 'export', 'id_panier' => $this->panier_courant->getId()]).'"> » '.$this->_('Exporter ce panier').'</a>'); + if($nombre_notices > 0) { + echo ' » '.$this->_('Exporter ce panier: '); + echo '<a href="'.$this->url(['action' => 'export-unimarc', 'id_panier' => $this->panier_courant->getId()]).'">Unimarc</a>'; + echo ' - '; + echo '<a href="'.$this->url(['action' => 'export-liste', 'id_panier' => $this->panier_courant->getId()]).'">Liste</a>'; + } // Contenu panier if($nombre_notices > 0) { diff --git a/library/Class/Notice.php b/library/Class/Notice.php index 28e9e4e940d..a82eb49c5b5 100644 --- a/library/Class/Notice.php +++ b/library/Class/Notice.php @@ -252,10 +252,9 @@ class Class_Notice extends Storm_Model_Abstract { $this->_avis = $list_avis; } - public function setUnimarc($unimarc) - { + public function setUnimarc($unimarc) { $this->getNoticeUnimarc()->setNotice($unimarc, 0); - parent::setUnimarc($unimarc); + return parent::setUnimarc($unimarc); } @@ -1577,6 +1576,24 @@ class Class_Notice extends Storm_Model_Abstract { public function getNoticeUnimarc() { return $this->_notice_unimarc; } + + + + /** + * @return String + */ + public function toTabbedList() { + $sep=chr(9); + + $enreg = Class_Codification::getLibelleFacette("T".$this->getTypeDoc()).$sep; + $enreg .= $this->getTitrePrincipal().$sep; + $enreg .= $this->getAuteurPrincipal().$sep; + $enreg .= $this->getEditeur().$sep; + $enreg .= implode(',', $this->getCollection()).$sep; + $enreg .= $this->getAnnee().chr(10); + + return $enreg; + } } ?> \ No newline at end of file diff --git a/library/Class/PanierNotice.php b/library/Class/PanierNotice.php index 822f35e4d15..9d7cb526555 100644 --- a/library/Class/PanierNotice.php +++ b/library/Class/PanierNotice.php @@ -144,67 +144,24 @@ class Class_PanierNotice extends Storm_Model_Abstract { } - //------------------------------------------------------------------------------------------------------ - // MAJ libellé d'1 panier - //------------------------------------------------------------------------------------------------------ - public function majTitre($id_user,$id_panier,$titre) { - $sql = Zend_Registry::get('sql'); - $titre=substr($sql->quote($titre),0,50); - $req="update notices_paniers set LIBELLE=$titre where ID_PANIER=$id_panier and ID_USER=$id_user"; - $sql->execute($req); + public function toUnimarcISO2709() { + $data = ''; + $notices = $this->getNoticesAsArray(); + foreach($notices as $notice) + $data .= $notice->toUnimarcISO2709(); + return $data; } - //------------------------------------------------------------------------------------------------------ - // Export paniers - //------------------------------------------------------------------------------------------------------ - public function exportPaniers($id_user,$id_panier) - { - // Controle de la presence du repertoire - $sep=chr(9); - $nom_fic=$id_user.$id_panier.".txt"; - $ret["fic_unimarc"]=BASE_URL."/temp/panierunimarc".$nom_fic; - $ret["fic_liste"]=BASE_URL."/temp/panier".$nom_fic; - $rep=getcwd()."/temp/"; - - if(file_exists($rep) == false) - { - $ret["erreur"]="Impossible d'acceder au dossier : ".$rep; - return $ret; - } - $fic_unimarc=fopen($rep."panierunimarc".$nom_fic,"wb"); - $fic_liste=fopen($rep."panier".$nom_fic,"wb"); - // Get des notices - $unimarc = new Class_NoticeUnimarc(); - $enreg=fetchEnreg("select * from notices_paniers where ID_USER = $id_user and ID_PANIER=$id_panier"); - if(!$enreg["NOTICES"]) { - $ret["erreur"]="Ce panier est vide"; - return $ret; - } - $ret["libelle"]=$enreg["LIBELLE"]; - $items=explode(";",$enreg["NOTICES"]); - - foreach($items as $clef_alpha) { - if(!$clef_alpha) continue; - $nombre++; - - if (!$notice = Class_Notice::getNoticeByClefAlpha($clef_alpha)) continue; - - fwrite($fic_unimarc, $notice->toUnimarcISO2709()); - $enreg=Class_Codification::getLibelleFacette("T".$notice->getTypeDoc()).$sep; - $enreg.=$notice->getTitrePrincipal().$sep; - $enreg.=$notice->getAuteurPrincipal().$sep; - $enreg.=$notice->getEditeur().$sep; - $enreg.=$notice->getCollection().$sep; - $enreg.=$notice->getAnnee().chr(10); - fwrite($fic_liste,utf8_decode($enreg)); - } - $ret["nombre"]=$nombre; - fclose($fic_unimarc); - fclose($fic_liste); - return $ret; - } + public function toTabbedList() { + $data = ''; + $notices = $this->getNoticesAsArray(); + foreach($notices as $notice) + $data .= $notice->toTabbedList(); + return $data; + } + //------------------------------------------------------------------------------- // liste des paniers pour une combo diff --git a/tests/application/modules/opac/controllers/PanierControllerTest.php b/tests/application/modules/opac/controllers/PanierControllerTest.php index d82037376c6..d9869c7c839 100644 --- a/tests/application/modules/opac/controllers/PanierControllerTest.php +++ b/tests/application/modules/opac/controllers/PanierControllerTest.php @@ -58,7 +58,8 @@ abstract class PanierControllerTestCase extends AbstractControllerTestCase { $montespan = Class_Notice::newInstanceWithId(4) ->setTitrePrincipal('Le Montespan') - ->setAuteurPrincipal('Jean Teulé'); + ->setAuteurPrincipal('Jean Teulé') + ->setUnimarc('00445nam0 2200181 450 0010008000000100028000081000041000361010008000771020007000851050018000922000032001102100026001422150038001687000022002068010022002289020007002509030006002570507356 a978-2-260-01723-3d22 ? a20080612d|||| uuuy0frey0103 ba| afre aFR ay|||||||000|y1 aMontespan (Le)fJean Teulé aPariscJulliardd2008 a333 p.cjaq. ill. en coul.d21 cm 1aTeulébJean4070 0aFRbBMVc20080612 arh aG'); $combat_ordinaire = Class_Notice::newInstanceWithId(10) ->setTitrePrincipal('Le combat ordinaire') @@ -164,10 +165,21 @@ class PanierControllerIndexWithPanierIdFifteenTest extends PanierControllerTestC } + /** @test */ + public function pageShouldContainsLinkToExportUnimarc() { + $this->assertXPath('//a[contains(@href, "panier/export-unimarc/id_panier/15")]'); + } + + + /** @test */ + public function pageShouldContainsLinkToExportList() { + $this->assertXPath('//a[contains(@href, "panier/export-liste/id_panier/15")]'); + } + + /** @test */ public function pageShouldContainsUrlToRemoveNoticeMontespan() { - $this->assertXPath('//a[contains(@href, "panier/paniersupprimernotice/id_panier/15/id_notice/4")]', - $this->_response->getBody()); + $this->assertXPath('//a[contains(@href, "panier/paniersupprimernotice/id_panier/15/id_notice/4")]'); } } @@ -319,4 +331,94 @@ class PanierControllerAjoutNoticeBlackSadToPanierMesRomansTest extends PanierCon } + + +class PanierControllerModifierTitrePanierMesRomansToMesLivresTest extends PanierControllerTestCase { + public function setUp() { + parent::setUp(); + + $this->postDispatch('/panier/majtitrepanier/id_panier/15', + ['new_libelle' => 'Mes livres'], + true); + } + + + /** @test */ + public function panierShouldHaveBeenSaved() { + $this->assertTrue(Class_PanierNotice::methodHasBeenCalled('save')); + } + + + /** @test */ + public function responseShouldRedirectToIndex() { + $this->assertRedirectTo('/opac/panier/index/id_panier/15'); + } + + + /** @test */ + public function panierLibelleShouldBeMesLivres() { + $this->assertEquals('Mes livres', $this->panier_romans->getLibelle()); + } +} + + + + +class PanierControllerExportUnimarcTest extends PanierControllerTestCase { + public function setUp() { + parent::setUp(); + + $this->dispatch('/panier/export-unimarc/id_panier/15', true); + } + + + /** @test */ + public function bodyShouldReturnUnimarcISO2709() { + $this->assertEquals('00471nam0 22001931 450 200001400000200003200014001000800046010002800054100004100082101000800123102000700131105001800138210002600156215003800182700002200220801002200242902000700264903000600271aLe Montespan1 aMontespan (Le)fJean TeulÂe0507356 a978-2-260-01723-3d22 ? a20080612d|||| uuuy0frey0103 ba| afre aFR ay|||||||000|y aPariscJulliardd2008 a333 p.cjaq. ill. en coul.d21 cm 1aTeulÂebJean4070 0aFRbBMVc20080612 arh aG', + $this->_response->getBody()); + } + + + /** @test */ + public function headerShouldContainsContentDispositionAttachment() { + $this->assertHeaderContains('Content-Disposition', 'attachment'); + } + + + /** @test */ + public function headerShouldContainsContentTypeTextPlain() { + $this->assertHeaderContains('Content-Type', 'text/plain'); + } +} + + + + +class PanierControllerExportListeTest extends PanierControllerTestCase { + public function setUp() { + parent::setUp(); + + $this->dispatch('/panier/export-liste/id_panier/15', true); + } + + + /** @test */ + public function bodyShouldReturnListe() { + $this->assertContains('Le Montespan Jean Teulé Julliard', + $this->_response->getBody()); + } + + + /** @test */ + public function headerShouldContainsContentDispositionAttachment() { + $this->assertHeaderContains('Content-Disposition', 'attachment'); + } + + + /** @test */ + public function headerShouldContainsContentTypeTextPlain() { + $this->assertHeaderContains('Content-Type', 'text/plain'); + } +} + ?> \ No newline at end of file -- GitLab