Robert Marshall 10 gadi atpakaļ
vecāks
revīzija
84630c3249

+ 16 - 1
Controller/Blog.php

@@ -47,6 +47,10 @@ class Blog {
 				$post=new BlogPost($params['delete']);
 				$post->Delete();
 			}
+			if (isset($params['publish'])){
+				$post=new BlogPost($params['publish']);
+				$post->Publish();
+			}
 			header("location:/blog/manage");
 			return;
 		}
@@ -55,6 +59,17 @@ class Blog {
 		return new View("Blog/manage.view",array("posts"=>$posts));
 	}
 	
+	public function Edit($params) {
+		$post=new BlogPost($params[0]);
+		Breadcrumbs::Add("Edit", "");
+		if(isset($params['title']) && isset($params['draft'])){
+			$post->PostTitle=$params['title'];
+			$post->PostDraft=$params['draft'];
+			$post->Save();
+		}
+		return new View("Blog/create.view",array("post"=>$post));
+	}
+	
 	public function View($params){
 		if (!isset($params[0])){
 			header("location:/blog");
@@ -63,7 +78,7 @@ class Blog {
 		$post=new BlogPost($params[0]);
 		if (!$post->PostId)
 			return new View("E404/index.view");
-		Breadcrumbs::Add("View", "");
+		Breadcrumbs::Add(htmlspecialchars($post->PostTitle), "");
 		return new View("Blog/view.view",array("post"=>$post));
 	}
 }

+ 0 - 5
Controller/Home.php

@@ -3,9 +3,4 @@ class Home{
 	public function Index($params){
 		return new View("Home/index.view");
 	}
-	
-	public function Test($params) {
-		Breadcrumbs::Add("Test", "/home");
-		return new View("Home/index.view");		
-	}
 }

+ 1 - 3
Controller/Navigation/HomeNav.php

@@ -7,9 +7,7 @@ class HomeNav implements INavigationController{
 	}
 	
 	public function GetItems() {
-		return array(
-			new URI("Test","test")
-		);
+		
 	}
 
 	public function GetURI() {

+ 17 - 10
Model/BlogPost.php

@@ -1,15 +1,19 @@
 <?php
 class BlogPost extends DBObjectAutoCreate {
-	private $_uri;
+	const PREVIEW_LENGTH=1000;
 	
+	private $_uri;
+
 	public static function GetCount() {
-		self::SetupPDO();
-		return self::$PDO->query("SELECT COUNT(*) FROM blog_posts WHERE post_deleted=0")->fetchColumn();
+		//self::SetupPDO();
+		self::CreateTable("blog_posts");
+		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_deleted=0 ORDER BY post_timestamp DESC LIMIT ?,?");
+		//self::SetupPDO();
+		self::CreateTable("blog_posts");
+		$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();
@@ -53,23 +57,26 @@ class BlogPost extends DBObjectAutoCreate {
 	public function MakeUniqueURL(){
 		$baseUrl=Helper::MakeStringUrlSafe($this->PostTitle);
 		$prep=self::$PDO->prepare("SELECT COUNT(*) FROM blog_posts WHERE post_url=?");
-		$found=false;
+		$found=(int)$prep->fetchColumn()>0;;
 		$url=$baseUrl;
 		$count=0;
-		do {
+		while($found) {
 			$prep->execute(array($url));
 			$found=(int)$prep->fetchColumn()>0;
 			if ($found){
 				$count++;
 				$url=$baseUrl.'-'.$count;
 			}
-		} while ($found);
+		}
 		$this->PostUrl=$url;
 	}
 	
 	public function GetPostPreview() {
-		return $this->PostDraft;
-//		/return $this->PostContent;
+		$content=$this->PostContent;
+		$pTagPos=strpos($content,"</p>",self::PREVIEW_LENGTH);
+		$content=substr($content, 0, $pTagPos);
+		$content=Helper::CloseOpenTags($content);
+		return $content;
 	}
 	
 	public function GetURI() {

+ 12 - 1
Model/DBObjectAutoCreate.php

@@ -8,12 +8,23 @@
  * @author robert.marshall
  */
 class DBObjectAutoCreate extends DBObject {
-	public function __construct($table, $key, $id) {
+	private static $_run=false;
+	
+	protected static function CreateTable($table){
+		if (self::$_run)
+			return;
+		
 		$PDO=self::GetPDO();
 		$prep=$PDO->prepare("SHOW TABLES LIKE ?");
 		$prep->execute(array($table));
 		if ($prep->rowCount()<1)
 			DBScriptRunner::RunScript("create_$table.sql");
+		
+		self::$_run=true;
+	}
+
+	public function __construct($table, $key, $id) {
+		self::CreateTable($table);
 		parent::__construct($table, $key, $id);
 	}
 }

+ 11 - 15
View/Blog/create.view

@@ -1,20 +1,16 @@
 @Title{Blog}@
 @JavaScript{
-	function HideUploadFields(){
-		$("#fileDiv").hide();
-		$("#linkDiv").hide();
-	}
-
-	$(function(){
-		HideUploadFields();
-		$("input[name=type]").change(function(e){
-			var value=$(this).val();
-			HideUploadFields();
-			$("#"+value+"Div").show();
-		});
-	});
+$(function(){
+	var top=$("textarea").offset().top;
+	var height=$(document).height();
+	var marginOffset=$("#content").css("margin-bottom").replace("px","");
+	$("textarea").height(height-top-marginOffset);
+});
 }@
 @ButtonsRight{
+	<button onclick="window.location.href='/blog/manage'" title="Manage">
+		<img src="/images/library.svg" alt="Library" />
+	</button><br/>
 	<button type="submit" form="blogForm" title="Save">
 		<img src="/images/save.svg" alt="Save" />
 	</button>
@@ -22,8 +18,8 @@
 @Body{
 <h3>Title</h3>
 <form action="" method="post" id="blogForm">
-	<input name="title" style="width:100%" value="<?= $post->PostTitle ?>" />
+	<input name="title" style="width:100%" value="<?= htmlspecialchars($post->PostTitle) ?>" />
 	<h3>Content</h3>
-	<textarea name="draft" style="width:100%"><?= $post->PostDraft ?></textarea>
+	<textarea name="draft" style="width:100%; height:200px" ><?= htmlspecialchars($post->PostDraft) ?></textarea>
 </form>
 }@

+ 3 - 2
View/Blog/index.view

@@ -18,7 +18,8 @@
 }@
 @Body{
 	<?php foreach ($posts as $post){ ?>
-		<h3><?=$post->PostTitle?></h3>
-		<p><?=$post->GetPostPreview()?></p>
+		<h2><a href="/blog/view/<?=$post->PostUrl?>"><?=$post->PostTitle?></a></h2>
+		<?=$post->GetPostPreview()?>
+		<p><a href="/blog/view/<?=$post->PostUrl?>">Read more...</a></p>
 	<?php } ?>
 }@

+ 24 - 2
View/Blog/manage.view

@@ -4,8 +4,16 @@
 		<img src="/images/add.svg" alt="Add" />
 	</button>
 }@
+@JavaScript{
+	$(function(){
+		$("button.jsNav").click(function(e){
+			Navigate($(this).val());
+			return false;
+		});
+	});
+}@
 @Body{
-<form action="" method="post">
+<form action="" method="post" id="manageForm">
 	<input type="hidden" name="formSubmitted" value="yes" />
 	<table>
 		<tr>
@@ -19,7 +27,21 @@
 			<td><?=$post->PostId?></td>
 			<td><?=$post->PostTitle?></td>
 			<td><?=$post->PostTimestamp?></td>
-			<td><button value="<?=$post->PostId?>" name="delete" title="Delete"><img src="/images/delete.svg" alt="Delete" /></button></td>
+			<td>
+				<button value="<?=$post->PostId?>" name="delete" title="Delete">
+					<img src="/images/delete.svg" alt="Delete" />
+				</button>
+				<button value="/blog/edit/<?=$post->PostId?>" name="edit" title="Edit" class="jsNav">
+					<img src="/images/edit.svg" alt="Edit" />
+				</button>
+				<?php if ($post->PostContent != $post->PostDraft) {?>
+					<button value="<?=$post->PostId?>" name="publish" title="Publish">
+						<img src="/images/send.svg" alt="Publish" />
+					</button>
+				<?php } else {?>
+					<img src="/images/done.svg" alt="Done" />
+				<?php } ?>
+			</td>
 		</tr>
 		<?php } ?>
 	</table>

+ 1 - 7
View/Blog/view.view

@@ -1,10 +1,4 @@
 @Title{Blog}@
-@ButtonsRight{
-	<button onclick="window.location.href='/blog/edit'">
-		E
-	</button>
-}@
 @Body{
-	<h3><?=$post->PostTitle?></h3>
-	<p><?=$post->GetPostPreview()?></p>
+	<p><?= $post->PostContent?></p>
 }@

+ 21 - 0
base/Helper.php

@@ -117,4 +117,25 @@ class Helper{
 
 		return $success;
 	}
+	
+	public static function CloseOpenTags($html) {
+		preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
+		$openedtags=$result[1];
+		preg_match_all('#</([a-z]+)>#iU', $html, $result);
+		$closedtags=$result[1];
+		$len_opened=count($openedtags);
+		if (count($closedtags)==$len_opened) {
+			return $html;
+		}
+		$openedtags=array_reverse($openedtags);
+		for ($i=0; $i<$len_opened; $i++) {
+			if (!in_array($openedtags[$i], $closedtags)) {
+				$html .= '</'.$openedtags[$i].'>';
+			} else {
+				unset($closedtags[array_search($openedtags[$i], $closedtags)]);
+			}
+		}
+		return $html;
+	}
+
 }

+ 6 - 4
images/add.svg

@@ -3,8 +3,10 @@
 <!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="Capa_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="#1E88E5" d="M4,6H2v14c0,1.1,0.9,2,2,2h14v-2H4V6z"/>
-<path fill="#42A5F5" d="M22,4v12c0,1.1-0.9,2-2,2H8c-1.1,0-2-0.9-2-2V4c0-1.1,0.9-2,2-2h12C21.1,2,22,2.9,22,4z"/>
-<path fill="#1E88E5" d="M22,4v12c0,1.1-0.9,2-2,2h-6V2h6C21.1,2,22,2.9,22,4z"/>
-<polygon fill="#FFFFFF" points="19,11 15,11 15,15 13,15 13,11 9,11 9,9 13,9 13,5 15,5 15,9 19,9 "/>
+<path fill="#CFD8DC" d="M19.967,7.972v12c0,1.1-0.9,2-2,2H5.958c-1.1,0-1.99-0.9-1.99-2l0.01-16c0-1.1,0.89-2,1.99-2h8L19.967,7.972
+	z"/>
+<path fill="none" d="M0,0h24v24H0V0z"/>
+<polygon fill="#455A64" points="16,16 13,16 13,19 11,19 11,16 8,16 8,14 11,14 11,11 13,11 13,14 16,14 "/>
+<polygon opacity="0.5" fill="#90A4AE" enable-background="new    " points="19.967,8.067 19.967,9.067 13.967,9.067 13.967,2.067 
+	"/>
 </svg>

+ 9 - 0
images/done.svg

@@ -0,0 +1,9 @@
+<?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="#81C784" d="M9,19L21,7l-1.41-1.41L9,16.17V19z"/>
+<path fill="#4CAF50" d="M9,16.17L4.83,12l-1.42,1.41L9,19V16.17z"/>
+</svg>

+ 8 - 0
images/send.svg

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.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="Capa_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">
+<polygon fill="#1E88E5" points="23,12 2.01,21 2,14 17,12 2,10 2.01,3 "/>
+<path fill="none" d="M0,0h24v24H0V0z"/>
+</svg>

+ 13 - 2
javascript.js

@@ -1,3 +1,7 @@
+function Navigate(url){
+	window.location=url;
+}
+
 $(function(){
 	var panels=$("nav > dl > .sub-pages");
 	var goIndicators=$("nav > dl > dt .go");
@@ -8,7 +12,7 @@ $(function(){
 	$("nav > dl > dt").click(function(e){
 		var next=$(this).next();
 		if (!next.hasClass("sub-pages") || next.is(":visible")){
-			window.location=$(this).children("a").attr("href");
+			Navigate($(this).children("a").attr("href"));
 			return;
 		}
 		panels.slideUp();
@@ -18,14 +22,21 @@ $(function(){
 		$(this).children(".go").show();
 		$(this).children(".expand").hide();
 	});
+	
+	$("nav > dl > dt").each(function(){
+		if ($(this).next().hasClass("sub-pages"))
+			$(this).attr("title", "Click once to expand, click again to go");
+	});
 
 	$("nav dd").click(function(e){
-		window.location=$(this).children("a").attr("href");
+		Navigate($(this).children("a").attr("href"));
 	});
 	
 	$("nav a").click(function(e){
 		$(this).parent().click();
 		return false;
 	});
+	
+	$("#content").css("margin-bottom",$("#buttons").height());
 });
 

+ 10 - 1
style.css

@@ -1,6 +1,6 @@
 @import url(http://fonts.googleapis.com/css?family=Roboto);
 
-html,body,h1,h2 {
+html,body,h1,h2, button {
 	padding:0;
 	margin:0;
 }
@@ -15,6 +15,10 @@ body{
 	background:#fafafa;
 }
 
+a {
+	color:#f44336;
+}
+
 nav img{
 	vertical-align: middle;
 	margin-right: 5px;
@@ -35,6 +39,10 @@ nav a{
 	text-decoration: none;
 }
 
+h2 a{
+	color:inherit;
+}
+
 h3:first-child{
 	margin-top: 0px;
 }
@@ -106,6 +114,7 @@ button:hover{
 
 #content{
 	padding:20px;
+	margin-bottom:60px;
 }
 
 #buttons{

+ 7 - 1
template.php

@@ -11,7 +11,13 @@ function FormatURI(URI $uri, $base=""){
 <!DOCTYPE html>
 <html>
 	<head>
-		<title>{@Title} | Robware</title>
+		<title>
+			{@Title}<?php
+				$__crumbs=Breadcrumbs::GetAll();
+				foreach ($__crumbs as $crumb)
+					echo ' &gt; ',$crumb['text'];
+			?> | Robware
+		</title>
 		<meta charset="UTF-8">
 		<meta name="viewport" content="width=device-width, initial-scale=1.0">
 		<link href="/style.css" rel="stylesheet" type="text/css" />