Elgg  Version master
GenericContentListing.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
8 
15 
16  protected ?\Elgg\Request $request = null;
17 
18  protected ?\Elgg\Router\Route $route = null;
19 
20  protected ?\ElggEntity $page_owner = null;
21 
30  final public function __invoke(\Elgg\Request $request): \Elgg\Http\Response {
31  $this->request = $request;
32  $this->route = $request->getHttpRequest()?->getRoute();
33  $this->page_owner = $this->route?->resolvePageOwner();
34 
35  $route_name = $request->getRoute();
36  $parsed_route = $this->parseRoute($route_name);
37 
38  elgg_register_title_button('add', $parsed_route['type'], $parsed_route['subtype']);
39 
40  $options = $this->getListingOptions($parsed_route['page'], [
41  'type' => $parsed_route['type'],
42  'subtype' => $parsed_route['subtype'],
43  ]);
44 
45  $listing_function = 'list' . str_replace(' ', '', ucwords(str_replace(['_', '-'], ' ', $parsed_route['page'])));
46  if (!is_callable([$this, $listing_function])) {
47  throw new ValidationException('Unsupported route name configuration');
48  }
49 
50  return elgg_ok_response($this->{$listing_function}($options));
51  }
52 
61  final protected function parseRoute(string $route_name): array {
62  $name_parts = explode(':', $route_name);
63  if (count($name_parts) < 3) {
64  throw new ValidationException('Unsupported route name configuration');
65  }
66 
67  if (!in_array($name_parts[0], ['default', 'collection'])) {
68  throw new ValidationException('Unsupported route name configuration');
69  }
70 
71  return [
72  'type' => $name_parts[1],
73  'subtype' => $name_parts[2],
74  'page' => $name_parts[3] ?? 'all',
75  ];
76  }
77 
83  protected function getGroupToolOption(): ?string {
84  return $this->route?->getOption('group_tool');
85  }
86 
96  protected function getListingOptions(string $page, array $options): array {
98  }
99 
109  protected function getPageOptions(string $page, array $options): array {
110  $sidebar_view = $this->route?->getOption('sidebar_view');
111  if (!empty($sidebar_view) && elgg_view_exists($sidebar_view)) {
112  $options['sidebar'] = elgg_view($sidebar_view, [
113  'page' => $page,
114  'entity' => $this->page_owner,
115  ]);
116  }
117 
118  return $options;
119  }
120 
132  protected function listAll(array $options): string {
133  return elgg_view_page('', $this->getPageOptions('all', [
134  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:all"),
135  'content' => elgg_view('page/list/all', [
136  'options' => $options,
137  'page' => 'all',
138  ]),
139  'filter_value' => 'all',
140  ]));
141  }
142 
154  protected function listFriends(array $options): string {
155  if (!$this->page_owner instanceof \ElggUser) {
156  throw new BadRequestException();
157  }
158 
159  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner, true);
160 
161  $friends_options = [
162  'relationship' => 'friend',
163  'relationship_guid' => $this->page_owner->guid,
164  'relationship_join_on' => 'owner_guid',
165  ];
166 
167  return elgg_view_page('', $this->getPageOptions('friends', [
168  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:friends"),
169  'content' => elgg_view('page/list/all', [
170  'entity' => $this->page_owner,
171  'options' => array_merge($options, $friends_options),
172  'page' => 'friends',
173  ]),
174  'filter_value' => $this->page_owner?->guid === elgg_get_logged_in_user_guid() ? 'friends' : 'none',
175  ]));
176  }
177 
190  protected function listGroup(array $options): string {
191  if (!$this->page_owner instanceof \ElggGroup) {
192  throw new BadRequestException();
193  }
194 
195  $group_tool = $this->getGroupToolOption();
196  if (isset($group_tool)) {
197  elgg_group_tool_gatekeeper($group_tool);
198  }
199 
200  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
201 
202  $group_options = [
203  'container_guid' => $this->page_owner->guid,
204  'preload_containers' => false,
205  ];
206 
207  return elgg_view_page('', $this->getPageOptions('group', [
208  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:group"),
209  'content' => elgg_view('page/list/all', [
210  'entity' => $this->page_owner,
211  'options' => array_merge($options, $group_options),
212  'page' => 'group',
213  ]),
214  'filter_id' => "{$options['subtype']}/group",
215  'filter_value' => 'all',
216  ]));
217  }
218 
230  protected function listOwner(array $options): string {
231  if (!$this->page_owner instanceof \ElggUser) {
232  throw new BadRequestException();
233  }
234 
235  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
236 
237  $owner_options = [
238  'owner_guid' => $this->page_owner->guid,
239  'preload_owners' => false,
240  ];
241 
242  return elgg_view_page('', $this->getPageOptions('owner', [
243  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:owner", [$this->page_owner->getDisplayName()]),
244  'content' => elgg_view('page/list/all', [
245  'entity' => $this->page_owner,
246  'options' => array_merge($options, $owner_options),
247  'page' => 'owner',
248  ]),
249  'filter_value' => $this->page_owner->guid === elgg_get_logged_in_user_guid() ? 'mine' : 'none',
250  ]));
251  }
252 }
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
Generic controller to handle entity listing routes.
listGroup(array $options)
Group content item listing.
listFriends(array $options)
Friends content item listing.
getListingOptions(string $page, array $options)
Get additional options to use for elgg_list_entities()
listAll(array $options)
All content item listing.
getPageOptions(string $page, array $options)
Get additional options to use when viewing a page.
parseRoute(string $route_name)
Parse the route name to usable parts.
__invoke(\Elgg\Request $request)
Handle a listing request.
listOwner(array $options)
Owner content item listing.
getGroupToolOption()
Get the name of the group tool option for group routes.
Thrown when request is malformatted.
Thrown when the requested group tool isn't enabled for a group.
Request container.
Definition: Request.php:12
getRoute()
Get the name of the route.
Definition: Request.php:42
getHttpRequest()
Get the HttpRequest for this request.
Definition: Request.php:166
if(count($css_vars)< 2) $options
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
elgg_register_title_button(string $name='add', string $entity_type='', string $entity_subtype='')
Convenience function for registering a button to the title menu.
Definition: navigation.php:120
elgg_group_tool_gatekeeper(string $tool_name, ?int $group_guid=null)
Checks if a group has a specific tool enabled.
Definition: gatekeepers.php:20
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_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.
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
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34
if(! $entity instanceof \ElggGroup) $owner_options
Definition: group.php:15
$page
Definition: admin.php:24