Start rewrite model for new article page

This commit is contained in:
Léo Serre 2018-11-04 19:01:10 +01:00
parent 1f7a77e0d6
commit 7d3a00fd68
3 changed files with 35 additions and 49 deletions

View File

@ -15,45 +15,47 @@ require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
class BlogArticle class BlogArticle
{ {
public $id = 0; public $id = 0;
public $title = NULL; public $permalink = 0;
public $url = NULL; public $version = 0;
public $locale = NULL; public $locale = NULL;
public $lastedit = NULL; public $creation_date = NULL;
public $archive = NULL; public $update_date = NULL;
public $content = NULL;
public $author = NULL; public $author = NULL;
public $comments = NULL; public $is_public = NULL;
public $is_archive = NULL;
public $is_commentable = NULL;
public $type = "blog";
public $name = NULL;
public $content = NULL;
/***** /*****
** Checks if a page at this URL exists and return the ID ** Checks if a page at this URL exists and return the ID
*****/ *****/
public function checkUrl($url, $withArchive=0, $elementNb=0) { public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
global $config; global $config;
$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");
$query = "SELECT id FROM blog_articles WHERE url=$1"; $query = "SELECT * FROM contents WHERE permalink=$1 AND type='blog'";
if($withArchive==0) { if($withArchive==0) {
$query .= " AND archive=FALSE"; $query .= " AND is_archive=FALSE AND is_public=TRUE";
} }
$query .= " ORDER BY lastedit DESC LIMIT 1 OFFSET $2"; $query .= " ORDER BY update_date DESC LIMIT 1 OFFSET $2";
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($url, $elementNb)) $result = pg_execute($con, "prepare1", array($permalink, $elementNb))
or die ("Cannot execute statement\n"); or die ("Cannot execute statement\n");
pg_close($con); pg_close($con);
if(pg_num_rows($result) == 1) { if(pg_num_rows($result) == 1) {
$article = pg_fetch_assoc($result); $row = pg_fetch_assoc($result);
$this->id = $article['id']; $this->populate($row);
$this->url = $url;
return 1; return 1;
} }
else { else {
$this->url = $url;
return 0; return 0;
} }
} }
@ -61,36 +63,20 @@ class BlogArticle
/***** /*****
** Populate the object using its ID ** Populate the object using its ID
*****/ *****/
public function populate() { public function populate($row) {
global $config; $this->id = $row['id'];
$this->permalink = $row['permalink'];
if($this->id != 0) { $this->version = $row['version'];
$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass']) $this->locale = $row['locale'];
or die ("Could not connect to server\n"); $this->creation_date = $row['creation_date'];
$this->update_date = $row['update_date'];
$query = "SELECT * FROM blog_articles WHERE id=$1"; $this->author = $row['author'];
$this->is_public = $row['is_public'];
pg_prepare($con, "prepare1", $query) $this->is_archive = $row['is_archive'];
or die ("Cannot prepare statement\n"); $this->is_commentable = $row['is_commentable'];
$result = pg_execute($con, "prepare1", array($this->id)) $this->type = $row['type'];
or die ("Cannot execute statement\n"); $this->name = $row['name'];
$this->content = $row['content'];
pg_close($con);
$blog_article = pg_fetch_assoc($result);
$this->title = $blog_article['title'];
$this->url = $blog_article['url'];
$this->locale = $blog_article['locale'];
$this->lastedit = $blog_article['lastedit'];
$this->archive = $blog_article['archive'];
$this->content = $blog_article['content'];
$this->author = $blog_article['author'];
$this->comments = $blog_article['comments'];
}
else {
die("Cannot populate a blog article without ID");
}
} }
/***** /*****
@ -232,7 +218,7 @@ class BlogArticles
if ($archive == 1) { if ($archive == 1) {
// You just want one per url and the criteria is ORDER BY archives = true, time DES=C // You just want one per url and the criteria is ORDER BY archives = true, time DES=C
$query = "SELECT * FROM (SELECT a.id, a.update_date , ROW_NUMBER() OVER (PARTITION BY a.permalink ORDER BY CASE WHEN a.is_archive IS TRUE THEN 1 ELSE 0 END, a.update_date DESC) AS r FROM contents WHERE type='blog' AS a) AS b WHERE r = 1 ORDER BY update_date DESC"; $query = "SELECT * FROM (SELECT a.id, a.update_date , ROW_NUMBER() OVER (PARTITION BY a.permalink ORDER BY CASE WHEN a.is_archive IS TRUE THEN 1 ELSE 0 END, a.update_date DESC) AS r FROM contents AS a) AS b WHERE r = 1 ORDER BY update_date DESC";
} }
else { else {
$query = "SELECT * FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC"; $query = "SELECT * FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC";
@ -264,7 +250,7 @@ class BlogArticles
if ($archive == 1) { if ($archive == 1) {
// You just want one per url and the criteria is ORDER BY archives = true, time DES=C // You just want one per url and the criteria is ORDER BY archives = true, time DES=C
$query = "SELECT * FROM (SELECT a.id, a.update_date , ROW_NUMBER() OVER (PARTITION BY a.permalink ORDER BY CASE WHEN a.is_archive IS TRUE THEN 1 ELSE 0 END, a.update_date DESC) AS r FROM contents WHERE type='blog' AS a) AS b WHERE r = 1 ORDER BY update_date DESC"; $query = "SELECT * FROM (SELECT a.id, a.update_date , ROW_NUMBER() OVER (PARTITION BY a.permalink ORDER BY CASE WHEN a.is_archive IS TRUE THEN 1 ELSE 0 END, a.update_date DESC) AS r FROM contents AS a) AS b WHERE r = 1 ORDER BY update_date DESC";
} }
else { else {
$query = "SELECT * FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC"; $query = "SELECT * FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC";

View File

@ -230,7 +230,7 @@ form.form label {
float: right; float: right;
font-size: 17px; font-size: 17px;
} }
#url { #permalink {
width: calc(100% - 20px); width: calc(100% - 20px);
} }

View File

@ -28,7 +28,7 @@
<p style="color: red;">L'URL sélectionnée est déjà prise.</p> <p style="color: red;">L'URL sélectionnée est déjà prise.</p>
<? } ?> <? } ?>
<textarea rows="30" name="content" id="content" placeholder="Contenu de la page"><?=$blogArticle->content?></textarea> <textarea rows="30" name="content" id="content" placeholder="Contenu de l'article"><?=$blogArticle->content?></textarea>
<? if(isset($new) AND $new==1) { ?> <? if(isset($new) AND $new==1) { ?>
<input type="text" value="<?=$blogArticle->permalink?>" name="permalink" id="permalink" placeholder="URL"> <input type="text" value="<?=$blogArticle->permalink?>" name="permalink" id="permalink" placeholder="URL">