Restored JobArgs approach. Previous introduction of hierarchic argument definitions has backfired: it was confusing what class to take arguments from, the concept of sharing arguments between different jobs was unintelligible and one never knew where given argument was actually defined. This appraoch makes it easier to maintain the arguments list and simplifies the code a lot.
48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
class EditPostRelationsJob extends AbstractPostJob
|
|
{
|
|
public function isSatisfied()
|
|
{
|
|
return $this->hasArgument(JobArgs::ARG_NEW_RELATED_POST_IDS);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$post = $this->post;
|
|
$relations = $this->getArgument(JobArgs::ARG_NEW_RELATED_POST_IDS);
|
|
|
|
$oldRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
|
|
$post->setRelationsFromText($relations);
|
|
$newRelatedIds = array_map(function($post) { return $post->getId(); }, $post->getRelations());
|
|
|
|
if ($this->getContext() == self::CONTEXT_NORMAL)
|
|
PostModel::save($post);
|
|
|
|
foreach (array_diff($oldRelatedIds, $newRelatedIds) as $post2id)
|
|
{
|
|
Logger::log('{user} removed relation between {post} and {post2}', [
|
|
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
|
'post' => TextHelper::reprPost($post),
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
}
|
|
|
|
foreach (array_diff($newRelatedIds, $oldRelatedIds) as $post2id)
|
|
{
|
|
Logger::log('{user} added relation between {post} and {post2}', [
|
|
'user' => TextHelper::reprUser(Auth::getCurrentUser()),
|
|
'post' => TextHelper::reprPost($post),
|
|
'post2' => TextHelper::reprPost($post2id)]);
|
|
}
|
|
|
|
return $post;
|
|
}
|
|
|
|
public function requiresPrivilege()
|
|
{
|
|
return new Privilege(
|
|
$this->getContext() == self::CONTEXT_BATCH_ADD
|
|
? Privilege::AddPostRelations
|
|
: Privilege::EditPostRelations,
|
|
Access::getIdentity($this->post->getUploader()));
|
|
}
|
|
}
|