Added support for post relations

This commit is contained in:
Marcin Kurczewski 2013-10-30 20:20:01 +01:00
parent 157572d9ca
commit 1714e9e665
8 changed files with 59 additions and 1 deletions

View file

@ -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

View file

@ -16,6 +16,7 @@ embed {
background: url('../img/bk-swf.png') lemonchiffon;
}
#sidebar .relations ul,
#sidebar .tags ul {
list-style-type: none;
margin: 0;

View file

@ -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);

View file

@ -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',

View file

@ -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;

10
src/Upgrades/Upgrade3.sql Normal file
View file

@ -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);

View file

@ -27,6 +27,13 @@
</div>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::EditPostRelations, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))): ?>
<div class="thumb">
<label class="left" for="relations">Relations:</label>
<div class="input-wrapper"><input type="text" name="relations" id="relations" placeholder="id1,id2,&hellip;" value="<?php echo join(',', array_map(function($post) { return $post->id; }, $this->context->transport->post->via('crossref')->sharedPost)) ?>"/></div>
</div>
<?php endif ?>
<?php if (PrivilegesHelper::confirm(Privilege::EditPostThumb, PrivilegesHelper::getIdentitySubPrivilege($this->context->transport->post->uploader))): ?>
<div class="thumb">
<label class="left" for="thumb">Thumb:</label>

View file

@ -132,6 +132,21 @@
<?php endif ?>
</div>
<?php if (count($this->context->transport->post->via('crossref')->sharedPost)): ?>
<div class="relations unit">
<h1>related</h1>
<ul>
<?php foreach ($this->context->transport->post->via('crossref')->sharedPost as $relatedPost): ?>
<li>
<a href="<?php echo \Chibi\UrlHelper::route('post', 'view', ['id' => $relatedPost->id]) ?>">
@<?php echo $relatedPost->id ?>
</a>
</li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
<div class="unit options">
<h1>options</h1>