Elgg  Version master
delete.php
Go to the documentation of this file.
1 <?php
6 $guid = (int) get_input('guid');
7 
9 if (!$entity instanceof \ElggEntity) {
10  return elgg_error_response(elgg_echo('entity:delete:item_not_found'));
11 }
12 
13 if (!$entity->canDelete() || $entity instanceof \ElggPlugin || $entity instanceof \ElggSite || $entity instanceof \ElggUser) {
14  return elgg_error_response(elgg_echo('entity:delete:permission_denied'));
15 }
16 
17 set_time_limit(0);
18 
19 // determine what name to show on success
20 $display_name = $entity->getDisplayName() ?: elgg_echo('entity:delete:item');
21 
22 $type = $entity->getType();
23 $subtype = $entity->getSubtype();
24 $container = $entity->getContainerEntity();
25 
26 if (!$entity->delete()) {
27  return elgg_error_response(elgg_echo('entity:delete:fail', [$display_name]));
28 }
29 
30 // determine forward URL
31 $forward_url = elgg_normalize_site_url((string) get_input('forward_url'));
32 if (!$forward_url) {
34  $referrer_url = elgg_extract('HTTP_REFERER', $_SERVER, '');
36 
37  $find_forward_url = function (\ElggEntity $container = null) use ($type, $subtype) {
38  $routes = _elgg_services()->routes;
39 
40  // check if there is a collection route (eg. blog/owner/username)
41  $route_name = false;
42  if ($container instanceof \ElggUser) {
43  $route_name = "collection:{$type}:{$subtype}:owner";
44  } elseif ($container instanceof \ElggGroup) {
45  $route_name = "collection:{$type}:{$subtype}:group";
46  }
47 
48  if ($route_name && $routes->get($route_name)) {
49  $params = $routes->resolveRouteParameters($route_name, $container);
50 
51  return elgg_generate_url($route_name, $params);
52  }
53 
54  // no route found, fallback to container url
55  if ($container instanceof \ElggEntity) {
56  return $container->getURL();
57  }
58 
59  // no container
60  return '';
61  };
62 
63  if (!empty($referrer_url) && elgg_strpos($referrer_url, $site_url) === 0) {
64  // referer is on current site
65  $referrer_path = elgg_substr($referrer_url, elgg_strlen($site_url));
66  $segments = explode('/', $referrer_path);
67 
68  if (in_array($guid, $segments)) {
69  // referrer URL contains a reference to the entity that will be deleted
70  $forward_url = $find_forward_url($container);
71  }
72  } elseif ($container instanceof \ElggEntity) {
73  $forward_url = $find_forward_url($container);
74  }
75 }
76 
78  "entity:delete:{$type}:{$subtype}:success",
79  "entity:delete:{$type}:success",
80  'entity:delete:success',
81 ];
82 
83 $message = '';
84 if (get_input('show_success', true)) {
85  foreach ($success_keys as $success_key) {
86  if (elgg_language_key_exists($success_key)) {
87  $message = elgg_echo($success_key, [$display_name]);
88  break;
89  }
90  }
91 }
92 
$display_name
Definition: delete.php:20
$params
Saves global plugin settings.
Definition: save.php:13
elgg_normalize_site_url(string $unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:160
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
$type
Definition: delete.php:22
$message
Definition: delete.php:83
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
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:67
elgg_strpos()
Wrapper function for mb_strpos().
Definition: mb_wrapper.php:71
$container
Definition: delete.php:24
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
if(!$forward_url) $success_keys
Definition: delete.php:77
if(!$entity->delete()) $forward_url
Definition: delete.php:31
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:230
$subtype
Definition: delete.php:23
$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:346
$segments
Definition: admin.php:13
$guid
Delete a user.
Definition: delete.php:10
$entity
Definition: delete.php:8