From 5638a7295d7e0430046cd66f15c40913088436cf Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Wed, 18 Jun 2014 15:10:56 +0200
Subject: [PATCH] hotline #13302 fix RSS cat renaming

---
 .../admin/controllers/RssController.php       | 129 ++++++------------
 .../admin/views/scripts/rss/catform.phtml     |   9 +-
 library/Class/Rss.php                         |  24 ++--
 library/Class/RssCategorie.php                |  35 +++--
 .../View/Helper/ComboParentCategorie.php      |   2 +-
 .../admin/controllers/RssControllerTest.php   |  91 ++++++++++++
 6 files changed, 166 insertions(+), 124 deletions(-)

diff --git a/application/modules/admin/controllers/RssController.php b/application/modules/admin/controllers/RssController.php
index 06e668aa0f6..f21d835db36 100644
--- a/application/modules/admin/controllers/RssController.php
+++ b/application/modules/admin/controllers/RssController.php
@@ -71,13 +71,18 @@ class Admin_RssController extends Zend_Controller_Action {
 	}
 
 
-	function cataddAction()
-	{
-		$this->view->titre = "Ajouter une cat&eacute;gorie de flux RSS";
+	function cataddAction()	{
+		$this->view->titre = $this->_("Ajouter une catégorie de flux RSS");
 		$class_rss = new Class_Rss();
+		$rss_categorie = new Class_RssCategorie();
 
-		if ($this->_request->isPost())
-		{
+		if ($parent_categorie = Class_RssCategorie::find((int)$this->_getParam('id'))) {
+			$rss_categorie
+				->setParentCategorie($parent_categorie)
+				->setIdSite($parent_categorie->getIdSite());
+		}
+
+		if ($this->_request->isPost()) {
 			$filter = new Zend_Filter_StripTags();
 			$libelle = trim($filter->filter($this->_request->getPost('libelle')));
 			$id_cat_mere = (int)$this->_request->getPost('id_cat_mere');
@@ -91,86 +96,49 @@ class Admin_RssController extends Zend_Controller_Action {
 			$menu_deploy = $this->saveTreeMenu($id_cat_mere);
 			$class_rss = new Class_Rss();
 			$errorMessage = $class_rss->addCategorie($data);
-			if ($errorMessage == ''){$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);}
-			else
-			{
-				$this->view->cat = new stdClass();
-				$this->view->cat->ID_CAT = null;
-				$this->view->cat->ID_CAT_MERE = $id_cat_mere;
-				$this->view->cat->LIBELLE = $libelle;
-				$this->view->id_cat_mere = $id_cat_mere;
-				$this->view->message = $errorMessage;
+			if ($errorMessage == ''){
+				$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);
+				return;
 			}
+			$this->view->message = $errorMessage;
 		}
-		else
-		{
-			$id = (int)$this->_request->getParam('id', 0); if(!$id || $id ==0) $id_cat_mere = 0; else $id_cat_mere = $id;
-			$menu_deploy = $this->saveTreeMenu($id_cat_mere);
-			// Cat _blank
-			$this->view->cat = new stdClass();
-			$this->view->cat->ID_CAT = null;
-			$this->view->cat->LIBELLE = '';
-			$this->view->cat->ID_CAT_MERE = $id_cat_mere;
+		else	{
+			$menu_deploy = $this->saveTreeMenu($rss_categorie->getIdCatMere());
 		}
 		// Action
 		$this->view->action = 'add';
 		$this->view->sess_id_zone = $this->id_zone;
 		$this->view->sess_id_site = $this->id_bib;
+		$this->view->rss_cat = $rss_categorie;
+    $this->view->combo_cat = $this->view->comboParentCategorie($rss_categorie);
 	}
 
-	function cateditAction()
-	{
-		$this->view->titre = "Modifier une cat&eacute;gorie de flux RSS";
-		$class_rss = new Class_Rss();
 
-		if ($this->_request->isPost())
-		{
-			$filter = new Zend_Filter_StripTags();
-			$id_cat_mere = (int)$this->_request->getPost('id_cat_mere');
-			$id_cat = (int)$this->_request->getParam('id_cat', 0);
-			$libelle = trim($filter->filter($this->_request->getPost('libelle')));
-			if ($id !== false) {
+	function cateditAction() {
+		$this->view->titre = $this->_("Modifier une catégorie de flux RSS");
+		
+		$id = (int)$this->_request->getParam('id', 0);
+		if (!$rss_categorie = Class_RssCategorie::find($id))
+			$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);
 
-				$data = array(
-				'ID_CAT_MERE' => $id_cat_mere,
-				'LIBELLE' => $libelle,
-				);
-
-				$errorMessage = $class_rss->editCategorie($data, $id_cat);
-				if ($errorMessage == ''){$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);}
-				else
-				{
-					$rssCategorie = $class_rss->getCategorieByIdCat($id_cat);
-					if (is_string($categorie) ){$this->_redirect('admin/error/database');}
-					else
-					{
-						$this->view->combo_cat = $this->rendComboCat($this->id_bib,$rssCategorie->ID_CAT,$rssCategorie->ID_CAT_MERE);
-						$this->view->rss = $rssCategorie;
-						$this->view->id_cat_mere = $id_cat_mere;
-						$this->view->message = $errorMessage;
-					}
-				}
+		if ($this->_request->isPost()) {
+			if ($rss_categorie->updateAttributes($this->_request->getPost())->save()) {
+				$this->_redirect('/admin/rss/index/z/'.$this->id_zone.'/b/'.$this->id_bib);
+				return;
 			}
+
+			$errorMessage = implode(',', $rss_categorie->getErrors());
 		}
-		else
-		{
-			$id = (int)$this->_request->getParam('id', 0);
-			$menu_deploy = $this->saveTreeMenu($id);
-			if ( $id > 0 )
-			{
-				$rssCategorie = $class_rss->getCategorieByIdCat($id);
-				$this->view->combo_cat = $this->rendComboCat($this->id_bib,$rssCategorie->ID_CAT,$rssCategorie->ID_CAT_MERE);
-				if ( is_string($rssCategorie) ){$this->_redirect('admin/error/database');}
-				elseif($rssCategorie == null){$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);}
-				else{$this->view->rss = $rssCategorie;	}
-			}
-			else {$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);}
-		}
-		// Action
+
+    $this->view->combo_cat = $this->view->comboParentCategorie($rss_categorie);
 		$this->view->action = 'edit';
 		$this->view->sess_id_zone = $this->id_zone;
 		$this->view->sess_id_site = $this->id_bib;
+		$this->view->id = $id;
+		$this->view->rss_cat = $rss_categorie;
 	}
+
+
 	function catdelAction()
 	{
 		$class_rss = new Class_Rss();
@@ -361,26 +329,7 @@ class Admin_RssController extends Zend_Controller_Action {
 		$this->_redirect('admin/rss?z='.$this->id_zone.'&b='.$this->id_bib);
 	}
 
-	function rendComboCat($id_bib,$id_cat,$id_cat_mere)
-	{
-		$class_rss = new Class_Rss();
-		$cat_array = $class_rss->getAllCategorieByIdBib($id_bib);
-		$html[]='<select name="id_cat_mere" id="id_cat_mere" style="width:100%">';
-		$html[]='<option value="'.$id_cat_mere.'" selected="selected">Aucune</option>';
-		foreach ($cat_array as $cat)
-		{
-			if ($id_bib ==0)
-			{
-				if ($id_cat !=$cat->ID_CAT && $cat->ID_CAT_MERE != $id_cat) $html[]='<option value="'.$cat->ID_CAT.'">'.$cat->LIBELLE.'</option>';
-			}
-			else
-			{
-				if($id_cat !=$cat["ID_CAT"] && $cat["ID_CAT_MERE"] != $id_cat)$html[]='<option value="'.$cat["ID_CAT"].'">'.$cat["LIBELLE"].'</option>';
-			}
-		}
-		$html[]='</select>';
-		return implode('',$html);
-	}
+
 
 	function saveTreeMenu($id_cat)
 	{
@@ -394,4 +343,6 @@ class Admin_RssController extends Zend_Controller_Action {
 		}
 		$_SESSION["MENU_DEPLOY"]["RSS"] = $id_menu;
 	}
-}
\ No newline at end of file
+}
+
+?>
\ No newline at end of file
diff --git a/application/modules/admin/views/scripts/rss/catform.phtml b/application/modules/admin/views/scripts/rss/catform.phtml
index 643627cf2c2..834eadedf4c 100644
--- a/application/modules/admin/views/scripts/rss/catform.phtml
+++ b/application/modules/admin/views/scripts/rss/catform.phtml
@@ -7,11 +7,12 @@
 			<br />
 			<table>
 				<tr>
-					<td class="droite"><?php echo $this->traduire('Nom'); ?> *</td>
-					<td class="gauche"><input type="text" id="libelle" name="libelle" maxlength="80" onkeypress="if (event.keyCode == 13) {javascript:PicToolbarOver( getElementById('menu_item975'), 'menu_item975');this.form.submit();return false;}" value="<?php echo $this->escape(trim($this->rss->LIBELLE));?>" style="width:536px;"/></td>
+					<td class="droite"><?php echo $this->_('Catégorie'); ?></td>
+					<td class="gauche"><?php echo $this->combo_cat; ?></td>
 				</tr>
 				<tr>
-					<?php echo $combo_move; ?>
+					<td class="droite"><?php echo $this->traduire('Nom'); ?> *</td>
+					<td class="gauche"><input type="text" id="libelle" name="libelle" maxlength="80" onkeypress="if (event.keyCode == 13) {javascript:PicToolbarOver( getElementById('menu_item975'), 'menu_item975');this.form.submit();return false;}" value="<?php echo $this->escape(trim($this->rss_cat->getLibelle()));?>" style="width:536px;"/></td>
 				</tr>
 				<tr>
 					<td colspan="2" align="center"> <br />
@@ -24,8 +25,6 @@
 						</table>
 				</tr>
 			</table>
-				<input type="hidden" name="id_cat" value="<?php echo $this->cat->ID_CAT; ?>" />
-				<input type="hidden" name="id_cat_mere" value="<?php echo $this->cat->ID_CAT_MERE; ?>" />
 			<br /></fieldset>
 	</form>
 </div>
diff --git a/library/Class/Rss.php b/library/Class/Rss.php
index 4954e10197b..d26285ba3eb 100644
--- a/library/Class/Rss.php
+++ b/library/Class/Rss.php
@@ -396,23 +396,17 @@ class Class_Rss extends Storm_Model_Abstract {
 		}
 		return $errorMessage;
 	}
