Elgg  Version 7.1
GenericEntity.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
9 
16 
17  protected ?\ElggEntity $entity = null;
18 
22  final protected function handleRequest(\Elgg\Request $request): Response {
23  $view_function = lcfirst(str_replace(' ', '', ucwords(str_replace(['_', '-'], ' ', $this->getPage())))) . 'Entity';
24  if (!is_callable([$this, $view_function])) {
25  throw new ValidationException('Unsupported route name configuration');
26  }
27 
28  $this->prepareBreadcrumbs();
29 
30  return elgg_ok_response($this->{$view_function}());
31  }
32 
36  protected function assertValidRoute(): void {
37  $route_parts = $this->getRouteParts();
38 
39  if (!in_array($route_parts[0], ['add', 'edit', 'view'])) {
40  throw new ValidationException('Unsupported route name configuration');
41  }
42  }
43 
47  protected function getPage(): string {
48  return $this->getRouteParts()[0];
49  }
50 
59  protected function getEntity(bool $validate_can_edit = false): \ElggEntity {
60  if (isset($this->entity) && !$validate_can_edit) {
61  return $this->entity;
62  }
63 
64  $guid = (int) $this->request->getParam('guid');
65 
66  $this->entity = elgg_entity_gatekeeper($guid, $this->getEntityType(), $this->getEntitySubtype(), $validate_can_edit);
67 
68  return $this->entity;
69  }
70 
77  protected function prepareBreadcrumbs(): void {
78  switch ($this->getPage()) {
79  case 'add':
80  $entity_type = $this->getEntityType();
81  $entity_subtype = $this->getEntitySubtype();
83 
85  break;
86  case 'edit':
87  elgg_push_entity_breadcrumbs($this->getEntity(true));
88  break;
89  case 'view':
90  elgg_push_entity_breadcrumbs($this->getEntity());
91  break;
92  }
93  }
94 
98  protected function getPageOptions(string $page, array $options): array {
99  if (!isset($options['filter_id'])) {
100  if ($page === 'view') {
101  $options['filter_id'] = "{$this->getEntitySubtype()}/view";
102  } else {
103  $options['filter_id'] = "{$this->getEntitySubtype()}/edit";
104  }
105  }
106 
107  if (!isset($options['sidebar'])) {
108  $sidebar_view = $this->route?->getOption('sidebar_view');
109  if (!empty($sidebar_view) && elgg_view_exists($sidebar_view)) {
110  $entity = null;
111  switch ($page) {
112  case 'edit':
113  $entity = $this->getEntity(true);
114  break;
115  case 'view':
116  $entity = $this->getEntity();
117  break;
118  }
119 
120  $options['sidebar'] = elgg_view($sidebar_view, [
121  'entity' => $entity,
122  'page' => $page,
123  ]);
124  }
125  }
126 
127  if (!isset($options['title'])) {
128  if ($page === 'view') {
129  $options['title'] = $this->getEntity()->getDisplayName();
130  } else {
131  $options['title'] = elgg_echo("{$page}:{$this->getEntityType()}:{$this->getEntitySubtype()}");
132  }
133  }
134 
135  return $options;
136  }
137 
144  protected function addEntity(): string {
145  $entity_type = $this->getEntityType();
146  $entity_subtype = $this->getEntitySubtype();
148  if (!$page_owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
149  throw new EntityPermissionsException();
150  }
151 
152  return elgg_view_page('', $this->getPageOptions('add', [
154  ]));
155  }
156 
163  protected function editEntity(): string {
164  $entity = $this->getEntity(true);
165  $entity_type = $this->getEntityType();
166  $entity_subtype = $this->getEntitySubtype();
167 
168  return elgg_view_page('', $this->getPageOptions('edit', [
170  ]));
171  }
172 
179  protected function viewEntity(): string {
180  $entity = $this->getEntity();
181 
182  return elgg_view_page('', $this->getPageOptions('view', [
183  'content' => elgg_view_entity($entity),
184  'entity' => $entity,
185  ]));
186  }
187 }
$entity
Definition: reset.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$page_owner
Definition: add.php:16
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/clear'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['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', 'controller'=> \Elgg\Diagnostics\DownloadController::class,], '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:76
if(!elgg_is_empty($icon_alt) &&!str_starts_with($icon_alt, '<')) switch( $type)
Definition: button.php:57
Generic base controller for content listing and content view/edit/add.
Generic controller to handle entity view/edit/add routes.
addEntity()
Render an entity add page.
prepareBreadcrumbs()
Prepare breadcrumbs.
getEntity(bool $validate_can_edit=false)
Get the entity from the request.
assertValidRoute()
{Validate that route is supported.void }
getPage()
{Get the page from the route name.string }
getPageOptions(string $page, array $options)
{Get additional options to use when viewing a page.for which page to get the options ('all',...
editEntity()
Render an entity edit page.
viewEntity()
Render the entity view page.
handleRequest(\Elgg\Request $request)
{Handle the request.the Elgg requestResponse }
Generic HTTP exception.
Thrown when entity can not be edited or container permissions do not allow it to be written.
Response builder.
Definition: Response.php:14
Request container.
Definition: Request.php:12
if(count($css_vars)< 2) $options
elgg_push_entity_breadcrumbs(\ElggEntity $entity)
Resolves and pushes entity breadcrumbs based on named routes.
Definition: breadcrumbs.php:33
elgg_push_collection_breadcrumbs(string $entity_type, string $entity_subtype, ?\ElggEntity $container=null, bool $friends=false)
Resolves and pushes collection breadcrumbs for a container.
Definition: breadcrumbs.php:58
$entity_subtype
Definition: edit.php:15
$entity_type
Generic entity edit/add form.
Definition: edit.php:14
elgg_entity_gatekeeper(int $guid, ?string $type=null, ?string $subtype=null, bool $validate_can_edit=false)
Can the viewer see this entity?
Definition: gatekeepers.php:88
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_view_exists(string $view, string $viewtype='', bool $recurse=true)
Returns whether the specified view exists.
Definition: views.php:131
elgg_view_entity(\ElggEntity $entity, array $vars=[])
Returns a string of a rendered entity.
Definition: views.php:508
elgg_view_entity_form(string $entity_type, string $entity_subtype, ?\ElggEntity $entity=null, array $vars=[])
Render an entity edit/add form.
Definition: views.php:1087
elgg_view_page(string $title, string|array $body, string $page_shell='default', array $vars=[])
Assembles and outputs a full page.
Definition: views.php:237
elgg_view(string $view, array $vars=[], string $viewtype='')
Return a parsed view.
Definition: views.php:156
elgg_ok_response($content='', string|array $message='', ?string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:23
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
$page
Definition: admin.php:24