From b4dfd48928c04f7256e4d6e4e2a316310935d28e Mon Sep 17 00:00:00 2001
From: Patrick Seeger <pseeger@ccwn.org>
Date: Tue, 25 Jun 2013 15:14:49 +0200
Subject: Upload von Hintergrundbildern

---
 .gitignore                                       |  3 ++-
 protected/components/VereinImageFunctions.php    | 28 +++++++++++++++++++-----
 protected/config/maincfg.php                     | 20 +++++++++--------
 protected/controllers/VereinController.php       | 11 ++++++++++
 protected/extensions/SimpleImage/SimpleImage.php | 10 +++++++++
 protected/models/Verein.php                      |  8 +++++--
 protected/views/verein/_form.php                 |  7 +++++-
 7 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/.gitignore b/.gitignore
index e85b464..055990e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 /assets/*
-/images/uploaded/*
\ No newline at end of file
+/images/uploaded/*
+/protected/runtime/application.log
\ No newline at end of file
diff --git a/protected/components/VereinImageFunctions.php b/protected/components/VereinImageFunctions.php
index 477e767..5f7bd4e 100644
--- a/protected/components/VereinImageFunctions.php
+++ b/protected/components/VereinImageFunctions.php
@@ -5,12 +5,24 @@ class VereinImageFunctions {
 	public static function resizeAndSave(CUploadedFile $uploadedImage, $vereinSlug) {
 		$filename = VereinImageFunctions::save($uploadedImage, $vereinSlug);
 		if (null !== $filename) {
-		    VereinImageFunctions::resize($filename);
+		    VereinImageFunctions::resizeLogo($filename);
 		}
 		
 		return $filename;
 	}
 	
+	public static function resizeAndSaveBG(CUploadedFile $uploadedImage, $vereinSlug) {
+		$filename = VereinImageFunctions::save($uploadedImage, $vereinSlug.".BG");
+		if (null !== $filename) {
+			//VereinImageFunctions::resize($filename,Yii::app()->params['verein_background_width'], Yii::app()->params['verein_background_height']);
+			$image = Yii::app()->simpleImage->load($filename);
+			$image->blend(Yii::app()->params['verein_background_width'], Yii::app()->params['verein_background_height']);
+			$image->save($filename);
+		}
+	
+		return $filename;
+	}
+	
 	public static function save(CUploadedFile $uploadedImage, $vereinSlug) {
 		$filename = VereinImageFunctions::createFilename($uploadedImage->getName(), $vereinSlug);
 		if ($uploadedImage->saveAs($filename)) {
@@ -19,16 +31,20 @@ class VereinImageFunctions {
 			return null;
 		}
 	}
+	public static function resizeLogo($filename) {
+		VereinImageFunctions::resize($filename,Yii::app()->params['logo_width'], Yii::app()->params['logo_height']);
+		
+	}
 	
-	public static function resize($filename) {
+	public static function resize($filename, $width, $height) {
 		$image = Yii::app()->simpleImage->load($filename);
 		
-		if ($image->getWidth() > Yii::app()->params['logo_width']) {
-		    $image->resizeToWidth(Yii::app()->params['logo_width']);
+		if ($image->getWidth() > $width) {
+		    $image->resizeToWidth($width);
 		}
 		
-		if ($image->getHeight() > Yii::app()->params['logo_height']) {
-		    $image->resizeToHeight(Yii::app()->params['logo_height']);
+		if ($image->getHeight() > $height) {
+		    $image->resizeToHeight($width);
 		}
 		$image->save($filename);
 	}
diff --git a/protected/config/maincfg.php b/protected/config/maincfg.php
index b9cfd08..2327468 100644
--- a/protected/config/maincfg.php
+++ b/protected/config/maincfg.php
@@ -38,9 +38,9 @@ return array(
 				'userIdColumn' => 'id', // the column name of the primary key for the user
 				'userNameColumn' => 'username', // the column name of the primary key for the user
 		),
-		'cms' => array(
-					'languages' => array('de','en'), // place all valid languages here
-					'layout' => '//layouts/column2',
+		'cms' => array(
+					'languages' => array('de','en'), // place all valid languages here
+					'layout' => '//layouts/column2',
 			),
 	),
 
@@ -56,12 +56,12 @@ return array(
 		'simpleImage'=>array(
         		'class' => 'application.extensions.SimpleImage.CSimpleImage',
 		),
-        'mail' => array(
-                'class' => 'application.extensions.yii-mail.YiiMail',
-                'transportType' => 'php',
-                'viewPath' => 'application.views.mail',
-                'logging' => true,
-                'dryRun' => false
+        'mail' => array(
+                'class' => 'application.extensions.yii-mail.YiiMail',
+                'transportType' => 'php',
+                'viewPath' => 'application.views.mail',
+                'logging' => true,
+                'dryRun' => false
         ),
 		// uncomment the following to enable URLs in path-format
 		
@@ -124,5 +124,7 @@ return array(
 		'end_time'=>'23:00',
 		'logo_width'=>150,
 		'logo_height'=>150,
+		'verein_background_width'=>850,
+		'verein_background_height'=>200,
 	),
 );
diff --git a/protected/controllers/VereinController.php b/protected/controllers/VereinController.php
index aad8776..7d2da81 100644
--- a/protected/controllers/VereinController.php
+++ b/protected/controllers/VereinController.php
@@ -62,9 +62,11 @@ class VereinController extends Controller
 		{
 			$model->attributes=$_POST['Verein'];
 			$model->uploadedImage = CUploadedFile::getInstance($model, 'uploadedImage');
+			$model->uploadedHintergrund = CUploadedFile::getInstance($model, 'uploadedHintergrund');
 			if($model->save()) {
 				Yii::trace("Verein gespeichert", "admin.astaf.verein");
 				Yii::trace("Bild: ".$model->uploadedImage, "admin.astaf.verein");
+				Yii::trace("Hintergrund: ".$model->uploadedHintergrund, "admin.astaf.verein");
 			    $this->saveImage($model);
 				$this->redirect(array('view','id'=>$model->id));
 			}
@@ -95,6 +97,7 @@ class VereinController extends Controller
 		{
 			$model->attributes=$_POST['Verein'];
 			$model->uploadedImage = CUploadedFile::getInstance($model, 'uploadedImage');
+			$model->uploadedHintergrund = CUploadedFile::getInstance($model, 'uploadedHintergrund');
 			if($model->save()) {
 			    $this->saveImage($model);
 				$this->redirect(array('view','id'=>$model->id));
@@ -119,6 +122,14 @@ class VereinController extends Controller
 	        $model->bild = "/".$filename;
 	        $model->save();
 	    }
+	    if (null !== $model->uploadedHintergrund)  {
+	    	$filename = VereinImageFunctions::resizeAndSaveBG($model->uploadedHintergrund, $model->slug);
+	    	if (null === $filename) {
+	    		throw new CHttpException(500, 'Error while saving image.');
+	    	}
+	    	$model->hintergrund = "/".$filename;
+	    	$model->save();
+	    }
 	}
 	
 	/**
diff --git a/protected/extensions/SimpleImage/SimpleImage.php b/protected/extensions/SimpleImage/SimpleImage.php
index bbccfa0..54c4ef8 100644
--- a/protected/extensions/SimpleImage/SimpleImage.php
+++ b/protected/extensions/SimpleImage/SimpleImage.php
@@ -147,5 +147,15 @@ class SimpleImage {
         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
         $this->image = $new_image;
     }
+    
+    function blend($width, $height) {
+   		$this->resizeToWidth($width);
+    	$new_image = imagecreatetruecolor($width, $height);
+    	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $width, $height);
+    	imagealphablending($new_image,true);
+    	imagefilledrectangle($new_image, 0, 0,  $width, $height, imagecolorallocatealpha($new_image, 255, 255, 255, 30));
+    	$this->image = $new_image;
+    	
+    }
 }
 ?>
\ No newline at end of file
diff --git a/protected/models/Verein.php b/protected/models/Verein.php
index 8556f48..9274111 100644
--- a/protected/models/Verein.php
+++ b/protected/models/Verein.php
@@ -14,10 +14,12 @@
  * @property string $beschreibung
  * @property integer $standort_id
  * @property integer $published
+ * @property string $hintergrund
  */
 class Verein extends CActiveRecord
 {
 	public $uploadedImage;
+	public $uploadedHintergrund;
 	/**
 	 * Returns the static model of the specified AR class.
 	 * @param string $className active record class name.
@@ -47,14 +49,14 @@ class Verein extends CActiveRecord
 			array("email, name", "required"),
 			array('standort_id, published', 'numerical', 'integerOnly'=>true),
 			array('name, url', 'length', 'max'=>255),
-			array('bild, email, slug', 'length', 'max'=>100),
+			array('bild, email, slug, hintergrund', 'length', 'max'=>100),
 			array("url", "url", "allowEmpty"=>true, "message"=>"Die eingebene URL ist ungültig."),
 			array("email", "email", "message"=>"Die eingebene eMail-Adresse ist ungültig."),
 			array("uploadedImage", "file", "types"=>"jpg, gif, png", "allowEmpty"=>true, "wrongType"=>'Die Datei "{file}" konnte nicht hochgeladen werden. Es sind nur Dateien mit den folgenden Endungen erlaubt: {extensions}.'),
 			array('kontaktdaten, beschreibung', 'safe'),
 			// The following rule is used by search().
 			// Please remove those attributes that should not be searched.
-			array('id, name, url, bild, email, slug, kontaktdaten, beschreibung, standort_id, published', 'safe', 'on'=>'search'),
+			array('id, name, url, email, slug, kontaktdaten, beschreibung, standort_id, published', 'safe', 'on'=>'search'),
 		);
 	}
 
@@ -86,6 +88,7 @@ class Verein extends CActiveRecord
 			'beschreibung' => 'Beschreibung',
 			'standort_id' => 'Standort',
 			'standort' => 'Standort',
+			'hintergrund' => 'Hintergrundbild',
 			'published' => 'Öffentlich',
 		);
 	}
@@ -105,6 +108,7 @@ class Verein extends CActiveRecord
 		$criteria->compare('name',$this->name,true);
 		$criteria->compare('url',$this->url,true);
 		$criteria->compare('bild',$this->bild,true);
+		$criteria->compare('hintergrund',$this->hintergrund,true);
 		$criteria->compare('email',$this->email,true);
 		$criteria->compare('slug',$this->slug);
 		$criteria->compare('kontaktdaten',$this->kontaktdaten,true);
diff --git a/protected/views/verein/_form.php b/protected/views/verein/_form.php
index 5b6bf46..6de5d5c 100644
--- a/protected/views/verein/_form.php
+++ b/protected/views/verein/_form.php
@@ -104,7 +104,12 @@
 		?>
 		<?php echo $form->error($model,'standort_id'); ?>
 	</div>
-
+	<div class="row">
+		<?php echo $form->labelEx($model,'hintergrund'); ?>
+		<?php echo CHtml::image($model->hintergrund,'height="80"'); ?><br/>
+		<?php echo $form->fileField($model,'uploadedHintergrund', array('size'=>60)); ?>
+		<?php echo $form->error($model,'hintergrund'); ?>
+	</div>
 	<div class="row">
 		<?php echo $form->labelEx($model,'published'); ?>
 		<?php
-- 
cgit v1.0-28-g1787

