Elgg  Version 5.1
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  public function delete(bool $recursive = true): bool {
30  $result = parent::delete($recursive);
31 
32  if ($result) {
33  // remove the threaded comments directly below this comment
34  elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function() use ($recursive) {
36  'type' => 'object',
37  'subtype' => 'comment',
38  'limit' => false,
39  'batch' => true,
40  'batch_inc_offset' => false,
41  'metadata_name_value_pairs' => [
42  'name' => 'parent_guid',
43  'value' => $this->guid,
44  ],
45  ]);
46 
47  /* @var $child \ElggComment */
48  foreach ($children as $child) {
49  $child->delete($recursive);
50  }
51  });
52  }
53 
54  return $result;
55  }
56 
60  public function canComment(int $user_guid = 0): bool {
61  if ($this->getLevel() >= (int) elgg_get_config('comments_max_depth')) {
62  return false;
63  }
64 
65  $container = $this->getContainerEntity();
66  if (!$container instanceof ElggEntity) {
67  return false;
68  }
69 
70  return $container->canComment($user_guid);
71  }
72 
79  public function isCreatedByContentOwner(): bool {
80  return elgg_call(ELGG_IGNORE_ACCESS, function() {
81  $container = $this->getContainerEntity();
82  if (!$container instanceof ElggEntity) {
83  return false;
84  }
85 
86  return $container->owner_guid === $this->owner_guid;
87  });
88  }
89 
96  public function getLevel(): int {
97  return isset($this->level) ? (int) $this->level : 1;
98  }
99 
106  public function getThreadGUID(): int {
107  if (isset($this->thread_guid)) {
108  return (int) $this->thread_guid;
109  }
110 
111  return $this->guid;
112  }
113 
120  public function getThreadEntity(): ?\ElggComment {
121  $entity = get_entity($this->getThreadGUID());
122  return $entity instanceof \ElggComment ? $entity : null;
123  }
124 }
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:299
getLevel()
Get the depth level of the comment.
Definition: ElggComment.php:96
canComment(int $user_guid=0)
Definition: ElggComment.php:60
$user_guid
Definition: login_as.php:10
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$children
Definition: item.php:17
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
$owner_guid
$entity
Definition: reset.php:8
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:132
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:67
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
$container
Definition: delete.php:24
initializeAttributes()
Set subtype to comment.
Definition: ElggComment.php:18
getThreadEntity()
Return the thread (top-level) comment.
getContainerEntity()
Get the container entity for this object.
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...
Definition: ElggComment.php:79
$guid
Reset an ElggUpgrade.
Definition: reset.php:6