-
-
Notifications
You must be signed in to change notification settings - Fork 826
/
DiscussionRenamedPost.php
executable file
·84 lines (73 loc) · 2.21 KB
/
DiscussionRenamedPost.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
* 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\Core\Posts;
/**
* A post which indicates that a discussion's title was changed.
*
* The content is stored as a sequential array containing the old title and the
* new title.
*/
class DiscussionRenamedPost extends EventPost implements MergeablePost
{
/**
* @inheritdoc
*/
public static $type = 'discussionRenamed';
/**
* @inheritdoc
*/
public function saveAfter(Post $previous)
{
// If the previous post is another 'discussion renamed' 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 title, delete it. Otherwise, update
// its content.
if ($previous instanceof static && $this->user_id === $previous->user_id) {
if ($previous->content[0] == $this->content[1]) {
$previous->delete();
} else {
$previous->content = static::buildContent($previous->content[0], $this->content[1]);
$previous->save();
}
return $previous;
}
$this->save();
return $this;
}
/**
* Create a new instance in reply to a discussion.
*
* @param int $discussionId
* @param int $userId
* @param string $oldTitle
* @param string $newTitle
* @return static
*/
public static function reply($discussionId, $userId, $oldTitle, $newTitle)
{
$post = new static;
$post->content = static::buildContent($oldTitle, $newTitle);
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
return $post;
}
/**
* Build the content attribute.
*
* @param string $oldTitle The old title of the discussion.
* @param string $newTitle The new title of the discussion.
* @return array
*/
protected static function buildContent($oldTitle, $newTitle)
{
return [$oldTitle, $newTitle];
}
}