Elgg  Version 6.1
delete.php
Go to the documentation of this file.
1 <?php
6 $guid = (int) get_input('guid');
8  return get_entity($guid);
9 });
10 if (!$entity instanceof \ElggEntity) {
11  return elgg_error_response(elgg_echo('entity:delete:item_not_found'));
12 }
13 
14 if (!$entity->canDelete() || $entity instanceof \ElggPlugin || $entity instanceof \ElggSite || $entity instanceof \ElggUser) {
15  return elgg_error_response(elgg_echo('entity:delete:permission_denied'));
16 }
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(true, true)) {
26  return elgg_error_response(elgg_echo('entity:delete:fail', [$display_name]));
27 }
28 
29 // determine forward URL
30 $forward_url = get_input('forward_url');
31 if (!empty($forward_url)) {
33 }
34 
35 if (empty($forward_url)) {
37  $referrer_url = elgg_extract('HTTP_REFERER', $_SERVER, '');
39 
40  $find_forward_url = function (\ElggEntity $container = null) use ($type, $subtype) {
41  $routes = _elgg_services()->routes;
42 
43  // check if there is a collection route (eg. blog/owner/username)
44  $route_name = false;
45  if ($container instanceof \ElggUser) {
46  $route_name = "collection:{$type}:{$subtype}:owner";
47  } elseif ($container instanceof \ElggGroup) {
48  $route_name = "collection:{$type}:{$subtype}:group";
49  }
50 
51  if ($route_name && $routes->get($route_name)) {
52  $params = $routes->resolveRouteParameters($route_name, $container);
53 
54  return elgg_generate_url($route_name, $params);
55  }
56 
57  // no route found, fallback to container url
58  if ($container instanceof \ElggEntity) {
59  return $container->getURL();
60  }
61 
62  // no container
63  return '';
64  };
65 
66  if (!empty($referrer_url) && elgg_strpos($referrer_url, $site_url) === 0) {
67  // referer is on current site
68  $referrer_path = elgg_substr($referrer_url, elgg_strlen($site_url));
69  $segments = explode('/', $referrer_path);
70 
71  if (in_array($guid, $segments)) {
72  // referrer URL contains a reference to the entity that will be deleted
73  $forward_url = $find_forward_url($container);
74  }
75  } elseif ($container instanceof \ElggEntity) {
76  $forward_url = $find_forward_url($container);
77  }
78 }
79 
81  "entity:delete:{$type}:{$subtype}:success",
82  "entity:delete:{$type}:success",
83  'entity:delete:success',
84 ];
85 
86 $message = '';
87 if (get_input('show_success', true)) {
88  foreach ($success_keys as $success_key) {
89  if (elgg_language_key_exists($success_key)) {
90  $message = elgg_echo($success_key, [$display_name]);
91  break;
92  }
93  }
94 }
95 
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:306
$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: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
$type
Definition: delete.php:21
$message
Definition: delete.php:86
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:256
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
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
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
if(!$entity->delete(true, true)) $forward_url
Definition: delete.php:30
$container
Definition: delete.php:23
elgg_get_site_url()
Get the URL for the current (or specified) site, ending with "/".
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
if(!$entity instanceof\ElggEntity) if(!$entity->canDelete()||$entity instanceof\ElggPlugin||$entity instanceof\ElggSite||$entity instanceof\ElggUser) $display_name
Definition: delete.php:19
elgg_substr()
Wrapper function for mb_substr().
Definition: mb_wrapper.php:195
$subtype
Definition: delete.php:22
$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:353
$segments
Definition: admin.php:13
$guid
Delete a user.
Definition: delete.php:10
if(!empty($forward_url)) if(empty($forward_url)) $success_keys
Definition: delete.php:80
$entity
Definition: delete.php:7