2016-02-26 11:56:55 +08:00
|
|
|
<?php
|
|
|
|
|
2015-09-04 11:00:25 +08:00
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
|
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2015-10-11 09:13:13 +08:00
|
|
|
namespace Flarum\Sticky\Post;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2017-10-04 01:45:54 +08:00
|
|
|
use Flarum\Post\AbstractEventPost;
|
|
|
|
use Flarum\Post\MergeableInterface;
|
2017-10-02 01:55:07 +08:00
|
|
|
use Flarum\Post\Post;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2015-10-11 09:13:13 +08:00
|
|
|
class DiscussionStickiedPost extends AbstractEventPost implements MergeableInterface
|
2015-05-07 20:56:02 +08:00
|
|
|
{
|
2015-10-11 09:13:13 +08:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-05-07 20:56:02 +08:00
|
|
|
public static $type = 'discussionStickied';
|
|
|
|
|
2015-10-11 09:13:13 +08:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-02-29 16:23:04 +08:00
|
|
|
public function saveAfter(Post $previous = null)
|
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.
|
|
|
|
*
|
2016-02-26 11:56:55 +08:00
|
|
|
* @param int $discussionId
|
|
|
|
* @param int $userId
|
|
|
|
* @param bool $isSticky
|
2015-05-07 20:56:02 +08:00
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function reply($discussionId, $userId, $isSticky)
|
|
|
|
{
|
|
|
|
$post = new static;
|
|
|
|
|
2016-02-26 11:56:55 +08:00
|
|
|
$post->content = static::buildContent($isSticky);
|
2018-09-17 02:50:31 +08:00
|
|
|
$post->created_at = time();
|
2015-05-07 20:56:02 +08:00
|
|
|
$post->discussion_id = $discussionId;
|
2016-02-26 11:56:55 +08:00
|
|
|
$post->user_id = $userId;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
|
|
|
return $post;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the content attribute.
|
|
|
|
*
|
2016-02-26 11:56:55 +08:00
|
|
|
* @param bool $isSticky Whether or not the discussion is stickied.
|
2015-05-07 20:56:02 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function buildContent($isSticky)
|
|
|
|
{
|
|
|
|
return ['sticky' => (bool) $isSticky];
|
|
|
|
}
|
|
|
|
}
|