framework/extensions/sticky/src/Post/DiscussionStickiedPost.php

82 lines
2.0 KiB
PHP
Raw Normal View History

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.
*/
namespace Flarum\Sticky\Post;
2015-05-07 20:56:02 +08:00
use Flarum\Core\Post;
use Flarum\Core\Post\AbstractEventPost;
use Flarum\Core\Post\MergeableInterface;
2015-05-07 20:56:02 +08:00
class DiscussionStickiedPost extends AbstractEventPost implements MergeableInterface
2015-05-07 20:56:02 +08:00
{
/**
* {@inheritdoc}
*/
2015-05-07 20:56:02 +08:00
public static $type = 'discussionStickied';
/**
* {@inheritdoc}
*/
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.
*
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);
$post->time = 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];
}
}