From 44a4629a20e0b6bde140510ba76757dbe8f2d4bb Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@git-test.afi-sa.fr>
Date: Fri, 8 Feb 2013 17:38:27 +0000
Subject: [PATCH] =?UTF-8?q?Validation=20des=20donn=C3=A9es=20sitoth=C3=A8q?=
 =?UTF-8?q?ue?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitattributes                                |  3 +-
 .../admin/controllers/SitoController.php      |  2 +
 .../views/scripts/cms/_newsform_head.phtml    |  2 +-
 .../admin/views/scripts/sito/sitoform.phtml   |  6 ++-
 library/Class/Article.php                     | 13 +-----
 library/Class/ArticleCategorie.php            | 16 +------
 library/Class/Sitotheque.php                  | 42 ++++++++++---------
 library/Class/SitothequeCategorie.php         | 12 ++----
 ...Viewable.php => TreeViewableCategorie.php} | 16 ++++++-
 library/Trait/TreeViewableItem.php            | 31 ++++++++++++++
 public/admin/css/global.css                   |  4 +-
 .../controllers/SitothequeControllerTest.php  | 26 +++++++++++-
 12 files changed, 110 insertions(+), 63 deletions(-)
 rename library/Trait/{TreeViewable.php => TreeViewableCategorie.php} (81%)
 create mode 100644 library/Trait/TreeViewableItem.php

diff --git a/.gitattributes b/.gitattributes
index 09da7caee91..e7d66c097e2 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2484,7 +2484,8 @@ library/Thumbs/thumb_plugins/gd_reflection.inc.php -text
 library/Trait/StaticFileWriter.php -text
 library/Trait/TimeSource.php -text
 library/Trait/Translator.php -text
-library/Trait/TreeViewable.php -text
+library/Trait/TreeViewableCategorie.php -text
+library/Trait/TreeViewableItem.php -text
 library/ZendAfi/Acl/AdminControllerRoles.php -text
 library/ZendAfi/Auth.php -text
 library/ZendAfi/Auth/Adapter/CommSigb.php -text
diff --git a/application/modules/admin/controllers/SitoController.php b/application/modules/admin/controllers/SitoController.php
index cfc3f7c65bc..f21f9343b5a 100644
--- a/application/modules/admin/controllers/SitoController.php
+++ b/application/modules/admin/controllers/SitoController.php
@@ -303,6 +303,7 @@ class Admin_SitoController extends Zend_Controller_Action {
 					->save()) {
 				$this->_helper->notify($this->_('Le site "%s" a été sauvegardé', $site->getTitre()));
 				$this->_redirect('/admin/sito/sitoedit/id/'.$site->getId());
+				return;
 			}
 		}
 
@@ -319,6 +320,7 @@ class Admin_SitoController extends Zend_Controller_Action {
 		}
 
 		$site->delete();
+		$this->_helper->notify($this->_('Le site "%s" a été supprimé', $site->getTitre()));
 		$this->_redirect('/admin/sito/index/id_cat/'.$site->getIdCat());
 	}
 
