Elgg  Version master
ElggComment.php
Go to the documentation of this file.
1 <?php
11 class ElggComment extends \ElggObject {
12 
18  protected function initializeAttributes() {
19  parent::initializeAttributes();
20 
21  $this->attributes['subtype'] = 'comment';
22 
23  $this->level = 1;
24  }
25 
29  protected function persistentDelete(bool $recursive = true): bool {
30  $result = parent::persistentDelete($recursive);
31 
32  if ($result) {
33  $this->deleteThreadedComments($recursive, true);
34  }
35 
36  return $result;
37  }
38 
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 
55  public function restore(bool $recursive = true): bool {
56  $result = parent::restore($recursive);
57 
58  if ($result) {
59  // restore threaded comments
61  /* @var $children \ElggBatch */
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 
92  protected function deleteThreadedComments(bool $recursive, bool $persistent): void {
93  elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_SHOW_DELETED_ENTITIES, function() use ($recursive, $persistent) {
94  /* @var $children \ElggBatch */
96  'type' => 'object',
97  'subtype' => 'comment',
98  'limit' => false,
99  'batch' => true,
100  'batch_inc_offset' => !$persistent,
101  'metadata_name_value_pairs' => [
102  'name' => 'parent_guid',
103  'value' => $this->guid,
104  ],
105  ]);
106 
107  /* @var $child \ElggComment */
108  foreach ($children as $child) {
109  if (!$child->delete($recursive, $persistent) && $persistent) {
110  $children->reportFailure();
111  }
112  }
113  });
114  }
115 
119  public function canComment(int $user_guid = 0): bool {
120  if ($this->getLevel() >= (int) elgg_get_config('comments_max_depth')) {
121  return false;
122  }
123 
124  $container = $this->getContainerEntity();
125  if (!$container instanceof ElggEntity) {
126  return false;
127  }
128 
129  return $container->canComment($user_guid);
130  }
131 
138  public function isCreatedByContentOwner(): bool {
139  return elgg_call(ELGG_IGNORE_ACCESS, function() {
140  $container = $this->getContainerEntity();
141  if (!$container instanceof ElggEntity) {
142  return false;
143  }
144 
145  return $container->owner_guid === $this->owner_guid;
146  });
147  }
148 
155  public function getLevel(): int {
156  return isset($this->level) ? (int) $this->level : 1;
157  }
158 
165  public function getThreadGUID(): int {
166  if (isset($this->thread_guid)) {
167  return (int) $this->thread_guid;
168  }
169 
170  return $this->guid;
171  }
172 
179  public function getThreadEntity(): ?\ElggComment {
180  $entity = get_entity($this->getThreadGUID());
181  return $entity instanceof \ElggComment ? $entity : null;
182  }
183 }
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:304
getLevel()
Get the depth level of the comment.
canComment(int $user_guid=0)
$user_guid
Definition: login_as.php:10
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
trash(bool $recursive=true)
{}
Definition: ElggComment.php:42
$children
Definition: item.php:17
restore(bool $recursive=true)
{}
Definition: ElggComment.php:55
deleteThreadedComments(bool $recursive, bool $persistent)
Delete threaded child comments on this comment.
Definition: ElggComment.php:92
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
$owner_guid
$entity
Definition: reset.php:8
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$container
Definition: delete.php:23
initializeAttributes()
Set subtype to comment.
Definition: ElggComment.php:18
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
getThreadEntity()
Return the thread (top-level) comment.
$persistent
Definition: login_as.php:21
getContainerEntity()
Get the container entity for this object.
Definition: ElggEntity.php:552
getThreadGUID()
Return the thread GUID this comment is a part of.
isCreatedByContentOwner()
Is this comment created by the same owner as the content of the item being commented on...
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
persistentDelete(bool $recursive=true)
{}
Definition: ElggComment.php:29