diff --git a/application/modules/admin/controllers/SitoController.php b/application/modules/admin/controllers/SitoController.php index 12fb20f0ddae22d0f5cef3a1a73b3e92889881e5..392b4c6cf669e9c04f99ded4384d04cde47f8943 100644 --- a/application/modules/admin/controllers/SitoController.php +++ b/application/modules/admin/controllers/SitoController.php @@ -45,6 +45,54 @@ class Admin_SitoController extends ZendAfi_Controller_Action { } + public function createAction() { + $import_form = $this->view + ->newForm(['id' => 'import', 'class' => 'form']) + ->setMethod('post') + ->addElement('url', 'url', ['label' => $this->view->_('URL du site web'), + 'required' => true, + 'allowEmpty' => false]) + ->addDisplayGroup(['url'], 'website', ['legend' => $this->view->_('Site web')]) + ->addElement('submit', 'submit', ['label' => $this->view->_('Importer')]); + + if ($this->getRequest()->isPost() && $import_form->isValid($this->getRequest()->getPost())) { + $album = $this->createAlbumFromUrl($this->getRequest()->getPost('url')); + if ($album && $album->save()) { + $this->_redirect('/admin/album/edit_album/id/'.$album->getId()); + return; + } + } + + $this->view->import_form = $import_form; + } + + + protected function createAlbumFromUrl($url) { + $html = (new Class_WebService_SimpleWebClient())->open_url($url); + $dom = new Zend_Dom_Query($html); + + $album = Class_Album::newInstance(['type_doc_id' => Class_TypeDoc::WEBSITE]); + $album->setTitre($dom->queryXpath('//head/title')->current()->textContent); + + $description_node = $dom->queryXpath('//head/meta[@name="description"]')->current(); + $album->setDescription($description_node->getAttribute('content')); + + $resource = Class_AlbumRessource::newInstance(['url' => $url, + 'titre' => $album->getTitre()]); + $album->addRessource($resource); + $album->save(); + + $thumbnailer = (new Class_WebService_WebSiteThumbnail()); + $poster_name = $thumbnailer->fileNameFromUrl($url); + $poster_path = $resource->getThumbnailPath(); + $resource->getFolderManager()->ensure($poster_path); + $thumbnailer->getThumbnailer()->fetchUrlToFile($url, $poster_path . $poster_name); + $resource->setPoster($poster_name); + + return $album; + } + + protected function _updateNewModel($sitotheque) { if (!$category = Class_SitothequeCategorie::find($this->_getParam('id_cat'))) { $this->_redirect('admin/sito'); diff --git a/library/Class/WebService/WebSiteThumbnail.php b/library/Class/WebService/WebSiteThumbnail.php index b5c97dad96c3540e2615205027318cef193d6efe..095d28e27958174fec61c0e948c01af0afd0737f 100644 --- a/library/Class/WebService/WebSiteThumbnail.php +++ b/library/Class/WebService/WebSiteThumbnail.php @@ -101,7 +101,7 @@ class Class_WebService_WebSiteThumbnail { return $this->getThumbsDir().$thumbnail; } - protected function fileNameFromUrl($url) { + public function fileNameFromUrl($url) { $decoded = urldecode($url); $wo_http = preg_replace('/^.*:\/\//', '', $decoded); $filename = preg_replace('/[^\w\-]/', '_', $wo_http); diff --git a/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php b/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php index ab073a1c829e23cc69c21b631d5cfbcd6c4ee24a..8849f4aed6ce2ad9424b963c52cbfee4eaf58076 100644 --- a/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php +++ b/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php @@ -130,6 +130,9 @@ class ZendAfi_View_Helper_Admin_MenuGaucheAdmin extends ZendAfi_View_Helper_Base .$this->addMenu("jamendo_16.png", $this->translate()->_("Jamendo"), "/admin/harvest/jamendo-browse", Class_AdminVar::isJamendoEnabled()) + .$this->addMenu("sitotheque_16.png", $this->translate()->_("Sitothèque"), "/admin/sito/create", + $this->filterAdmin($this->user) || $this->user->hasRightToAccess(Class_UserGroup::RIGHT_USER_SITOTHEQUE)) + /* disabled until interactive harvesting is implemented .$this->addMenu("oai_16.png", $this->translate()->_("Cyberlibris"), "/admin/harvest/cyberlibris-browse", Class_AdminVar::isCyberlibrisEnabled() diff --git a/tests/application/modules/admin/controllers/SitothequeControllerTest.php b/tests/application/modules/admin/controllers/SitothequeControllerTest.php index f5d95a7bb0c2c8cb24c707b0063bef063a388815..75e835510478998aa7c8f9562359fdb2867ea42b 100644 --- a/tests/application/modules/admin/controllers/SitothequeControllerTest.php +++ b/tests/application/modules/admin/controllers/SitothequeControllerTest.php @@ -605,4 +605,59 @@ class SitothequeControllerDeleteCategorieInformationsTest extends SitothequeCont } + + +class SitothequeControllerImportFromUrlTest extends AbstractControllerTestCase { + protected + $_storm_default_to_volatile = true, + $_cned; + + public function setUp() { + parent::setUp(); + $this->postDispatch('/admin/sito/create', + ['url' => 'http://www.cned.fr']); + + $this->_cned = Class_Album::find(1); + } + + + /** @test */ + public function titleShouldBeFormationToutAuLongDeLaVie() { + $this->assertEquals('Formation tout au long de la vie - CNED', + $this->_cned->getTitre()); + } + + + /** @test */ + public function albumShouldBeWebSite() { + $this->assertTrue($this->_cned->isWebSite()); + } + + + /** @test */ + public function albumShouldHaveOneResourceWithUrlCnedDotFr() { + $this->assertNotNull($resource = $this->_cned->getRessources()[0]); + $this->assertEquals('http://www.cned.fr', $resource->getUrl()); + return $resource; + } + + + /** + * @depends albumShouldHaveOneResourceWithUrlCnedDotFr + * @test + */ + public function resourceTitleShouldBeFormationToutAuLongDeLaVie($resource) { + $this->assertEquals('Formation tout au long de la vie - CNED', + $resource->getTitre()); + + } + + + /** @test */ + public function albumDescriptionShouldBeFormationsEnLigne() { + $this->assertContains('Formations en ligne et cours', $this->_cned->getDescription()); + } +} + + ?> \ No newline at end of file