Browse Source

Restore blog behavior with three tables

Léo Serre 8 months ago
parent
commit
fa8f50a3d9
4 changed files with 55 additions and 46 deletions
  1. 2
    2
      controllers/d.blog.php
  2. 51
    42
      models/d.blog.php
  3. 1
    1
      models/d.wiki.php
  4. 1
    1
      views/d.blog.view.html

+ 2
- 2
controllers/d.blog.php View File

@@ -132,7 +132,7 @@ switch ($controller->splitted_url[1]) {
132 132
 						$blogComment = new Kabano\BlogComment();
133 133
 						$blogComment->locale = $user->locale;
134 134
 						$blogComment->author = $user->id;
135
-						$blogComment->content = $blogArticle->id;
135
+						$blogComment->content = $blogArticle->content_id;
136 136
 						$blogComment->comment = $_POST['comment'];
137 137
 						$blogComment->insert();
138 138
 					}
@@ -163,7 +163,7 @@ switch ($controller->splitted_url[1]) {
163 163
 				// Manage comments
164 164
 				if ($blogArticle->is_commentable == "t") {
165 165
 					$blogArticles_comments = new Kabano\BlogComments();
166
-					$blogArticles_comments->listComments($blogArticle->id, ($user->rankIsHigher("premium")));
166
+					$blogArticles_comments->listComments($blogArticle->content_id, ($user->rankIsHigher("premium")));
167 167
 
168 168
 					$i = 0;
169 169
 					foreach ($blogArticles_comments->objs as $comment) {

+ 51
- 42
models/d.blog.php View File

@@ -14,7 +14,9 @@ require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
14 14
 
15 15
 class BlogArticle
16 16
 {
17
-	public $id = NULL;
17
+	public $content_id = NULL;
18
+	public $locale_id = NULL;
19
+	public $version_id = NULL;
18 20
 	public $permalink = NULL;
19 21
 	public $version = 0;
20 22
 	public $locale = NULL;
@@ -37,7 +39,7 @@ class BlogArticle
37 39
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
38 40
 			or die ("Could not connect to server\n");
39 41
 
40
-		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='blog'";
42
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog'";
41 43
 		if($withArchive==0) {
42 44
 			$query .= " AND is_archive=FALSE AND is_public=TRUE";
43 45
 		}
@@ -64,7 +66,9 @@ class BlogArticle
64 66
 	** Populate the object using its ID
65 67
 	*****/
66 68
 	public function populate($row) {
67
-		$this->id = $row['id'];
69
+		$this->content_id = $row['content_id'];
70
+		$this->locale_id = $row['locale_id'];
71
+		$this->version_id = $row['version_id'];
68 72
 		$this->permalink = $row['permalink'];
69 73
 		$this->version = $row['version'];
70 74
 		$this->locale = $row['locale'];
@@ -86,46 +90,37 @@ class BlogArticle
86 90
 		global $config;
87 91
 		global $user;
88 92
 
89
-		if($this->id == 0)
93
+		if($this->content_id == 0 || $this->locale_id == 0 || $this->version_id == 0)
90 94
 			die("Cannot update entry without giving ID");
91
-
92
-		$oldId = $this->id;
93 95
 		
94 96
 		$this->version++;
95 97
 
96 98
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
97 99
 			or die ("Could not connect to server\n");
98 100
 
99
-		$query = "UPDATE contents SET is_archive = TRUE WHERE permalink = $1 AND type='blog'";
101
+		$query = "UPDATE content_versions SET is_archive = TRUE WHERE locale_id = $1";
100 102
 
101 103
 		pg_prepare($con, "prepare1", $query) 
102 104
 			or die ("Cannot prepare statement\n");
103
-		$result = pg_execute($con, "prepare1", array($this->permalink))
105
+		$result = pg_execute($con, "prepare1", array($this->locale_id))
104 106
 			or die ("Cannot execute statement\n");
105 107
 
106
-		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
107
-			($1, $2, $3, $4, $5, $6, TRUE, FALSE, $7, 'blog', $8, $9) RETURNING id";
108
+		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
109
+			($1, $2, FALSE, $3, $4, $5) RETURNING id";
108 110
 
109 111
 		pg_prepare($con, "prepare2", $query) 
110 112
 			or die ("Cannot prepare statement\n");
111
-		$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))
113
+		$result = pg_execute($con, "prepare2", array($this->version, date('r'), $this->name, $this->content, $this->locale_id))
112 114
 			or die ("Cannot execute statement\n");
113 115
 
114
-		$this->id = pg_fetch_assoc($result)['id'];
115
-
116
-		$query = "INSERT INTO content_contributors (content, contributor) SELECT $1, contributor FROM content_contributors AS old WHERE old.content = $2";
117
-
118
-		pg_prepare($con, "prepare3", $query) 
119
-			or die ("Cannot prepare statement\n");
120
-		$result = pg_execute($con, "prepare3", array($this->id, $oldId))
121
-			or die ("Cannot execute statement\n");
116
+		$this->version_id = pg_fetch_assoc($result)['id'];
122 117
 
123 118
 		$query = "INSERT INTO content_contributors (content, contributor) VALUES
124 119
 			($1, $2) ON CONFLICT (content, contributor) DO NOTHING";
125 120
 
126
-		pg_prepare($con, "prepare4", $query) 
121
+		pg_prepare($con, "prepare3", $query) 
127 122
 			or die ("Cannot prepare statement\n");
128
-		$result = pg_execute($con, "prepare4", array($this->id, $user->id))
123
+		$result = pg_execute($con, "prepare3", array($this->locale_id, $user->id))
129 124
 			or die ("Cannot execute statement\n");
130 125
 
131 126
 		pg_close($con);
@@ -196,22 +191,42 @@ class BlogArticle
196 191
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
197 192
 			or die ("Could not connect to server\n");
198 193
 
199
-		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
200
-			($1, '0', $2, $3, $4, $5, TRUE, FALSE, $6, 'blog', $7, $8) RETURNING id";
194
+		$query = "INSERT INTO contents (permalink, creation_date, is_public, is_commentable, type) VALUES
195
+			($1, $2, TRUE, $3, 'blog') RETURNING id";
201 196
 
202 197
 		pg_prepare($con, "prepare1", $query) 
203 198
 			or die ("Cannot prepare statement\n");
204
-		$result = pg_execute($con, "prepare1", array($this->permalink, $this->locale, date('r'), date('r'), $user->id, $this->is_commentable, $this->name, $this->content))
199
+		$result = pg_execute($con, "prepare1", array($this->permalink, date('r'), $this->is_commentable))
205 200
 			or die ("Cannot execute statement\n");
206 201
 
207
-		$this->id = pg_fetch_assoc($result)['id'];
202
+		$this->content_id = pg_fetch_assoc($result)['id'];
203
+
204
+		$query = "INSERT INTO content_locales (content_id, locale, author) VALUES
205
+			($1, $2, $3) RETURNING id";
206
+
207
+		pg_prepare($con, "prepare2", $query) 
208
+			or die ("Cannot prepare statement\n");
209
+		$result = pg_execute($con, "prepare2", array($this->content_id, $this->locale, $user->id))
210
+			or die ("Cannot execute statement\n");
211
+
212
+		$this->locale_id = pg_fetch_assoc($result)['id'];
213
+
214
+		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
215
+			('0', $1, FALSE, $2, $3, $4) RETURNING id";
216
+
217
+		pg_prepare($con, "prepare3", $query) 
218
+			or die ("Cannot prepare statement\n");
219
+		$result = pg_execute($con, "prepare3", array(date('r'), $this->name, $this->content, $this->locale_id))
220
+			or die ("Cannot execute statement\n");
221
+
222
+		$this->version_id = pg_fetch_assoc($result)['id'];
208 223
 
209 224
 		$query = "INSERT INTO content_contributors (content, contributor) VALUES
210 225
 			($1, $2)";
211 226
 
212
-		pg_prepare($con, "prepare2", $query) 
227
+		pg_prepare($con, "prepare4", $query) 
213 228
 			or die ("Cannot prepare statement\n");
214
-		$result = pg_execute($con, "prepare2", array($this->id, $user->id))
229
+		$result = pg_execute($con, "prepare4", array($this->id, $user->id))
215 230
 			or die ("Cannot execute statement\n");
216 231
 
217 232
 		pg_close($con);
@@ -261,13 +276,10 @@ class BlogArticles
261 276
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
262 277
 			or die ("Could not connect to server\n");
263 278
 
264
-		if ($archive == 1) {
265
-			// You just want one per url and the criteria is ORDER BY archives = true, time DES=C
266
-			$query = "SELECT * FROM (SELECT *, 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 WHERE type='blog') AS b WHERE r = 1 ORDER BY update_date DESC";
267
-		}
268
-		else {
269
-			$query = "SELECT * FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC";
270
-		}
279
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE ";
280
+		if ($archive == 1)
281
+			$query .= "AND is_public=TRUE ";
282
+		$query .= "AND type='blog' ORDER BY update_date DESC";
271 283
 		$query .= " LIMIT $1 OFFSET $2";
272 284
 
273 285
 		pg_prepare($con, "prepare1", $query) 
@@ -293,13 +305,10 @@ class BlogArticles
293 305
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
294 306
 			or die ("Could not connect to server\n");
295 307
 
296
-		if ($archive == 1) {
297
-			// You just want one per url and the criteria is ORDER BY archives = true, time DES=C
298
-			$query = "SELECT * FROM (SELECT 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 WHERE type='blog') AS b WHERE r = 1 ORDER BY update_date DESC";
299
-		}
300
-		else {
301
-			$query = "SELECT update_date FROM contents WHERE is_archive IS NOT TRUE AND is_public IS TRUE AND type='blog' ORDER BY update_date DESC";
302
-		}
308
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE is_archive=FALSE ";
309
+		if ($archive == 1)
310
+			$query .= "AND is_public=TRUE ";
311
+		$query .= "AND type='blog' ORDER BY update_date DESC";
303 312
 
304 313
 		pg_prepare($con, "prepare1", $query) 
305 314
 			or die ("Cannot prepare statement\n");
@@ -320,7 +329,7 @@ class BlogArticles
320 329
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
321 330
 			or die ("Could not connect to server\n");
322 331
 
323
-		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='blog' ORDER BY update_date DESC";
332
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='blog' ORDER BY update_date DESC";
324 333
 
325 334
 		pg_prepare($con, "prepare1", $query) 
326 335
 			or die ("Cannot prepare statement\n");

+ 1
- 1
models/d.wiki.php View File

@@ -41,7 +41,7 @@ class WikiPage
41 41
 
42 42
 		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki'";
43 43
 		if($withArchive==0) {
44
-			$query .= " AND is_public=TRUE";
44
+			$query .= " AND is_archive=FALSE AND is_public=TRUE";
45 45
 		}
46 46
 		$query .= " ORDER BY update_date DESC LIMIT 1 OFFSET $2";
47 47
 

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

@@ -15,7 +15,7 @@
15 15
 						<select id="bloghistory">
16 16
 							<? $i = 0;
17 17
 							foreach ($blogHistory->objs as $row) { ?>
18
-								<option <?=$row->id==$blogArticle->id?'selected':''?> value="<?=$i?>"><?=$row->is_archive=="f"?'&bull; ':''?><? echo strftime('%d/%m/%Y %H:%M:%S',strtotime($row->update_date)) ?></option>
18
+								<option <?=$row->version_id==$blogArticle->version_id?'selected':''?> value="<?=$i?>"><?=$row->is_archive=="f"?'&bull; ':''?><? echo strftime('%d/%m/%Y %H:%M:%S',strtotime($row->update_date)) ?></option>
19 19
 							<? $i++;
20 20
 							} ?>
21 21
 						</select>

Loading…
Cancel
Save