From 1714e9e665731936657ae562896fe7360d210282 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 30 Oct 2013 20:20:01 +0100 Subject: [PATCH] Added support for post relations --- config.ini | 3 +++ public_html/media/css/post-view.css | 1 + src/Bootstrap.php | 1 + src/Controllers/PostController.php | 22 +++++++++++++++++++++- src/Models/Privilege.php | 1 + src/Upgrades/Upgrade3.sql | 10 ++++++++++ src/Views/post-edit.phtml | 7 +++++++ src/Views/post-view.phtml | 15 +++++++++++++++ 8 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/Upgrades/Upgrade3.sql diff --git a/config.ini b/config.ini index b9deeb05..64aa45d9 100644 --- a/config.ini +++ b/config.ini @@ -20,6 +20,7 @@ thumbHeight=150 thumbStyle=outside endlessScrollingDefault=1 maxSearchTokens=4 +maxRelatedPosts=50 [comments] minLength = 5 @@ -65,6 +66,8 @@ editPostSafety.all=moderator editPostTags=registered editPostThumb=moderator editPostSource=moderator +editPostRelations.own=registered +editPostRelations.all=moderator hidePost.own=moderator hidePost.all=moderator deletePost.own=moderator diff --git a/public_html/media/css/post-view.css b/public_html/media/css/post-view.css index 15b4b0b4..7aedfefc 100644 --- a/public_html/media/css/post-view.css +++ b/public_html/media/css/post-view.css @@ -16,6 +16,7 @@ embed { background: url('../img/bk-swf.png') lemonchiffon; } +#sidebar .relations ul, #sidebar .tags ul { list-style-type: none; margin: 0; diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 15bd878d..f2a67b4e 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -56,6 +56,7 @@ class Bootstrap $this->context->transport->errorMessage = rtrim($e->getMessage(), '.') . '.'; $this->context->transport->errorHtml = TextHelper::parseMarkdown($this->context->transport->errorMessage, true); $this->context->transport->exception = $e; + $this->context->transport->queries = array_map(function($x) { return preg_replace('/\s+/', ' ', $x); }, queryLogger()->getLogs()); $this->context->transport->success = false; $this->context->viewName = 'error-exception'; (new \Chibi\View())->renderFile($this->context->layoutName); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index c9cd91da..76a6a45a 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -407,7 +407,28 @@ class PostController $edited = true; } + + /* relations */ + $suppliedRelations = InputHelper::get('relations'); + if ($suppliedRelations !== null) + { + PrivilegesHelper::confirmWithException(Privilege::EditPostRelations, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); + $relatedIds = array_filter(preg_split('/\D/', $suppliedRelations)); + $relatedPosts = []; + foreach ($relatedIds as $relatedId) + { + if ($relatedId == $post->id) + continue; + if (count($relatedPosts) > $this->config->browsing->maxRelatedPosts) + throw new SimpleException('Too many related posts (maximum: ' . $this->config->browsing->maxRelatedPosts . ')'); + $relatedPosts []= Model_Post::locate($relatedId); + } + $post->via('crossref')->sharedPost = $relatedPosts; + } + R::store($post); + + $this->context->transport->success = true; } } @@ -551,7 +572,6 @@ class PostController { $post = Model_Post::locate($id); R::preload($post, [ - 'favoritee' => 'user', 'uploader' => 'user', 'tag', 'comment', diff --git a/src/Models/Privilege.php b/src/Models/Privilege.php index 1f391214..e0d38f84 100644 --- a/src/Models/Privilege.php +++ b/src/Models/Privilege.php @@ -10,6 +10,7 @@ class Privilege extends Enum const EditPostTags = 7; const EditPostThumb = 8; const EditPostSource = 26; + const EditPostRelations = 30; const HidePost = 9; const DeletePost = 10; const FeaturePost = 25; diff --git a/src/Upgrades/Upgrade3.sql b/src/Upgrades/Upgrade3.sql new file mode 100644 index 00000000..4783e1e5 --- /dev/null +++ b/src/Upgrades/Upgrade3.sql @@ -0,0 +1,10 @@ +CREATE TABLE crossref +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + post_id INTEGER, + post2_id INTEGER, + FOREIGN KEY(post_id) REFERENCES post(id) ON DELETE CASCADE ON UPDATE SET NULL, + FOREIGN KEY(post2_id) REFERENCES post(id) ON DELETE CASCADE ON UPDATE SET NULL +); +CREATE INDEX idx_fk_crossref_post_id ON crossref(post_id); +CREATE INDEX idx_fk_crossref_post2_id ON crossref(post2_id); diff --git a/src/Views/post-edit.phtml b/src/Views/post-edit.phtml index dac63efd..b119987e 100644 --- a/src/Views/post-edit.phtml +++ b/src/Views/post-edit.phtml @@ -27,6 +27,13 @@ + context->transport->post->uploader))): ?> +
+ +
+
+ + context->transport->post->uploader))): ?>
diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index ba7f4fdf..087ce66e 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -132,6 +132,21 @@
+ context->transport->post->via('crossref')->sharedPost)): ?> +
+

related

+ +
+ +

options