Elgg  Version 6.1
unsubscribe.php
Go to the documentation of this file.
1 <?php
6 $entity_guid = (int) get_input('guid');
8 $methods = (array) get_input('methods', []);
9 $all_subscriptions = (bool) get_input('all');
10 $type = get_input('type');
11 $subtype = get_input('subtype');
12 $action = get_input('action');
13 
14 $forward_url = get_input('forward_url');
15 $show_success = (bool) get_input('show_success', true);
16 
18 if (!$entity instanceof \ElggEntity) {
19  return elgg_error_response(elgg_echo('error:missing_data'));
20 }
21 
23 if (!$user instanceof \ElggUser || !$user->canEdit()) {
24  return elgg_error_response(elgg_echo('actionunauthorized'));
25 }
26 
27 $display_name = $entity->getDisplayName() ?: elgg_echo('item');
28 
29 if ($all_subscriptions) {
30  if (!$entity->removeSubscriptions($user->guid)) {
31  return elgg_error_response(elgg_echo('entity:unsubscribe:fail', [$display_name]));
32  }
33 } else {
34  if (!$entity->removeSubscription($user->guid, $methods, $type, $subtype, $action)) {
35  return elgg_error_response(elgg_echo('entity:unsubscribe:fail', [$display_name]));
36  }
37 }
38 
39 $success_keys = [
40  "entity:unsubscribe:{$entity->type}:{$entity->subtype}:success",
41  "entity:unsubscribe:{$entity->type}:success",
42  'entity:unsubscribe:success',
43 ];
44 
45 $message = '';
46 if ($show_success) {
47  foreach ($success_keys as $success_key) {
48  if (elgg_language_key_exists($success_key)) {
49  $message = elgg_echo($success_key, [$display_name]);
50  break;
51  }
52  }
53 }
54 
$show_success
Definition: unsubscribe.php:15
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.
$subtype
Definition: unsubscribe.php:11
if(!$user instanceof\ElggUser||!$user->canEdit()) $display_name
Definition: unsubscribe.php:27
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$entity_guid
Handle unsubscribing to an entity.
Definition: unsubscribe.php:6
$action
Definition: unsubscribe.php:12
$forward_url
Definition: unsubscribe.php:14
canEdit(int $user_guid=0)
Can a user edit this entity?
Definition: ElggEntity.php:397
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
$all_subscriptions
Definition: unsubscribe.php:9
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.
$message
Definition: unsubscribe.php:45
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
if(!$entity instanceof\ElggEntity) $user
Definition: unsubscribe.php:22
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
$methods
Definition: unsubscribe.php:8
$type
Definition: unsubscribe.php:10
if(!$entity->save()) $success_keys
$user_guid
Definition: unsubscribe.php:7
$entity
Definition: unsubscribe.php:17
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34