summaryrefslogtreecommitdiff
path: root/plugins/Report/classes/Reports.php
blob: 19327b48329b66671c5ceca5908416170074c9e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
	/**
	 * Class to manage reports.  Note how this class is designed to use
	 * the functions provided by the database driver exclusively, and hence
	 * will work with any database without modification.
	 *
	 * $Id: Reports.php,v 1.18 2007/04/16 11:02:35 mr-russ Exp $
	 */

	class Reports {

		// A database driver
		var $driver;
		var $conf;

		/* Constructor */
		function __construct(&$conf, &$status) {
			global $misc, $data;

			$this->conf = $conf;

			// Check to see if the reports database exists
			$rs = $data->getDatabase($this->conf['reports_db']);
			if ($rs->recordCount() != 1) $status = -1;
			else {
				// Create a new database access object.
				$this->driver = $misc->getDatabaseAccessor($this->conf['reports_db']);
				// Reports database should have been created in public schema
				$this->driver->setSchema($this->conf['reports_schema']);
				$status = 0;
			}
		}

		/**
		 * Finds all reports
		 * @return A recordset
		 */
		function getReports() {
			global $misc;
			// Filter for owned reports if necessary
			if ($this->conf['owned_reports_only']) {
				$server_info = $misc->getServerInfo();
				$filter['created_by'] = $server_info['username'];
				$ops = array('created_by' => '=');
			}
			else $filter = $ops = array();

			$sql = $this->driver->getSelectSQL($this->conf['reports_table'],
				array('report_id', 'report_name', 'db_name', 'date_created', 'created_by', 'descr', 'report_sql', 'paginate'),
				$filter, $ops, array('db_name' => 'asc', 'report_name' => 'asc'));

			return $this->driver->selectSet($sql);
		}

		/**
		 * Finds a particular report
		 * @param $report_id The ID of the report to find
		 * @return A recordset
		 */
		function getReport($report_id) {
			$sql = $this->driver->getSelectSQL($this->conf['reports_table'],
				array('report_id', 'report_name', 'db_name', 'date_created', 'created_by', 'descr', 'report_sql', 'paginate'),
				array('report_id' => $report_id), array('report_id' => '='), array());

			return $this->driver->selectSet($sql);
		}

		/**
		 * Creates a report
		 * @param $report_name The name of the report
		 * @param $db_name The name of the database
		 * @param $descr The comment on the report
		 * @param $report_sql The SQL for the report
		 * @param $paginate The report should be paginated
		 * @return 0 success
		 */
		function createReport($report_name, $db_name, $descr, $report_sql, $paginate) {
			global $misc;
			$server_info = $misc->getServerInfo();
			$temp = array(
				'report_name' => $report_name,
				'db_name' => $db_name,
				'created_by' => $server_info['username'],
				'report_sql' => $report_sql,
				'paginate' => $paginate ? 'true' : 'false',
			);
			if ($descr != '') $temp['descr'] = $descr;

			return $this->driver->insert($this->conf['reports_table'], $temp);
		}

		/**
		 * Alters a report
		 * @param $report_id The ID of the report
		 * @param $report_name The name of the report
		 * @param $db_name The name of the database
		 * @param $descr The comment on the report
		 * @param $report_sql The SQL for the report
		 * @param $paginate The report should be paginated
		 * @return 0 success
		 */
		function alterReport($report_id, $report_name, $db_name, $descr, $report_sql, $paginate) {
			global $misc;
			$server_info = $misc->getServerInfo();
			$temp = array(
				'report_name' => $report_name,
				'db_name' => $db_name,
				'created_by' => $server_info['username'],
				'report_sql' => $report_sql,
				'paginate' => $paginate ? 'true' : 'false',
				'descr' => $descr
			);

			return $this->driver->update($this->conf['reports_table'], $temp,
							array('report_id' => $report_id));
		}

		/**
		 * Drops a report
		 * @param $report_id The ID of the report to drop
		 * @return 0 success
		 */
		function dropReport($report_id) {
			return $this->driver->delete($this->conf['reports_table'], array('report_id' => $report_id));
		}

	}
?>