<?php
/*
Plugin Name: Alert Record Saved
Plugin URI:
Description: Send an email notification when a record is added or modified.
Version: 1.02
Requires at least: 2.16
Author: Djulia
Author URI: http://www.interactivetools.com/iforum/
License : Freeware
*/

addAction('record_postsave', '_plugin_alertRecordSaved', null, 4);
addAction('record_posterase', '_plugin_alertRecordErased', null, 4);


// send email on save
function _plugin_alertRecordSaved() { 
	global $SETTINGS, $CURRENT_USER, $tableName, $isNewRecord, $oldRecord;

	//
	$emailTemplate = "alert-record-saved.mel.php";
	$emailHeaders  = emailTemplate_load(array(
		'template'			=> $emailTemplate,
		'template'			=> saved_sendAlertMessage_pluginDir() . "/$emailTemplate",
		'subject'			=> '[CMSB :: Alert New Record Is Saved]',
		'from'				=> $SETTINGS['adminEmail'],
		'to'				=> $SETTINGS['adminEmail'],
		'placeholders'		=> array(
			'username'		=> $CURRENT_USER['username'],
			'email'			=> $CURRENT_USER['email'],
			'tableName'		=> $tableName,
			'isNewRecord'	=> $isNewRecord,
			'recordNum'		=> intval(@$_REQUEST['num']),
			'title'			=> htmlspecialchars(@$_REQUEST['title']),
			'oldTitle'		=> htmlspecialchars($oldRecord['title']),
		),
		'disabled'			=> false,
		'logging'			=> false,
	));
	$mailErrors   = sendMessage($emailHeaders);
	if ($mailErrors) { die("Mail Error: $mailErrors"); }
}


// send email on erase
function _plugin_alertRecordErased() { 
	global $SETTINGS, $CURRENT_USER, $tableName;

	//
	$emailTemplate = "alert-record-erased.mel.php";
	$emailHeaders  = emailTemplate_load(array(
		'template'			=> $emailTemplate,
		'template'			=> saved_sendAlertMessage_pluginDir() . "/$emailTemplate",
		'subject'			=> '[CMSB :: Alert Record Erased]',
		'from'				=> $SETTINGS['adminEmail'],
		'to'				=> $SETTINGS['adminEmail'],
		'placeholders'		=> array(
			'username'		=> $CURRENT_USER['username'],
			'email'			=> $CURRENT_USER['email'],
			'tableName'		=> $tableName,
			'recordNum'		=> intval(@$_REQUEST['selectedRecords'][0]),
		),
		'disabled'			=> false,
		'logging'			=> false,
	));
	$mailErrors   = sendMessage($emailHeaders);
	if ($mailErrors) { die("Mail Error: $mailErrors"); }
}


//
function saved_sendAlertMessage_pluginDir() {
	return dirname(__FILE__);
}


?>
