########################################################
# 	Mantis Bugtracker Add-On
# 	Query Version 1.00
#	2012 plugin by Cas Nuy www.NUY.info
########################################################

This plugin will enable you to define multiple queries/scripts which:
1. can be run online (by admin)
2. can be scheduled in which case results are emailed to nominated recipients

Scripts
In case the report requirements go beyond a straight forward sql query, you can also insert a script to the query engine.
Your script should return the value that should be downloaded and contain no PHP tags.
A very simple script would look like:
	$to_download  = "Hello world";
	$to_download .= "\r\n";
	$to_download .= "Greetings, Cas";
	return $to_download; 
When adding the code to the system it will not try to trap any parsing errors so you should ensure that it does generate the required output.
Finally there is the option to add a script which handles everything itself, so no emailing is done by the system.

Mailing
It uses a direct mail function hence smtp host needs to be set correctly in php.ini
Would have prefered to use the mantis mail function but this does not support attachments.

Scheduling
Scheduling has 2 parts.
Within the plugin a set of jobs can be defined which should run as a type of batch job.
For each job the following  can be defined :
1. Description
2. Select query definition to be used
3. Optionally add an additional  filter
4. Define recipients (mantis usernames separated by comma)
5. Define frequency
There are 4 frequencies available:
1. Every day
2. Weekdays only (Monday till Friday)
3. Weekly (Mondays0
4. Monthly (First day of the month)

Finally one job needs to be scheduled on your server to run daily, shortly after midnight.
This needs to be set up by your admin using either CRON or Windows Scheduled tasks.
In order to run in batch mode, one needs to schedule the following job:
1. exec_schedule.php (http://localhost/m1210/plugins/Query/pages/exec_schedule.php)


The tables are not (yet) created automatically, so please execute the following on yor SQL database:
CREATE TABLE mantis_plugin_query_definitions_table (
  query_id int(10) unsigned NOT NULL AUTO_INCREMENT,
  query_name varchar(100) DEFAULT NULL,
  query_desc varchar(200) NOT NULL,
  query_type varchar(1) DEFAULT NULL,
  query_script longtext,
  query_tables longtext,
  query_joins longtext,
  query_fields longtext,
  query_filter longtext,
  query_order longtext,
  query_group longtext,
  query_sql longtext,
  PRIMARY KEY (query_id)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE mantis_plugin_query_schedule_table (
  schedule_id int(10) unsigned NOT NULL AUTO_INCREMENT,
  schedule_desc varchar(200) NOT NULL,
  query_id int(11) NOT NULL,
  schedule_filter longtext NOT NULL,
  target longtext NOT NULL,
  frequency char(1) NOT NULL,
  PRIMARY KEY (schedule_id)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Test data if you like:
INSERT INTO mantis_plugin_query_definitions_table (query_id, query_name, query_desc, query_type, query_script, query_tables, query_joins, query_fields, query_filter, query_order, query_group, query_sql) VALUES
(1, 'Complaint table', 'Download of Mantis_bug_table (for testing purposes)', 'Q', '', 'mantis_bug_table', '', '*', '', 'status', '', 'select * from mantis_bug_table order by status'),
(2, 'Test with script', 'Download as result of script (for testing purposes)', 'S', '$content = &quot;hello world&quot;;\r\n$content .= &quot;\\r\\n&quot;;\r\n$content .= &quot;Greetings, Cas&quot;;\r\nreturn $content;', NULL, NULL, NULL, NULL, NULL, NULL, '');

INSERT INTO mantis_plugin_query_schedule_table (schedule_id, schedule_desc, query_id, schedule_filter, target, frequency) VALUES
(1, 'Test schedule', 1, 'status=10', 'administrator', 'D'),
(3, 'test2', 2, '', 'administrator', 'W');
