fix blog edition

This commit is contained in:
Léo Serre 2018-11-05 22:29:14 +01:00
parent 7a29f0825f
commit b5115182d5
4 changed files with 37 additions and 25 deletions

View File

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

View File

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

View File

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

View File

@ -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> <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') { ?> <? 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>
<div class="comment_content"> <div class="comment_content">