Elgg  Version 6.3
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 {
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 }
$entity
Definition: reset.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$container
Definition: delete.php:23
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
trash(bool $recursive=true)
{Move the entity to the trash.If true (default) then all entities which are owned or contained by $th...
Definition: ElggComment.php:42
isCreatedByContentOwner()
Is this comment created by the same owner as the content of the item being commented on.
restore(bool $recursive=true)
{Restore the entity.Recursively restores all entities trashed with the entity?bool 6....
Definition: ElggComment.php:55
getThreadGUID()
Return the thread GUID this comment is a part of.
canComment(int $user_guid=0)
Can a user comment on an entity?@tip Can be overridden by registering for the 'permissions_check:comm...
persistentDelete(bool $recursive=true)
{Permanently delete the entity from the database.If true (default) then all entities which are owned ...
Definition: ElggComment.php:29
getLevel()
Get the depth level of the comment.
deleteThreadedComments(bool $recursive, bool $persistent)
Delete threaded child comments on this comment.
Definition: ElggComment.php:92
getThreadEntity()
Return the thread (top-level) comment.
initializeAttributes()
Set subtype to comment.
Definition: ElggComment.php:18
$owner_guid
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
$children
Definition: item.php:17
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:290
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$user_guid
Definition: login_as.php:10
$persistent
Definition: login_as.php:21
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10