Elgg  Version 3.0
delete.php
Go to the documentation of this file.
1 <?php
5 $guid = (int) get_input('guid');
7 
8 if (!$entity instanceof ElggEntity) {
9  return elgg_error_response(elgg_echo('entity:delete:item_not_found'));
10 }
11 
12 if (!$entity->canDelete() || $entity instanceof ElggPlugin || $entity instanceof ElggSite) {
13  return elgg_error_response(elgg_echo('entity:delete:permission_denied'));
14 }
15 
16 set_time_limit(0);
17 
18 // determine what name to show on success
19 $display_name = $entity->getDisplayName() ?: elgg_echo('entity:delete:item');
20 
21 $type = $entity->getType();
22 $subtype = $entity->getSubtype();
23 $container = $entity->getContainerEntity();
24 
25 if (!$entity->delete()) {
26  return elgg_error_response(elgg_echo('entity:delete:fail', [$display_name]));
27 }
28 
29 // determine forward URL
31 if (!$forward_url) {
33  $referrer_url = $_SERVER['HTTP_REFERER'] ?: '';
35 
36  $find_forward_url = function (ElggEntity $container = null) use ($type, $subtype) {
37  $routes = _elgg_services()->routes;
38 
39  // check if there is a collection route (eg. blog/owner/username)
40  $route_name = false;
41  if ($container instanceof ElggUser) {
42  $route_name = "collection:{$type}:{$subtype}:owner";
43  } elseif ($container instanceof ElggGroup) {
44  $route_name = "collection:{$type}:{$subtype}:group";
45  }
46 
47  if ($route_name && $routes->get($route_name)) {
48  $params = $routes->resolveRouteParameters($route_name, $container);
49 
50  return elgg_generate_url($route_name, $params);
51  }
52 
53  // no route found, fallback to container url
54  if ($container instanceof ElggEntity) {
55  return $container->getURL();
56  }
57 
58  // no container
59  return '';
60  };
61 
62  if ($referrer_url && 0 == strpos($referrer_url, $site_url)) {
63  // referer is on current site
64  $referrer_path = substr($referrer_url, strlen($site_url));
65  $segments = explode('/', $referrer_path);
66 
67  if (in_array($guid, $segments)) {
68  // referrer URL contains a reference to the entity that will be deleted
69  $forward_url = $find_forward_url($container);
70  }
71  } elseif ($container) {
72  $forward_url = $find_forward_url($container);
73  }
74 }
75 
77  "entity:delete:$type:$subtype:success",
78  "entity:delete:$type:success",
79  "entity:delete:success",
80 ];
81 
82 $message = '';
83 foreach ($success_keys as $success_key) {
84  if (elgg_language_key_exists($success_key)) {
85  $message = elgg_echo($success_key, [$display_name]);
86  break;
87  }
88 }
89 
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:96
$display_name
Definition: delete.php:19
if(!$entity->delete()) $forward_url
Definition: delete.php:30
$params
Saves global plugin settings.
Definition: save.php:13
elgg_normalize_site_url($unsafe_url)
From untrusted input, get a site URL safe for forwarding.
Definition: output.php:237
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
$message
Definition: delete.php:82
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$guid
Delete a user.
Definition: delete.php:10
$container
Definition: delete.php:23
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_error_response($error= '', $forward_url=REFERRER, $status_code=ELGG_HTTP_OK)
Prepare an error response to be returned by a page or an action handler.
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
Definition: pagehandler.php:58
if(!$forward_url) $success_keys
Definition: delete.php:76
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif(is_callable([$item, 'getType']))
Definition: item.php:39
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
const REFERER
Definition: constants.php:52
$site_url
Definition: upgrade.php:3
$entity
Definition: delete.php:6
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
getURL()
Gets the URL for this entity.
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87