Elgg  Version 7.1
subscriptions.php
Go to the documentation of this file.
1 <?php
6 $guid = (int) get_input('guid');
8 
9 if (!$user || !$user->canEdit()) {
10  return elgg_error_response(elgg_echo('actionunauthorized'));
11 }
12 
14 if (empty($methods)) {
15  return elgg_error_response(elgg_echo('usersettings:notifications:subscriptions:save:fail'));
16 }
17 
18 $subscriptions = (array) get_input('subscriptions', []);
19 if (empty($subscriptions)) {
20  return elgg_error_response(elgg_echo('error:missing_data'));
21 }
22 
23 $cleanup_subscriptions = (bool) get_input('cleanup_subscriptions', false);
24 
26  'guids' => array_keys($subscriptions),
27  'limit' => false,
28  'batch' => true,
29 ]);
30 
32 foreach ($targets as $target) {
33  $keys = $subscriptions[$target->guid];
34 
35  $saved_relationships = [];
36  foreach ($keys as $key => $preferred_methods) {
37  list (, $type, $subtype, $action) = array_pad(explode(':', $key), 4, null); // array_pad() is to prevent PHP notice undefined index in list()
38 
39  if (!is_array($preferred_methods)) {
40  $preferred_methods = [];
41  }
42 
43  foreach ($methods as $method) {
44  if (in_array($method, $preferred_methods)) {
45  $target->addSubscription($user->guid, $method, $type, $subtype, $action);
46  $saved_relationships[] = _elgg_services()->subscriptions->getSubscriptionRelationshipName($method, $type, $subtype, $action);
47  } else {
48  $target->removeSubscription($user->guid, $method, $type, $subtype, $action);
49  }
50  }
51  }
52 
54  continue;
55  }
56 
57  $all_subscriptions = $target->getSubscriptions($user->guid);
58  foreach ($all_subscriptions as $subscription) {
59  if (!in_array($subscription->relationship, $saved_relationships)) {
60  $subscription->delete();
61  }
62  }
63 }
64 
65 return elgg_ok_response('', elgg_echo('usersettings:notifications:subscriptions:save:ok'));
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ 'email' => 'email', 'sms' => 'sms',...
$target
Definition: create.php:17
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_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.
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$action
Definition: subscribe.php:11
$preferred_methods
Definition: record.php:28
if(empty($subscriptions)) $cleanup_subscriptions
$user
if(! $user||! $user->canEdit()) $methods
$targets
if(empty($methods)) $subscriptions
$guid
Saves subscription record notification settings.
$all_subscriptions
Definition: unsubscribe.php:9
$keys
Definition: access.php:31