+
+
 	// Edit une cat
-	public function editCategorie($data, $id_cat)
-	{
-    $errorMessage = $this->verifCategorieData($data);
-    if ($errorMessage == '')
-			{
-        try{
-					$where = $this->_rssCategorie->getAdapter()->quoteInto('ID_CAT=?', $id_cat);
-					$this->_rssCategorie->update($data, $where);
-				}catch (Exception $e){
-					logErrorMessage('Class: Clas_Rss; Function: editCategorie' . NL . $e->getMessage());
-					$errorMessage = $this->_dataBaseError;
-				}
-			}
-		return $errorMessage;
+	public function editCategorie($data, $id_cat)	{
+		if ( $errorMessage = $this->verifCategorieData($data))
+			return $errorMessage;
+
+		Class_RssCategorie::find($id_cat)->updateAttributes($data)->save();
 	}
 
+
 	// Supprimer une cat
 	public function delCategorie($id_cat)
 	{
@@ -438,7 +432,7 @@ class Class_Rss extends Storm_Model_Abstract {
 		$errorMessage = '';
 		if ( $data['LIBELLE'] == '' ){
 			$errorMessage = "Vous devez compléter le champ 'Nom de la catégorie'";
-		}elseif (  mb_strlen($data['libelle'],'UTF-8') > 50 ){
+		}elseif (mb_strlen($data['LIBELLE'],'UTF-8') > 50) {
 			$errorMessage = "Le champ 'Nom de la catégorie' doit être inférieur à 50 caractères";
 		}
 		return $errorMessage;
diff --git a/library/Class/RssCategorie.php b/library/Class/RssCategorie.php
index 70c3ec3c0fb..ae6b7114352 100644
--- a/library/Class/RssCategorie.php
+++ b/library/Class/RssCategorie.php
@@ -20,20 +20,27 @@
  */
 
 class Class_RssCategorie extends Storm_Model_Abstract {
-	protected $_table_name = 'rss_categorie';
-	protected $_table_primary = 'ID_CAT';
-	protected $_belongs_to = array('parent_categorie' => array('model' => 'Class_RssCategorie',
-																														 'referenced_in' => 'ID_CAT_MERE'),
-																 'bib' => array('model' => 'Class_Bib',
-																								'referenced_in' => 'id_site'));
-
-	protected $_has_many = array('sous_categories' => array('model' => 'Class_RssCategorie',
-																													 'role' => 'parent_categorie',
-																													 'dependents' => 'delete'),
-
-                                 'feeds' => array('model' => 'Class_Rss',
-																									'role' => 'categorie',
-																									'dependents' => 'delete'));
+	protected 
+		$_table_name = 'rss_categorie',
+		$_table_primary = 'ID_CAT',
+		
+		$_belongs_to = ['parent_categorie' => ['model' => 'Class_RssCategorie',
+																					 'referenced_in' => 'id_cat_mere'],
+										'bib' => ['model' => 'Class_Bib',
+															'referenced_in' => 'id_site']],
+		
+		$_has_many = ['sous_categories' => ['model' => 'Class_RssCategorie',
+																				'role' => 'parent_categorie',
+																				'dependents' => 'delete'],
+
+									'feeds' => ['model' => 'Class_Rss',
+															'role' => 'categorie',
+															'dependents' => 'delete']];
+
+
+	protected $_default_attribute_values = ['libelle' => '',
+																					'id_cat_mere' => 0,
+																					'id_site' => 0];
 
 
 	/**
diff --git a/library/ZendAfi/View/Helper/ComboParentCategorie.php b/library/ZendAfi/View/Helper/ComboParentCategorie.php
index caaca92f47f..dda1147177b 100644
--- a/library/ZendAfi/View/Helper/ComboParentCategorie.php
+++ b/library/ZendAfi/View/Helper/ComboParentCategorie.php
@@ -31,7 +31,7 @@ class ZendAfi_View_Helper_ComboParentCategorie extends ZendAfi_View_Helper_Combo
 		}
 
 		return sprintf('<select name="id_cat_mere" id="id_cat_mere"><option value="0">Aucune</option>%s</select>', 
-									 $this->_getAllBibCategories($bibs, $category)		
+									 $this->_getAllBibCategories($bibs, $category)
 		);
 	}
 
diff --git a/tests/application/modules/admin/controllers/RssControllerTest.php b/tests/application/modules/admin/controllers/RssControllerTest.php
index 2422e0d89f9..a787dfb8faf 100644
--- a/tests/application/modules/admin/controllers/RssControllerTest.php
+++ b/tests/application/modules/admin/controllers/RssControllerTest.php
@@ -21,6 +21,22 @@
 require_once 'AdminAbstractControllerTestCase.php';
 
 abstract class RssControllerTestCase extends Admin_AbstractControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->fixture('Class_Rss',
+									 ['id' => 3,
+										'titre' => 'linuxfr',
+										'categorie' => $this->fixture('Class_RssCategorie',
+																									['id' => 1,
+																									 'libelle' => 'Geeks',
+																									 'id_cat_mere' => 0,
+																									 'bib' => $this->fixture('Class_Bib',
+																																					 ['id' => 74,
+																																						'libelle' => 'Annecy'])
+																										])
+											]);
+										
+	}
 }
 
 
@@ -53,4 +69,79 @@ class RssControllerAddActionTest extends RssControllerTestCase {
 	}
 }
 
+
+
+
+class RssControllerCatEditActionTest extends RssControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->dispatch('/admin/rss/catedit/id/1', true);
+	}
+
+
+	/** @test */
+	public function formActionShouldBeRssCatEditIdOne() {
+		$this->assertXPath('//form[contains(@action, "/admin/rss/catedit/id/1")]');
+	}
+
+
+	/** @test */
+	public function formShouldContainsIdCatMereSelect() {
+		$this->assertXPath('//select[@name="id_cat_mere"]');
+	}
+}
+
+
+
+
+class RssControllerPostCatEditActionTest extends RssControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->postDispatch('/admin/rss/catedit/id/1',
+												['libelle' => 'News']);
+		Class_RssCategorie::clearCache();
+	}
+
+	/** @test */
+	public function linuxFrCategorieShouldBeNews() {
+		$this->assertEquals('News', Class_RssCategorie::find(1)->getLibelle());
+	}
+
+
+	/** @test */
+	public function responseShouldRedirectToIndexWithZoneAndIdBibPORTAIL() {
+		$this->assertRedirectTo('/admin/rss/index/z/ALL/b/1');
+	}
+}
+
+
+
+
+class RssControllerCatAddActionTest extends RssControllerTestCase {
+	public function setUp() {
+		parent::setUp();
+		$this->dispatch('/admin/rss/catadd/id/1', true);
+	}
+
+
+	/** @test */
+	public function titleShouldBeAjouterCategorie() {
+		$this->assertXPathContentContains('//h1', 'Ajouter une catégorie de flux RSS');
+	}
+
+
+	/** @test */
+	public function formActionShouldBeRssCatAdd() {
+		$this->assertXPath('//form[contains(@action, "/admin/rss/catadd")]');
+	}
+
+
+	/** @test */
+	public function selectedCatMereShouldBeIdOne() {
+		$this->assertXPathContentContains('//select[@name="id_cat_mere"]//option[@selected="selected"][@value="1"]',
+																			'Geeks');
+	}
+
+}
+
 ?>
\ No newline at end of file
-- 
GitLab