diff --git a/_ressources/sql_skeleton.sql b/_ressources/sql_skeleton.sql index 2d72a70..59b8bdf 100644 --- a/_ressources/sql_skeleton.sql +++ b/_ressources/sql_skeleton.sql @@ -174,7 +174,6 @@ SET default_with_oids = false; CREATE TABLE public.content_comments ( id integer DEFAULT nextval('public.content_comments_sequence'::regclass) NOT NULL, - permalink character varying(255), version integer, creation_date timestamp without time zone, update_date timestamp without time zone, diff --git a/controllers/d.blog.php b/controllers/d.blog.php index 3335b88..982de6f 100755 --- a/controllers/d.blog.php +++ b/controllers/d.blog.php @@ -136,8 +136,8 @@ switch ($controller->splitted_url[1]) { $blogComment = new Kabano\BlogComment(); $blogComment->locale = $user->locale; $blogComment->author = $user->id; - $blogComment->article = $blogArticle->id; - $blogComment->content = $_POST['comment']; + $blogComment->content = $blogArticle->id; + $blogComment->comment = $_POST['comment']; $blogComment->insert(); } } @@ -146,10 +146,9 @@ switch ($controller->splitted_url[1]) { if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="delete_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->delete(); + if($blogComment->checkId($controller->splitted_url[3])) + if ($user->rankIsHigher("moderator") || $user->id == $blogComment->author) + $blogComment->delete(); } } @@ -157,10 +156,9 @@ switch ($controller->splitted_url[1]) { 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->restore(); + if($blogComment->checkId($controller->splitted_url[3])) + if ($user->rankIsHigher("moderator") || $user->id == $blogComment->author) + $blogComment->restore(); } } @@ -172,15 +170,10 @@ switch ($controller->splitted_url[1]) { $blogArticles_comments->listComments($blogArticle->id, ($user->rankIsHigher("premium"))); $i = 0; - foreach ($blogArticles_comments->ids as $row) { - $blogArticles_comments_list[$i] = new Kabano\BlogComment(); - $blogArticles_comments_list[$i]->id = $row; - $blogArticles_comments_list[$i]->populate(); - $blogArticles_comments_list[$i]->md2html(); - $blogArticles_comments_list[$i]->author_obj = new Kabano\User(); - $blogArticles_comments_list[$i]->author_obj->id = $blogArticles_comments_list[$i]->author; - $blogArticles_comments_list[$i]->author_obj->populate(); - $i++; + foreach ($blogArticles_comments->objs as $comment) { + $comment->md2html(); + $comment->author_obj = new Kabano\User(); + $comment->author_obj->checkId($comment->author); } } diff --git a/models/d.blog.php b/models/d.blog.php index af758ad..2891f50 100755 --- a/models/d.blog.php +++ b/models/d.blog.php @@ -349,45 +349,60 @@ class BlogArticles class BlogComment { - public $id = 0; - public $locale = NULL; - public $lastedit = NULL; - public $archive = NULL; - public $content = NULL; + public $id = NULL; + public $version = 0; + public $creation_date = NULL; + public $update_date = NULL; public $author = NULL; - public $article = NULL; + public $is_public = NULL; + public $is_archive = NULL; + public $content = NULL; + public $comment = NULL; + public $locale = NULL; + + + /***** + ** Connect to correct account using ID and stores its ID + *****/ + public function checkID($id) { + global $config; + + $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"); + + $query = "SELECT * FROM content_comments WHERE id=$1"; + + pg_prepare($con, "prepare1", $query) + or die ("Cannot prepare statement\n"); + $result = pg_execute($con, "prepare1", array($id)) + or die ("Cannot execute statement\n"); + + pg_close($con); + + if(pg_num_rows($result) == 1) { + $row = pg_fetch_assoc($result); + $this->populate($row); + return 1; + } + else { + return 0; + } + } /***** ** Populate the object using its ID *****/ - public function populate() { - global $config; - - if($this->id != 0) { - $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"); - - $query = "SELECT * FROM blog_comments WHERE id=$1"; - - pg_prepare($con, "prepare1", $query) - or die ("Cannot prepare statement\n"); - $result = pg_execute($con, "prepare1", array($this->id)) - or die ("Cannot execute statement\n"); - - pg_close($con); - - $blog_comment = pg_fetch_assoc($result); - - $this->locale = $blog_comment['locale']; - $this->lastedit = $blog_comment['lastedit']; - $this->archive = $blog_comment['archive']; - $this->content = $blog_comment['content']; - $this->author = $blog_comment['author']; - $this->article = $blog_comment['article']; - } - else { - die("Cannot populate a blog article without ID"); - } + public function populate($row) { + $this->id = $row['id']; + $this->version = $row['version']; + $this->creation_date = $row['creation_date']; + $this->update_date = $row['update_date']; + $this->author = $row['author']; + $this->is_public = $row['is_public']; + $this->is_archive = $row['is_archive']; + $this->content = $row['content']; + $this->comment = $row['comment']; + $this->locale = $row['locale']; } /***** @@ -399,14 +414,16 @@ class BlogComment $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"); - $query = "INSERT INTO blog_comments (content, lastedit, archive, locale, author, article) VALUES - ($1, $2, FALSE, $3, $4, $5)"; + $query = "INSERT INTO content_comments (version, creation_date, update_date, author, is_public, is_archive, content, comment, locale) VALUES + ('0', $1, $2, $3, TRUE, FALSE, $4, $5, $6) RETURNING id"; - pg_prepare($con, "prepare2", $query) + pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n"); - $result = pg_execute($con, "prepare2", array($this->content, date('r'), $this->locale, $this->author, $this->article)) + $result = pg_execute($con, "prepare1", array(date('r'), date('r'), $this->author, $this->content, $this->comment, $this->locale)) or die ("Cannot execute statement\n"); + $this->id = pg_fetch_assoc($result)['id']; + pg_close($con); } @@ -420,11 +437,11 @@ class BlogComment $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"); - $query = "UPDATE blog_comments SET archive = TRUE WHERE id = $1"; + $query = "UPDATE content_comments SET is_public = FALSE WHERE id = $1"; - pg_prepare($con, "prepare2", $query) + pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n"); - $result = pg_execute($con, "prepare2", array($this->id)) + $result = pg_execute($con, "prepare1", array($this->id)) or die ("Cannot execute statement\n"); pg_close($con); @@ -436,20 +453,20 @@ class BlogComment } /***** - ** DeArchive a comment + ** Restore a comment *****/ - public function undelete() { + public function restore() { global $config; global $user; $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"); - $query = "UPDATE blog_comments SET archive = FALSE WHERE id = $1"; + $query = "UPDATE content_comments SET is_public = TRUE WHERE id = $1"; - pg_prepare($con, "prepare2", $query) + pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n"); - $result = pg_execute($con, "prepare2", array($this->id)) + $result = pg_execute($con, "prepare1", array($this->id)) or die ("Cannot execute statement\n"); pg_close($con); @@ -461,18 +478,18 @@ class BlogComment } /***** - ** Converts the Markdown content to HTML + ** Converts the Markdown comment to HTML *****/ public function md2html() { - $this->content_html = \Michelf\MarkdownExtra::defaultTransform($this->content); + $this->comment_html = \Michelf\MarkdownExtra::defaultTransform($this->comment); } /***** - ** Converts the Markdown content to text + ** Converts the Markdown comment to text *****/ public function md2txt() { $this->md2html(); - $this->content_txt = strip_tags($this->content_html); + $this->comment_txt = strip_tags($this->comment_html); } } @@ -487,7 +504,7 @@ class BlogComment class BlogComments { - public $ids = array(); + public $objs = array(); public $number = NULL; /***** @@ -499,10 +516,10 @@ class BlogComments $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"); - $query = "SELECT id FROM blog_comments WHERE article = $1 "; + $query = "SELECT * FROM content_comments WHERE content = $1 "; if ($archive == 0) - $query .= "AND archive IS FALSE "; - $query .= "ORDER BY lastedit DESC"; + $query .= "AND is_archive IS FALSE AND is_public IS TRUE "; + $query .= "ORDER BY update_date DESC"; pg_prepare($con, "prepare1", $query) or die ("Cannot prepare statement\n"); @@ -515,7 +532,8 @@ class BlogComments for($i = 0; $i < pg_num_rows($result); $i++) { $row = pg_fetch_assoc($result, $i); - $this->ids[$i] = $row['id']; + $this->objs[$i] = new BlogComment; + $this->objs[$i]->populate($row); } } } diff --git a/views/d.blog.view.html b/views/d.blog.view.html index 6cbb553..18f5bd0 100755 --- a/views/d.blog.view.html +++ b/views/d.blog.view.html @@ -66,31 +66,31 @@ - + is_commentable == 't') { + foreach ($blogArticles_comments->objs as $comment) { ?> -
archive == 't') echo 'class="comment_archive" '; ?>> +
is_archive == 't' || $comment->is_public == 'f') echo 'class="comment_archive" '; ?>>
- author_obj->avatar=='t') { ?> - Avatar + author_obj->is_avatar_present=='t') { ?> + Avatar rankIsHigher("blocked")) { ?> - author_obj->name?> + author_obj->name?> - author_obj->name?> + author_obj->name?> - le lastedit)) ?> UTC - rankIsHigher("moderator") || $user->id == $row->author) && $row->archive == 'f') { ?> - Effacer le commentaire + le update_date)) ?> UTC + rankIsHigher("moderator") || $user->id == $comment->author) && $comment->is_public == 't') { ?> + Effacer le commentaire - rankIsHigher("moderator") || $user->id == $row->author) && $row->archive == 't') { ?> - Restaurer le commentaire + rankIsHigher("moderator") || $user->id == $comment->author) && $comment->is_public == 'f') { ?> + Restaurer le commentaire
- content_html?> + comment_html?>