Elgg  Version 6.0
trash.php
Go to the documentation of this file.
1 <?php
6 $guid = (int) get_input('guid');
8 if (!$entity instanceof \ElggEntity) {
9  return elgg_error_response(elgg_echo('entity:delete:item_not_found'));
10 }
11 
12 if (!$entity->canDelete() || !$entity->hasCapability('restorable') || $entity instanceof \ElggPlugin || $entity instanceof \ElggSite || $entity instanceof \ElggUser) {
13  return elgg_error_response(elgg_echo('entity:delete:permission_denied'));
14 }
15 
16 // determine what name to show on success
17 $display_name = $entity->getDisplayName() ?: elgg_echo('entity:delete:item');
18 
19 $type = $entity->getType();
20 $subtype = $entity->getSubtype();
21 $container = $entity->getContainerEntity();
22 
23 if (!$entity->delete(true, false)) {
24  return elgg_error_response(elgg_echo('entity:delete:fail', [$display_name]));
25 }
26 
27 // determine forward URL
28 $forward_url = get_input('forward_url');
29 if (!empty($forward_url)) {
31 }
32 
33 if (empty($forward_url)) {
35  $referrer_url = elgg_extract('HTTP_REFERER', $_SERVER, '');
37 
38  $find_forward_url = function (\ElggEntity $container = null) use ($type, $subtype) {
39  $routes = _elgg_services()->routes;
40 
41  // check if there is a collection route (eg. blog/owner/username)
42  $route_name = false;
43  if ($container instanceof \ElggUser) {
44  $route_name = "collection:{$type}:{$subtype}:owner";
45  } elseif ($container instanceof \ElggGroup) {
46  $route_name = "collection:{$type}:{$subtype}:group";
47  }
48 
49  if ($route_name && $routes->get($route_name)) {
50  $params = $routes->resolveRouteParameters($route_name, $container);
51 
52  return elgg_generate_url($route_name, $params);
53  }
54 
55  // no route found, fallback to container url
56  if ($container instanceof \ElggEntity) {
57  return $container->getURL();
58  }
59 
60  // no container
61  return '';
62  };
63 
64  if (!empty($referrer_url) && elgg_strpos($referrer_url, $site_url) === 0) {
65  // referer is on current site
66  $referrer_path = elgg_substr($referrer_url, elgg_strlen($site_url));
67  $segments = explode('/', $referrer_path);
68 
69  if (in_array($guid, $segments)) {
70  // referrer URL contains a reference to the entity that will be deleted
71  $forward_url = $find_forward_url($container);
72  }
73  } elseif ($container instanceof \ElggEntity) {
74  $forward_url = $find_forward_url($container);
75  }
76 }
77 
79  "entity:delete:{$type}:{$subtype}:success",
80  "entity:delete:{$type}:success",
81  'entity:delete:success',
82 ];
83 
84 $message = '';
85 if (get_input('show_success', true)) {
86  foreach ($success_keys as $success_key) {
87  if (elgg_language_key_exists($success_key)) {
88  $message = elgg_echo($success_key, [$display_name]);
89  break;
90  }
91  }
92 }
93 
$type
Definition: trash.php:19
$params
Saves global plugin settings.
Definition: save.php:13
$message
Definition: trash.php:84
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:175
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_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$subtype
Definition: trash.php:20
$entity
Definition: trash.php:7
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
if(!$entity instanceof\ElggEntity) if(!$entity->canDelete()||!$entity->hasCapability('restorable')||$entity instanceof\ElggPlugin||$entity instanceof\ElggSite||$entity instanceof\ElggUser) $display_name
Definition: trash.php:17
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:53
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
elgg_language_key_exists(string $key, string $language= 'en')
Check if a given language key exists.
Definition: languages.php:44
elgg_error_response(string|array $message= '', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
$container
Definition: trash.php:21
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:195
$guid
Default entity trash action.
Definition: trash.php:6
$site_url
Definition: upgrade.php:3
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
$segments
Definition: admin.php:13
if(!$entity->delete(true, false)) $forward_url
Definition: trash.php:28
if(!empty($forward_url)) if(empty($forward_url)) $success_keys
Definition: trash.php:78