Skip to content
Snippets Groups Projects
Unverified Commit f64d6788 authored by Alexander Varwijk's avatar Alexander Varwijk Committed by GitHub
Browse files

Revert "Issue #3083980 by chmez: Push notifications provider" (#11)

parent 98bf5d9d
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,6 @@
"type": "drupal-module",
"license": "GPL-2.0+",
"require": {
"drupal/open_social": "^7.2",
"minishlink/web-push": "^2.0",
"piwik/device-detector": "^3.0"
}
......
......@@ -7,7 +7,7 @@ use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
/**
* Class MessageTemplatesOverrides.
* Class MessageTemplatesOverrides
*
* @package Drupal\activity_send_push
*/
......@@ -33,7 +33,6 @@ class MessageTemplatesOverrides implements ConfigFactoryOverrideInterface {
* Templates we need to add the 'web push' destination to.
*
* @return array
* The list of templates.
*/
protected function getTemplates() {
return [
......
......@@ -5,5 +5,4 @@ core: 8.x
package: Social
configure: social_pwa.settings
dependencies:
- social:activity_send_push_notification
- social_pwa:activity_send_push
- social_pwa:activity_send_push
......@@ -75,15 +75,6 @@ function social_pwa_requirements($phase) {
return $requirements;
}
/**
* Install the "Activity Send Push Notification" module.
*/
function social_pwa_update_8001() {
\Drupal::service('module_installer')->install([
'activity_send_push_notification',
]);
}
/**
* Implements hook_uninstall().
*/
......
......@@ -5,8 +5,10 @@
* Generates and attaches the <meta> and <link> tags needed for the PWA.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\social_pwa\BrowserDetector;
/**
* Implements hook_page_attachments().
......@@ -158,3 +160,140 @@ function social_pwa_page_attachments(array &$page) {
$page['#attached']['drupalSettings']['pushNotificationPromptTime'] = (isset($prompt_time) && is_numeric($prompt_time)) ? $prompt_time : 3;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function social_pwa_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'user_register_form') {
return;
}
$push_enabled = \Drupal::config('social_pwa.settings')->get('status.all');
if (!$push_enabled) {
return;
}
// Target user.
$uid = \Drupal::routeMatch()->getParameter('user')->id();
// Current user.
$current_uid = \Drupal::currentUser()->id();
// Hide the Push notifications fieldset if target and current user is not the
// same.
if ($uid !== $current_uid) {
return;
}
// Get the device and subscription information about this user.
$useragent = $_SERVER['HTTP_USER_AGENT'];
// Browser detector.
$bd = new BrowserDetector($useragent);
// The device type for the icon.
$device_type = $bd->getDeviceType();
// The device/browser description.
$device_description = $bd->getFormattedDescription();
// Get the uploaded icon.
$icon = \Drupal::config('social_pwa.settings')->get('icons.icon');
if ($icon === NULL || !isset($icon[0])) {
return;
}
$form['push_notifications'] = [
'#type' => 'fieldset',
'#title' => t('Push notifications'),
'#tree' => TRUE,
'#attributes' => [
'class' => ['form-horizontal'],
],
];
$form['push_notifications']['description'] = [
'#markup' => t('Show notifications in the corner of your computer screen, even if the website is closed.'),
];
$form['push_notifications']['current_device'] = [
'#type' => 'details',
'#title' => t('<h5>Current device</h5>'),
'#attributes' => [
'class' => ['form-fieldset'],
],
];
// Prepare the toggle element. We determine default value in the frontend.
$form['push_notifications']['current_device']['toggle'] = [
'#type' => 'checkbox',
'#title' => '<span class="control-label__icon--bg icon-' . $device_type . '"></span>' . $device_description,
'#disabled' => FALSE,
'#default_value' => FALSE,
'#attributes' => [
'data-switch' => TRUE,
],
];
// Get the help text for enabling push notifications per (major) browser.
switch ($bd->getBrowserName()) {
case 'Chrome':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3D';
$operating_systems = ['Android', 'iOS'];
$operating_system = $bd->getOsName();
if (in_array($operating_system, $operating_systems)) {
$url .= $operating_system;
}
else {
$url .= 'Desktop';
}
$url .= '&oco=1';
break;
case 'Chrome Mobile':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3DAndroid&oco=1';
break;
case 'Chrome Mobile iOS':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3DiOS&oco=1';
break;
case 'Firefox':
case 'Firefox Focus':
case 'Firefox Mobile':
$url = 'https://support.mozilla.org/en-US/kb/push-notifications-firefox';
break;
case 'Microsoft Edge':
$url = 'https://blogs.windows.com/msedgedev/2016/05/16/web-notifications-microsoft-edge';
break;
case 'Opera':
case 'Opera Mini':
case 'Opera Mobile':
case 'Opera Next':
$url = 'http://help.opera.com/opera/Windows/1656/en/controlPages.html#manageNotifications';
break;
}
// Prepare the text. If we have an url we can point the user in the right
// direction.
if (isset($url)) {
$text = t('You have denied receiving push notifications through your browser. Please <a href="@url" target="_blank">reset your browser setting</a> for receiving notifications.', [
'@url' => $url,
]);
}
else {
$text = t('You have denied receiving push notifications through your browser. Please reset your browser setting for receiving notifications.');
}
// Prepare the blocked notice element with the help text.
$form['push_notifications']['current_device']['blocked_notice'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => ['help-block', 'blocked-notice', 'hide'],
],
'#value' => $text,
];
}
<?php
namespace Drupal\social_pwa\Plugin\Push;
use Drupal\activity_send_push_notification\PushBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\social_pwa\BrowserDetector;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Define a concrete class for PWA Push.
*
* @Push(
* id = "pwa_push",
* title = @Translation("Current device"),
* )
*/
class PwaPush extends PushBase {
/**
* TRUE if target and current user is the same.
*
* @var bool
*/
protected $isValidUser;
/**
* Constructs a PwaPush object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param int $current_user_id
* The current active user ID.
* @param int $route_user_id
* The user ID from route parameters.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
TranslationInterface $string_translation,
ConfigFactoryInterface $config_factory,
$current_user_id,
$route_user_id
) {
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$string_translation,
$config_factory,
$current_user_id
);
$this->isValidUser = $route_user_id === $current_user_id;
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('string_translation'),
$container->get('config.factory'),
$container->get('current_user')->id(),
$container->get('current_route_match')->getRawParameter('user')
);
}
/**
* {@inheritdoc}
*/
public function access() {
$config = $this->configFactory->get('social_pwa.settings');
if (!$push_enabled = $config->get('status.all')) {
return FALSE;
}
// Hide the Push notifications fieldset if target and current user is not
// the same.
if (!$this->isValidUser) {
return FALSE;
}
// Get the uploaded icon.
$icon = $config->get('icons.icon');
if ($icon === NULL || !isset($icon[0])) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function buildForm() {
$form = parent::buildForm();
// Get the device and subscription information about this user.
$useragent = $_SERVER['HTTP_USER_AGENT'];
// Browser detector.
$bd = new BrowserDetector($useragent);
// The device type for the icon.
$device_type = $bd->getDeviceType();
// The device/browser description.
$device_description = $bd->getFormattedDescription();
// Prepare the toggle element. We determine default value in the frontend.
$form['toggle'] = [
'#type' => 'checkbox',
'#title' => '<span class="control-label__icon--bg icon-' . $device_type . '"></span>' . $device_description,
'#disabled' => FALSE,
'#default_value' => FALSE,
'#attributes' => [
'data-switch' => TRUE,
],
];
// Get the help text for enabling push notifications per (major) browser.
switch ($bd->getBrowserName()) {
case 'Chrome':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3D';
$operating_systems = ['Android', 'iOS'];
$operating_system = $bd->getOsName();
if (in_array($operating_system, $operating_systems)) {
$url .= $operating_system;
}
else {
$url .= 'Desktop';
}
$url .= '&oco=1';
break;
case 'Chrome Mobile':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3DAndroid&oco=1';
break;
case 'Chrome Mobile iOS':
$url = 'https://support.google.com/chrome/answer/3220216?co=GENIE.Platform%3DiOS&oco=1';
break;
case 'Firefox':
case 'Firefox Focus':
case 'Firefox Mobile':
$url = 'https://support.mozilla.org/en-US/kb/push-notifications-firefox';
break;
case 'Microsoft Edge':
$url = 'https://blogs.windows.com/msedgedev/2016/05/16/web-notifications-microsoft-edge';
break;
case 'Opera':
case 'Opera Mini':
case 'Opera Mobile':
case 'Opera Next':
$url = 'http://help.opera.com/opera/Windows/1656/en/controlPages.html#manageNotifications';
break;
}
// Prepare the text. If we have an url we can point the user in the right
// direction.
if (isset($url)) {
$text = $this->t('You have denied receiving push notifications through your browser. Please <a href="@url" target="_blank">reset your browser setting</a> for receiving notifications.', [
'@url' => $url,
]);
}
else {
$text = $this->t('You have denied receiving push notifications through your browser. Please reset your browser setting for receiving notifications.');
}
// Prepare the blocked notice element with the help text.
$form['blocked_notice'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => ['help-block', 'blocked-notice', 'hide'],
],
'#value' => $text,
];
return $form;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment