1<?php
2require_once('./classes/Plugin.php');
3require_once('./plugins/Report/classes/Reports.php');
4
5class Report extends Plugin {
6
7	/**
8	 * Attributes
9	 */
10	protected $name = 'Report';
11	protected $lang;
12	protected $conf = array();
13	protected $_reportsdb = null;
14
15	/**
16	 * Constructor
17	 * Call parent constructor, passing the language that will be used.
18	 * @param $language Current phpPgAdmin language. If it was not found in the plugin, English will be used.
19	 */
20	function __construct($language) {
21
22		/* loads $this->lang and $this->conf */
23		parent::__construct($language);
24
25		/* default values */
26		if (! isset($this->conf['reports_db'])) {
27			$this->conf['reports_db'] = 'phppgadmin';
28		}
29		if (! isset($this->conf['reports_schema'])) {
30			$this->conf['reports_schema'] = 'public';
31		}
32		if (! isset($this->conf['reports_table'])) {
33			$this->conf['reports_table'] = 'ppa_reports';
34		}
35		if (! isset($this->conf['owned_reports_only'])) {
36			$this->conf['owned_reports_only'] = false;
37		}
38	}
39
40	function get_reportsdb() {
41		if ($this->_reportsdb === null) {
42			$status = 0;
43			$this->_reportsdb = new Reports($this->conf, $status);
44
45			if ($status !== 0) {
46				global $misc;
47				$misc->printHeader($this->lang['strreports']);
48				$misc->printBody();
49				$misc->printTrail('server');
50				$misc->printTabs('server','reports');
51				$misc->printMsg($this->lang['strnoreportsdb']);
52				$misc->printFooter();
53				exit;
54			}
55		}
56
57		return $this->_reportsdb;
58	}
59
60	/**
61	 * This method returns the functions that will hook in the phpPgAdmin core.
62	 * To include a function just put in the $hooks array the following code:
63	 * 'hook' => array('function1', 'function2').
64	 *
65	 * Example:
66	 * $hooks = array(
67	 *	'toplinks' => array('add_plugin_toplinks'),
68	 *	'tabs' => array('add_tab_entry'),
69	 *  'action_buttons' => array('add_more_an_entry')
70	 * );
71	 *
72	 * @return $hooks
73	 */
74	function get_hooks() {
75		$hooks = array(
76			'tabs' => array('add_plugin_tabs'),
77			'trail' => array('add_plugin_trail'),
78			'navlinks' => array('plugin_navlinks')
79		);
80		return $hooks;
81	}
82
83	/**
84	 * This method returns the functions that will be used as actions.
85	 * To include a function that will be used as action, just put in the $actions array the following code:
86	 *
87	 * $actions = array(
88	 *	'show_page',
89	 *	'show_error',
90	 * );
91	 *
92	 * @return $actions
93	 */
94	function get_actions() {
95		$actions = array(
96			'save_edit',
97			'edit',
98			'properties',
99			'save_create',
100			'create',
101			'drop',
102			'confirm_drop',
103			'execute',
104			'default_action'
105		);
106		return $actions;
107	}
108
109	/**
110	 * Add plugin in the tabs
111	 * @param $plugin_functions_parameters
112	 */
113	function add_plugin_tabs(&$plugin_functions_parameters) {
114		global $misc;
115
116		$tabs = &$plugin_functions_parameters['tabs'];
117
118		if ($plugin_functions_parameters['section'] == 'server') {
119			$tabs['report_plugin'] = array (
120				'title' => $this->lang['strplugindescription'],
121				'url' => 'plugin.php',
122				'urlvars' => array(
123					'subject' => 'server',
124					'action' => 'default_action',
125					'plugin' => $this->name
126				),
127				'hide' => false,
128				'icon' => $this->icon('Report')
129			);
130		}
131
132		if ($plugin_functions_parameters['section'] == 'report') {
133			$tabs['report_plugin'] = array (
134				'title' => $this->lang['strplugindescription'],
135				'url' => 'plugin.php',
136				'urlvars' => array(
137					'subject' => 'server',
138					'action' => 'default_action',
139					'plugin' => $this->name
140				),
141				'hide' => false,
142				'icon' => $this->icon('Report')
143			);
144		}
145	}
146
147	/**
148	 * Add plugin in the trail
149	 * @param $plugin_functions_parameters
150	 */
151	function add_plugin_trail(&$plugin_functions_parameters) {
152		global $misc;
153		$trail = &$plugin_functions_parameters['trail'];
154		$done = false;
155		$subject = '';
156		if (isset($_REQUEST['subject'])) {
157			$subject = $_REQUEST['subject'];
158		}
159
160		$action = '';
161		if (isset($_REQUEST['action'])) {
162			$action = $_REQUEST['action'];
163		}
164
165		if (isset($_REQUEST['plugin']) and $_REQUEST['plugin'] == 'Report') {
166			$url = array (
167				'url' => 'plugin.php',
168				'urlvars' => array (
169					'plugin' => $this->name,
170					'action' => 'default_action'
171				)
172			);
173			$trail['report_plugin'] = array (
174				'title' => $this->lang['strreport'],
175				'text' => $this->lang['strreport'],
176				'url'   => $misc->getActionUrl($url, $_REQUEST, null, false),
177				'icon' => $this->icon('Reports')
178			);
179		}
180
181		if (isset($_REQUEST['plugin'])
182			and $_REQUEST['plugin'] == 'Report'
183			and $action != 'default_action'
184			and in_array($action, $this->get_actions())
185		) {
186
187			$url = array (
188				'url' => 'plugin.php',
189				'urlvars' => array (
190					'plugin' => $this->name,
191					'action' => 'properties',
192					'report_id' => field('report_id'),
193				)
194			);
195
196			if (isset($_REQUEST['report']))
197				$url['urlvars']['report'] = field('report');
198
199			$trail['report_plugin_name'] = array (
200				'title' => $this->lang['strreport'],
201				'text' => $this->lang['strreport'],
202				'url'   => $misc->getActionUrl($url, $_REQUEST, null, false),
203				'icon' => $this->icon('Report')
204			);
205
206			if (isset($_REQUEST['report']))
207				$trail['report_plugin_name']['text'] = $_REQUEST['report'];
208
209		}
210	}
211
212	/**
213	 * Add plugin in the navlinks
214	 * @param $plugin_functions_parameters
215	 */
216	function plugin_navlinks(&$params) {
217		global $misc, $lang;
218
219		if (
220			($params['place'] == 'sql-form'
221				or $params['place'] == 'display-browse')
222			and ( isset($params['env']['rs'])
223				 and is_object($params['env']['rs'])
224				 and $params['env']['rs']->recordCount() > 0))
225		{
226			if ( ! (isset($_REQUEST['plugin'])
227					and $_REQUEST['plugin'] == $this->name)
228			) {
229				/* ResultSet doesn't come from a plugin:
230				 * show a create report link. */
231				$params['navlinks']['report_link'] = array (
232					'attr'=> array (
233						'href' => array (
234							'url' => 'plugin.php',
235							'urlvars' => array (
236								'plugin' => $this->name,
237								'action' => 'create',
238								'server' => $_REQUEST['server'],
239								'database' => $_REQUEST['database'],
240							)
241						)
242					),
243					'content' => $this->lang['strcreatereport']
244				);
245
246				if (isset($_REQUEST['paginate']))
247				$params['navlinks']['report_link']['attr']['href']['urlvars']['paginate']
248					= $_REQUEST['paginate'];
249
250				if (!empty($_SESSION['sqlquery'])) {
251					$params['navlinks']['report_link']['attr']['href']['urlvars']['fromsql']
252						= 1;
253				}
254				else {
255					if (isset($_REQUEST['subject'])
256						and isset($_REQUEST[$_REQUEST['subject']]))
257					{
258						$params['navlinks']['report_link']['attr']['href']['urlvars']['subject']
259							= $_REQUEST['subject'];
260						$params['navlinks']['report_link']['attr']['href']['urlvars'][$_REQUEST['subject']]
261							= $_REQUEST[$_REQUEST['subject']];
262
263						$params['navlinks']['report_link']['attr']['href']['urlvars']['sortkey']
264							= isset($_REQUEST['sortkey']) ? $_REQUEST['sortkey'] : '';
265
266						$params['navlinks']['report_link']['attr']['href']['urlvars']['sortdir']
267							= isset($_REQUEST['sortdir']) ? $_REQUEST['sortdir'] : '';
268					}
269					else {
270						unset($params['navlinks']['report_link']);
271					}
272				}
273			}
274			else {
275				/* ResultSet comes from a plugin:
276				 * show a edit report link. */
277				$params['navlinks']['report_link'] = array (
278					'attr'=> array (
279						'href' => array (
280							'url' => 'plugin.php',
281							'urlvars' => array (
282								'plugin' => $this->name,
283								'action' => 'edit',
284								'server' => $_REQUEST['server'],
285								'database' => $_REQUEST['database'],
286								'report_id' => $_REQUEST['report_id']
287							)
288						)
289					),
290					'content' => $this->lang['streditreport']
291				);
292
293				/* edit collapse link to add report related vars */
294				$params['navlinks']['collapse']['attr']['href']['urlvars']
295					['plugin'] = $this->name;
296				$params['navlinks']['collapse']['attr']['href']['urlvars']
297					['report_id'] = $_REQUEST['report_id'];
298				$params['navlinks']['collapse']['attr']['href']['urlvars']
299					['report'] = $_REQUEST['report'];
300
301				/* edit refresh link to add report related vars */
302				$params['navlinks']['refresh']['attr']['href']['urlvars']
303					['plugin'] = $this->name;
304				$params['navlinks']['refresh']['attr']['href']['urlvars']
305					['report_id'] = $_REQUEST['report_id'];
306				$params['navlinks']['refresh']['attr']['href']['urlvars']
307					['report'] = $_REQUEST['report'];
308
309				if (isset($_REQUEST['action'])) {
310					$params['navlinks']['collapse']['attr']['href']['urlvars']
311						['action'] = $_REQUEST['action'];
312
313					$params['navlinks']['refresh']['attr']['href']['urlvars']
314						['action'] = $_REQUEST['action'];
315				}
316			}
317
318			if (isset($_REQUEST['schema']))
319				$params['navlinks']['report_link']['attr']['href']['urlvars']['schema']
320					= $_REQUEST['schema'];
321		}
322	}
323
324	function get_subject_params() {
325		$vars = array();
326
327		if (! isset($_REQUEST['action']))
328			return $vars;
329
330		$action = $_REQUEST['action'];
331
332		switch ($action) {
333			case 'execute':
334				$vars = array(
335					'report_id' => $_REQUEST['report_id'],
336					'report' => $_REQUEST['report'],
337					'action' => 'properties' /*defaults to properties*/
338				);
339				if (isset($_REQUEST['back']))
340					$vars['action'] = $_REQUEST['back'];
341			break;
342		}
343
344		return $vars;
345	}
346
347	function edit($msg = '') {
348		global $data, $misc, $lang;
349
350		$reportsdb = $this->get_reportsdb();
351
352		$misc->printHeader($this->lang['strreports']);
353		$misc->printBody();
354		$misc->printTrail('server');
355		$misc->printTabs('server', 'report_plugin');
356		$misc->printMsg($msg);
357
358		// If it's a first, load then get the data from the database
359		$report = $reportsdb->getReport($_REQUEST['report_id']);
360
361		if ($_REQUEST['action'] == 'edit') {
362			$_POST['report_name'] = $report->fields['report_name'];
363			$_POST['db_name'] = $report->fields['db_name'];
364			$_POST['descr'] = $report->fields['descr'];
365			$_POST['report_sql'] = $report->fields['report_sql'];
366			if ($report->fields['paginate'] == 't') {
367				$_POST['paginate'] = true;
368			}
369		}
370
371		// Get a list of available databases
372		$databases = $data->getDatabases();
373
374		$_REQUEST['report'] = $report->fields['report_name'];
375
376		echo "<form action=\"plugin.php?plugin={$this->name}\" method=\"post\">\n";
377		echo $misc->form;
378		echo "<table style=\"width: 100%\">\n";
379		echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
380		echo "<td class=\"data1\"><input name=\"report_name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
381			htmlspecialchars($_POST['report_name']), "\" /></td></tr>\n";
382		echo "<tr><th class=\"data left required\">{$lang['strdatabase']}</th>\n";
383		echo "<td class=\"data1\"><select name=\"db_name\">\n";
384		while (!$databases->EOF) {
385			$dbname = $databases->fields['datname'];
386			echo "<option value=\"", htmlspecialchars($dbname), "\"",
387			($dbname == $_POST['db_name']) ? ' selected="selected"' : '', ">",
388				htmlspecialchars($dbname), "</option>\n";
389			$databases->moveNext();
390		}
391		echo "</select></td></tr>\n";
392		echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
393		echo "<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"5\" cols=\"50\" name=\"descr\">",
394			htmlspecialchars($_POST['descr']), "</textarea></td></tr>\n";
395		echo "<tr><th class=\"data left required\">{$lang['strsql']}</th>\n";
396		echo "<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"15\" cols=\"50\" name=\"report_sql\">",
397			htmlspecialchars($_POST['report_sql']), "</textarea></td></tr>\n";
398		echo "</table>\n";
399		echo "<label for=\"paginate\"><input type=\"checkbox\" id=\"paginate\" name=\"paginate\"", (isset($_POST['paginate']) ? ' checked="checked"' : ''), " />&nbsp;{$lang['strpaginate']}</label>\n";
400		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
401		echo "<input type=\"submit\" value=\"{$lang['strsave']}\" />\n";
402		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
403		echo "<input type=\"hidden\" name=\"report_id\" value=\"{$report->fields['report_id']}\" />\n";
404		echo "</form>\n";
405		$misc->printFooter();
406	}
407
408	/**
409	 * Saves changes to a report
410	 */
411	function save_edit() {
412		$reportsdb = $this->get_reportsdb();
413
414		if (isset($_REQUEST['cancel'])) {
415			$this->default_action();
416			exit;
417		}
418
419		if (!isset($_POST['report_name'])) $_POST['report_name'] = '';
420		if (!isset($_POST['db_name'])) $_POST['db_name'] = '';
421		if (!isset($_POST['descr'])) $_POST['descr'] = '';
422		if (!isset($_POST['report_sql'])) $_POST['report_sql'] = '';
423
424		// Check that they've given a name and a definition
425		if ($_POST['report_name'] == '') {
426			$this->edit($this->lang['strreportneedsname']);
427		} elseif ($_POST['report_sql'] == '') {
428			$this->edit($this->lang['strreportneedsdef']);
429		} else {
430			$status = $reportsdb->alterReport($_POST['report_id'], $_POST['report_name'], $_POST['db_name'],
431				$_POST['descr'], $_POST['report_sql'], isset($_POST['paginate']));
432			if ($status == 0)
433				$this->default_action($this->lang['strreportcreated']);
434			else
435				$this->edit($this->lang['strreportcreatedbad']);
436		}
437	}
438
439	/**
440	 * Display read-only properties of a report
441	 */
442	function properties($msg = '') {
443		global $data, $reportsdb, $misc;
444		global $lang;
445
446		$reportsdb = $this->get_reportsdb();
447
448		$misc->printHeader($this->lang['strreports']);
449		$misc->printBody();
450		$misc->printTrail('server');
451		$misc->printTabs('server', 'report_plugin');
452		$misc->printMsg($msg);
453
454		$report = $reportsdb->getReport($_REQUEST['report_id']);
455
456		$_REQUEST['report'] = $report->fields['report_name'];
457
458		if ($report->recordCount() == 1) {
459			echo "<table>\n";
460			echo "<tr><th class=\"data left\">{$lang['strname']}</th>\n";
461			echo "<td class=\"data1\">", $misc->printVal($report->fields['report_name']), "</td></tr>\n";
462			echo "<tr><th class=\"data left\">{$lang['strdatabase']}</th>\n";
463			echo "<td class=\"data1\">", $misc->printVal($report->fields['db_name']), "</td></tr>\n";
464			echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
465			echo "<td class=\"data1\">", $misc->printVal($report->fields['descr']), "</td></tr>\n";
466			echo "<tr><th class=\"data left\">{$lang['strpaginate']}</th>\n";
467			echo "<td class=\"data1\">", $misc->printVal($report->fields['paginate'], 'yesno', array('align' => 'left')), "</td></tr>\n";
468			echo "<tr><th class=\"data left\">{$lang['strsql']}</th>\n";
469			echo "<td class=\"data1\">", $misc->printVal($report->fields['report_sql']), "</td></tr>\n";
470			echo "</table>\n";
471		}
472		else echo "<p>{$lang['strinvalidparam']}</p>\n";
473
474		$urlvars = array (
475			'plugin' => $this->name,
476			'server' => $_REQUEST['server']
477		);
478		if (isset($_REQUEST['schema'])) $urlvars['schema'] = $_REQUEST['schema'];
479		if (isset($_REQUEST['schema'])) $urlvars['database'] = $_REQUEST['schema'];
480
481		$navlinks = array (
482			'showall' => array (
483				'attr'=> array (
484					'href' => array (
485						'url' => 'plugin.php',
486						'urlvars' => array_merge($urlvars, array('action' => 'default_action'))
487					)
488				),
489				'content' => $this->lang['strshowallreports']
490			),
491			'edit' => array (
492				'attr'=> array (
493					'href' => array (
494						'url' => 'plugin.php',
495						'urlvars' => array_merge($urlvars, array(
496							'action' => 'edit',
497							'report_id' => $report->fields['report_id'])
498						)
499					)
500				),
501				'content' => $lang['stredit']
502			),
503			'execute' => array (
504				'attr'=> array (
505					'href' => array (
506						'url' => 'plugin.php',
507						'urlvars' => array_merge($urlvars, array(
508							'action' => 'execute',
509							'report' => $report->fields['report_name'],
510							'database' => $report->fields['db_name'],
511							'report_id' => $report->fields['report_id'],
512							'paginate' => $report->fields['paginate'],
513							'nohistory' => 't',
514							'return' => 'plugin',
515							'back' => 'properties'
516						))
517					)
518				),
519				'content' => $lang['strexecute']
520			)
521		);
522		$misc->printNavLinks($navlinks, 'reports-properties');
523	}
524
525	/**
526	 * Displays a screen where they can enter a new report
527	 */
528	function create($msg = '') {
529		global $data, $reportsdb, $misc;
530		global $lang;
531
532		$misc->printHeader($this->lang['strreports']);
533		$misc->printBody();
534		$misc->printTrail('server');
535		$misc->printTabs('server', 'report_plugin');
536		$misc->printMsg($msg);
537
538		if (!isset($_REQUEST['report_name'])) $_REQUEST['report_name'] = '';
539		if (!isset($_REQUEST['db_name'])) $_REQUEST['db_name'] = '';
540		if (!isset($_REQUEST['descr'])) $_REQUEST['descr'] = '';
541		if (!isset($_REQUEST['report_sql'])) {
542			// Set the query from session if linked from a user query result
543			if (isset($_REQUEST['fromsql']) and $_REQUEST['fromsql'] == 1 ) {
544				$_REQUEST['report_sql'] = $_SESSION['sqlquery'];
545			}
546			else {
547				$_REQUEST['sortkey'] = isset($_REQUEST['sortkey']) ? $_REQUEST['sortkey'] : '';
548				if (preg_match('/^[0-9]+$/', $_REQUEST['sortkey']) && $_REQUEST['sortkey'] > 0) $orderby = array($_REQUEST['sortkey'] => $_REQUEST['sortdir']);
549					else $orderby = array();
550
551				$subject = isset($_REQUEST['subject']) && isset($_REQUEST[$_REQUEST['subject']])
552					? $_REQUEST[$_REQUEST['subject']]
553					: '';
554
555				$_REQUEST['report_sql'] = $data->getSelectSQL($subject, array(), array(), array(), $orderby);
556			}
557		}
558
559		if (isset($_REQUEST['database'])) {
560			$_REQUEST['db_name'] = $_REQUEST['database'];
561			unset($_REQUEST['database']);
562			$misc->setForm();
563		}
564
565		$databases = $data->getDatabases();
566
567		echo "<form action=\"plugin.php?plugin={$this->name}\" method=\"post\">\n";
568		echo $misc->form;
569		echo "<table style=\"width: 100%\">\n";
570		echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
571		echo "<td class=\"data1\"><input name=\"report_name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
572			htmlspecialchars($_REQUEST['report_name']), "\" /></td></tr>\n";
573		echo "<tr><th class=\"data left required\">{$lang['strdatabase']}</th>\n";
574		echo "<td class=\"data1\"><select name=\"db_name\">\n";
575		while (!$databases->EOF) {
576			$dbname = $databases->fields['datname'];
577			echo "<option value=\"", htmlspecialchars($dbname), "\"",
578			($dbname == $_REQUEST['db_name']) ? ' selected="selected"' : '', ">",
579				htmlspecialchars($dbname), "</option>\n";
580			$databases->moveNext();
581		}
582		echo "</select></td></tr>\n";
583		echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
584		echo "<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"5\" cols=\"50\" name=\"descr\">",
585			htmlspecialchars($_REQUEST['descr']), "</textarea></td></tr>\n";
586		echo "<tr><th class=\"data left required\">{$lang['strsql']}</th>\n";
587		echo "<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"15\" cols=\"50\" name=\"report_sql\">",
588			htmlspecialchars($_REQUEST['report_sql']), "</textarea></td></tr>\n";
589		echo "</table>\n";
590		echo "<label for=\"paginate\"><input type=\"checkbox\" id=\"paginate\" name=\"paginate\"", (isset($_REQUEST['paginate']) ? ' checked="checked"' : ''), " />&nbsp;{$lang['strpaginate']}</label>\n";
591		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
592		echo "<input type=\"submit\" value=\"{$lang['strsave']}\" />\n";
593		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
594		echo "</form>\n";
595		$misc->printFooter();
596	}
597
598	/**
599	 * Actually creates the new report in the database
600	 */
601	function save_create() {
602		if (isset($_REQUEST['cancel'])) {
603			$this->default_action();
604			exit;
605		}
606
607		$reportsdb = $this->get_reportsdb();
608
609		if (!isset($_POST['report_name'])) $_POST['report_name'] = '';
610		if (!isset($_POST['db_name'])) $_POST['db_name'] = '';
611		if (!isset($_POST['descr'])) $_POST['descr'] = '';
612		if (!isset($_POST['report_sql'])) $_POST['report_sql'] = '';
613
614		// Check that they've given a name and a definition
615		if ($_POST['report_name'] == '') $this->create($this->lang['strreportneedsname']);
616		elseif ($_POST['report_sql'] == '') $this->create($this->lang['strreportneedsdef']);
617		else {
618			$status = $reportsdb->createReport($_POST['report_name'], $_POST['db_name'],
619					$_POST['descr'], $_POST['report_sql'], isset($_POST['paginate']));
620			if ($status == 0)
621				$this->default_action($this->lang['strreportcreated']);
622			else
623				$this->create($this->lang['strreportcreatedbad']);
624		}
625	}
626
627	/**
628	 * Show confirmation of drop and perform actual drop
629	 */
630	function drop() {
631		global $reportsdb, $misc;
632		global $lang;
633
634		$confirm = false;
635		if (isset($_REQUEST['confirm'])) $confirm = true;
636
637		$reportsdb = $this->get_reportsdb();
638
639		$misc->printHeader($this->lang['strreports']);
640		$misc->printBody();
641
642		if (isset($_REQUEST['cancel'])) {
643			$this->default_action();
644			exit;
645		}
646
647		if ($confirm) {
648			// Fetch report from the database
649			$report = $reportsdb->getReport($_REQUEST['report_id']);
650
651			$_REQUEST['report'] = $report->fields['report_name'];
652			$misc->printTrail('report');
653			$misc->printTitle($lang['strdrop']);
654
655			echo "<p>", sprintf($this->lang['strconfdropreport'], $misc->printVal($report->fields['report_name'])), "</p>\n";
656
657			echo "<form action=\"plugin.php?plugin={$this->name}\" method=\"post\">\n";
658			echo $misc->form;
659			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
660			echo "<input type=\"hidden\" name=\"report_id\" value=\"", htmlspecialchars($_REQUEST['report_id']), "\" />\n";
661			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
662			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
663			echo "</form>\n";
664		} else {
665			$status = $reportsdb->dropReport($_POST['report_id']);
666			if ($status == 0)
667				$this->default_action($this->lang['strreportdropped']);
668			else
669				$this->default_action($this->lang['strreportdroppedbad']);
670		}
671
672		$misc->printFooter();
673	}
674
675	function execute() {
676		global $misc, $data;
677
678		$reportsdb = $this->get_reportsdb();
679
680		$report = $reportsdb->getReport($_REQUEST['report_id']);
681
682		$_POST['query'] = $report->fields['report_sql'];
683
684		include('./sql.php');
685	}
686
687	/**
688	 * Show default list of reports in the database
689	 */
690	function default_action($msg = '') {
691		global $data, $misc, $lang;
692
693		$reportsdb = $this->get_reportsdb();
694
695		$misc->printHeader($this->lang['strreports']);
696		$misc->printBody();
697		$misc->printTrail('server');
698		$misc->printTabs('server', 'report_plugin');
699		$misc->printMsg($msg);
700
701		$reports = $reportsdb->getReports();
702
703		$columns = array(
704			'report' => array(
705				'title' => $this->lang['strreport'],
706				'field' => field('report_name'),
707				'url'   => "plugin.php?plugin={$this->name}&amp;action=properties&amp;{$misc->href}&amp;",
708				'vars'  => array(
709					'report_id' => 'report_id',
710					'report' => 'report_name'
711				),
712			),
713			'database' => array(
714				'title' => $lang['strdatabase'],
715				'field' => field('db_name'),
716			),
717			'created' => array(
718				'title' => $lang['strcreated'],
719				'field' => field('date_created'),
720			),
721			'paginate' => array(
722				'title' => $lang['strpaginate'],
723				'field' => field('paginate'),
724				'type' => 'yesno',
725			),
726			'actions' => array(
727				'title' => $lang['stractions'],
728			),
729			'comment' => array(
730				'title' => $lang['strcomment'],
731				'field' => field('descr'),
732			),
733		);
734
735		//$return_url = urlencode("plugin.php?plugin={$this->name}&amp;{$misc->href}");
736		$urlvars = $misc->getRequestVars();
737
738		$actions = array(
739			'run' => array (
740				'content' => $lang['strexecute'],
741				'attr'=> array (
742					'href' => array (
743						'url' => 'plugin.php',
744						'urlvars' => array_merge($urlvars, array (
745							'plugin' => $this->name,
746							'action' => 'execute',
747							'report' => field('report_name'),
748							'database' => field('db_name'),
749							'report_id' => field('report_id'),
750							'paginate' => field('paginate'),
751							'nohistory' => 't',
752							'return' => 'plugin',
753							'back' => 'default_action'
754						))
755					)
756				)
757			),
758			'edit' => array (
759				'content' => $lang['stredit'],
760				'attr'=> array (
761					'href' => array (
762						'url' => 'plugin.php',
763						'urlvars' => array_merge($urlvars, array (
764							'plugin' => $this->name,
765							'action' => 'edit',
766							'report_id' => field('report_id'),
767						))
768					)
769				)
770			),
771			'drop' => array(
772				'content' => $lang['strdrop'],
773				'attr'=> array (
774					'href' => array (
775						'url' => 'plugin.php',
776						'urlvars' => array_merge($urlvars, array (
777							'plugin' => $this->name,
778							'action' => 'drop',
779							'confirm' => 'true',
780							'report_id' => field('report_id'),
781						))
782					)
783				)
784			),
785		);
786
787		$misc->printTable($reports, $columns, $actions, 'reports-reports', $this->lang['strnoreports']);
788
789		$navlinks = array (
790			array (
791				'attr'=> array (
792					'href' => array (
793						'url' => 'plugin.php',
794						'urlvars' => array (
795							'plugin' => $this->name,
796							'server' => field('server'),
797							'action' => 'create')
798					)
799				),
800				'content' => $this->lang['strcreatereport']
801			)
802		);
803		$misc->printNavLinks($navlinks, 'reports-reports');
804		$misc->printFooter();
805	}
806}
807?>
808