Robert Marshall 10 éve
szülő
commit
62ca2f5381
8 módosított fájl, 206 hozzáadás és 13 törlés
  1. 0 1
      Controller/Blog.php
  2. 33 2
      Controller/Gallery.php
  3. 1 0
      DB Scripts/create_images.sql
  4. 58 1
      Model/Image.php
  5. 60 5
      View/Gallery/manage.view
  6. 22 1
      base/Helper.php
  7. 10 0
      images/error.svg
  8. 22 3
      style.css

+ 0 - 1
Controller/Blog.php

@@ -60,7 +60,6 @@ class Blog {
 	}
 	
 	public function Edit($params) {
-		var_dump($params);
 		$post=new BlogPost($params[0]);
 		Breadcrumbs::Add("Edit", "");
 		if(isset($params['title']) && isset($params['draft'])){

+ 33 - 2
Controller/Gallery.php

@@ -12,9 +12,13 @@ class Gallery {
 	}
 	
 	public function Manage($params) {
+		$vars=array();
 		Breadcrumbs::Add("Manage", "");
-		$albums=Album::GetAlbums();
-		return new View("Gallery/manage.view",array("albums"=>$albums));
+		$vars["albums"]=Album::GetAlbums();
+		if (isset($params['errors']))
+			$vars["errors"]=$params['errors'];
+		$vars['images']=Image::GetImagesByAlbum(0);
+		return new View("Gallery/manage.view",$vars);
 	}
 	
 	public function CreateAlbum($params) {
@@ -32,4 +36,31 @@ class Gallery {
 		}
 		return new View("Gallery/create_album.view",array("album"=>$album));
 	}
+	
+	public function Upload($params) {
+		$errors=array();
+		$filename=$_FILES['imageFile']['name'];
+		$tempFile=$_FILES['imageFile']['tmp_name'];
+		if ($params['imageTitle']=="")
+			$errors[]="The image doesn't have a title";
+		if ($_FILES['imageFile']['error'])
+			$errors[]=Helper::FileUploadErrorToMessage($_FILES['imageFile']['error']);
+		if ($filename=="")
+			$errors[]="A file was not selected";
+		if (count($errors)==0) {
+			if(!Image::IsValidType(pathinfo($filename,PATHINFO_EXTENSION)))
+				$errors[]="File is of an invalid type";
+			if (getimagesize($tempFile)===false)
+				$errors[]="File is not an image";
+		}
+		if (count($errors)==0){
+			$image=new Image($filename,$tempFile);
+			$image->ImageTitle=$params['imageTitle'];
+			$image->ImageDescription=$params['imageDesc'];
+			$image->Save();
+			header("location: /gallery/manage");
+			return;
+		}
+		return $this->Manage(array("errors"=>$errors));
+	}
 }

+ 1 - 0
DB Scripts/create_images.sql

@@ -1,6 +1,7 @@
 CREATE TABLE `robware-test`.`images` (
   `image_id` INT NOT NULL AUTO_INCREMENT,
   `album_id` INT NULL DEFAULT 0,
+  `image_filename` VARCHAR(255) NOT NULL,
   `image_title` VARCHAR(255) NULL,
   `image_description` TEXT NULL,
   `image_timestamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,

+ 58 - 1
Model/Image.php

@@ -1,5 +1,33 @@
 <?php
+ApplicationSettings::RegisterDefaultSetting("images", "upload_location", "images/uploads");
+ApplicationSettings::RegisterDefaultSetting("images", "thumbnail_width", "480");
+ApplicationSettings::RegisterDefaultSetting("images", "thumbnail_height", "360");
+
+
 class Image extends DBObjectAutoCreate {
+	public $Path,$ThumbnailPath;
+	private $Filename,$TempFile;
+	
+	public static function IsValidType($ext){
+		// use a switch because it's quicker than a loop
+		switch (strtolower($ext)){
+			case "bmp":
+			case "dib":
+			case "jpeg":
+			case "jpg":
+			case "jpe":
+			case "jfif":
+			case "gif":
+			case "tif":
+			case "tiff":
+			case "png":
+			case "ico":
+				return true;
+			default:
+				return false;
+		}
+	}
+	
 	public static function GetImagesByAlbum($albumId) {
 		self::CreateTable("images");
 		$images=array();
@@ -7,9 +35,38 @@ class Image extends DBObjectAutoCreate {
 		$prep->execute(array($albumId));
 		while ($imageId=$prep->fetchColumn())
 			$images[]=new Image($imageId);
+		return $images;
 	}
 	
-	public function __construct($id) {
+	public function __construct($id=0,$tempFile="") {
+		if (!is_numeric($id)){
+			$this->Filename=$id;
+			$this->TempFile=$tempFile;
+			$id=0;
+		}
 		parent::__construct("images", "image_id", $id);
+		
+		if ($this->ImageId){
+			$dir=ApplicationSettings::GetSetting("images", "upload_location");
+			$info=pathinfo($this->ImageFilename);
+			$this->Path=$dir.'/'.$this->ImageFilename;
+			$this->ThumbnailPath=$dir.'/'.$info['filename'].'.thumbnail.'.$info['extension'];
+		}
+	}
+	
+	private function MakeFileName() {
+		return $this->ImageId."_".Helper::MakeStringUrlSafe($this->Filename);
+	}
+	
+	public function Save() {
+		$newSave=!$this->ImageId;
+		parent::Save();
+		if ($newSave && $this->ImageId){
+			$this->ImageFilename=$this->MakeFileName();
+			$imageDir=ApplicationSettings::GetSetting("images", "upload_location");
+			if(!move_uploaded_file($this->TempFile, $imageDir.'/'.$this->ImageFilename))
+				throw new Exception("Unable to move uploaded file: ".$imageDir.'/'.$this->ImageFilename.', '.$this->TempFile);
+			parent::Save();
+		}
 	}
 }

+ 60 - 5
View/Gallery/manage.view

@@ -10,27 +10,42 @@
 		margin-bottom:10px;
 		position:fixed;
 		width:100%;
+		box-shadow: 0px 4px 5px -2px rgba(50, 50, 50, 0.8);
 	}
 
 	#tabs a{
 		display:inline-block;
-		color:#ffccbc;
+		color:#ffcdd2;
 		padding:15px 30px;
 		text-decoration:none;
+		text-transform: uppercase;
 	}
 	
 	#tabs a.active{
 		color:#fff;
 		border-bottom:3px solid #fff;
 	}
+	
+	.image{
+		width:25%;
+		display:inline-block;
+		vertical-align:top;
+		text-align:center;
+	}
+	
+	.image>div{
+		margin:5px;
+		padding:5px;
+		background:#e0e0e0;
+	}
 }@
 @CSSSmall{
 	@media(orientation:portrait){
-		td:nth-child(3), th:nth-child(3){
+		#albums td:nth-child(3), #albums th:nth-child(3){
 			display:none;
 		}
 	}
-	td:first-child, th:first-child{
+	#albums td:first-child, #albums th:first-child{
 		display:none;
 	}
 	
@@ -38,7 +53,7 @@
 		width:100%;
 	}
 	
-	td:last-child,th:last-child,td:nth-child(3), th:nth-child(3){
+	#albums td:last-child, #albums th:last-child, #albums td:nth-child(3), #albums th:nth-child(3){
 		width:1px;
 	}
 }@
@@ -67,7 +82,47 @@
 </div>
 <div id="tabs-content" style="margin-top:55px">
 	<div id="images">
-			asdasd
+		<?php if (isset($errors) && count($errors)>0) {
+			echo '<div class="errors">The following errors occured with the image submission:<ul>';
+			foreach ($errors as $error)
+				echo '<li>',$error,'</li>';
+			echo '</ul>Please rectify them and try again.</div>';
+		} ?>
+		<h3>New Image</h3>
+		<form action="/gallery/upload/" method="post" enctype="multipart/form-data">
+			<table>
+				<tr>
+					<td><label for="imageTitle">Image title:</label></td>
+					<td><input type="text" name="imageTitle" id="imageTitle" /></td>
+				</tr>
+				<tr>
+					<td><label for="imageDesc">Image description</label></td>
+					<td><textarea name="imageDesc" id="imageDesc"></textarea></td>
+				</tr>
+				<tr>
+					<td><label for="imageFile">Select image:</label></td>
+					<td><input type="file" name="imageFile" id="imageFile"></td>
+				</tr>
+				<tr>
+					<td colspan="2" align="right">
+						<button type="submit" title="Upload" name="imageUpload">
+							<img src="/images/send.svg" alt="Upload" />
+						</button>
+					</td>
+				</tr>
+			</table>
+		</form>
+		<div>
+			<?php
+			foreach ($images as $image)
+				echo '<div class="image">',
+						'<div>',
+							'<img src="/',$image->/*Thumbnail*/Path,'" alt="',$image->ImageTitle,'" />',
+							'<span>',$image->ImageTitle,'</span>',
+						'</div>',
+					'</div>';
+			?>
+		</div>
 	</div>
 	<div id="albums">
 		<form action="" method="post" id="manageForm">

+ 22 - 1
base/Helper.php

@@ -73,7 +73,7 @@ class Helper{
 	
 	public static function MakeStringUrlSafe($string){
 		$string=strtolower($string);
-		$string=preg_replace('/[^a-zA-Z0-9]+/', '-', $string);
+		$string=preg_replace('/[^a-zA-Z0-9\.]+/', '-', $string);
 		return $string;
 	}
 	
@@ -157,4 +157,25 @@ class Helper{
 		return $html;
 	}
 
+	public static function FileUploadErrorToMessage($errorNo){
+		switch($errorNo){
+			case UPLOAD_ERR_OK:
+				return "Success";
+			case UPLOAD_ERR_INI_SIZE:
+			case UPLOAD_ERR_FORM_SIZE:
+				return "Exceeded file size limit";
+			case UPLOAD_ERR_PARTIAL:
+				return "File not fully uploaded";
+			case UPLOAD_ERR_NO_FILE:
+				return "No file uploaded";
+			case UPLOAD_ERR_NO_TMP_DIR:
+				return "No temp dir";
+			case UPLOAD_ERR_CANT_WRITE:
+				return "Failed to write file to disc";
+			case UPLOAD_ERR_EXTENSION:
+				return "Upload stopped by PHP extension";
+			default:
+				return "Uknown error";
+		}
+	}
 }

+ 10 - 0
images/error.svg

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 15.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
+<path fill="none" d="M0,0h24v24H0V0z"/>
+<path fill="#E57373" d="M12,2C6.48,2,2,6.48,2,12c0,5.52,4.48,10,10,10c5.52,0,10-4.48,10-10C22,6.48,17.52,2,12,2z"/>
+<path fill="#F44336" d="M12,4c-4.417,0-8,3.583-8,8c0,4.414,3.583,8,8,8c4.414,0,8-3.586,8-8C20,7.583,16.414,4,12,4z"/>
+<path fill="#CFD8DC" d="M13,17h-2v-2h2V17z M13,13h-2V7h2V13z"/>
+</svg>

+ 22 - 3
style.css

@@ -60,12 +60,16 @@ h3{
 	margin-bottom: 10px;
 }
 
-form button{
+table input[type=text], table textarea{
+	width: 100%;
+}
+
+form button, form input[type=submit]{
 	border:none;
 	background:none;
 }
 
-form button:hover{
+form button:hover, form input[type=submit]:hover{
 	background: #EEE;
 }
 
@@ -142,7 +146,8 @@ td, th{
 	padding-left: 280px;
 	left:0;
 	right:0;
-	box-shadow: 0 5px 10px rgba(249,249,249, 0.9);
+	/*box-shadow: 0 5px 10px rgba(249,249,249, 0.9);*/
+	box-shadow: 0px 4px 5px -2px rgba(50, 50, 50, 0.8);
 }
 
 #main-header .header>*{
@@ -274,4 +279,18 @@ td, th{
 	left:0;
 	z-index:100;
 	display:none;
+}
+
+.errors{
+	background: url(/images/error.svg) no-repeat scroll 10px 10px #424242;
+	margin:-20px;
+	margin-bottom: 20px;
+	padding:10px;
+	padding-left:40px;
+	color:#fff;
+}
+
+.errors ul{
+	margin: 0;
+	list-style: square;
 }