You are here

classified_notifications.module in Classified Ads 7.3

Same filename and directory in other branches
  1. 6.3 modules/classified_notifications/classified_notifications.module

Optional notification features for classified module.

  • Allows notifications for:

    • Expired ads,
    • Purged ads,
    • Intermediary lifetime intervals reached.
  • Same-kind notifications are grouped by user.
  • Notifications sending is not synchronous with gathering, but batched using Queue API.

Copyright 2009 Ouest Systemes Informatiques (OSInet)

Licensed under the General Public License, version 2 or later

Original code, not derived from the ed_classified module.

@todo Convert to Queue API http://api.drupal.org/api/search/7/queue%20operations Note that, unlike job_queue, Queue API does not provide a default queue consumer.

File

modules/classified_notifications/classified_notifications.module
View source
<?php

/**
 * @file
 * Optional notification features for classified module.
 *
 * - Allows notifications for:
 *   - Expired ads,
 *   - Purged ads,
 *   - Intermediary lifetime intervals reached.
 * - Same-kind notifications are grouped by user.
 * - Notifications sending is not synchronous with gathering, but batched using
 *   Queue API.
 *
 * Copyright 2009 Ouest Systemes Informatiques (OSInet)
 *
 * Licensed under the General Public License, version 2 or later
 *
 * Original code, not derived from the ed_classified module.
 *
 * @todo Convert to Queue API
 *   http://api.drupal.org/api/search/7/queue%20operations
 *   Note that, unlike job_queue, Queue API does not provide a default queue
 *   consumer.
 */

/**
 * Implements hook_classified_expire_alter().
 */
function classified_notifications_classified_expire_alter($ads_bunch) {
  foreach ($ads_bunch as $uid => $ads) {

    // TODO Convert "user_load" to "user_load_multiple" if "$uid" is not a uid.
    // To return a single user object, wrap "user_load_multiple" with
    // "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $uid))
    $account = user_load($uid);
    $account->ads = $ads;

    // Do not notify anonyms or users without mail.
    if (!$account->uid || !$account->mail) {
      continue;
    }
    $params['account'] = $account;
    $message = drupal_mail('classified_notifications', 'expire', $account->mail, user_preferred_language($account), $params, NULL, FALSE);
    $job = array(
      'description' => t('Classified Ads: expire notifications for !name (@mail)', array(
        '!name' => l($account->name, 'user/' . $account->uid),
        '@mail' => $account->mail,
      )),
      'key' => 'expire',
      'message' => $message,
    );
    $queue = DrupalQueue::get('classified_notifications', TRUE);
    $queue
      ->createQueue();
    $queue
      ->createItem($job);
  }
}

/**
 * Implements hook_classified_notify_alter().
 */
function classified_notifications_classified_notify_alter($ads_bunch, $kind) {
  foreach ($ads_bunch as $uid => $ads) {

    // TODO Convert "user_load" to "user_load_multiple" if "$uid" is not a uid.
    // To return a single user object, wrap "user_load_multiple" with
    // "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $uid))
    $account = user_load($uid);
    $account->ads = $ads;

    // Do not notify anonyms or users without mail.
    if (!$account->uid || !$account->mail) {
      continue;
    }
    $params['account'] = $account;
    $message = drupal_mail('classified_notifications', $kind, $account->mail, user_preferred_language($account), $params, NULL, FALSE);
    $job = array(
      'description' => t('Classified Ads: lifetime notifications (@kind) for !name (@mail)', array(
        '!name' => l($account->name, 'user/' . $account->uid),
        '@kind' => $kind,
        '@mail' => $account->mail,
      )),
      'key' => $kind,
      'message' => $message,
    );
    $queue = DrupalQueue::get('classified_notifications', TRUE);
    $queue
      ->createQueue();
    $queue
      ->createItem($job);
  }
}

/**
 * Implements hook_classified_purge_alter().
 */
function classified_notifications_classified_purge_alter($ads_bunch) {
  foreach ($ads_bunch as $uid => $ads) {

    // TODO Convert "user_load" to "user_load_multiple" if "$uid" is not a uid.
    // To return a single user object, wrap "user_load_multiple" with
    // "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $uid))
    $account = user_load($uid);
    $account->ads = $ads;

    // Do not notify anonyms or users without mail.
    if (!$account->uid || !$account->mail) {
      continue;
    }
    $params['account'] = $account;
    $message = drupal_mail('classified_notifications', 'purge', $account->mail, user_preferred_language($account), $params, NULL, TRUE);
    $job = array(
      'description' => t('Classified Ads: purge notifications for !name (@mail)', array(
        '!name' => l($account->name, 'user/' . $account->uid),
        '@mail' => $account->mail,
      )),
      'key' => 'purge',
      'message' => $message,
    );
    $queue = DrupalQueue::get('classified_notifications', TRUE);
    $queue
      ->createQueue();
    $queue
      ->createItem($job);
  }
}

/**
 * Implements hook_mail().
 *
 * @TODO support multiple languages, based on user language, not site language.
 */
function classified_notifications_mail($key, &$message, $params) {
  if (in_array($key, array(
    'half-life',
    'pre-expire',
    'expire',
    'pre-purge',
    'purge',
  ))) {
    $data = array(
      'subject' => token_replace(_classified_get('notifications-' . $key . '-subject'), array(
        'user' => $params['account'],
      )),
      'body' => array(
        token_replace(_classified_get('notifications-' . $key . '-body'), array(
          'user' => $params['account'],
        )),
      ),
    );
    $message = array_merge($message, $data);
  }
}

/**
 * Implements hook_cron_queue_info().
 *
 * Declare our worker to actually deliver mails.
 */
function classified_notifications_cron_queue_info() {
  $queues['classified_notifications'] = array(
    'worker callback' => 'classified_notifications_deliver',
    'time' => 15,
  );
  return $queues;
}

/**
 * Callback for cron to send queues mail jobs.
 *
 * @param array $job
 *   The job about which info has to be sent.
 *
 * @throws \Exception
 */
function classified_notifications_deliver(array $job) {
  drupal_mail_system('classified_notifications', $job['key'])
    ->mail($job['message']);
}

/**
 * Implements hook_menu().
 *
 * @see classified_notifications_menu_alter()
 */
function classified_notifications_menu() {
  $items['admin/config/content/classified/notifications'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'Notifications',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'classified_notifications_admin_settings',
    ),
    'file' => 'classified_notifications.admin.inc',
    // Permission from classified.module.
    'access arguments' => array(
      'administer classified ads',
    ),
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 *
 * Convert admin/settings/classified from a normal item to a default local task.
 */
function classified_notifications_menu_alter(&$items) {
  $items['admin/config/content/classified/base'] = array_merge($items['admin/config/content/classified'], array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'title' => 'Basic settings',
    'weight' => -1,
  ));
  return;

  // Dead code below, just to force potx to add the string to the translation.
  // phpcs:disable Squiz.PHP.NonExecutableCode
  t('Basic settings');
}