From c4dfa1241ad49f97551c7165b77a185d801193ff Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Thu, 13 Sep 2012 08:28:22 +0000
Subject: [PATCH] =?UTF-8?q?BibNum:=20si=20le=20m=C3=A9dia=20est=20une=20im?=
 =?UTF-8?q?age=20l'input=20affiche=20sa=20vignette?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../admin/controllers/AlbumController.php     |  2 +-
 library/Class/AlbumRessource.php              |  3 ++-
 library/ZendAfi/Form/Album/Ressource.php      |  9 +++++++-
 library/ZendAfi/Form/Element/File.php         | 21 ++++++++++++++-----
 library/ZendAfi/Form/Element/Image.php        |  5 ++---
 .../library/ZendAfi/Form/Element/FileTest.php | 17 +++++++++++++--
 6 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/application/modules/admin/controllers/AlbumController.php b/application/modules/admin/controllers/AlbumController.php
index b2fb9c03128..87934f3155e 100644
--- a/application/modules/admin/controllers/AlbumController.php
+++ b/application/modules/admin/controllers/AlbumController.php
@@ -260,7 +260,7 @@ class Admin_AlbumController extends Zend_Controller_Action {
 		if ($this->_request->isPost() && ($form->isValid($this->_request->getPost()))) {
 			$ressource->updateAttributes($this->_request->getPost());
 			if ($ressource->getAlbum()->save() && $ressource->save() && $ressource->receiveFile()) {
-				$this->_helper->notify(sprintf('Média %s sauvegardé', 
+				$this->_helper->notify(sprintf('Média "%s" sauvegardé', 
 																			 $ressource->getTitre()));
 				$this->_redirect('admin/album/edit_ressource/id/'
 												 . $ressource->getId());
diff --git a/library/Class/AlbumRessource.php b/library/Class/AlbumRessource.php
index 43a3fcf823a..c77960cfaba 100644
--- a/library/Class/AlbumRessource.php
+++ b/library/Class/AlbumRessource.php
@@ -177,7 +177,8 @@ class Class_AlbumRessource extends Storm_Model_Abstract {
 			unlink($oldOriginal);
 		}
 
-		unlink($oldThumbnail);
+		if (file_exists($oldThumbnail))
+			@unlink($oldThumbnail);
 
 		return $this->createThumbnail();
 	}
diff --git a/library/ZendAfi/Form/Album/Ressource.php b/library/ZendAfi/Form/Album/Ressource.php
index 1dca6ecb9e6..111f78b39a4 100644
--- a/library/ZendAfi/Form/Album/Ressource.php
+++ b/library/ZendAfi/Form/Album/Ressource.php
@@ -75,7 +75,14 @@ class ZendAfi_Form_Album_Ressource extends ZendAfi_Form {
 	 * @return ZendAfi_Form_Album_Ressource
 	 */
 	public function addFileFor($model) {
-		return $this->addElement('file', 'fichier', ['label' => 'Fichier']);
+		$element = new ZendAfi_Form_Element_Image('fichier', ['label' => 'Fichier']);
+		if ($model) {
+			$element
+				->setBasePath($model->getOriginalsPath())
+				->setBaseUrl($model->getThumbnailsUrl())
+				->setThumbnailUrl($model->getThumbnailUrl());
+		}
+		return $this->addElement($element);
 	}
 }
 
diff --git a/library/ZendAfi/Form/Element/File.php b/library/ZendAfi/Form/Element/File.php
index 81c1267fa29..8549eaf9550 100644
--- a/library/ZendAfi/Form/Element/File.php
+++ b/library/ZendAfi/Form/Element/File.php
@@ -35,18 +35,29 @@ class ZendAfi_Form_Element_File extends Zend_Form_Element_Xhtml {
 	/** @var string */
 	protected $_actionUrl;
 
+
 	public function __construct($spec, $options = null) {
 		parent::__construct($spec, $options);
 
+		$this->_insertDecoratorsBefore(['File' => new ZendAfi_Form_Decorator_File(),
+				                            'DeleteButton' => new ZendAfi_Form_Decorator_DeleteButton()]);
+	}
+	
+
+	/**
+	 * @param $decoratorsToInsert array
+	 */
+	protected function _insertDecoratorsBefore($decoratorsToInsert) {
+		if (!$decoratorsToInsert)
+			return;
+
 		$decorators = $this->_decorators;
-		$this->_decorators = array('File' => new ZendAfi_Form_Decorator_File(),
-															 'DeleteButton' => new ZendAfi_Form_Decorator_DeleteButton());
+		$this->_decorators = $decoratorsToInsert;
 
-		foreach ($decorators as $name => $value) {
+		foreach ($decorators as $name => $value)
 			$this->_decorators[$name] = $value;
-		}
 	}
-
+	
 
 	/**
 	 * @param string $path
diff --git a/library/ZendAfi/Form/Element/Image.php b/library/ZendAfi/Form/Element/Image.php
index ddb9bad5850..3deb6130c3b 100644
--- a/library/ZendAfi/Form/Element/Image.php
+++ b/library/ZendAfi/Form/Element/Image.php
@@ -27,11 +27,10 @@ class ZendAfi_Form_Element_Image extends ZendAfi_Form_Element_File {
 		parent::__construct($spec, $options);
 
 		$decorators = $this->_decorators;
-		$this->_decorators = array('Image' => new ZendAfi_Form_Decorator_Image());
+		$this->_decorators = ['Image' => new ZendAfi_Form_Decorator_Image()];
 
-		foreach ($decorators as $name => $value) {
+		foreach ($decorators as $name => $value)
 			$this->_decorators[$name] = $value;
-		}
 
 		$this->setAutoInsertNotEmptyValidator(false);
 	}
diff --git a/tests/library/ZendAfi/Form/Element/FileTest.php b/tests/library/ZendAfi/Form/Element/FileTest.php
index 2109674271c..5f3284ef5a9 100644
--- a/tests/library/ZendAfi/Form/Element/FileTest.php
+++ b/tests/library/ZendAfi/Form/Element/FileTest.php
@@ -19,10 +19,23 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
  */
 class ZendAfi_Form_Element_FileTest extends PHPUnit_Framework_TestCase {
+	protected $_element;
+
+	public function setUp() {
+		$this->_element = new ZendAfi_Form_Element_File('fichier');
+	}
+
+
 	/** @test */
 	public function instanceShouldHaveFileDecorartor() {
-		$element = new ZendAfi_Form_Element_File('fichier');
 		$this->assertInstanceOf('ZendAfi_Form_Decorator_File',
-																								$element->getDecorator('File'));
+														$this->_element->getDecorator('File'));
+	}
+
+
+	/** @test */
+	public function instanceShouldHaveLabelDecorator() {
+		$this->assertInstanceOf('Zend_Form_Decorator_Label',
+			                      $this->_element->getDecorator('label'));
 	}
 }
\ No newline at end of file
-- 
GitLab