diff --git a/application/modules/admin/views/scripts/cms/_newsform_head.phtml b/application/modules/admin/views/scripts/cms/_newsform_head.phtml
index fb3546b11e1..61c90dabe3d 100644
--- a/application/modules/admin/views/scripts/cms/_newsform_head.phtml
+++ b/application/modules/admin/views/scripts/cms/_newsform_head.phtml
@@ -1,4 +1,4 @@
-<div id="cms_edit_head"><?php echo $this->_('Localisation') . ' : ' . $this->article->getBibLibelle(); ?>
+<div class="content_edit_head"><?php echo $this->_('Localisation') . ' : ' . $this->article->getBibLibelle(); ?>
 	<?php if (!$this->article->isNew())  { 
 		  $article_url = $this->url(array('module' => 'opac', 
 																			'controller' => 'cms', 
diff --git a/application/modules/admin/views/scripts/sito/sitoform.phtml b/application/modules/admin/views/scripts/sito/sitoform.phtml
index 14abb772e9f..63c08bf1fdf 100644
--- a/application/modules/admin/views/scripts/sito/sitoform.phtml
+++ b/application/modules/admin/views/scripts/sito/sitoform.phtml
@@ -1,10 +1,12 @@
-<?php echo $this->BarreLocalisation($this->site->getIdZone(), $this->site->getIdBib()); ?>
+<div class="content_edit_head">
+<?php echo $this->_('Localisation') . ' : ' . $this->site->getBibLibelle(); ?>
+</div>
 
 <div class="form">
 	<form name="form" action="<?php echo $this->url() ?>" method="post">
 		<fieldset>
 			<legend><?php echo $this->traduire('Sitothèque'); ?></legend>
-			<span id="abonne_erreur" class="abonne"><?php echo $this->message; ?></span>
+      <span class="error"><?php echo implode('<br/>', $this->site->getErrors()); ?></span>
 			<table>
 				<tr>
 					<td class="droite"><?php echo $this->traduire('Catégorie'); ?> </td>
diff --git a/library/Class/Article.php b/library/Class/Article.php
index 08d4abc18ec..34bcdf75e43 100644
--- a/library/Class/Article.php
+++ b/library/Class/Article.php
@@ -370,6 +370,8 @@ class ArticleLoader extends Storm_Model_Loader {
 
 
 class Class_Article extends Storm_Model_Abstract {
+	use Trait_TreeViewableItem;
+
 	const END_TAG='{FIN}';
 
 	const STATUS_DRAFT = 1;
@@ -933,17 +935,6 @@ class Class_Article extends Storm_Model_Abstract {
 	}
 
 
-
-
-
-	/**
-	 * @return String
-	 */
-	public function getBibLibelle() {
-		return $this->getCategorie()->getBib()->getLibelle();
-	}
-
-
 	public function getRank() {
 		return Class_CmsRank::getLoader()->findFirstBy(array('id_cms' => $this->getId()));
 	}
diff --git a/library/Class/ArticleCategorie.php b/library/Class/ArticleCategorie.php
index f3fa522c2e4..ab0a4caa263 100644
--- a/library/Class/ArticleCategorie.php
+++ b/library/Class/ArticleCategorie.php
@@ -20,7 +20,7 @@
  */
 
 class Class_ArticleCategorie extends Storm_Model_Abstract {
-	use Trait_TreeViewable;
+	use Trait_TreeViewableCategorie;
 
 	protected $_table_name = 'cms_categorie';
 	protected $_table_primary = 'ID_CAT';
@@ -94,20 +94,6 @@ class Class_ArticleCategorie extends Storm_Model_Abstract {
 		$this->check($this->getLibelle(), "Vous devez compléter le champ 'Libellé'");
 		return $this;
 	}
-
-
-	/**
-	 * @return Class_Bib
-	 */
-	public function getBib() {
-		if ($bib = parent::_get('bib'))
-			return $bib;
-
-		if (!$this->hasParentCategorie())
-			return Class_Bib::getLoader()->getPortail();
-
-		return  $this->getParentCategorie()->getBib();
-	}
 }
 
 ?>
diff --git a/library/Class/Sitotheque.php b/library/Class/Sitotheque.php
index aca7a150dc2..ecd2d956d76 100644
--- a/library/Class/Sitotheque.php
+++ b/library/Class/Sitotheque.php
@@ -99,6 +99,8 @@ class SitothequeLoader extends Storm_Model_Loader {
 
 
 class Class_Sitotheque extends Storm_Model_Abstract {
+	use Trait_Translator, Trait_TreeViewableItem;
+
 	protected $_loader_class = 'SitothequeLoader';
 	protected $_table_name = 'sito_url';
 	protected $_table_primary = 'ID_SITO';
@@ -372,28 +374,28 @@ class Class_Sitotheque extends Storm_Model_Abstract {
 	}
 
 
-	public function verifSitoData($data)
-	{
-		$errorMessage = '';
+	public function validate()	{
+		$this->check($this->getTitre(), $this->_("Vous devez compléter le champ 'Titre'"));
 
-		if ( $data['TITRE'] == '' ){$errorMessage = "Vous devez compléter le champ 'Titre'";}
-		elseif (  strlen_utf8($data['TITRE']) > 100 ){$errorMessage = "Le champ 'Titre' doit être inférieur à 100 caractères";}
-		elseif ( $data['URL'] == '' ){$errorMessage = "Vous devez compléter le champ 'Url'";}
-		elseif (  strlen_utf8($data['URL']) > 250 ){$errorMessage = "Le champ 'Url' doit être inférieur à 250 caractères";}
-		elseif (  strlen_utf8($data['DESCRIPTION']) > 250 ){$errorMessage = "Le champ 'Commentaire' doit être inférieur à 250 caractères";}
-		else{
-			try{
-				$httpClient = Zend_Registry::get('httpClient');
-				$httpClient->setUri($data['URL']);
-				$response = $httpClient->request();
-				if ($response->isError()){
-					$errorMessage = "Il y a un problème avec l'URL";
-				}
-			}catch (Exception $e){
-				$errorMessage = "Il y a un problème avec l'URL";
-			}
+		$this->check(strlen_utf8($this->getTitre()) <= 100,
+								 $this->_("Le champ 'Titre' doit être inférieur à 100 caractères"));
+
+		$this->check($this->getUrl(),
+								 $this->_("Vous devez compléter le champ 'Url'"));
+
+		$this->check(strlen_utf8($this->getUrl()) <= 250,
+								 $this->_("Le champ 'Url' doit être inférieur à 250 caractères"));
+
+		$this->check(strlen_utf8($this->getDescription()) <= 250,
+								 $this->_("Le champ 'Commehtaire' doit être inférieur à 250 caractères"));
+		
+		$http_client = new Class_WebService_SimpleWebClient();
+		try {
+			$this->check($http_client->open_url($this->getUrl()),
+									 $this->_('L\URL n\'est pas joignable'));
+		} catch (Exception $e) {
+			$this->addError($this->_($e->getMessage()));
 		}
-		return $errorMessage;
 	}
 
 
diff --git a/library/Class/SitothequeCategorie.php b/library/Class/SitothequeCategorie.php
index 7c29651ae49..d0bceeff8f1 100644
--- a/library/Class/SitothequeCategorie.php
+++ b/library/Class/SitothequeCategorie.php
@@ -19,14 +19,14 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
 class Class_SitothequeCategorie extends Storm_Model_Abstract {
-	use Trait_TreeViewable;
+	use Trait_TreeViewableCategorie;
 
 	protected $_table_name = 'sito_categorie';
 	protected $_table_primary = 'ID_CAT';
 	protected $_belongs_to = array('parent_categorie' => array('model' => 'Class_SitothequeCategorie',
-																														 'referenced_in' => 'ID_CAT_MERE'),
+																														 'referenced_in' => 'id_cat_mere'),
 																 'bib' => array('model' => 'Class_Bib',
-																								'referenced_in' => 'ID_SITE'));
+																								'referenced_in' => 'id_site'));
 
 	protected $_has_many = array('sous_categories' => array('model' => 'Class_SitothequeCategorie',
 																													 'role' => 'parent_categorie',
@@ -36,12 +36,6 @@ class Class_SitothequeCategorie extends Storm_Model_Abstract {
 																												'role' => 'categorie',
 																												'dependents' => 'delete'));
 
-
-	public static function getLoader() {
-		return self::getLoaderFor(__CLASS__);
-	}
-
-
 	/**
 	 * @return array
 	 */
diff --git a/library/Trait/TreeViewable.php b/library/Trait/TreeViewableCategorie.php
similarity index 81%
rename from library/Trait/TreeViewable.php
rename to library/Trait/TreeViewableCategorie.php
index d6a8e3d5f72..e2e3fd9a18f 100644
--- a/library/Trait/TreeViewable.php
+++ b/library/Trait/TreeViewableCategorie.php
@@ -19,7 +19,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
 
-trait Trait_TreeViewable {
+trait Trait_TreeViewableCategorie {
 	/**
 	 * @return bool
 	 */
@@ -40,6 +40,20 @@ trait Trait_TreeViewable {
 	public function getChildrenCount() {
 		return count($this->getItems()) + count($this->getSousCategories());
 	}
+
+
+	/**
+	 * @return Class_Bib
+	 */
+	public function getBib() {
+		if ($bib = parent::_get('bib'))
+			return $bib;
+
+		if (!$this->hasParentCategorie())
+			return Class_Bib::getLoader()->getPortail();
+
+		return  $this->getParentCategorie()->getBib();
+	}
 }
 
 ?>
\ No newline at end of file
diff --git a/library/Trait/TreeViewableItem.php b/library/Trait/TreeViewableItem.php
new file mode 100644
index 00000000000..1eba36b9c5e
--- /dev/null
+++ b/library/Trait/TreeViewableItem.php
@@ -0,0 +1,31 @@
+<?php
+/**
+ * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * 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 
+ */
+
+trait Trait_TreeViewableItem {
+	/**
+	 * @return String
+	 */
+	public function getBibLibelle() {
+		return $this->getCategorie()->getBib()->getLibelle();
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/public/admin/css/global.css b/public/admin/css/global.css
index 94d5aec1511..84bc32a4e38 100644
--- a/public/admin/css/global.css
+++ b/public/admin/css/global.css
@@ -128,12 +128,12 @@ form#user_subscribe label {
     color: #575757;
 }
 
-#cms_edit_head {
+.content_edit_head {
     color: #0058A5;
     font-weight:bold;
 }
 
-#cms_edit_head img {
+.content_edit_head img {
     vertical-align:middle;
 }
 
diff --git a/tests/application/modules/admin/controllers/SitothequeControllerTest.php b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
index 4c45a9f3b39..e7073a8bf18 100644
--- a/tests/application/modules/admin/controllers/SitothequeControllerTest.php
+++ b/tests/application/modules/admin/controllers/SitothequeControllerTest.php
@@ -130,6 +130,13 @@ class SitothequeControllerSitoEditTest extends SitothequeControllerTestCase {
 		$this->assertXPath('//form[contains(@action, "admin/sito/sitoedit/id/22")]',
 											 $this->_response->getBody());
 	}
+
+
+
+	/** @test */
+	public function pageShouldDisplayLocalisationAnnecy() {
+		$this->assertXPathContentContains('//div', 'Localisation : Annecy');
+	}
 }
 
 
@@ -161,7 +168,9 @@ class SitothequeControllerPostAddActionTest extends SitothequeControllerTestCase
 	public function setUp() {
 		parent::setUp();
 		Class_Sitotheque::whenCalled('save')->willDo(function($site) {$site->setId(25);});
-		$this->postDispatch('/admin/sito/sitoadd/id_cat/2', ['titre' => 'google'], true);
+		$this->postDispatch('/admin/sito/sitoadd/id_cat/2', 
+												['titre' => 'google',
+												 'url' => 'http://www.google.fr'], true);
 	}
 
 
@@ -174,6 +183,21 @@ class SitothequeControllerPostAddActionTest extends SitothequeControllerTestCase
 
 
 
+class SitothequeControllerInvalidPostActionTest extends SitothequeControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->postDispatch('/admin/sito/sitoadd/id_cat/2', [], true);
+	}
+
+	/** @test */
+	public function pageShouldContainsErrorValeurRequise() {
+		$this->assertXPathContentContains('//span[@class="error"]', "Vous devez compléter le champ 'Titre'");
+	}
+}
+
+
+
+
 class SitothequeControllerSitoPostEditLeMondeTest extends SitothequeControllerTestCase {
 	public function setUp() {
 		parent::setUp();
-- 
GitLab