Browse Source

fix blog edition

leosw 7 months ago
parent
commit
b5115182d5
4 changed files with 37 additions and 25 deletions
  1. 11 9
      controllers/d.blog.php
  2. 23 13
      models/d.blog.php
  3. 2 2
      views/d.blog.edit.html
  4. 1 1
      views/d.blog.view.html

+ 11 - 9
controllers/d.blog.php

@@ -97,21 +97,23 @@ switch ($controller->splitted_url[1]) {
 		if ($blogArticle->checkPermalink($controller->splitted_url[1],$user->rankIsHigher("premium"))) {
 			if (isset($controller->splitted_url[2]) && $controller->splitted_url[2] == "delete" && $user->rankIsHigher("moderator")) {
 				$blogArticle->delete();
-				header('Location: '.$config['rel_root_folder']."blog/".$blogArticle->url);
+				header('Location: '.$config['rel_root_folder']."blog/".$blogArticle->permalink);
 			}
 			else if (isset($controller->splitted_url[2]) && $controller->splitted_url[2] == "edit" && $user->rankIsHigher("moderator")) {
 				if(isset($_POST['submit'])) {
 					$blogArticle->content = $_POST['content'];
 					$blogArticle->locale = $_POST['locale'];
-					$blogArticle->title = $_POST['title'];
-					$blogArticle->comments = isset($_POST['comments'])?'t':'f';
+					$blogArticle->name = $_POST['name'];
+					$blogArticle->is_commentable = isset($_POST['is_commentable'])?'t':'f';
 					$blogArticle->author = $user->id;
 					$blogArticle->update();
-					header('Location: '.$config['rel_root_folder']."blog/".$blogArticle->url);
+					header('Location: '.$config['rel_root_folder']."blog/".$blogArticle->permalink);
 				}
 				else {
-					$blogArticle->populate();
-					$head['title'] = $blogArticle->title;
+					$locales = new Kabano\Locales();
+					$locales->getAll();
+
+					$head['title'] = $blogArticle->name;
 					include ($config['views_folder']."d.blog.edit.html");
 				}
 			}
@@ -147,14 +149,14 @@ switch ($controller->splitted_url[1]) {
 					}
 				}
 
-				// Manage comment undeletion
-				if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="undelete_comment") {
+				// Manage comment restoration
+				if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="restore_comment") {
 					if (isset($controller->splitted_url[3]) && is_numeric($controller->splitted_url[3])) {
 						$blogComment = new Kabano\BlogComment();
 						$blogComment->id = $controller->splitted_url[3];
 						$blogComment->populate();
 						if ($user->rankIsHigher("moderator") || $user->id == $blogComment->author)
-							$blogComment->undelete();
+							$blogComment->restore();
 					}
 				}
 

+ 23 - 13
models/d.blog.php

@@ -82,41 +82,51 @@ class BlogArticle
 	/*****
 	** Edit a page by archiving the current one and inserting a new one ID
 	*****/
-/*	public function update() {
+	public function update() {
 		global $config;
 		global $user;
+
+		if($this->id == 0)
+			die("Cannot update entry without giving ID");
+
+		$oldId = $this->id;
 		
+		$this->version++;
+
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		// Archive previous article
-		$query = "UPDATE blog_articles SET archive = TRUE WHERE url = $1";
+		$query = "UPDATE contents SET is_archive = TRUE WHERE permalink = $1 AND type='blog'";
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare1", array($this->url))
+		$result = pg_execute($con, "prepare1", array($this->permalink))
 			or die ("Cannot execute statement\n");
 
-		// Publish the new one
-		$query = "INSERT INTO blog_articles (url, title, content, lastedit, archive, locale, author, comments) VALUES
-			($1, $2, $3, $4, FALSE, $5, $6, $7) RETURNING id";
+		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
+			($1, $2, $3, $4, $5, $6, TRUE, FALSE, $7, 'blog', $8, $9) RETURNING id";
 
 		pg_prepare($con, "prepare2", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare2", array($this->url, $this->title, $this->content, date('r'), $this->locale, $this->author, $this->comments))
+		$result = pg_execute($con, "prepare2", array($this->permalink, $this->version, $this->locale, $this->creation_date, date('r'), $this->author, $this->is_commentable, $this->name, $this->content))
 			or die ("Cannot execute statement\n");
 
 		$this->id = pg_fetch_assoc($result)['id'];
 
-		// Move all comments to the new one
-
-		$query = "UPDATE blog_comments bc SET article = $1 FROM blog_articles ba WHERE bc.article = ba.id AND ba.url = $2";
+		$query = "INSERT INTO content_contributors (content, contributor) SELECT $1, contributor FROM content_contributors AS old WHERE old.content = $2";
 
 		pg_prepare($con, "prepare3", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare3", array($this->id, $this->url))
+		$result = pg_execute($con, "prepare3", array($this->id, $oldId))
 			or die ("Cannot execute statement\n");
 
+		$query = "INSERT INTO content_contributors (content, contributor) VALUES
+			($1, $2) ON CONFLICT (content, contributor) DO NOTHING";
+
+		pg_prepare($con, "prepare4", $query) 
+			or die ("Cannot prepare statement\n");
+		$result = pg_execute($con, "prepare4", array($this->id, $user->id))
+			or die ("Cannot execute statement\n");
 
 		pg_close($con);
 
@@ -124,7 +134,7 @@ class BlogArticle
 			date('r')." \t".$user->name." (".$user->id.") \tUPDATE \tEdit blog article '".$this->url."'\r\n",
 			3,
 			$config['logs_folder'].'blog.articles.log');
-	}*/
+	}
 
 	/*****
 	** Delete an article by archiving it

+ 2 - 2
views/d.blog.edit.html

@@ -34,8 +34,8 @@
 					<input type="text" value="<?=$blogArticle->permalink?>" name="permalink" id="permalink" placeholder="URL">
 				<? } ?>
 
-				<label for="comments">
-					<input type="checkbox" name="comments" id="comments" value="comments" 
+				<label for="is_commentable">
+					<input type="checkbox" name="is_commentable" id="is_commentable"
 					<? if($blogArticle->is_commentable == 't') { ?>
 						checked
 					<? } ?>

+ 1 - 1
views/d.blog.view.html

@@ -86,7 +86,7 @@
 									<span class="delete_link"><a href="<?=$config['rel_root_folder']?>blog/<?=$blogArticle->permalink?>/delete_comment/<?=$row->id?>"><i class="fas fa-trash"></i> Effacer le commentaire</a></span>
 								<? } ?>
 								<? if (($user->rankIsHigher("moderator") || $user->id == $row->author) && $row->archive == 't') { ?>
-									<span class="delete_link"><a href="<?=$config['rel_root_folder']?>blog/<?=$blogArticle->permalink?>/undelete_comment/<?=$row->id?>"><i class="fas fa-eye"></i> Réafficher le commentaire</a></span>
+									<span class="delete_link"><a href="<?=$config['rel_root_folder']?>blog/<?=$blogArticle->permalink?>/restore_comment/<?=$row->id?>"><i class="fas fa-eye"></i> Restaurer le commentaire</a></span>
 								<? } ?>
 							</div>
 							<div class="comment_content">