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 {
134 
135  return elgg_view_page('', $this->getPageOptions('all', [
136  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:all"),
137  'content' => elgg_view('page/list/all', [
138  'options' => $options,
139  'page' => 'all',
140  ]),
141  'filter_value' => 'all',
142  ]));
143  }
144 
156  protected function listFriends(array $options): string {
157  if (!$this->page_owner instanceof \ElggUser) {
158  throw new BadRequestException();
159  }
160 
161  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner, true);
162 
163  $friends_options = [
164  'relationship' => 'friend',
165  'relationship_guid' => $this->page_owner->guid,
166  'relationship_join_on' => 'owner_guid',
167  ];
168 
169  return elgg_view_page('', $this->getPageOptions('friends', [
170  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:friends"),
171  'content' => elgg_view('page/list/all', [
172  'entity' => $this->page_owner,
173  'options' => array_merge($options, $friends_options),
174  'page' => 'friends',
175  ]),
176  'filter_value' => $this->page_owner?->guid === elgg_get_logged_in_user_guid() ? 'friends' : 'none',
177  ]));
178  }
179 
192  protected function listGroup(array $options): string {
193  if (!$this->page_owner instanceof \ElggGroup) {
194  throw new BadRequestException();
195  }
196 
197  $group_tool = $this->getGroupToolOption();
198  if (isset($group_tool)) {
199  elgg_group_tool_gatekeeper($group_tool);
200  }
201 
202  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
203 
204  $group_options = [
205  'container_guid' => $this->page_owner->guid,
206  'preload_containers' => false,
207  ];
208 
209  return elgg_view_page('', $this->getPageOptions('group', [
210  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:group"),
211  'content' => elgg_view('page/list/all', [
212  'entity' => $this->page_owner,
213  'options' => array_merge($options, $group_options),
214  'page' => 'group',
215  ]),
216  'filter_id' => "{$options['subtype']}/group",
217  'filter_value' => 'all',
218  ]));
219  }
220 
232  protected function listOwner(array $options): string {
233  if (!$this->page_owner instanceof \ElggUser) {
234  throw new BadRequestException();
235  }
236 
237  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
238 
239  $owner_options = [
240  'owner_guid' => $this->page_owner->guid,
241  'preload_owners' => false,
242  ];
243 
244  return elgg_view_page('', $this->getPageOptions('owner', [
245  'title' => elgg_echo("collection:{$options['type']}:{$options['subtype']}:owner", [$this->page_owner->getDisplayName()]),
246  'content' => elgg_view('page/list/all', [
247  'entity' => $this->page_owner,
248  'options' => array_merge($options, $owner_options),
249  'page' => 'owner',
250  ]),
251  'filter_value' => $this->page_owner->guid === elgg_get_logged_in_user_guid() ? 'mine' : 'none',
252  ]));
253  }
254 }
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
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($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
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:235
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