2015-07-20 16:44:42 +08:00
|
|
|
<?php namespace Flarum\Sticky\Posts;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2015-07-20 16:44:42 +08:00
|
|
|
use Flarum\Core\Posts\Post;
|
|
|
|
use Flarum\Core\Posts\EventPost;
|
|
|
|
use Flarum\Core\Posts\MergeablePost;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2015-07-20 16:44:42 +08:00
|
|
|
class DiscussionStickiedPost extends EventPost implements MergeablePost
|
2015-05-07 20:56:02 +08:00
|
|
|
{
|
|
|
|
public static $type = 'discussionStickied';
|
|
|
|
|
2015-07-20 16:44:42 +08:00
|
|
|
public function saveAfter(Post $previous)
|
2015-05-07 20:56:02 +08:00
|
|
|
{
|
2015-07-20 16:44:42 +08:00
|
|
|
// If the previous post is another 'discussion stickied' post, and it's
|
|
|
|
// by the same user, then we can merge this post into it. If we find
|
|
|
|
// that we've in fact reverted the sticky status, delete it. Otherwise,
|
|
|
|
// update its content.
|
|
|
|
if ($previous instanceof static && $this->user_id === $previous->user_id) {
|
2015-05-07 20:56:02 +08:00
|
|
|
if ($previous->content['sticky'] != $this->content['sticky']) {
|
2015-07-20 16:44:42 +08:00
|
|
|
$previous->delete();
|
|
|
|
} else {
|
|
|
|
$previous->content = $this->content;
|
|
|
|
|
|
|
|
$previous->save();
|
2015-05-07 20:56:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $previous;
|
|
|
|
}
|
|
|
|
|
2015-07-20 16:44:42 +08:00
|
|
|
$this->save();
|
|
|
|
|
2015-05-07 20:56:02 +08:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance in reply to a discussion.
|
|
|
|
*
|
|
|
|
* @param integer $discussionId
|
|
|
|
* @param integer $userId
|
|
|
|
* @param boolean $isSticky
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function reply($discussionId, $userId, $isSticky)
|
|
|
|
{
|
|
|
|
$post = new static;
|
|
|
|
|
|
|
|
$post->content = static::buildContent($isSticky);
|
|
|
|
$post->time = time();
|
|
|
|
$post->discussion_id = $discussionId;
|
|
|
|
$post->user_id = $userId;
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the content attribute.
|
|
|
|
*
|
|
|
|
* @param boolean $isSticky Whether or not the discussion is stickied.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function buildContent($isSticky)
|
|
|
|
{
|
|
|
|
return ['sticky' => (bool) $isSticky];
|
|
|
|
}
|
|
|
|
}
|