1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \ElggComment |
4
|
|
|
* |
5
|
|
|
* @property int $level depth of the comment (default 1 = top level) |
6
|
|
|
* @property int $parent_guid direct parent of the comment |
7
|
|
|
* @property int $thread_guid reference to the top comment |
8
|
|
|
* |
9
|
|
|
* @since 1.9.0 |
10
|
|
|
*/ |
11
|
|
|
class ElggComment extends \ElggObject { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Set subtype to comment |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
28 |
|
protected function initializeAttributes() { |
19
|
28 |
|
parent::initializeAttributes(); |
20
|
|
|
|
21
|
28 |
|
$this->attributes['subtype'] = 'comment'; |
22
|
|
|
|
23
|
28 |
|
$this->level = 1; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
22 |
|
protected function persistentDelete(bool $recursive = true): bool { |
30
|
22 |
|
$result = parent::persistentDelete($recursive); |
31
|
|
|
|
32
|
22 |
|
if ($result) { |
33
|
22 |
|
$this->deleteThreadedComments($recursive, true); |
34
|
|
|
} |
35
|
|
|
|
36
|
22 |
|
return $result; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function trash(bool $recursive = true): bool { |
43
|
|
|
$result = parent::trash($recursive); |
44
|
|
|
|
45
|
|
|
if ($result) { |
46
|
|
|
$this->deleteThreadedComments($recursive, false); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $result; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function restore(bool $recursive = true): bool { |
56
|
|
|
$result = parent::restore($recursive); |
57
|
|
|
|
58
|
|
|
if ($result) { |
59
|
|
|
// restore threaded comments |
60
|
|
|
elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_SHOW_DELETED_ENTITIES, function() use ($recursive) { |
61
|
|
|
/* @var $children \ElggBatch */ |
62
|
|
|
$children = elgg_get_entities([ |
63
|
|
|
'type' => 'object', |
64
|
|
|
'subtype' => 'comment', |
65
|
|
|
'limit' => false, |
66
|
|
|
'batch' => true, |
67
|
|
|
'metadata_name_value_pairs' => [ |
68
|
|
|
'name' => 'parent_guid', |
69
|
|
|
'value' => $this->guid, |
70
|
|
|
], |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
/* @var $child \ElggComment */ |
74
|
|
|
foreach ($children as $child) { |
75
|
|
|
$child->restore($recursive); |
76
|
|
|
} |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Delete threaded child comments on this comment |
85
|
|
|
* |
86
|
|
|
* @param bool $recursive recursive delete contained entities |
87
|
|
|
* @param bool $persistent persistently remove the threaded comments |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
* @since 6.0 |
91
|
|
|
*/ |
92
|
22 |
|
protected function deleteThreadedComments(bool $recursive, bool $persistent): void { |
93
|
22 |
|
elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_SHOW_DELETED_ENTITIES, function() use ($recursive, $persistent) { |
94
|
|
|
/* @var $children \ElggBatch */ |
95
|
22 |
|
$children = elgg_get_entities([ |
96
|
22 |
|
'type' => 'object', |
97
|
22 |
|
'subtype' => 'comment', |
98
|
22 |
|
'limit' => false, |
99
|
22 |
|
'batch' => true, |
100
|
22 |
|
'batch_inc_offset' => !$persistent, |
101
|
22 |
|
'metadata_name_value_pairs' => [ |
102
|
22 |
|
'name' => 'parent_guid', |
103
|
22 |
|
'value' => $this->guid, |
104
|
22 |
|
], |
105
|
22 |
|
]); |
106
|
|
|
|
107
|
|
|
/* @var $child \ElggComment */ |
108
|
22 |
|
foreach ($children as $child) { |
109
|
|
|
if (!$child->delete($recursive, $persistent) && $persistent) { |
110
|
|
|
$children->reportFailure(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
22 |
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
5 |
|
public function canComment(int $user_guid = 0): bool { |
120
|
5 |
|
if ($this->getLevel() >= (int) elgg_get_config('comments_max_depth')) { |
121
|
5 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$container = $this->getContainerEntity(); |
125
|
2 |
|
if (!$container instanceof ElggEntity) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $container->canComment($user_guid); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Is this comment created by the same owner as the content of the item being commented on |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
* @since 4.1 |
137
|
|
|
*/ |
138
|
4 |
|
public function isCreatedByContentOwner(): bool { |
139
|
4 |
|
return elgg_call(ELGG_IGNORE_ACCESS, function() { |
140
|
4 |
|
$container = $this->getContainerEntity(); |
141
|
4 |
|
if (!$container instanceof ElggEntity) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
return $container->owner_guid === $this->owner_guid; |
146
|
4 |
|
}); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the depth level of the comment |
151
|
|
|
* |
152
|
|
|
* @return int 1: toplevel, 2: first level, etc |
153
|
|
|
* @since 4.1 |
154
|
|
|
*/ |
155
|
6 |
|
public function getLevel(): int { |
156
|
6 |
|
return isset($this->level) ? (int) $this->level : 1; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the thread GUID this comment is a part of |
161
|
|
|
* |
162
|
|
|
* @return int |
163
|
|
|
* @since 4.1 |
164
|
|
|
*/ |
165
|
5 |
|
public function getThreadGUID(): int { |
166
|
5 |
|
if (isset($this->thread_guid)) { |
167
|
2 |
|
return (int) $this->thread_guid; |
168
|
|
|
} |
169
|
|
|
|
170
|
5 |
|
return $this->guid; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Return the thread (top-level) comment |
175
|
|
|
* |
176
|
|
|
* @return \ElggComment |
177
|
|
|
* @since 4.1 |
178
|
|
|
*/ |
179
|
5 |
|
public function getThreadEntity(): ?\ElggComment { |
180
|
5 |
|
$entity = get_entity($this->getThreadGUID()); |
181
|
5 |
|
return $entity instanceof \ElggComment ? $entity : null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|