2014-05-03 14:20:48 +02:00
|
|
|
<?php
|
2014-05-06 19:39:41 +02:00
|
|
|
class EditPostRelationsJob extends AbstractPostJob
|
2014-05-03 14:20:48 +02:00
|
|
|
{
|
|
|
|
const RELATED_POST_IDS = 'related-post-ids';
|
|
|
|
|
2014-05-06 19:39:41 +02:00
|
|
|
public function isSatisfied()
|
|
|
|
{
|
|
|
|
return $this->hasArgument(self::RELATED_POST_IDS);
|
|
|
|
}
|
|
|
|
|
2014-05-03 14:20:48 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$post = $this->post;
|
|
|
|
$relations = $this->getArgument(self::RELATED_POST_IDS);
|
|
|
|
|
2014-05-05 21:20:40 +02:00
|
|
|
$oldRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
|
2014-05-03 14:20:48 +02:00
|
|
|
$post->setRelationsFromText($relations);
|
2014-05-05 21:20:40 +02:00
|
|
|
$newRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
|
2014-05-03 14:20:48 +02:00
|
|
|
|
2014-05-06 19:39:41 +02:00
|
|
|
if ($this->getContext() == self::CONTEXT_NORMAL)
|
2014-05-04 17:53:40 +02:00
|
|
|
PostModel::save($post);
|
2014-05-03 14:20:48 +02:00
|
|
|
|
|
|
|
foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id)
|
|
|
|
{
|
2014-05-04 19:23:09 +02:00
|
|
|
Logger::log('{user} removed relation between {post} and {post2}', [
|
2014-05-03 14:20:48 +02:00
|
|
|
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array_diff($newRelatedIds, $oldRelatedIds) as $post2id)
|
|
|
|
{
|
2014-05-04 19:23:09 +02:00
|
|
|
Logger::log('{user} added relation between {post} and {post2}', [
|
2014-05-03 14:20:48 +02:00
|
|
|
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
|
|
|
'post' => TextHelper::reprPost($post),
|
|
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresPrivilege()
|
|
|
|
{
|
2014-05-04 16:27:15 +02:00
|
|
|
return new Privilege(
|
2014-05-06 19:39:41 +02:00
|
|
|
$this->getContext() == self::CONTEXT_BATCH_ADD
|
|
|
|
? Privilege::AddPostRelations
|
|
|
|
: Privilege::EditPostRelations,
|
2014-05-04 16:27:15 +02:00
|
|
|
Access::getIdentity($this->post->getUploader()));
|
2014-05-03 14:20:48 +02:00
|
|
|
}
|
|
|
|
}
|