You are here

admin_content_notification.module in Admin Content Notification 7.2

Module admin_content_notification file.

File

admin_content_notification.module
View source
<?php

/**
 * @file
 * Module admin_content_notification file.
 */

/**
 * Implements hook_menu().
 */
function admin_content_notification_menu() {
  $items['admin/content/admin-content-notification'] = array(
    'title' => 'Admin Content Notification Configuration Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'admin_content_notification_settings_form',
    ),
    'access arguments' => array(
      'administer content',
    ),
    'file' => 'includes/admin_content_notification.admin.inc',
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_help().
 */
function admin_content_notification_help($path, $arg) {
  switch ($path) {
    case 'admin/help#admin_content_notification':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('It is very simple module which can be used to send email to admin or on any specific email id when a contnet has been posted on Drupal site.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_node_insert().
 */
function admin_content_notification_node_insert($node) {
  if (admin_content_notification_is_current_user_role_allowed_to_send_notification()) {
    admin_content_notification_send_email($node, TRUE);
  }
}

/**
 * Implements hook_node_update().
 */
function admin_content_notification_node_update($node) {
  if (variable_get('admin_content_notification_trigger_on_node_update', FALSE) && admin_content_notification_is_current_user_role_allowed_to_send_notification()) {
    admin_content_notification_send_email($node);
  }
}

/**
 * Return roles on which email should be send.
 *
 * @global Object $user
 *   Drupal User
 *
 * @return boolean
 *   True if mail should be send.
 */
function admin_content_notification_is_current_user_role_allowed_to_send_notification() {
  global $user;
  $allowed_roles = array_values(array_filter(variable_get('admin_content_notification_allowed_roles')));
  if (count($allowed_roles)) {
    foreach ($allowed_roles as $rid) {
      if (array_key_exists($rid, $user->roles)) {
        return TRUE;
      }
    }
    return FALSE;
  }
  return FALSE;
}

/**
 * Send email notification.
 *
 * @param Object $node
 *   Node Object.
 * @param bool $is_new
 *   True if node is new.
 */
function admin_content_notification_send_email($node, $is_new = FALSE) {
  if (in_array($node->type, variable_get('admin_content_notification_node_types', array()))) {
    drupal_mail('admin_content_notification', 'notification', admin_content_notification_get_admin_email(), language_default(), array(
      'message' => admin_content_notification_get_email_body($node, $is_new),
      'subject' => admin_content_notification_get_email_subject($node, $is_new),
    ));
  }
}

/**
 * Get Email subject.
 *
 * @return string
 *   Email subject.
 */
function admin_content_notification_get_email_subject($node, $is_new) {
  global $base_url;
  $user = user_load($node->uid);
  $variables = array(
    '!posted_by' => $user->name,
    '!content_link' => l($node->title, $base_url . '/node/' . $node->nid),
    '!content_title' => $node->title,
    '!content_type' => $node->type,
    '!action' => $is_new ? 'posted' : 'updated',
  );
  return t(variable_get('admin_content_notification_email_subject', 'A New Content is Submitted'), $variables);
}

/**
 * Get Email Body.
 *
 * @param Object $node
 *   Node Object.
 *
 * @return string
 *   Email body.
 */
function admin_content_notification_get_email_body($node, $is_new) {
  global $base_url;
  $user = user_load($node->uid);
  $variables = array(
    '!posted_by' => $user->name,
    '!content_link' => l($node->title, $base_url . '/node/' . $node->nid),
    '!content_title' => $node->title,
    '!content_type' => $node->type,
    ' !action' => $is_new ? 'posted' : 'updated',
  );
  return t(variable_get('admin_content_notification_email_body', 'A new content has been submitted on your site. <br/> Submitted by : !posted_by <br/> Content link : !content_link'), $variables);
}

/**
 * Get admin email.
 *
 * @return string
 *   Admin Email.
 */
function admin_content_notification_get_admin_email() {
  $roles = array_values(array_filter(variable_get('admin_content_notification_send_to_allowed_roles')));
  $query = 'SELECT DISTINCT(u.mail)
    FROM {users_roles} AS ur INNER JOIN {users} AS u ON u.uid = ur.uid
    WHERE ur.rid IN (:rids)';
  $result = db_query($query, array(
    ':rids' => $roles,
  ));
  $users_email = implode(',', array_unique(array_merge(explode(',', variable_get('admin_content_notification_email')), $result
    ->fetchCol())));
  return $users_email;
}

/**
 * Implements hook_mail().
 */
function admin_content_notification_mail($key, &$message, $params) {
  switch ($key) {
    case 'notification':
      $message['subject'] = $params['subject'];
      $message['body'] = explode('<br/>', $params['message']);
      break;
  }
}