git-svn-id: https://192.168.0.254/svn/Proyectos.FundacionLQDVI_WebCongresos/trunk@2 94ccb1af-fd9d-d947-8d90-7f70ea60afc8
1636 lines
50 KiB
PHP
1636 lines
50 KiB
PHP
<?php
|
|
/*
|
|
* @component SQL 2 Excel Component
|
|
* @copyright Copyright (C) Joomla-R-Us, joomla-r-us.com
|
|
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
|
|
*/
|
|
|
|
// no direct access
|
|
defined( '_JEXEC' ) or die( 'Restricted access' );
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
// Debugging
|
|
//
|
|
// Change to define( 'DEBUG',1); for debugging
|
|
///////////////////////////////////////////////////////
|
|
define( 'DEBUG', 0 );
|
|
///////////////////////////////////////////////////////
|
|
|
|
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_sql2excel'.DS.'controllers'.DS.'download.php' );
|
|
require_once( JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'common.php' );
|
|
require_once( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'parms.php');
|
|
require_once( JPATH_SITE.DS.'components'.DS.'com_sql2excel'.DS.'helpers'.DS.'zip.lib.php');
|
|
|
|
$SCHEDULE_ERR_MSG = '';
|
|
|
|
class Sql2excelSchedule
|
|
{
|
|
|
|
function compress_file($fileName, $zipFN) {
|
|
$ret = false;
|
|
|
|
if (DEBUG)
|
|
print "Compress " . $fileName . " into " . $zipFN . "\n";
|
|
|
|
|
|
$fsize = @filesize($fileName);
|
|
if ( $fsize > 0 ) {
|
|
$fh = fopen($fileName, 'rb', false);
|
|
if ($fh == false) {
|
|
die("Can't read file.");
|
|
}
|
|
$data = fread($fh, $fsize);
|
|
fclose($fh);
|
|
|
|
$fh = fopen($fileName, 'rb', false);
|
|
|
|
|
|
$zip = new zipfile();
|
|
$fh = fopen($fileName, 'rb', false);
|
|
$data = fread($fh, $fsize);
|
|
fclose($fh);
|
|
|
|
$path_info = pathinfo($fileName);
|
|
$baseFN = $path_info['basename'];
|
|
$zip->addFile($data,$baseFN);
|
|
$zipcontents = $zip->file();
|
|
|
|
$fh = fopen($zipFN, 'wb', false);
|
|
fputs($fh,$zipcontents);
|
|
fclose($fh);
|
|
|
|
// Remove original file
|
|
unlink($fileName);
|
|
|
|
if (DEBUG)
|
|
print "Compression Done\n";
|
|
|
|
$ret = true;
|
|
} elseif ( DEBUG ) {
|
|
print "ERROR : Filesize == 0\n";
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
function getNextDate( $frequency, $currNextDate ) {
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
function get_all_workbooks() {
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT a.* ' .
|
|
'FROM #__sql2excel_workbooks a ' .
|
|
'WHERE a.published=1';
|
|
$db->setQuery( $query );
|
|
$wbList = $db->loadObjectList();
|
|
return $wbList;
|
|
}
|
|
|
|
|
|
function get_next_date($schedule) {
|
|
|
|
$curr_next_date = $schedule->ux_next_date;
|
|
$now = $schedule->ux_now;
|
|
|
|
$next_date = '';
|
|
$what = '';
|
|
$interval = 0;
|
|
switch ($schedule->frequency) {
|
|
case 10:
|
|
// Hourly
|
|
$interval = 60 * 60;
|
|
$what = 'HOUR';
|
|
break;
|
|
case 20:
|
|
// Daily
|
|
$interval = 60 * 60 * 24;
|
|
$what = 'DAY';
|
|
break;
|
|
case 30:
|
|
// Weekly
|
|
$interval = 60 * 60 * 24 * 7;
|
|
$what = 'DAY';
|
|
break;
|
|
case 40:
|
|
// Monthly
|
|
$interval = 60 * 60 * 24 * 30;
|
|
$what = 'MONTH';
|
|
break;
|
|
case 50:
|
|
// Yearly
|
|
$interval = 60 * 60 * 24 * 30;
|
|
$what = 'YEAR';
|
|
break;
|
|
}
|
|
|
|
// Calculate how many hours/days etc that needs to be added
|
|
if ( $interval > 0 ) {
|
|
if (DEBUG)
|
|
print "INTERVAL=" . $interval . "\n";
|
|
|
|
$cnt = 0;
|
|
while ( $curr_next_date < $now ) {
|
|
$curr_next_date = $curr_next_date + $interval;
|
|
$cnt++;
|
|
}
|
|
|
|
if ( $cnt > 0 ) {
|
|
if ( $schedule->frequency == 30 ) {
|
|
// Weekly, but use days as interval
|
|
$cnt = $cnt * 7;
|
|
}
|
|
$next_date = ', next_date = DATE_ADD(next_date, INTERVAL ' . $cnt . ' ' . $what . ') ';
|
|
if (DEBUG)
|
|
print "NEXT DATE=" . $next_date . "\n";
|
|
return $next_date;
|
|
}
|
|
}
|
|
|
|
// Something went wrong, do not update next_date
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
function get_sched_workbooks($sched_id) {
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT a.* ' .
|
|
'FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b ' .
|
|
'WHERE a.published=1 AND a.id=b.wb_id AND b.sched_id=' . $sched_id . ' ' .
|
|
'ORDER BY b.ordering';
|
|
$db->setQuery( $query );
|
|
$wbList = $db->loadObjectList();
|
|
return $wbList;
|
|
}
|
|
|
|
function process_cron_job() {
|
|
if ( DEBUG )
|
|
print "Entering process_cron_job()\n";
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT a.*, UNIX_TIMESTAMP(a.next_date) as `ux_next_date`, UNIX_TIMESTAMP(now()) as `ux_now` ' .
|
|
'FROM #__sql2excel_schedules a ' .
|
|
'WHERE a.published=1 AND a.next_date <= now() AND ( max_count=0 OR count < max_count ) AND ( a.end_date > now() OR a.end_date = \'0000-00-00 00:00:00\' ) ' .
|
|
'ORDER BY a.ordering';
|
|
$db->setQuery( $query );
|
|
$schedules = $db->loadObjectList();
|
|
|
|
if ( DEBUG ) {
|
|
print "Number of Schedules to Process = " . count($schedules) . "\n\n";
|
|
}
|
|
|
|
foreach ( $schedules as $sched ) {
|
|
if ( DEBUG )
|
|
print "Process Schedule ID=" . $sched->id ."\n";
|
|
$res = Sql2excelSchedule::process_schedule($sched->id, 0 );
|
|
|
|
if ( DEBUG ) {
|
|
print "\n#################################################\n";
|
|
print_r($res);
|
|
}
|
|
|
|
if ( $res === false ) {
|
|
if ( DEBUG ) {
|
|
print "=> Schedule FAIL\n";
|
|
}
|
|
$query = 'UPDATE #__sql2excel_schedules SET last_status=-1, last_date=now(), failures=failures+1 ' .
|
|
'WHERE ID=' . $sched->id;
|
|
$db->setQuery( $query );
|
|
$db->query();
|
|
} elseif ( $res < -99 ) {
|
|
if ( DEBUG )
|
|
print "=> Run-If Rule prevented schedule execution\n";
|
|
}
|
|
elseif ( $res ) {
|
|
if ( DEBUG )
|
|
print "=> Processed OK\n";
|
|
$next_date = Sql2excelSchedule::get_next_date($sched);
|
|
$query = 'UPDATE #__sql2excel_schedules SET count=count+1, last_status=1, last_date=now() ' . $next_date . ' ' .
|
|
'WHERE ID=' . $sched->id;
|
|
|
|
$db->setQuery( $query );
|
|
if ( $db->query() ) {
|
|
if (DEBUG)
|
|
print "=>Count & Status Updated OK\n";
|
|
} elseif ( DEBUG ) {
|
|
print "=> FAILED to update Count & Status\n";
|
|
print " SQL=" . $query . "\n";
|
|
print " DB Error Number=" . $db->getErrorNum() . "\n";
|
|
print " DB Error Message=" . $db->getErrorMsg() . "\n";
|
|
}
|
|
|
|
}
|
|
if ( DEBUG )
|
|
print "#################################################\n";
|
|
}
|
|
|
|
if ( DEBUG )
|
|
print "DONE\n";
|
|
|
|
}
|
|
|
|
|
|
function process_schedule($sched_id, $test=0 ) {
|
|
|
|
if ( DEBUG ) {
|
|
print "#################################################\n";
|
|
print "Processing Schedule ID=" . $sched_id . "\n";
|
|
print "#################################################\n";
|
|
}
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT * FROM #__sql2excel_schedules WHERE ID=' . $sched_id;
|
|
$db->setQuery( $query );
|
|
$sched = $db->loadObject();
|
|
|
|
$status = false;
|
|
|
|
if ( $sched ) {
|
|
if ( DEBUG )
|
|
print "Schedule Action = " . $sched->action . "\n";
|
|
|
|
if ( Sql2excelSchedule::run_schedule_ok($sched->id, 0) ) {
|
|
|
|
if ( DEBUG )
|
|
print "Run-If Rule => OK => Execute Schedule!\n";
|
|
|
|
switch ($sched->action) {
|
|
case 10:
|
|
// Update Cache for Selected Workbooks
|
|
$status = Sql2excelSchedule::update_cache_for_selected_wb($sched);
|
|
break;
|
|
case 20:
|
|
// Update Cache for All Workbooks
|
|
$status = Sql2excelSchedule::update_cache_for_all_wb($sched);
|
|
break;
|
|
case 25:
|
|
// Send Email (no links)
|
|
$status = Sql2excelSchedule::email_with_links($sched,0);
|
|
break;
|
|
case 30:
|
|
// Send Email with Links to Workbooks
|
|
$status = Sql2excelSchedule::email_with_links($sched,1);
|
|
break;
|
|
case 40:
|
|
// Send Email with Attached Workbooks
|
|
$status = Sql2excelSchedule::email_with_attachments($sched);
|
|
break;
|
|
case 50:
|
|
// Run SQL
|
|
$status = Sql2excelSchedule::run_sql($sched, $test);
|
|
if ( $test ) {
|
|
echo '<tr><td></td>';
|
|
}
|
|
break;
|
|
//case 60:
|
|
// Run PHP
|
|
// $status = Sql2excelSchedule::run_php($sched);
|
|
// break;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
// On Success/Failure Handling
|
|
if ( !$test ) {
|
|
if ( $status == false ) {
|
|
Sql2excelSchedule::on_schedule_failure($sched_id, $test);
|
|
} else {
|
|
Sql2excelSchedule::on_schedule_success($sched_id, $test);
|
|
}
|
|
}
|
|
|
|
return $status;
|
|
} else {
|
|
if ( DEBUG )
|
|
print "Run-If Rule => NOT OK => Do NOT execute Schedule!\n";
|
|
|
|
// Run-If prevents schedule from running
|
|
return -100;
|
|
}
|
|
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
function email_with_attachments($sched)
|
|
{
|
|
if ( DEBUG ) {
|
|
print "Email with Attachments...\n";
|
|
}
|
|
|
|
// Get global cache setting
|
|
$cmpParms = Sql2excelParms::getParms();
|
|
$cache_enabled = Sql2excelParms::get($cmpParms,'cache_enabled', 0);
|
|
|
|
// Admin Name & Email
|
|
$admin = Sql2excelParms::getAdmin();
|
|
$adminEmail = $admin->email;
|
|
$adminName = $admin->name;
|
|
$email_from = Sql2excelParms::get($cmpParms,'schedule_email_from', $adminEmail);
|
|
$email_name = Sql2excelParms::get($cmpParms,'schedule_email_name', $adminName);
|
|
$replyto_email = Sql2excelParms::get($cmpParms,'schedule_replyto_email', $adminEmail);
|
|
$replyto_name = Sql2excelParms::get($cmpParms,'schedule_replyto_name', $adminName);
|
|
|
|
|
|
// Substitute Subject
|
|
$subject = Sql2excelSchedule::substitute_string($sched->email_title);
|
|
|
|
// Get attached workbooks
|
|
$wbList = Sql2excelSchedule::get_sched_workbooks($sched->id);
|
|
|
|
// Generate the files
|
|
$attachments = Sql2excelSchedule::get_attachments($sched,$wbList,$cmpParms);
|
|
|
|
|
|
if ( is_array($attachments) && count($attachments) > 0 ) {
|
|
if ( DEBUG ) {
|
|
print "ATTACHMENTS=";
|
|
print_r($attachments);
|
|
print "Email FROM=" . $email_from . "\n";
|
|
print "Email FROM_NAME=" . $email_name . "\n";
|
|
print "REPLYTO=" . $replyto_email . "\n";
|
|
print "REPLYTO NAME=" . $replyto_name . "\n";
|
|
print "Email TO=" . $sched->email_to . "\n";
|
|
print "Email CC=" . $sched->email_cc . "\n";
|
|
print "Email BCC=" . $sched->email_bcc . "\n";
|
|
print "Email Title=" . $sched->email_title . "\n";
|
|
print "Email HTML=" . $sched->html . "\n";
|
|
}
|
|
} else {
|
|
$msg = "ERROR: NO ATTACHMENTS FOUND!!???\n -Check Workbook/Worksheet permissions\n -Check that cache folder exists and is writable\n";
|
|
$GLOBALS['SCHEDULE_ERR_MSG'] .= "\n\n" . $msg;
|
|
if ( DEBUG )
|
|
print $msg;
|
|
return false;
|
|
}
|
|
|
|
// Substitute email body
|
|
$body = Sql2excelSchedule::substitute_string($sched->email_body);
|
|
|
|
if ( count($attachments) ) {
|
|
$retVal = Sql2excelSchedule::send_email($email_from,
|
|
$email_name,
|
|
$sched->email_to,
|
|
$subject,
|
|
$body,
|
|
$sched->html,
|
|
$sched->email_cc,
|
|
$sched->email_bcc,
|
|
$attachments,
|
|
$replyto_email,
|
|
$replyto_name
|
|
);
|
|
if ( DEBUG )
|
|
{
|
|
print "Email Sent! - Status=";
|
|
if ( $retVal )
|
|
print "OK\n";
|
|
else
|
|
print "ERROR!\n";
|
|
}
|
|
// Cleanup the attachment files
|
|
foreach ( $attachments as $fn ) {
|
|
unlink($fn);
|
|
}
|
|
|
|
if ( DEBUG )
|
|
print "Email attachments - temp files removed.\n";
|
|
|
|
return $retVal;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
function email_with_links($sched, $include_links=1)
|
|
{
|
|
|
|
|
|
// Get global cache setting
|
|
$cmpParms = Sql2excelParms::getParms();
|
|
$cache_enabled = Sql2excelParms::get($cmpParms,'cache_enabled', 0);
|
|
|
|
|
|
// Admin Name & Email
|
|
$admin = Sql2excelParms::getAdmin();
|
|
$adminEmail = $admin->email;
|
|
$adminName = $admin->name;
|
|
$email_from = Sql2excelParms::get($cmpParms,'schedule_email_from', $adminEmail);
|
|
$email_name = Sql2excelParms::get($cmpParms,'schedule_email_name', $adminName);
|
|
$replyto_email = Sql2excelParms::get($cmpParms,'schedule_replyto_email', $adminEmail);
|
|
$replyto_name = Sql2excelParms::get($cmpParms,'schedule_replyto_name', $adminName);
|
|
|
|
// Substitute Subject
|
|
$subject = Sql2excelSchedule::substitute_string($sched->email_title);
|
|
|
|
if ( DEBUG ) {
|
|
print "Email with Links...\n";
|
|
print "Email FROM=" . $email_from . "\n";
|
|
print "Email FROM_NAME=" . $email_name . "\n";
|
|
print "REPLYTO=" . $replyto_email . "\n";
|
|
print "REPLYTO NAME=" . $replyto_name . "\n";
|
|
print "Email TO=" . $sched->email_to . "\n";
|
|
print "Email CC=" . $sched->email_cc . "\n";
|
|
print "Email BCC=" . $sched->email_bcc . "\n";
|
|
print "Email Title=" . $sched->email_title . "\n";
|
|
print "Email HTML=" . $sched->html . "\n";
|
|
}
|
|
|
|
// Substitute email body
|
|
$body = Sql2excelSchedule::substitute_string($sched->email_body);
|
|
|
|
if ( $include_links ) {
|
|
$wbList = Sql2excelSchedule::get_sched_workbooks($sched->id);
|
|
|
|
// Get the WB links
|
|
$links = Sql2excelSchedule::get_wb_links($sched,$wbList);
|
|
|
|
|
|
if ( DEBUG ) {
|
|
print "Email Links=" . $links . "\n";
|
|
}
|
|
|
|
|
|
if ( strpos($body,'{LINKS}') > 0 ) {
|
|
$body = str_replace('{LINKS}',$links,$body);
|
|
} else {
|
|
$body .= $links;
|
|
}
|
|
}
|
|
|
|
return Sql2excelSchedule::send_email( $email_from,
|
|
$email_name,
|
|
$sched->email_to,
|
|
$subject,
|
|
$body,
|
|
$sched->html,
|
|
$sched->email_cc,
|
|
$sched->email_bcc,
|
|
'',
|
|
$replyto_email,
|
|
$replyto_name
|
|
);
|
|
}
|
|
|
|
|
|
function substitute_string($str, $extraParms=0) {
|
|
// Substitute
|
|
$substParms = Sql2excelControllerDownload::getSubstParms();
|
|
return writeExcel::replace_vars($str, $substParms, $extraParms);
|
|
}
|
|
|
|
|
|
function get_attachments($sched, $wbList,$cmpparams)
|
|
{
|
|
$attachments = array();
|
|
$substParms = Sql2excelControllerDownload::getSubstParms();
|
|
|
|
$cacheDir = writeExcel::get_cache_dir($cmpparams);
|
|
$emailTmpDir = $cacheDir.'scheduler';
|
|
$cache_enabled = Sql2excelParms::get($cmpparams,'cache_enabled', 0);
|
|
|
|
// Check that cache directory exists
|
|
if ( $cacheDir && file_exists($cacheDir) ) {
|
|
if ( !file_exists($emailTmpDir)) {
|
|
mkdir($emailTmpDir,0777);
|
|
}
|
|
} else {
|
|
// Cache directory doesn't exist!
|
|
return false;
|
|
}
|
|
|
|
|
|
if ( file_exists($emailTmpDir)) {
|
|
// Scheduler cache subfolder does exist...ok so far
|
|
|
|
foreach ( $wbList as $wb ) {
|
|
$extraParms = array();
|
|
$extraParms['SQL2EXCEL_WB_LINK'] = $wb->link_title;
|
|
$extraParms['SQL2EXCEL_WB_DLCNT'] = $wb->count;
|
|
$fileName = trim($wb->filename);
|
|
$fileName = writeExcel::replace_vars($fileName, $substParms, $extraParms );
|
|
$fileName = $emailTmpDir.DS.trim($fileName);
|
|
$path_info = pathinfo($fileName);
|
|
if ( !isset($path_info['extension']) ||
|
|
( isset($path_info['extension']) && strtolower($path_info['extension']) != 'xls' ) ) {
|
|
$fileName .= '.xls';
|
|
}
|
|
|
|
// XLS or CSV format?
|
|
$dlformat = 1;
|
|
if ( isset($wb->dlformat) ) {
|
|
$dlformat = $wb->dlformat;
|
|
}
|
|
if ( $dlformat == 3 || ( $dlformat == 1 && Sql2excelParms::get($cmpParms,'dlformat', '2') == 3 ) ) {
|
|
$fileName = str_replace('.xls','.csv', $fileName);
|
|
} elseif ( $dlformat == 4 || ( $dlformat == 1 && Sql2excelParms::get($cmpParms,'dlformat', '2') == 4 ) ) {
|
|
$fileName = str_replace('.xls','.htm', $fileName);
|
|
}
|
|
|
|
Sql2excelSchedule::update_cache($wb ,$cache_enabled, $fileName);
|
|
|
|
// Compress file?
|
|
if ( file_exists($fileName) ) {
|
|
if ( $sched->compress_attachments ) {
|
|
$zipName = substr($fileName,0,strlen($fileName)-4) . '.zip';
|
|
if ( Sql2excelSchedule::compress_file($fileName, $zipName) ) {
|
|
$fileName = $zipName;
|
|
}
|
|
}
|
|
$attachments[]=$fileName;
|
|
} else {
|
|
$msg = "ERROR: Attachment file was not found on disk! $fileName\n => Check write permission of cache folder\n";
|
|
print $msg;
|
|
$GLOBALS['SCHEDULE_ERR_MSG'] .= "\n\n" . $msg;
|
|
}
|
|
|
|
}
|
|
}
|
|
return $attachments;
|
|
}
|
|
|
|
|
|
function get_wb_links($sched, $wbList)
|
|
{
|
|
$baseurl = JURI::root() . 'index.php?option=com_sql2excel&controller=download&task=dl&id=';
|
|
$baseurl_parm = JURI::root() .'index.php?option=com_sql2excel&view=parms&format=parms&id=';
|
|
|
|
$links = '';
|
|
foreach ( $wbList as $wb ) {
|
|
|
|
if ( $wb->parms_prompt && $wb->parms != '' ) {
|
|
$url = $baseurl_parm . $wb->id;
|
|
} else {
|
|
$url = $baseurl . $wb->id;
|
|
}
|
|
$title = $wb->link_title;
|
|
if ( $sched->html ) {
|
|
$links .= '<li><a href="' . $url . '">' . $title . '</a></li>';
|
|
} else {
|
|
$links .= "$title\n" . $url . "\n\n";
|
|
}
|
|
}
|
|
|
|
if ( $links != '' && $sched->html ) {
|
|
$links = '<ul>' . $links . '</ul>';
|
|
} else {
|
|
$links = "\n\n" . $links;
|
|
}
|
|
return $links;
|
|
}
|
|
|
|
/*
|
|
function run_php($sched)
|
|
{
|
|
$php = $sched->sdata;
|
|
if ( $php != '' ) {
|
|
$db = & JFactory::getDBO();
|
|
$db->setQuery( $sql );
|
|
return $db->query();
|
|
}
|
|
return false;
|
|
}
|
|
*/
|
|
function run_sql($sched, $test=0)
|
|
{
|
|
|
|
// Substitute variables in query
|
|
$sql = $sched->sdata;
|
|
$sql = Sql2excelSchedule::substitute_string($sql);
|
|
|
|
if ( $sql != '' ) {
|
|
// Get database connectioon
|
|
$conn = Sql2excelSchedule::get_db_conn($sched->sql_db,$test);
|
|
$mydb = $conn[0];
|
|
$dbtype = $conn[1];
|
|
if ( $mydb ) {
|
|
$res = writeExcel::getResults($mydb, $sql, Sql2excelParms::getParms(), $dbtype, 0);
|
|
if ( is_string($res) ) {
|
|
if ( $test ) {
|
|
echo '<tr><td></td><td><font color="red"><b>SQL Execution ERROR</b> : ' . $res . '</font></td></tr>';
|
|
}
|
|
return 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
function send_email($fromEmail, $fromName, $recipient, $subject, $email , $htmlEmail=0, $cc='', $bcc='', $attachements='', $reply_to_email='', $reply_to_name='') {
|
|
|
|
global $mainframe;
|
|
$mailHandler = $mainframe->getCfg('mailer');
|
|
|
|
if ( $mailHandler == 'smtp' ) {
|
|
// REPLY TO NAME/EMAIL DOES NOT WORK WHEN USING SMTP - JOOMLA BUG!! //
|
|
// Workaround/Bug Fix - See: http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=19428
|
|
|
|
/* SMTP Mailer */
|
|
$mail =& JFactory::getMailer();
|
|
$mail->addRecipient( $recipient );
|
|
if ( $cc != '' ) {
|
|
$mail->addCC($cc);
|
|
}
|
|
if ( $bcc != '' ) {
|
|
$mail->addBCC($bcc);
|
|
}
|
|
if ( $reply_to_email != '' ) {
|
|
$mail->addReplyTo( $reply_to_email, $reply_to_name );
|
|
}
|
|
$mail->setSubject($subject);
|
|
$mail->setBody($email);
|
|
foreach ( $attachements as $fn ) {
|
|
$mail->addAttachment($fn);
|
|
}
|
|
$mail->IsHTML($htmlEmail);
|
|
return $mail->Send();
|
|
|
|
} else {
|
|
return JUtility::sendMail($fromEmail, $fromName, $recipient, $subject, $email, $htmlEmail, $cc, $bcc, $attachements, $reply_to_email, $reply_to_name);
|
|
}
|
|
}
|
|
|
|
|
|
function update_cache_for_all_wb($sched)
|
|
{
|
|
|
|
if ( DEBUG ) {
|
|
print "Update Cache for all Workbooks...\n";
|
|
}
|
|
|
|
// Get global cache setting
|
|
$cmpparams = Sql2excelParms::getParms();
|
|
$cache_enabled = Sql2excelParms::get($cmpparams,'cache_enabled', 0);
|
|
|
|
$wbList = Sql2excelSchedule::get_all_workbooks();
|
|
|
|
if ( DEBUG ) {
|
|
print "Workbooks:\n";
|
|
print_r($wbList);
|
|
|
|
}
|
|
|
|
foreach ($wbList as $wb) {
|
|
|
|
Sql2excelSchedule::update_cache($wb, $cache_enabled);
|
|
}
|
|
|
|
if ( count($wbList) ) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function update_cache_for_selected_wb($sched)
|
|
{
|
|
|
|
if ( DEBUG ) {
|
|
print "Update Cache for Selected Workbooks...\n";
|
|
}
|
|
|
|
|
|
// Get global cache setting
|
|
$cmpParams = Sql2excelParms::getParms();
|
|
$cache_enabled = Sql2excelParms::get($cmpParams,'cache_enabled', 0);
|
|
|
|
$wbList = Sql2excelSchedule::get_sched_workbooks($sched->id);
|
|
|
|
if ( DEBUG ) {
|
|
print "Workbooks:\n";
|
|
print_r($wbList);
|
|
|
|
}
|
|
|
|
foreach ($wbList as $wb) {
|
|
Sql2excelSchedule::update_cache($wb , $cache_enabled);
|
|
}
|
|
|
|
if ( count($wbList) ) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
function update_cache($wb, $cache_enabled=0, $wbFN='-' ) {
|
|
if ( ($wb->cache == 'Global' && $cache_enabled==1) || $wb->cache == 'Yes' || $wbFN != '-') {
|
|
$noComp = 0;
|
|
if ( $wbFN != '-' ) { $noComp = 1; }
|
|
// Silent "download" to update cache
|
|
Sql2excelControllerDownload::download($wb->id, 1, $wbFN, $noComp);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
function sched_action_str($sched) {
|
|
|
|
$mytypes[] = JHTML::_('select.option', '20', JText::_( 'Update Cache for All Workbooks'));
|
|
$mytypes[] = JHTML::_('select.option', '10', JText::_( 'Update Cache for Selected Workbooks'));
|
|
$mytypes[] = JHTML::_('select.option', '25', JText::_( 'Send Email'));
|
|
$mytypes[] = JHTML::_('select.option', '30', JText::_( 'Send Email with Links to Workbooks'));
|
|
$mytypes[] = JHTML::_('select.option', '40', JText::_( 'Send Email with Attached Workbooks'));
|
|
$mytypes[] = JHTML::_('select.option', '50', JText::_( 'Run SQL'));
|
|
|
|
if ( $sched->action == 10 ) { $str = JText::_( 'Update Cache for Selected Workbooks'); }
|
|
elseif ( $sched->action == 20 ) { $str = JText::_( 'Update Cache for All Workbooks'); }
|
|
elseif ( $sched->action == 25 ) { $str = JText::_( 'Send Email'); }
|
|
elseif ( $sched->action == 30 ) { $str = JText::_( 'Send Email with Links to Workbooks'); }
|
|
elseif ( $sched->action == 40 ) { $str = JText::_( 'Send Email with Attached Workbooks'); }
|
|
elseif ( $sched->action == 50 ) { $str = JText::_( 'Run SQL'); }
|
|
else { $str = '?'; }
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Schedule Action') . '</b></td>';
|
|
echo '<td><b>' . $str . '</b></td>';
|
|
echo '</tr>';
|
|
|
|
}
|
|
|
|
function test_schedule($sched_id) {
|
|
|
|
$indent = ' ';
|
|
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT * ' .
|
|
'FROM #__sql2excel_schedules ' .
|
|
'WHERE id=' . $sched_id;
|
|
|
|
$db->setQuery( $query );
|
|
$sched = $db->loadObject();
|
|
|
|
|
|
$accessWarn = false;
|
|
|
|
// Display Schedule Action
|
|
Sql2excelSchedule::sched_action_str($sched);
|
|
|
|
|
|
// Check that cachedir is writable
|
|
$cmpParams = Sql2excelParms::getParms();
|
|
|
|
$cacheDir = writeExcel::get_cache_dir($cmpParams);
|
|
Sql2excelSchedule::checkDirectory($cacheDir, 'Cache Directory');
|
|
|
|
|
|
$cacheDir .= 'scheduler'.DS;
|
|
Sql2excelSchedule::checkDirectory($cacheDir, 'Scheduler Cache Directory');
|
|
|
|
|
|
$admin = Sql2excelParms::getAdmin();
|
|
$adminEmail = $admin->email;
|
|
$adminName = $admin->name;
|
|
$email_from = Sql2excelParms::get($cmpParams,'schedule_email_from', $adminEmail);
|
|
$email_name = Sql2excelParms::get($cmpParams,'schedule_email_name', $adminName);
|
|
$replyto_email = Sql2excelParms::get($cmpParams,'schedule_replyto_email', $adminEmail);
|
|
$replyto_name = Sql2excelParms::get($cmpParams,'schedule_replyto_name', $adminName);
|
|
|
|
if ( $sched->action >= 25 && $sched->action <= 40 ) {
|
|
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Email') . '</b></td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('TO') . '</b></td>';
|
|
if ( $sched->email_to == "" ) {
|
|
echo '<td><font color="red">' . JText::_('MISSING!') . '</font></td>';
|
|
} else {
|
|
echo '<td>' . $sched->email_to . '</td>';
|
|
}
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('CC') . '</b></td>';
|
|
echo '<td>' . $sched->email_cc . '</td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('BCC') . '</b></td>';
|
|
echo '<td>' . $sched->email_bcc . '</td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Subject') . '</b></td>';
|
|
echo '<td>' . $sched->email_title . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('FROM Email') . '</b></td>';
|
|
echo '<td>' . $email_from . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('FROM Name') . '</b></td>';
|
|
echo '<td>' . $email_name . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('REPLY TO Email') . '</b></td>';
|
|
echo '<td>' . $replyto_email . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('REPLY TO Name') . '</b></td>';
|
|
echo '<td>' . $replyto_name . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
|
|
// Check for Workbook, Section or Category that is non Public
|
|
if ( $sched->action >= 30 && $sched->action <= 40 ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Workbook, Section and Category Access') . '</b></td>';
|
|
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT COUNT(a.id) as CNT ' .
|
|
'FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b, #__sql2excel_sections c, #__sql2excel_categories d ' .
|
|
'WHERE a.published=1 AND a.id=b.wb_id AND b.sched_id=' . $sched_id . ' AND c.id = a.section AND d.id=a.category ' .
|
|
' AND ( a.access > 0 OR c.access > 0 OR d.access > 0 ) ';
|
|
$db->setQuery( $query );
|
|
$nrAccess = $db->loadResult();
|
|
|
|
if ( $nrAccess > 0 ) {
|
|
$accessWarn = true;
|
|
echo '<td><font color="purple">' . JText::_( 'One or more Workbook, Section or Category has got access setting other than Public!') . ' *) </font></td>';
|
|
} else {
|
|
echo '<td><font color="green">' . JText::_('Public') . '</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
|
|
// Check for Worksheets that is non Public
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Worksheet Access') . '</b></td>';
|
|
$query = 'SELECT COUNT(d.id) as CNT ' .
|
|
'FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b, #__sql2excel_worksheet2book c, #__sql2excel_worksheets d ' .
|
|
'WHERE a.published=1 AND a.id=b.wb_id AND b.sched_id=' . $sched_id . ' AND c.wb_id = a.id AND c.ws_id=d.id ' .
|
|
' AND d.access > 0 ';
|
|
$db->setQuery( $query );
|
|
$nrAccess = $db->loadResult();
|
|
|
|
if ( $nrAccess > 0 ) {
|
|
$accessWarn = true;
|
|
echo '<td><font color="purple">' . JText::_( 'One or more Worksheets has got access setting other than Public!') . ' *) </font></td>';
|
|
} else {
|
|
echo '<td><font color="green">' . JText::_('Public') . '</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
|
|
|
|
// Check for Worksheets Queries that contains user substitution variables
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Worksheet Queries') . '</b></td>';
|
|
$query = 'SELECT COUNT(d.id) as CNT ' .
|
|
'FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b, #__sql2excel_worksheet2book c, #__sql2excel_worksheets d ' .
|
|
'WHERE a.published=1 AND a.id=b.wb_id AND b.sched_id=' . $sched_id . ' AND c.wb_id = a.id AND c.ws_id=d.id ' .
|
|
' AND d.query like \'%{USER_%}%\' ';
|
|
$db->setQuery( $query );
|
|
$nrAccess = $db->loadResult();
|
|
|
|
if ( $nrAccess > 0 ) {
|
|
$accessWarn = true;
|
|
echo '<td><font color="purple">' . JText::_( 'One or more Worksheet queries contains user specific substitution variables : ') . '{USER_XYZ} *) </font></td>';
|
|
} else {
|
|
echo '<td><font color="green">' . JText::_('OK') . '</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
|
|
|
|
// Check for Worksheets Header/Footer which contains user substitution variables
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Worksheet Header/Footer') . '</b></td>';
|
|
$query = 'SELECT COUNT(d.id) as CNT ' .
|
|
'FROM #__sql2excel_workbooks a, #__sql2excel_schedule2book b, #__sql2excel_worksheet2book c, #__sql2excel_worksheets d ' .
|
|
'WHERE a.published=1 AND a.id=b.wb_id AND b.sched_id=' . $sched_id . ' AND c.wb_id = a.id AND c.ws_id=d.id ' .
|
|
' AND ( d.header like \'%{USER_%}%\' OR d.footer like \'%{USER_%}%\') ';
|
|
$db->setQuery( $query );
|
|
$nrAccess = $db->loadResult();
|
|
|
|
if ( $nrAccess > 0 ) {
|
|
$accessWarn = true;
|
|
echo '<td><font color="purple">' . JText::_( 'One or more Worksheet Header/Footer contains user specific substitution variables : ') . '{USER_XYZ} *) </font></td>';
|
|
} else {
|
|
echo '<td><font color="green">' . JText::_('OK') . '</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
|
|
if ( $accessWarn ) {
|
|
echo '<tr><td height=30 colspan=2> </td></tr>';
|
|
echo '<tr><td valign="top" colspan=2>*) <b>' . JText::_( 'The schedule may not run or give unwanted results if you run this schedule as a Public user') . '.</b><p>' . JText::_('See') . ' <a href="http://support.joomla-r-us.com/sql2excelproscheduler"><b>http://support.joomla-r-us.com/sql2excelproscheduler</b></a> ' . JText::_('for information on how to run a cron job as a specific Joomla user') . '.</p><td></tr>';
|
|
echo '<tr><td height=30 colspan=2> </td></tr>';
|
|
}
|
|
|
|
}
|
|
|
|
if ( $sched->action >= 50 ) {
|
|
$query = Sql2excelSchedule::substitute_string($sched->sdata);
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Schedule SQL - Raw') . '</b></td>';
|
|
if ( $sched->sdata == "" ) {
|
|
echo '<td><font color="red">' . JText::_('MISSING!') . '</font></td>';
|
|
} else {
|
|
echo '<td>' . $sched->sdata . '</td>';
|
|
}
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Schedule SQL - Substituted') . '</b></td>';
|
|
if ( $query == "" ) {
|
|
echo '<td><font color="red">' . JText::_('EMPTY STRING!') . '</font></td>';
|
|
} else {
|
|
echo '<td>' . $query . '</td>';
|
|
}
|
|
echo '</tr>';
|
|
}
|
|
|
|
}
|
|
|
|
function checkDirectory($dir, $name) {
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_($name) . '</b></td>';
|
|
echo '<td>' . $dir . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_($name . ' Exists') . '</b></td>';
|
|
if ( file_exists($dir) ) {
|
|
echo '<td><font color="green">Yes</font></td>';
|
|
$tmpFile = tempnam($dir,'write');
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_($name . ' Writable') . '</b></td>';
|
|
$fh = fopen($tmpFile,"w");
|
|
if ( $fh ) {
|
|
echo '<td><font color="green">Yes</font></td>';
|
|
fclose($fh);
|
|
unlink($tmpFile);
|
|
} else {
|
|
echo '<td><font color="red">No</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
} else {
|
|
echo '<td><font color="red">No</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
}
|
|
|
|
|
|
// Check Run-If rule!?
|
|
function run_schedule_ok($sched_id, $test) {
|
|
|
|
$indent = ' ';
|
|
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT * ' .
|
|
'FROM #__sql2excel_schedules ' .
|
|
'WHERE id=' . $sched_id;
|
|
|
|
$db->setQuery( $query );
|
|
$sched = $db->loadObject();
|
|
|
|
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . JText::_('Run If Rule') . '</b></td>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('Run If Rule') . " : ";
|
|
}
|
|
|
|
|
|
$run_if_type = $sched->run_if_type;
|
|
|
|
if ( $run_if_type == 0 ) {
|
|
if ( $test ) {
|
|
echo '<td>' . JText::_('Disabled => Schedule will be executed!') . '</td>';
|
|
echo '</tr>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('Disabled => Schedule will be executed!') . "\n";
|
|
}
|
|
return true;
|
|
} else {
|
|
|
|
// Substitute variables in Rule Query
|
|
$query = Sql2excelSchedule::substitute_string($sched->run_if_sql);
|
|
|
|
if ( $test ) {
|
|
$runifstr = '';
|
|
switch ($run_if_type) {
|
|
case 1:
|
|
$runifstr = JText::_('SQL query returns one or more rows');
|
|
break;
|
|
case 2:
|
|
$runifstr = JText::_('SQL query returns no rows');
|
|
break;
|
|
case 3:
|
|
$runifstr = JText::_('SQL query returns X rows');
|
|
break;
|
|
case 4:
|
|
$runifstr = JText::_('SQL query returns between X and Y rows');
|
|
break;
|
|
case 5:
|
|
$runifstr = JText::_('SQL query returns value X');
|
|
break;
|
|
case 6:
|
|
$runifstr = JText::_('SQL query returns value between X and Y');
|
|
break;
|
|
case 7:
|
|
$runifstr = JText::_('SQL query generates error');
|
|
break;
|
|
case 8:
|
|
$runifstr = JText::_('SQL query does not generate error');
|
|
break;
|
|
case 9:
|
|
$runifstr = JText::_('SQL query does not return X rows');
|
|
break;
|
|
case 10:
|
|
$runifstr = JText::_('SQL query does not return between X and Y rows');
|
|
break;
|
|
case 11:
|
|
$runifstr = JText::_('SQL query does not return value X');
|
|
break;
|
|
case 12:
|
|
$runifstr = JText::_('SQL query does not return value between X and Y');
|
|
break;
|
|
case 13:
|
|
$runifstr = JText::_('SQL query returns >X rows');
|
|
break;
|
|
case 14:
|
|
$runifstr = JText::_('SQL query returns >=X rows');
|
|
break;
|
|
case 15:
|
|
$runifstr = JText::_('SQL query returns <X rows');
|
|
break;
|
|
case 16:
|
|
$runifstr = JText::_('SQL query returns <=X rows');
|
|
break;
|
|
case 17:
|
|
$runifstr = JText::_('SQL query returns value >X');
|
|
break;
|
|
case 18:
|
|
$runifstr = JText::_('SQL query returns value >=X');
|
|
break;
|
|
case 19:
|
|
$runifstr = JText::_('SQL query returns value <X');
|
|
break;
|
|
case 20:
|
|
$runifstr = JText::_('SQL query returns value <=X');
|
|
break;
|
|
|
|
|
|
default:
|
|
$runifstr = '?';
|
|
}
|
|
echo '<td>' . JText::_('Run If') . ' - ' . $runifstr . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Raw') . '</b></td>';
|
|
echo '<td>' . $sched->run_if_sql . '</td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Substituted') . '</b></td>';
|
|
echo '<td>' . $query . '</td>';
|
|
echo '</tr>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('SQL - Raw') . " : " . $sched->run_if_sql . "\n";
|
|
print JText::_('SQL - Substituted') . " : " . $query . "\n";
|
|
}
|
|
|
|
|
|
// Get Database Connection
|
|
$conn = Sql2excelSchedule::get_db_conn($sched->run_if_db, $test);
|
|
$mydb = $conn[0];
|
|
$dbtype = $conn[1];
|
|
if ( !$mydb )
|
|
return false;
|
|
|
|
|
|
// Get Results!
|
|
$res = writeExcel::getResults($mydb, $query, Sql2excelParms::getParms(), $dbtype);
|
|
$numRows = 0;
|
|
$errNum = 0;
|
|
$retval = null;
|
|
if ( is_array($res) ) {
|
|
$colNames = writeExcel::getColumnNames($res);
|
|
$resRow = $res[0];
|
|
if ( count($colNames) && isset($resRow->$colNames[0]) ) {
|
|
$retval = $resRow->$colNames[0];
|
|
}
|
|
$numRows = count($res);
|
|
|
|
} elseif ( $res != '' && is_string($res) ) {
|
|
$errNum = 1;
|
|
$errMsg = $res;
|
|
}
|
|
|
|
|
|
if ( $test ) {
|
|
|
|
echo '<tr>';
|
|
echo '<td valign="top"><b>' . $indent . JText::_('SQL - Execution') . '</b></td>';
|
|
if ( $errNum == 0 ) {
|
|
echo '<td><font color="blue">OK</font></td>';
|
|
} else {
|
|
echo '<td><font color="blue">FAIL</font>';
|
|
echo '<p>Error Number = ' . $errNum . '<br>';
|
|
echo 'Error Message = ' . $errMsg . '</p>';
|
|
}
|
|
echo '</tr>';
|
|
|
|
|
|
if ( $errNum == 0 ) {
|
|
if ( $errNum == 0 ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Number of Rows Returned') . '</b></td>';
|
|
echo '<td>' . $numRows . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
if ( $run_if_type == 3 || $run_if_type == 9 || ( $run_if_type >= 13 && $run_if_type <= 16 ) ) {
|
|
$op = '';
|
|
if ( $run_if_type == 9 ) { $op = '<> '; }
|
|
if ( $run_if_type == 13 ) { $op = '> '; }
|
|
if ( $run_if_type == 14 ) { $op = '>= '; }
|
|
if ( $run_if_type == 15 ) { $op = '< '; }
|
|
if ( $run_if_type == 16 ) { $op = '<= '; }
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Desired Number of Rows') . '</b></td>';
|
|
echo '<td>' . $op . $sched->run_if_val_1 . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
if ( $run_if_type == 4 || $run_if_type == 10 ) {
|
|
$op = '';
|
|
if ( $run_if_type == 10 ) { $op = 'not '; }
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Desired Number of Rows') . '</b></td>';
|
|
echo '<td>' . $op . 'between ' . $sched->run_if_val_1 . ' and ' . $sched->run_if_val_2 . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
if ( $run_if_type == 5 || $run_if_type == 6 || $run_if_type == 11 || $run_if_type == 12 || ($run_if_type >= 17 && $run_if_type <= 20 ) ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Returned Value') . '</b></td>';
|
|
if ( $numRows > 0 ) {
|
|
echo '<td>' . $retval . '</td>';
|
|
} else {
|
|
echo '<td><font color="red">' . JText::_('No rows returned!') . '</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
}
|
|
if ( $run_if_type == 5 || $run_if_type == 11 || ( $run_if_type >= 17 && $run_if_type <= 20 ) ) {
|
|
$op = '';
|
|
if ( $run_if_type == 11 ) { $op = '<> '; }
|
|
if ( $run_if_type == 17 ) { $op = '> '; }
|
|
if ( $run_if_type == 18 ) { $op = '>= '; }
|
|
if ( $run_if_type == 19 ) { $op = '< '; }
|
|
if ( $run_if_type == 20 ) { $op = '<= '; }
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Desired Value') . '</b></td>';
|
|
echo '<td>' . $op . $sched->run_if_val_1 . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
|
|
if ( $run_if_type == 6 || $run_if_type == 12 ) {
|
|
$op = '';
|
|
if ( $run_if_type == 12 ) { $op = 'not '; }
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Desired Value') . '</b></td>';
|
|
echo '<td>' . $op . 'between ' . $sched->run_if_val_1 . ' and ' . $sched->run_if_val_2 . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
}
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('SQL - Execution') . " : ";
|
|
if ( $errNum == 0 ) {
|
|
print "OK\n";
|
|
} else {
|
|
print "FAIL!\n";
|
|
print " Error Message=" . $errMsg . "\n";
|
|
}
|
|
|
|
if ( $errNum == 0 ) {
|
|
print JText::_('SQL - Number of Rows Returned') . " : " . $numRows . "\n";
|
|
|
|
if ( $run_if_type == 3 || $run_if_type == 9 || ( $run_if_type >= 13 && $run_if_type <= 16 ) ) {
|
|
$op = '';
|
|
if ( $run_if_type == 9 ) { $op = '<> '; }
|
|
if ( $run_if_type == 13 ) { $op = '> '; }
|
|
if ( $run_if_type == 14 ) { $op = '>= '; }
|
|
if ( $run_if_type == 15 ) { $op = '< '; }
|
|
if ( $run_if_type == 16 ) { $op = '<= '; }
|
|
print JText::_('Desired Number of Rows') . " : " . $op . $sched->run_if_val_1 . "\n";
|
|
}
|
|
|
|
if ( $run_if_type == 4 || $run_if_type == 10 ) {
|
|
$op = '';
|
|
if ( $run_if_type == 10 ) { $op = 'not '; }
|
|
print JText::_('Desired Number of Rows') . " : " . $op . 'between ' . $sched->run_if_val_1 . ' and ' . $sched->run_if_val_2 . "\n";
|
|
}
|
|
|
|
if ( $run_if_type == 5 || $run_if_type == 6 || $run_if_type == 11 || $run_if_type == 12 || ($run_if_type >= 17 && $run_if_type <= 20 ) ) {
|
|
print JText::_('SQL - Returned Value') . " : ";
|
|
if ( $numRows > 0 ) {
|
|
print $retval . "\n";
|
|
} else {
|
|
print JText::_('No rows returned!') . "\n";
|
|
}
|
|
}
|
|
|
|
if ( $run_if_type == 5 || $run_if_type == 11 || ( $run_if_type >= 17 && $run_if_type <= 20 ) ) {
|
|
$op = '';
|
|
if ( $run_if_type == 11 ) { $op = '<> '; }
|
|
if ( $run_if_type == 17 ) { $op = '> '; }
|
|
if ( $run_if_type == 18 ) { $op = '>= '; }
|
|
if ( $run_if_type == 19 ) { $op = '< '; }
|
|
if ( $run_if_type == 20 ) { $op = '<= '; }
|
|
print JText::_('Desired Value') . " : " . $op . $sched->run_if_val_1 . "\n";
|
|
}
|
|
|
|
if ( $run_if_type == 6 || $run_if_type == 12 ) {
|
|
$op = '';
|
|
if ( $run_if_type == 12 ) { $op = 'not '; }
|
|
print JText::_('Desired Value') . " : " . $op . 'between ' . $sched->run_if_val_1 . ' and ' . $sched->run_if_val_2 . "\n";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
$decision = false;
|
|
if ( $run_if_type == 1 && $errNum == 0 && $numRows > 0 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 2 && $errNum == 0 && $numRows == 0 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 3 && $errNum == 0 && $numRows == $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 4 && $errNum == 0 && $numRows >= $sched->run_if_val_1 && $numRows <= $sched->run_if_val_2 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 5 && $errNum == 0 && $numRows > 0 && $retval == $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 6 && $errNum == 0 && $numRows > 0 && $retval >= $sched->run_if_val_1 && $retval <= $sched->run_if_val_2 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 7 && $errNum > 0 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 8 && $errNum == 0 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 9 && $errNum == 0 && $numRows <> $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 10 && $errNum == 0 && ( $numRows < $sched->run_if_val_1 || $numRows > $sched->run_if_val_2 ) ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 11 && $errNum == 0 && $numRows > 0 && $retval <> $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 12 && $errNum == 0 && $numRows > 0 && ( $retval < $sched->run_if_val_1 || $retval > $sched->run_if_val_2) ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 13 && $errNum == 0 && $numRows > $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 14 && $errNum == 0 && $numRows >= $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 15 && $errNum == 0 && $numRows < $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 16 && $errNum == 0 && $numRows <= $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 17 && $errNum == 0 && $numRows > 0 && $retval > $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 18 && $errNum == 0 && $numRows > 0 && $retval >= $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 19 && $errNum == 0 && $numRows > 0 && $retval < $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
if ( $run_if_type == 20 && $errNum == 0 && $numRows > 0 && $retval <= $sched->run_if_val_1 ) {
|
|
$decision = true;
|
|
}
|
|
|
|
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Rule Decision') . '</b></td>';
|
|
if ( $decision ) {
|
|
echo '<td><font color="green">' . JText::_('RUN') . '</font>';
|
|
} else {
|
|
echo '<td><font color="blue">' . JText::_('DO NOT RUN') . '</font>';
|
|
}
|
|
echo '</tr>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('Rule Decision') . " : ";
|
|
if ( $decision ) { print "RUN\n"; }
|
|
else { print "DO NOT RUN\n"; }
|
|
}
|
|
|
|
|
|
return $decision;
|
|
}
|
|
|
|
}
|
|
|
|
function on_schedule_success($sched_id, $test) {
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT * FROM #__sql2excel_schedules WHERE ID=' . $sched_id;
|
|
$db->setQuery( $query );
|
|
$sched = $db->loadObject();
|
|
|
|
if ( $sched ) {
|
|
$str = "";
|
|
if ( $sched->on_success_sql != "" ) {
|
|
$str = Sql2excelSchedule::on_schedule_success_sql($sched,true, $test);
|
|
}
|
|
|
|
if ( $sched->on_success_email != "" ) {
|
|
Sql2excelSchedule::on_schedule_success_email($sched, true, $str, $test);
|
|
}
|
|
}
|
|
}
|
|
|
|
function on_schedule_failure($sched_id, $test) {
|
|
$db = & JFactory::getDBO();
|
|
$query = 'SELECT * FROM #__sql2excel_schedules WHERE ID=' . $sched_id;
|
|
$db->setQuery( $query );
|
|
$sched = $db->loadObject();
|
|
|
|
if ( $sched ) {
|
|
$str = "";
|
|
if ( $sched->on_failure_sql != "" ) {
|
|
$str = Sql2excelSchedule::on_schedule_success_sql($sched,false, $test);
|
|
}
|
|
|
|
Sql2excelSchedule::on_schedule_success_email($sched, false, $str, $test);
|
|
}
|
|
}
|
|
|
|
function on_schedule_success_sql($sched, $success, $test) {
|
|
$indent = ' ';
|
|
$retStr = "";
|
|
|
|
if ( $success ) {
|
|
$label = JText::_('On Schedule Success SQL...');
|
|
$rawsql = $sched->on_success_sql;
|
|
$dbid = $sched->on_success_db;
|
|
} else {
|
|
$label = JText::_('On Schedule Failure SQL...');
|
|
$rawsql = $sched->on_failure_sql;
|
|
$dbid = $sched->on_failure_db;
|
|
}
|
|
|
|
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $label . '</b></td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
} elseif ( DEBUG ) {
|
|
$label . "\n";
|
|
}
|
|
|
|
|
|
// Database Connection
|
|
$conn = Sql2excelSchedule::get_db_conn($dbid, $test);
|
|
$mydb = $conn[0];
|
|
$dbtype = $conn[1];
|
|
|
|
$query = Sql2excelSchedule::substitute_string($rawsql);
|
|
|
|
if ( $mydb ) {
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Raw') . '</b></td>';
|
|
echo '<td>' . $rawsql . '</td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Substituted') . '</b></td>';
|
|
echo '<td>' . $query . '</td>';
|
|
echo '</tr>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('SQL - Raw') . " : " . $rawsql . "\n";
|
|
print JText::_('SQL - Substituted') . " : " . $query . "\n";
|
|
}
|
|
|
|
|
|
// Execute Query
|
|
$res = writeExcel::getResults($mydb, $query, Sql2excelParms::getParms(), $dbtype, 0);
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('SQL - Execution') . '</b></td>';
|
|
if ( is_string($res) && $res != '' ) {
|
|
echo '<td><font color="red">' . $res . '</font></td>';
|
|
} else {
|
|
echo '<td><font color="green">';
|
|
print_r($res);
|
|
echo '</font></td>';
|
|
echo '</tr>';
|
|
}
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('SQL - Execution') . " : ";
|
|
print_r($res);
|
|
print "\n";
|
|
}
|
|
}
|
|
|
|
return $retStr;
|
|
}
|
|
|
|
|
|
|
|
function on_schedule_success_email($sched, $success, $sql_results, $test) {
|
|
|
|
$indent = ' ';
|
|
|
|
// Admin Name & Email
|
|
$cmpParms = Sql2excelParms::getParms();
|
|
$admin = Sql2excelParms::getAdmin();
|
|
$adminEmail = $admin->email;
|
|
$adminName = $admin->name;
|
|
$email_from = Sql2excelParms::get($cmpParms,'schedule_email_from', $adminEmail);
|
|
$email_name = Sql2excelParms::get($cmpParms,'schedule_email_name', $adminName);
|
|
$replyto_email = Sql2excelParms::get($cmpParms,'schedule_replyto_email', $adminEmail);
|
|
$replyto_name = Sql2excelParms::get($cmpParms,'schedule_replyto_name', $adminName);
|
|
$extraInfo = '';
|
|
|
|
if ( $success ) {
|
|
$label = JText::_('On Schedule Success Email...');
|
|
$subject = "Schedule Executed Successfully : Schedule ID=" . $sched->id;
|
|
$email_str = "=> Executed Successfully!";
|
|
$email_to = $sched->on_success_email;
|
|
|
|
} else {
|
|
$label = JText::_('On Schedule Failure Email...');
|
|
$subject = "SQL 2 Excel Schedule Failed : Schedule ID=" . $sched->id;
|
|
$email_str = "=> FAILED!";
|
|
$email_to = $sched->on_failure_email;
|
|
|
|
// No failure email specified, use Admin Email
|
|
if ( $email_to == '' ) {
|
|
if ( DEBUG ) {
|
|
print "Schedule onFailure email address NOT defined => Sending to Admin...\n";
|
|
}
|
|
$email_to = $adminEmail;
|
|
}
|
|
if ( $GLOBALS['SCHEDULE_ERR_MSG'] != '' ) {
|
|
$extraInfo = "\n\n\nError Info:" . $GLOBALS['SCHEDULE_ERR_MSG'];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ( $test ) {
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $label . '</b></td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('EMAIL TO') . '</b></td>';
|
|
echo '<td>' . $email_to . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<td><b>' . $indent . JText::_('FROM Email') . '</b></td>';
|
|
echo '<td>' . $email_from . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('FROM Name') . '</b></td>';
|
|
echo '<td>' . $email_name . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('REPLY TO Email') . '</b></td>';
|
|
echo '<td>' . $replyto_email . '</td>';
|
|
echo '</tr>';
|
|
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('REPLY TO Name') . '</b></td>';
|
|
echo '<td>' . $replyto_name . '</td>';
|
|
echo '</tr>';
|
|
|
|
} elseif ( DEBUG ) {
|
|
print $label . "\n";
|
|
print " Email FROM=" . $email_from . "\n";
|
|
print " Email FROM_NAME=" . $email_name . "\n";
|
|
print " REPLYTO=" . $replyto_email . "\n";
|
|
print " REPLYTO NAME=" . $replyto_name . "\n";
|
|
print " Email TO=" . $email_to . "\n";;
|
|
}
|
|
|
|
$body = "Date : " . date("F j, Y, g:i a") . "\n";
|
|
$body .= "Schedule ID = " . $sched->id . "\n";
|
|
$body .= "Schedule Title = " . $sched->title . "\n\n";
|
|
$body .= $email_str . $extraInfo;
|
|
|
|
|
|
$ret = Sql2excelSchedule::send_email( $email_from,
|
|
$email_name,
|
|
$email_to,
|
|
$subject,
|
|
$body,
|
|
0,
|
|
'',
|
|
'',
|
|
'',
|
|
$replyto_email,
|
|
$replyto_name
|
|
);
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Sent') . '</b></td>';
|
|
if ( $ret ) {
|
|
echo '<td><font color="green">OK</font></td>';
|
|
} else {
|
|
echo '<td><font color="red">FAIL</font></td>';
|
|
}
|
|
echo '</tr>';
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('Sent') . " : ";
|
|
if ( $ret ) { print "OK\n"; }
|
|
else { print "FAIL\n"; }
|
|
}
|
|
}
|
|
|
|
|
|
// Get database connection and type
|
|
function get_db_conn($db_id, $test) {
|
|
|
|
$indent = ' ';
|
|
|
|
$mydb = null;
|
|
$dbtype = 1;
|
|
if ( $db_id == 1 ) {
|
|
$mydb = & JFactory::getDBO();
|
|
} else {
|
|
$db = & JFactory::getDBO();
|
|
$dbquery = "SELECT * FROM #__sql2excel_databases WHERE id=" . $db_id;
|
|
$db->setQuery( $dbquery );
|
|
$dbInfo = $db->loadObject();
|
|
if ( $dbInfo ) {
|
|
$mydb = writeExcel::getDB($dbInfo);
|
|
$dbtype = $dbInfo->db_type;
|
|
}
|
|
}
|
|
|
|
if ( $test ) {
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Database ID') . '</b></td>';
|
|
echo '<td>' . $db_id . '</td>';
|
|
echo '</tr>';
|
|
echo '<tr>';
|
|
echo '<td><b>' . $indent . JText::_('Database Connection') . '</b></td>';
|
|
if ( $mydb )
|
|
echo '<td><font color="green">OK</font></td>';
|
|
else
|
|
echo '<td><font color="red">FAIL</font></td>';
|
|
echo '</tr>';
|
|
|
|
} elseif ( DEBUG ) {
|
|
print JText::_('Database ID') . " : " . $db_id . "\n";
|
|
print JText::_('Database Connection') . " : ";
|
|
if ( $mydb )
|
|
print "OK\n";
|
|
else
|
|
print "FAIL\n";
|
|
}
|
|
|
|
return array($mydb,$dbtype);
|
|
}
|
|
} |