Easy subscription for Drupal 7: a revision of the module
This is a little revision of Easy subscription for Drupal 7 thanks to which the notifications are sent only when the node is published and in the outgoing email are now present the title, a small preview of the contents and the link of the new node.
Here there is the code:
<?php
/**
* @file
* This is an easy subscription module
*/
/**
* DB management.
*/
function easy_subscription_form($form, &$form_state) {
$form = array();
// Add a title to help baffled users
$form['description'] = array(
'#type' => 'item',
'#title' => t('Subscribe to receive updates'),
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('email'),
'#size' => 20,
'#required' => TRUE,
'#validate' => array('filter_form_validate' => array('email_validation')),
);
$form['unsuscribe'] = array(
'#type' => 'checkbox',
'#title' => t('Rimuovi la sottoscrizione'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* Submit handler for 'add email' form.
*/
function easy_subscription_form_submit($form, &$form_state) {
if ($validation = valid_email_address($form_state['values']['email']) ){
global $_domain;
$domain_id = 0;
if ( $_domain['domain_id'] ){
$domain_id = $_domain['domain_id'];
}
// Process the submitted $entry.
$entry = array(
'email'=> $form_state['values']['email'],
'did' => $domain_id ,
);
if ( $form_state['values']['unsuscribe'] == 0 ){
$act = "create";
$msg = t("Suscribed for address ").$form_state['values']['email'];
}
else{
$act = "delete";
$msg = t("Unsuscribed for address ").$form_state['values']['email'] ;
}
$result = es_process(array ('entry' =>$entry, 'act' => $act, 'msg' => $msg) );
}
else{
$result = 'Invalid address';
}
drupal_set_message($result);
}
function es_process($parms) {
$entry = $parms['entry'];
$msg = $parms['msg'];
try {
if ($parms['act'] == 'create'){
$return_value = db_insert('easy_subscription_people')
->fields($parms['entry'])
->execute();
}
else{
$return_value = db_delete('easy_subscription_people')
->condition('email', $entry['email'])
->condition('did', $entry['did'])
->execute();
}
}
catch (Exception $e) {
drupal_set_message(t("The address is already subscribed"));
$msg = null;
}
return $msg;
}
/* Block*/
function easy_subscription_block_info(){
$blocks['subscription_form'] = array(
'info' => t('Easy Subscription Block'),
'status' => TRUE,
'region' => 'sidebar_first',
);
return $blocks;
}
function easy_subscription_block_view($delta=''){
switch ($delta){
case 'subscription_form':
$block['subject'] = t('Subscription');
$block['content'] = drupal_get_form('easy_subscription_form');
break;
}
return $block;
}
/*Action when node publish*/
function easy_subscription_node_update($node) {
$body = $node->body['und'][0]['value'];
$body = trim(strip_tags($body));
$width = 300;
if (strlen($body) > $width) {
$body_trimmed = substr($body, 0, strpos(wordwrap($body, $width), "\n"))."...";
}else {
$body_trimmed = $body;
}
$params = array( '@site' => variable_get('site_name'), '@title' => $node->title, '@url' => url('node/' . $node->nid, array('absolute' => TRUE)), '@body' => $body_trimmed, );
if (isset($node->original->status) && $node->original->status == 0 && $node->status == 1){
send_subscription_mail($params);
}
}
function send_subscription_mail($params){
global $_domain;
$domain_id = 0;
if ( $_domain['domain_id'] ){
$domain_id = $_domain['domain_id'];
}
$result = db_query("SELECT email FROM {easy_subscription_people} WHERE did = :domain_id", array( ':domain_id' => $domain_id,));
$Bcc = '';
foreach ($result as $record) {
$Bcc .= $record->email.',';
}
$module = 'easy_subscription';
$key = 'send_mail';
$email = variable_get('site_mail','');
$language = language_default();
$params;
$from = variable_get('site_name','') .'<'.variable_get('site_mail','').'>' ;
$send = FALSE;
$message = drupal_mail($module, $key, $email, $language, $params, $from, $send);
$message['headers']['Bcc'] = $Bcc;
$message['subject'] = t("New content published on @site", $params);
$message['body'] = array();
$message['body'][] = t("Hello, a new content has been published on @site, here you can see a preview:", $params);
$message['body'][] = t("@title", $params);
$message['body'][] = t("@body", $params);
$message['body'][] = t("Read more: @url", $params);
$system = drupal_mail_system($module, $key);
$message = $system->format($message);
$message['result'] = $system->mail($message);
watchdog('Easy subscription', 'e-mail: %email', array('%email' => $message['body']), WATCHDOG_NOTICE);
}
Attachment | Size |
---|---|
easy_subscription-7.x-1.3-revisited.zip | 9.67 KB |
Section:
Add new comment