Robert Marshall há 9 anos atrás
pai
commit
e14154a1cc

+ 6 - 3
Controller/Blog.php

@@ -6,12 +6,14 @@ class Blog {
 		if (!isset($params['page']) || $params['page']<1)
 			$params['page']=1;
 		
+		$repo=new BlogPostRepository();
+		
 		$count=ApplicationSettings::GetSetting("blog", "posts_per_page");
 		$offset=($params['page']-1)*$count;
-		$total=BlogPost::GetCount();
+		$total=$repo->GetCount();
 		$maxPages=$total/$count;
 		
-		$posts=BlogPost::GetLatest($count,$offset);
+		$posts=$repo->GetLatest($count,$offset);
 		
 		$vars=array(
 			"posts"=>$posts,
@@ -55,7 +57,8 @@ class Blog {
 			return;
 		}
 		
-		$posts=BlogPost::GetAll();
+		$repo=new BlogPostRepository();
+		$posts=$repo->GetAll();
 		return new View("Blog/manage.view",array("posts"=>$posts));
 	}
 	

+ 9 - 4
Controller/Gallery.php

@@ -1,7 +1,8 @@
 <?php
 class Gallery {
 	public function Index($params) {
-		$albums=Album::GetAlbums();
+		$repo=new AlbumRespository();
+		$albums=$repo->GetAlbums();
 		return new View("Gallery/index.view",array("albums"=>$albums));
 	}
 	
@@ -12,12 +13,15 @@ class Gallery {
 	}
 	
 	public function Manage($params) {
+		$albumRepo=new AlbumRespository();
+		$imageRepo=new ImageRepository();
+		
 		$vars=array();
 		Breadcrumbs::Add("Manage", "");
-		$vars["albums"]=Album::GetAlbums(true,true);
+		$vars["albums"]=$albumRepo->GetAlbums(true,true);
 		if (isset($params['errors']))
 			$vars["errors"]=$params['errors'];
-		$vars['images']=Image::GetImagesByAlbum(0);
+		$vars['images']=$imageRepo->GetImagesByAlbum(0);
 		if(isset($params['image']))
 			$vars['image']=$params['image'];
 		else
@@ -83,8 +87,9 @@ class Gallery {
 	}
 	
 	public function JsonLoadAlbum($params) {
+		$repo=new ImageRepository();
 		$json='[';
-		$images=Image::GetImagesByAlbum($params[0]);
+		$images=$repo->GetImagesByAlbum($params[0]);
 		foreach ($images as $image)
 			$json.='{"ImageId":"'.$image->ImageId.'","ImageTitle":"'.$image->ImageTitle.'","Path":"'.$image->Path.'","ThumbnailPath":"'.$image->ThumbnailPath.'"},';
 		$json=trim($json,',');

+ 2 - 1
Controller/Home.php

@@ -1,6 +1,7 @@
 <?php
 class Home{
 	public function Index($params){
-		return new View("Home/index.view",array("post"=>BlogPost::GetLatest()[0]));
+		$repo=new BlogPostRepository();
+		return new View("Home/index.view",array("post"=>$repo->GetLatest()[0]));
 	}
 }

+ 2 - 1
Controller/Navigation/BlogNav.php

@@ -7,7 +7,8 @@ class BlogNav implements INavigationController{
 	}
 	
 	public function GetItems() {
-		$posts=BlogPost::GetLatest(5);
+		$repo=new BlogPostRepository();
+		$posts=$repo->GetLatest(5);
 		$links=array();
 		foreach ($posts as $post){
 			$title=$post->PostTitle;

+ 2 - 1
Controller/Navigation/GalleryNav.php

@@ -7,7 +7,8 @@ class GalleryNav implements INavigationController{
 	}
 	
 	public function GetItems() {
-		$albums=Album::GetAlbums();
+		$repo=new AlbumRespository();
+		$albums=$repo->GetAlbums();
 		$uris=array();
 		foreach ($albums as $a)
 			$uris[]=new URI($a->AlbumTitle,'view/'.$a->AlbumUrl);

+ 3 - 1
Controller/Weight.php

@@ -1,7 +1,9 @@
 <?php
 class Weight {
 	public function Index($params) {
-		return new View("Weight/index.view",array("readings"=>WeightReading::GetAll(Session::GetLoggedInUser()->UserId)));
+		$userId=Session::GetLoggedInUser()->UserId;
+		$repo=new WeightReadingRepository();
+		return new View("Weight/index.view",array("readings"=>$repo::GetAll($userId)));
 	}
 	
 	public function Add($params){

+ 14 - 0
Database/AlbumRespository.php

@@ -0,0 +1,14 @@
+<?php
+class AlbumRespository extends BaseRepository {
+	public function GetAlbums($includeHidden=false, $includeEmpty=false) {
+		$albums=array();
+		$hidden=$includeHidden?" OR album_hidden=1":"";
+		$albumIds=self::$PDO->query("SELECT album_id FROM albums WHERE album_deleted=0 AND album_hidden=0".$hidden);
+		while ($albumId=$albumIds->fetchColumn()){
+			$album=new Album($albumId);
+			if ($includeEmpty || count($album->Images)>0)
+				$albums[]=$album;
+		}
+		return $albums;
+	}
+}

+ 29 - 0
Database/BaseRepository.php

@@ -0,0 +1,29 @@
+<?php
+ApplicationSettings::RegisterDefaultSetting("database", "host", "localhost");
+ApplicationSettings::RegisterDefaultSetting("database", "database", "php-mvc");
+ApplicationSettings::RegisterDefaultSetting("database", "username", "root");
+ApplicationSettings::RegisterDefaultSetting("database", "password", "");
+
+class BaseRepository {
+	protected static $PDO=null;
+
+	protected static function SetupPDO(){
+		if (self::$PDO!=null)
+			return;
+		$host=ApplicationSettings::GetSetting("database", "host");
+		$db=ApplicationSettings::GetSetting("database", "database");
+		$username=ApplicationSettings::GetSetting("database", "username");
+		$password=ApplicationSettings::GetSetting("database", "password");
+		self::$PDO=new PDO("mysql:host=$host;dbname=$db",$username,$password);
+		self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
+	}
+	
+	public static function GetPDO(){
+		self::SetupPDO();
+		return self::$PDO;
+	}
+	
+	public function __construct() {
+		self::SetupPDO();
+	}
+}

+ 31 - 0
Database/BlogPostRepository.php

@@ -0,0 +1,31 @@
+<?php
+class BlogPostRepository extends BaseRepository {
+	public function __construct() {
+		parent::__construct();
+	}
+	
+	public function GetCount() {
+		return self::$PDO->query("SELECT COUNT(*) FROM blog_posts WHERE post_content<>'' AND post_deleted=0")->fetchColumn();
+	}
+	
+	public function GetLatest($count=1,$offset=0){
+		$prep=self::$PDO->prepare("SELECT post_id FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT ?,?");
+		$prep->bindValue(1,(int)$offset,PDO::PARAM_INT); // can't just use array in execute this time as execute array is treated like strings
+		$prep->bindValue(2,(int)$count,PDO::PARAM_INT);
+		$prep->execute();
+		$ids=$prep->fetchAll(PDO::FETCH_COLUMN);
+		$posts=array();
+		foreach ($ids as $id)
+			$posts[]=new BlogPost($id);
+		return $posts;
+	}
+	
+	public function GetAll(){
+		$posts=array();
+		self::SetupPDO();
+		$ids=self::$PDO->query("SELECT post_id FROM blog_posts WHERE post_deleted=0")->fetchAll(PDO::FETCH_COLUMN);
+		foreach ($ids as $id)
+			$posts[]=new BlogPost($id);
+		return $posts;
+	}
+}

+ 11 - 0
Database/ImageRepository.php

@@ -0,0 +1,11 @@
+<?php
+class ImageRepository extends BaseRepository {
+	public static function GetImagesByAlbum($albumId) {
+		$images=array();
+		$prep=self::$PDO->prepare("SELECT image_id FROM images WHERE album_id=? AND image_deleted=0");
+		$prep->execute(array($albumId));
+		while ($imageId=$prep->fetchColumn())
+			$images[]=new Image($imageId);
+		return $images;
+	}
+}

+ 11 - 0
Database/WeightReadingRepository.php

@@ -0,0 +1,11 @@
+<?php
+class WeightReadingRepository extends BaseRepository {
+	public static function GetAll($userId){
+		$readings=array();
+		self::SetupPDO();
+		$ids=self::$PDO->query("SELECT reading_id FROM weight_readings WHERE user_id=".(int)$userId)->fetchAll(PDO::FETCH_COLUMN);
+		foreach ($ids as $id)
+			$readings[]=new WeightReading($id);
+		return $readings;
+	}
+}

+ 3 - 13
Model/Album.php

@@ -2,25 +2,15 @@
 class Album extends DBObject {
 	public $Images=array();
 	
-	public static function GetAlbums($includeHidden=false, $includeEmpty=false) {
-		$albums=array();
-		$hidden=$includeHidden?" OR album_hidden=1":"";
-		$albumIds=self::$PDO->query("SELECT album_id FROM albums WHERE album_deleted=0 AND album_hidden=0".$hidden);
-		while ($albumId=$albumIds->fetchColumn()){
-			$album=new Album($albumId);
-			if ($includeEmpty || count($album->Images)>0)
-				$albums[]=$album;
-		}
-		return $albums;
-	}
-	
 	public function __construct($id=0,$forceUrl=false) {
+		$repo=new ImageRepository();
+		
 		$field="album_id";
 		if ($forceUrl || !is_numeric($id))
 			$field="album_url";
 		parent::__construct("albums", $field, $id);
 		
-		$this->Images=Image::GetImagesByAlbum($this->AlbumId);
+		$this->Images=$repo->GetImagesByAlbum($this->AlbumId);
 	}
 	
 	public function Save() {

+ 0 - 30
Model/BlogPost.php

@@ -2,38 +2,8 @@
 ApplicationSettings::RegisterDefaultSetting("blog", "preview_length", 1000);
 
 class BlogPost extends DBObject {
-	//const PREVIEW_LENGTH=1000;
-	
 	private $_uri;
 
-	public static function GetCount() {
-		//self::SetupPDO();
-		return self::$PDO->query("SELECT COUNT(*) FROM blog_posts WHERE post_content<>'' AND post_deleted=0")->fetchColumn();
-	}
-	
-	public static function GetLatest($count=1,$offset=0){
-		//self::SetupPDO();
-		$prep=self::$PDO->prepare("SELECT post_id FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT ?,?");
-		$prep->bindValue(1,(int)$offset,PDO::PARAM_INT); // can't just use array in execute this time as execute array is treated like strings
-		$prep->bindValue(2,(int)$count,PDO::PARAM_INT);
-		$prep->execute();
-		$ids=$prep->fetchAll(PDO::FETCH_COLUMN);
-		$posts=array();
-		foreach ($ids as $id)
-			$posts[]=new BlogPost($id);
-		return $posts;
-		//return new BlogPost($url);
-	}
-	
-	public static function GetAll(){
-		$posts=array();
-		self::SetupPDO();
-		$ids=self::$PDO->query("SELECT post_id FROM blog_posts WHERE post_deleted=0")->fetchAll(PDO::FETCH_COLUMN);
-		foreach ($ids as $id)
-			$posts[]=new BlogPost($id);
-		return $posts;
-	}
-
 	public function __construct($id=0) {
 		$field="post_id";
 		if (!is_numeric($id))

+ 10 - 31
Model/DBObject.php

@@ -1,12 +1,5 @@
 <?php
-ApplicationSettings::RegisterDefaultSetting("database", "host", "localhost");
-ApplicationSettings::RegisterDefaultSetting("database", "database", "php-mvc");
-ApplicationSettings::RegisterDefaultSetting("database", "username", "root");
-ApplicationSettings::RegisterDefaultSetting("database", "password", "");
-
-
 class DBObject implements ISavableObject{
-	protected static $PDO=null;
 	protected static $PREPARED_STATEMENTS=array();
 	
 	private static $_classFields=array();
@@ -15,22 +8,6 @@ class DBObject implements ISavableObject{
 	protected $_changedFields=array();
 	
 	private $_table,$_key,$_id;
-
-	protected static function SetupPDO(){
-		if (self::$PDO!=null)
-			return;
-		$host=ApplicationSettings::GetSetting("database", "host");
-		$db=ApplicationSettings::GetSetting("database", "database");
-		$username=ApplicationSettings::GetSetting("database", "username");
-		$password=ApplicationSettings::GetSetting("database", "password");
-		self::$PDO=new PDO("mysql:host=$host;dbname=$db",$username,$password);
-		self::$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
-	}
-	
-	public static function GetPDO(){
-		self::SetupPDO();
-		return self::$PDO;
-	}
 	
 	public static function VariableToDBField($variableName) {
 		$parts=preg_split('/(?=[A-Z])/', $variableName);
@@ -51,8 +28,6 @@ class DBObject implements ISavableObject{
 		$this->_key=$key;
 		$this->_id=$id;
 		
-		self::SetupPDO();
-		
 		$this->Load();
 	}
 	
@@ -74,17 +49,19 @@ class DBObject implements ISavableObject{
 	}
 	
 	public function Load(){
+		$PDO=BaseRepository::GetPDO();
+		
 		$class=get_class($this);
 		
 		if (!isset(self::$_classFields[$class])){
-			self::$_classFields[$class]=self::$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
+			self::$_classFields[$class]=$PDO->query("DESCRIBE `{$this->_table}`")->fetchAll(PDO::FETCH_COLUMN);
 		}
 		
 		$statementKey=$class.'_construct_'.$this->_key;
 		if (!isset(self::$PREPARED_STATEMENTS[$statementKey])){
 			$fields=implode(", ", self::$_classFields[$class]);
 			$sql="SELECT $fields FROM `{$this->_table}` WHERE `{$this->_key}`=?";
-			self::$PREPARED_STATEMENTS[$statementKey]=self::$PDO->prepare($sql);
+			self::$PREPARED_STATEMENTS[$statementKey]=$PDO->prepare($sql);
 		}
 		$prep=self::$PREPARED_STATEMENTS[$statementKey];
 		$prep->execute(array($this->_id));
@@ -100,6 +77,8 @@ class DBObject implements ISavableObject{
 	}
 	
 	public function Save() {
+		$PDO=BaseRepository::GetPDO();
+		
 		if (count($this->_changedFields)==0)
 			return;
 		
@@ -117,7 +96,7 @@ class DBObject implements ISavableObject{
 			$sql="INSERT INTO `{$this->_table}` SET ".implode(", ", $fields);
 		}
 		
-		$prep=self::$PDO->prepare($sql);
+		$prep=$PDO->prepare($sql);
 		$prep->execute($execData);
 		
 		//var_dump($prep->errorInfo());
@@ -127,9 +106,9 @@ class DBObject implements ISavableObject{
 		
 		if ($this->_id===0){ // If this is a new object we want to reload fromt he DB to make sure all fields are correct.
 			// In order to do so we need to find the value for the key we're using
-			$id=self::$PDO->lastInsertId();
-			$key=self::$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
-			$this->_id=self::$PDO->query("SELECT `{$this->_key}` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
+			$id=$PDO->lastInsertId();
+			$key=$PDO->query("SHOW INDEX FROM `{$this->_table}` WHERE Key_name='PRIMARY'")->fetch()['Column_name'];
+			$this->_id=$PDO->query("SELECT `{$this->_key}` FROM `{$this->_table}` WHERE `$key`=$id")->fetchColumn();
 			$this->Load();
 		}
 	}

+ 0 - 9
Model/Image.php

@@ -30,15 +30,6 @@ class Image extends DBObject {
 		}
 	}
 	
-	public static function GetImagesByAlbum($albumId) {
-		$images=array();
-		$prep=self::$PDO->prepare("SELECT image_id FROM images WHERE album_id=? AND image_deleted=0");
-		$prep->execute(array($albumId));
-		while ($imageId=$prep->fetchColumn())
-			$images[]=new Image($imageId);
-		return $images;
-	}
-	
 	public function __construct($id=0,$tempFile="") {
 		if (!is_numeric($id)){
 			$this->Filename=$id;

+ 1 - 10
Model/WeightReading.php

@@ -1,14 +1,5 @@
 <?php
-class WeightReading extends DBObject {
-	public static function GetAll($userId){
-		$readings=array();
-		self::SetupPDO();
-		$ids=self::$PDO->query("SELECT reading_id FROM weight_readings WHERE user_id=".(int)$userId)->fetchAll(PDO::FETCH_COLUMN);
-		foreach ($ids as $id)
-			$readings[]=new WeightReading($id);
-		return $readings;
-	}
-	
+class WeightReading extends DBObject {	
 	public function __construct($id=0) {
 		parent::__construct("weight_readings", "reading_id", $id);
 	}

+ 7 - 1
base/Application.php

@@ -5,8 +5,14 @@ spl_autoload_register(function($class){
 		include("Interfaces/$class.php");
 		return;
 	}
-	if (file_exists("Model/$class.php"))
+	
+	if (file_exists("Model/$class.php")){
 		include("Model/$class.php");
+		return;
+	}
+	
+	if (file_exists("Database/$class.php"))
+		include("Database/$class.php");
 });
 
 $files=glob("base/*.php");

+ 14 - 30
css/style.css

@@ -21,7 +21,7 @@ body {
   background: #fafafa;
 }
 a {
-  color: #00e676;
+  color: #c6ff00;
 }
 nav img {
   vertical-align: middle;
@@ -163,7 +163,7 @@ p:first-child {
   box-shadow: 0px 2px 3px 0px rgba(0, 0, 0, 0.25);
   vertical-align: middle;
   border: none;
-  background: #00e676;
+  background: #c6ff00;
   width: 40px;
   height: 40px;
   color: white;
@@ -263,39 +263,23 @@ p:first-child {
   width: 100%;
   float: left;
 }
-.row .col-lg-1 > .col {
+.row.col-lg-1 > .col,
+.row.col-md-1 > .col,
+.row.col-sm-1 > .col {
   width: 100%;
 }
-.row .col-lg-2 > .col {
+.row.col-lg-2 > .col,
+.row.col-md-2 > .col,
+.row.col-sm-2 > .col {
   width: 50%;
 }
-.row .col-lg-3 > .col {
+.row.col-lg-3 > .col,
+.row.col-md-3 > .col,
+.row.col-sm-3 > .col {
   width: 33.3333%;
 }
-.row .col-lg-4 > .col {
-  width: 25%;
-}
-.row .col-md-1 > .col {
-  width: 100%;
-}
-.row .col-md-2 > .col {
-  width: 50%;
-}
-.row .col-md-3 > .col {
-  width: 33.3333%;
-}
-.row .col-md-4 > .col {
-  width: 25%;
-}
-.row .col-sm-1 > .col {
-  width: 100%;
-}
-.row .col-sm-2 > .col {
-  width: 50%;
-}
-.row .col-sm-3 > .col {
-  width: 33.3333%;
-}
-.row .col-sm-4 > .col {
+.row.col-lg-4 > .col,
+.row.col-md-4 > .col,
+.row.col-sm-4 > .col {
   width: 25%;
 }

+ 1 - 1
less/colours.less

@@ -273,6 +273,6 @@
 
 @background:@Grey-50;
 @primary:@Red-500;
-@accent:@LimeGreen-A400;
+@accent:@Lime-A400;
 @control:@Grey-200;
 @control2:@Grey-300;

+ 16 - 40
less/style.less

@@ -310,51 +310,27 @@ p:first-child{
 		float:left;
 	}
 
-	.col-lg-1>.col{
-		width:100%;
-	}
-
-	.col-lg-2>.col{
-		width:50%;
-	}
-
-	.col-lg-3>.col{
-		width:33.3333%;
-	}
-
-	.col-lg-4>.col{
-		width:25%;
-	}
-
-	.col-md-1>.col{
-		width:100%;
-	}
-
-	.col-md-2>.col{
-		width:50%;
-	}
-
-	.col-md-3>.col{
-		width:33.3333%;
-	}
-
-	.col-md-4>.col{
-		width:25%;
-	}
-
-	.col-sm-1>.col{
-		width:100%;
+	&.col-lg-1, &.col-md-1, &.col-sm-1{
+		>.col{
+			width:100%;
+		}
 	}
 
-	.col-sm-2>.col{
-		width:50%;
+	&.col-lg-2, &.col-md-2, &.col-sm-2{
+		>.col{
+			width:50%;
+		}
 	}
 
-	.col-sm-3>.col{
-		width:33.3333%;
+	&.col-lg-3, &.col-md-3, &.col-sm-3{
+		>.col{
+			width:33.3333%;
+		}
 	}
 
-	.col-sm-4>.col{
-		width:25%;
+	&.col-lg-4, &.col-md-4, &.col-sm-4{
+		>.col{
+			width:25%;
+		}
 	}
 }