Elgg  Version 7.1
GenericContentListing.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
8 
15 
16  protected ?\ElggEntity $page_owner = null;
17 
21  final protected function handleRequest(\Elgg\Request $request): \Elgg\Http\Response {
22  $this->page_owner = $this->route?->resolvePageOwner();
23 
24  $parsed_route = $this->parseRoute();
25 
26  elgg_register_title_button('add', $parsed_route['type'], $parsed_route['subtype']);
27 
28  $options = $this->getListingOptions($parsed_route['page'], [
29  'type' => $parsed_route['type'],
30  'subtype' => $parsed_route['subtype'],
31  ]);
32 
33  $listing_function = 'list' . str_replace(' ', '', ucwords(str_replace(['_', '-'], ' ', $parsed_route['page'])));
34  if (!is_callable([$this, $listing_function])) {
35  throw new ValidationException('Unsupported route name configuration');
36  }
37 
38  return elgg_ok_response($this->{$listing_function}($options));
39  }
40 
44  protected function assertValidRoute(): void {
45  $route_parts = $this->getRouteParts();
46 
47  if (!in_array($route_parts[0], ['default', 'collection'])) {
48  throw new ValidationException('Unsupported route name configuration');
49  }
50  }
51 
55  protected function getPage(): string {
56  return $this->getRouteParts()[3] ?? 'all';
57  }
58 
65  final protected function parseRoute(): array {
66  $route_parts = $this->getRouteParts();
67 
68  return [
69  'type' => $route_parts[1],
70  'subtype' => $route_parts[2],
71  'page' => $route_parts[3] ?? 'all',
72  ];
73  }
74 
80  protected function getGroupToolOption(): ?string {
81  return $this->route?->getOption('group_tool');
82  }
83 
93  protected function getListingOptions(string $page, array $options): array {
95  }
96 
100  protected function getPageOptions(string $page, array $options): array {
101  if (!isset($options['filter_id']) && $page === 'group') {
102  $options['filter_id'] = "{$this->getEntitySubtype()}/group";
103  }
104 
105  if (!isset($options['filter_value'])) {
106  $filter_value = $page;
107  switch ($page) {
108  case 'friends':
109  $filter_value = $this->page_owner?->guid === elgg_get_logged_in_user_guid() ? 'friends' : 'none';
110  break;
111  case 'group':
112  $filter_value = 'all';
113  break;
114  case 'owner':
115  $filter_value = $this->page_owner?->guid === elgg_get_logged_in_user_guid() ? 'mine' : 'none';
116  break;
117  }
118 
119  $options['filter_value'] = $filter_value;
120  }
121 
122  if (!isset($options['sidebar'])) {
123  $sidebar_view = $this->route?->getOption('sidebar_view');
124  if (!empty($sidebar_view) && elgg_view_exists($sidebar_view)) {
125  $options['sidebar'] = elgg_view($sidebar_view, [
126  'page' => $page,
127  'entity' => $this->page_owner,
128  ]);
129  }
130  }
131 
132  if (!isset($options['title'])) {
133  if ($page === 'owner') {
134  $options['title'] = elgg_echo("collection:{$this->getEntityType()}:{$this->getEntitySubtype()}:{$page}", [$this->page_owner?->getDisplayName()]);
135  } else {
136  $options['title'] = elgg_echo("collection:{$this->getEntityType()}:{$this->getEntitySubtype()}:{$page}");
137  }
138  }
139 
140  return $options;
141  }
142 
154  protected function listAll(array $options): string {
155  return elgg_view_page('', $this->getPageOptions('all', [
156  'content' => elgg_view('page/list/all', [
157  'options' => $options,
158  'page' => 'all',
159  ]),
160  'filter_value' => 'all',
161  ]));
162  }
163 
175  protected function listFriends(array $options): string {
176  if (!$this->page_owner instanceof \ElggUser) {
177  throw new BadRequestException();
178  }
179 
180  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner, true);
181 
182  $friends_options = [
183  'relationship' => 'friend',
184  'relationship_guid' => $this->page_owner->guid,
185  'relationship_join_on' => 'owner_guid',
186  ];
187 
188  return elgg_view_page('', $this->getPageOptions('friends', [
189  'content' => elgg_view('page/list/all', [
190  'entity' => $this->page_owner,
191  'options' => array_merge($options, $friends_options),
192  'page' => 'friends',
193  ]),
194  ]));
195  }
196 
209  protected function listGroup(array $options): string {
210  if (!$this->page_owner instanceof \ElggGroup) {
211  throw new BadRequestException();
212  }
213 
214  $group_tool = $this->getGroupToolOption();
215  if (isset($group_tool)) {
216  elgg_group_tool_gatekeeper($group_tool);
217  }
218 
219  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
220 
221  $group_options = [
222  'container_guid' => $this->page_owner->guid,
223  'preload_containers' => false,
224  ];
225 
226  return elgg_view_page('', $this->getPageOptions('group', [
227  'content' => elgg_view('page/list/all', [
228  'entity' => $this->page_owner,
229  'options' => array_merge($options, $group_options),
230  'page' => 'group',
231  ]),
232  ]));
233  }
234 
246  protected function listOwner(array $options): string {
247  if (!$this->page_owner instanceof \ElggUser) {
248  throw new BadRequestException();
249  }
250 
251  elgg_push_collection_breadcrumbs($options['type'], $options['subtype'], $this->page_owner);
252 
253  $owner_options = [
254  'owner_guid' => $this->page_owner->guid,
255  'preload_owners' => false,
256  ];
257 
258  return elgg_view_page('', $this->getPageOptions('owner', [
259  'content' => elgg_view('page/list/all', [
260  'entity' => $this->page_owner,
261  'options' => array_merge($options, $owner_options),
262  'page' => 'owner',
263  ]),
264  ]));
265  }
266 }
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.
getPage()
{Get the page from the route name.string }
assertValidRoute()
{Validate that route is supported.void }
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.for which page to get the options ('all',...
handleRequest(\Elgg\Request $request)
{Handle the request.the Elgg requestResponse }
parseRoute()
Parse the route name to usable parts.
listOwner(array $options)
Owner content item listing.
getGroupToolOption()
Get the name of the group tool option for group routes.
Generic base controller for content listing and content view/edit/add.
Thrown when request is malformatted.
Thrown when the requested group tool isn't enabled for a group.
Response builder.
Definition: Response.php:14
Request container.
Definition: Request.php:12
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