/** * Add a subscribe/unsubscribe link to the supported entity types * * @param string $hook the name of the hook * @param string $type the type of the hook * @param \ElggMenuItem[] $return_value the current menu items * @param array $params supplied params * * @return void|\ElggMenuItem[] */ public static function register($hook, $type, $return_value, $params) { if (!elgg_is_logged_in()) { return; } if (empty($params) || !is_array($params)) { return; } $entity = elgg_extract('entity', $params); if (empty($entity) || !content_subscriptions_can_subscribe($entity)) { return; } $subscribed = false; if (content_subscriptions_check_subscription($entity->getGUID())) { $subscribed = true; } $methods = content_subscriptions_get_notification_settings(); if (!empty($methods)) { $return_value[] = \ElggMenuItem::factory(['name' => 'content_subscription_subscribe', 'text' => elgg_echo('content_subscriptions:subscribe'), 'href' => "action/content_subscriptions/subscribe?entity_guid={$entity->getGUID()}", 'is_action' => true, 'priority' => 100, 'item_class' => $subscribed ? 'hidden' : '']); } $return_value[] = \ElggMenuItem::factory(['name' => 'content_subscription_unsubscribe', 'text' => elgg_echo('content_subscriptions:unsubscribe'), 'href' => "action/content_subscriptions/subscribe?entity_guid={$entity->getGUID()}", 'is_action' => true, 'priority' => 101, 'item_class' => $subscribed ? '' : 'hidden']); return $return_value; }
<?php elgg_require_js('content_subscriptions/personal_notifications'); $user = elgg_extract('user', $vars); $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethods(); $notification_settings = content_subscriptions_get_notification_settings($user->getGUID()); $content = elgg_format_element('td', ['class' => 'namefield'], elgg_view('output/longtext', ['value' => elgg_echo('content_subscriptions:settings:description')])); $i = 0; foreach ($NOTIFICATION_HANDLERS as $method) { $checkbox_settings = ['id' => "content_subscriptions_{$method}_checkbox", 'name' => "content_subscriptions_{$method}", 'value' => $method, 'onclick' => "adjust{$method}('content_subscriptions_{$method}')"]; if (in_array($method, $notification_settings)) { $checkbox_settings['checked'] = true; } if ($i > 0) { $content .= elgg_format_element('td', ['class' => 'spacercolumn'], ' '); } $content .= elgg_format_element('td', ['class' => "{$method}togglefield"], elgg_view('output/url', ['border' => '0', 'id' => "content_subscriptions_{$method}", 'class' => "{$method}toggleOff", 'onclick' => "adjust{$method}_alt('content_subscriptions_{$method}')", 'text' => elgg_view('input/checkbox', $checkbox_settings)])); $i++; } $content .= elgg_format_element('td', [], ' '); $content = elgg_format_element('tr', [], $content); echo elgg_format_element('table', ['id' => 'content-subscriptions-notification-settings', 'class' => 'hidden'], $content);
/** * Verify that the subscribed users still have their preferences * * @param string $hook the name of the hook * @param stirng $type the type of the hook * @param array $return_value the current return value * @param array $params supplied values * * @return array */ function content_subscriptions_get_subscriptions_verify_hook($hook, $type, $return_value, $params) { if (empty($params) || !is_array($params)) { return $return_value; } if (empty($return_value)) { // no subscribers to check return $return_value; } $event = elgg_extract("event", $params); if (empty($event) || !$event instanceof \Elgg\Notifications\Event) { return $return_value; } $object = $event->getObject(); if (empty($object) || !elgg_instanceof($object, "object", "discussion_reply") && !elgg_instanceof($object, "object", "comment")) { return $return_value; } foreach ($return_value as $user_guid => $preferences) { $settings = content_subscriptions_get_notification_settings($user_guid); if (!empty($settings)) { $return_value[$user_guid] = $settings; } else { unset($return_value[$user_guid]); } } return $return_value; }
/** * Verify that the subscribed users still have their preferences * * @param string $hook the name of the hook * @param stirng $type the type of the hook * @param array $return_value the current return value * @param array $params supplied values * * @return void|array */ public static function verifySubscribersSettings($hook, $type, $return_value, $params) { if (empty($params) || !is_array($params)) { return; } if (empty($return_value)) { // no subscribers to check return; } $event = elgg_extract("event", $params); if (!$event instanceof \Elgg\Notifications\Event) { return; } $object = $event->getObject(); if (!$object instanceof \ElggComment) { return; } foreach ($return_value as $user_guid => $preferences) { $settings = content_subscriptions_get_notification_settings($user_guid); if (!empty($settings)) { $return_value[$user_guid] = $settings; } else { unset($return_value[$user_guid]); } } return $return_value; }