You are here

function pwa_webpush_send_notification in Progressive Web App 7.2

_state

Parameters

$form:

1 string reference to 'pwa_webpush_send_notification'
pwa_webpush_menu in modules/pwa_webpush/pwa_webpush.module
Implements hook_menu().

File

modules/pwa_webpush/pwa_webpush.admin.inc, line 136
PWA administration forms.

Code

function pwa_webpush_send_notification($form, &$form_state) {

  // Only users that have an active subscription.
  $uids = db_query('SELECT DISTINCT(uid) FROM {pwa_webpush_subscription} WHERE expired = 0')
    ->fetchCol();
  $users = user_load_multiple($uids);
  $form['users'] = [
    '#type' => 'select',
    '#title' => t('Send notification to users'),
    '#description' => t('Only users who have an active subscriptions are listed.'),
    '#required' => TRUE,
    '#multiple' => TRUE,
    '#options' => array_map(function ($u) {
      return $u->uid == 0 ? 'anonymous' : $u->name;
    }, $users),
  ];
  $form['notification'] = [
    '#type' => 'fieldset',
    '#title' => t('Notification'),
  ];
  $form['notification']['title'] = [
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#description' => '',
    '#default_value' => '',
  ];
  $form['notification']['body'] = [
    '#type' => 'textarea',
    '#title' => t('Body (plain text)'),
    '#required' => TRUE,
    '#description' => '',
    '#default_value' => '',
  ];
  $form['notification']['url'] = [
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#description' => t('Destination when clicking on the notification.'),
    '#default_value' => url('<front>', [
      'absolute' => TRUE,
    ]),
  ];
  $form['metadata'] = [
    '#type' => 'fieldset',
    '#title' => t('Metadata'),
  ];

  // Get icons from the manifest file.
  $icons = [];
  foreach (_pwa_manifest_file()['icons'] as $icon) {
    $icons[$icon['src']] = basename($icon['src']) . ' (' . $icon['sizes'] . ')';
  }
  $form['metadata']['icon'] = [
    '#type' => 'select',
    '#title' => t('Notification icon'),
    '#description' => '',
    '#options' => $icons,
  ];
  $form['metadata']['tag'] = [
    '#type' => 'textfield',
    '#title' => t('Tag'),
    '#description' => t('Used to group and manage notifications on the client side.'),
    '#default_value' => '',
  ];
  $form['actions'] = [];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Send notification'),
  ];
  return $form;
}