From 1970cf7f536e6430f45504b1e70d1cbf3319a6d3 Mon Sep 17 00:00:00 2001 From: efalcy <efalcy@git-test.afi-sa.fr> Date: Tue, 22 Jan 2013 14:51:16 +0000 Subject: [PATCH] Import de fichier pour generation du site : affichage dans l'interface d'admin et clean de la base de donnees --- .gitattributes | 3 +- .../admin/controllers/SystemeController.php | 37 +++++++++++ .../scripts/systeme/generationsite.phtml | 1 + ...le.php => ImportFichierGenerationSite.php} | 61 +++++++++++++++---- .../View/Helper/Admin/MenuGaucheAdmin.php | 1 + .../controllers/ImportArboFromTxtFileTest.php | 44 +++++++++++-- 6 files changed, 128 insertions(+), 19 deletions(-) create mode 100644 application/modules/admin/views/scripts/systeme/generationsite.phtml rename library/Class/{ImportTxtFile.php => ImportFichierGenerationSite.php} (70%) diff --git a/.gitattributes b/.gitattributes index 849f34e19ef..55efc544505 100644 --- a/.gitattributes +++ b/.gitattributes @@ -640,6 +640,7 @@ application/modules/admin/views/scripts/subModal.phtml -text application/modules/admin/views/scripts/suggestion-achat/edit.phtml -text application/modules/admin/views/scripts/suggestion-achat/index.phtml -text application/modules/admin/views/scripts/systeme/cacheimages.phtml -text +application/modules/admin/views/scripts/systeme/generationsite.phtml -text application/modules/admin/views/scripts/systeme/importavisopac2.phtml -text application/modules/admin/views/scripts/systeme/mailtest.phtml -text application/modules/admin/views/scripts/systeme/phpinfo.phtml -text @@ -2194,7 +2195,7 @@ library/Class/Formulaire.php -text library/Class/Hash.php -text library/Class/I18n.php -text library/Class/I18nTranslator.php -text -library/Class/ImportTxtFile.php -text +library/Class/ImportFichierGenerationSite.php -text library/Class/Indexation.php -text library/Class/IntBib.php -text library/Class/Isbn.php -text diff --git a/application/modules/admin/controllers/SystemeController.php b/application/modules/admin/controllers/SystemeController.php index 7a4bcb1b0c5..8e9249ad80b 100644 --- a/application/modules/admin/controllers/SystemeController.php +++ b/application/modules/admin/controllers/SystemeController.php @@ -136,6 +136,43 @@ class Admin_SystemeController extends Zend_Controller_Action { } + public function generationsiteAction() { + $this->view->titre="Génération du site"; + + $form = $this->_formGenerationSite(); + $this->view->form_generation_site = $form; + + if (!$this->_request->isPost()) + return; + + if ($form->isValid($this->_request->getPost()) && $form->siteTxt->receive()) { + $importFichier = new Class_ImportFichierGenerationSite(); + $importFichier->importFile($form->siteTxt->getFileName()); + $this->_helper->notify('site généré'); + $this->_redirect('admin/systeme/generationsite'); + return; + } + + $this->_helper->notify('Le fichier reçu n\'est pas valide'); + $this->_redirect('admin/systeme/generationsite'); + + } + + + protected function _formGenerationSite() { + return $this->view + ->newForm(array('id' => 'import_generationsite', + 'class' => 'form')) + ->setMethod('post') + ->setAttrib('enctype', 'multipart/form-data') + ->setAction($this->view->url(array('action' => 'generationsite'))) + ->addElement($this->view->newFormElementFile('siteTxt', 'txt'), 'siteTxt') + ->addElement('submit', 'submit', array('label' => 'Importer le fichier de description du site')); + } + + + + public function phpinfoAction() { $this->view->titre = 'Informations système'; } diff --git a/application/modules/admin/views/scripts/systeme/generationsite.phtml b/application/modules/admin/views/scripts/systeme/generationsite.phtml new file mode 100644 index 00000000000..f0d0420f459 --- /dev/null +++ b/application/modules/admin/views/scripts/systeme/generationsite.phtml @@ -0,0 +1 @@ +<?php echo $this->form_generation_site; ?> \ No newline at end of file diff --git a/library/Class/ImportTxtFile.php b/library/Class/ImportFichierGenerationSite.php similarity index 70% rename from library/Class/ImportTxtFile.php rename to library/Class/ImportFichierGenerationSite.php index c513b0152a4..7ed84ce2421 100644 --- a/library/Class/ImportTxtFile.php +++ b/library/Class/ImportFichierGenerationSite.php @@ -19,7 +19,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -class Class_ImportTxtFile { +class Class_ImportFichierGenerationSite { use Trait_StaticFileWriter; protected $_file_contents; @@ -29,6 +29,19 @@ class Class_ImportTxtFile { } + public function importFile($filename) { + $this->deleteAllProfils(); + $this->deleteAllCategories(); + $this->setFileContents(file_get_contents($filename, FILE_USE_INCLUDE_PATH)); + $profils = $this->readAllProfils(); + foreach ($profils as $profil) { + $profil->save(); + $this->createCategoriesForProfil($profil); + $this->createDirectoriesForProfil($profil); + } + } + + public function readAllProfils() { $profiles=[]; @@ -48,17 +61,24 @@ class Class_ImportTxtFile { } - public function getPages($lines) { + public function saveProfils($profils) { + foreach ($profils as $profil) { + $profil->save(); + } + } + + + protected function getPages($lines) { return preg_grep('/\*\*(?!\*)/',$lines); } - public function splitLines($block) { + protected function splitLines($block) { return preg_split('/\n/',$block,0,PREG_SPLIT_NO_EMPTY); } - public function getFirstLineName($line) { + protected function getFirstLineName($line) { return trim(preg_replace('/^\*(?!\*)/','$1',$line)); } @@ -68,7 +88,7 @@ class Class_ImportTxtFile { } - public function getFirstBlocks() { + protected function getFirstBlocks() { return preg_split('/\n\*(?!\*)/', $this->_file_contents,0,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); } @@ -97,19 +117,16 @@ class Class_ImportTxtFile { public function getDefaultPath() { - if (isset($this->config['doc_root'])) { - return $this->config['doc_root']; - } else { - return $_SERVER['DOCUMENT_ROOT']; - } + return USERFILESURL.'image/'; } - public function createDirectory($directory_name) { + protected function createDirectory($directory_name) { $file_writer = self::getFileWriter(); $cleaned_name=iconv('UTF-8','ASCII//TRANSLIT',$directory_name); $cleaned_name=str_replace(' ','_',$cleaned_name); - $file_writer->mkdir($this->getDefaultPath().'/'.$cleaned_name); + echo $this->getDefaultPath().'/'.$cleaned_name; + $file_writer->mkdir($this->getDefaultPath().$cleaned_name); return $cleaned_name; } @@ -121,7 +138,27 @@ class Class_ImportTxtFile { $subcategory->setLibelle($subprofil->getLibelle()); $category->addSousCategorie($subcategory); } + $category->save(); return $category; } + + public function deleteAllProfils() { + $profils = Class_Profil::findAll(); + foreach ($profils as $profil) { + if ($profil->isPortail()) + continue; + $profil->delete(); + } + } + + + public function deleteAllCategories() { + $categories= Class_ArticleCategorie::findAll(); + foreach ($categories as $category) { + $category->delete(); + } + } + + } \ No newline at end of file diff --git a/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php b/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php index fad3d4ee661..de01cfbe278 100644 --- a/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php +++ b/library/ZendAfi/View/Helper/Admin/MenuGaucheAdmin.php @@ -127,6 +127,7 @@ class ZendAfi_View_Helper_Admin_MenuGaucheAdmin extends ZendAfi_View_Helper_Base $menu_systeme .= $this->addMenu("php.png", $this->translate()->_("Informations système"), "/admin/systeme/phpinfo", $acl_super_admin); $menu_systeme .= $this->addMenu("images.png", $this->translate()->_("Cache des images"), "/admin/systeme/cacheimages", $acl_super_admin); $menu_systeme .= $this->addMenu("chat.gif.png", $this->translate()->_("Import avis opac2"), "/admin/systeme/importavisopac2",$acl_super_admin); + $menu_systeme .= $this->addMenu("systeme.png", $this->translate()->_("Génération du site"), "/admin/systeme/generationsite",$acl_super_admin); $menu_systeme .= $this->closeBoite(); // Activation des menus en fonction du rôle diff --git a/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php b/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php index e93115c7df7..ee547672cdc 100644 --- a/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php +++ b/tests/application/modules/admin/controllers/ImportArboFromTxtFileTest.php @@ -37,9 +37,17 @@ class ImportArboFormTxtFileReadProfilTest extends PHPUnit_Framework_TestCase { *Cinema ** Series_tv "; + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Profil') + ->whenCalled('save') + ->answers(true); - $this->_importTxt= new Class_ImportTxtFile(); - Class_ImportTxtFile::setFileWriter($this->file_writer=Storm_Test_ObjectWrapper::mock()); + + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_ArticleCategorie') + ->whenCalled('save') + ->answers(true); + + $this->_importTxt= new Class_ImportFichierGenerationSite(); + Class_ImportFichierGenerationSite::setFileWriter($this->file_writer=Storm_Test_ObjectWrapper::mock()); $this->file_writer->whenCalled('mkdir')->answers(true); $this->_importTxt->setFileContents($_file_contents); @@ -156,6 +164,9 @@ class ImportArboFormTxtFileDirectoryCreationReadProfilTest extends PHPUnit_Frame protected $_importTxt; protected $file_writer; protected $profils; + + + public function setUp() { parent::setUp(); @@ -167,9 +178,17 @@ class ImportArboFormTxtFileDirectoryCreationReadProfilTest extends PHPUnit_Frame ** Series tv "; - $this->_importTxt= new Class_ImportTxtFile(); - Class_ImportTxtFile::setFileWriter($this->file_writer=Storm_Test_ObjectWrapper::mock()); - + $this->_importTxt= new Class_ImportFichierGenerationSite(); + Class_ImportFichierGenerationSite::setFileWriter($this->file_writer=Storm_Test_ObjectWrapper::mock()); + $this->_category=Storm_Test_ObjectWrapper::mock(); + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Profil') + ->whenCalled('save') + ->answers(true); + + + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_ArticleCategorie') + ->whenCalled('save') + ->answers(true); $this->file_writer->whenCalled('mkdir') ->with('/Nature_et_loisirs/Sport') ->answers(true) @@ -219,7 +238,20 @@ class ImportArboFormTxtFileDirectoryCreationReadProfilTest extends PHPUnit_Frame $this->assertEquals('Cinema',$category->getLibelle()); } - + + + /** @test */ + public function importFileShouldSaveCreatedProfil() { + $this->_importTxt->saveProfils($this->profils); + $this->assertTrue(Class_Profil::methodHasBeenCalled('save')); + } + + + /** @test */ + public function importFileShouldSaveCreatedCategories() { + $category = $this->_importTxt->createCategoriesForProfil($this->profils[1]); + $this->assertTrue(Class_ArticleCategorie::methodHasBeenCalled('save')); + } } -- GitLab