MTrackWeb/Report.php
[web.mtrack] / MTrackWeb / Report.php
1 <?php # vim:ts=4:sw=4:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4
5 require_once 'MTrackWeb.php';
6  
7 class MTrackWeb_Report extends MTrackWeb
8 {
9     var $template = 'report.html';
10  
11     function getAuth() 
12     {
13         parent::getAuth();
14         require_once 'MTrack/ACL.php';
15         MTrackACL::requireAllRights('Reports', 'read');
16         return true;
17     }
18     
19     function get($pi=0)
20     {
21         $pi = (int) $pi;
22         $this->edit = !empty($_REQUEST['edit']);
23         
24         if ($this->edit && !$pi) {
25             MTrackACL::requireAllRights('Reports', 'create');
26             $rep = new MTrack_Report;
27         }
28         // only support id???
29         if ($pi) {
30             $rep = DB_DataObject::factory('reports');
31             $rep->get($pi);
32             MTrackACL::requireAllRights("report:" . $rep->rid, $this->edit ? 'modify' : 'read');
33         }
34     
35         ///$rep = MTrack_Report::loadBySummary($pi);
36         ///MTrackACL::requireAllRights("report:" . $rep->rid, $edit ? 'modify' : 'read');
37
38         if (isset($_GET['format'])) {
39           // targeted report format; omit decoration <<< ??? tab delimited only at present?
40             $params = $_GET;
41             unset($params['format']);
42             switch ($_GET['format']) {
43                 case 'tab':
44                     header('Content-Type: text/plain');
45                     break;
46             }
47             echo $rep->renderReport($rep->query, $params, $_GET['format']);
48             exit;
49         }
50         
51         $this->title = "Create Report";
52         if ($pi) {
53             $this->title = $this->edit ? 
54                 ('{' . $rep->rid . '} ' . $rep->summary . " (edit)") : 
55                 '{' . $rep->rid . '} ' . $rep->summary;
56         }
57         $this->canModify = MTrackACL::hasAllRights("report:" . $rep->rid, 'modify');
58         $this->rep = $rep;
59         
60         MTrack_Report::$link = $this->link;
61
62     }
63     
64     function post($pi=0)
65     {
66         $pi = (int) $pi;
67         if (!$pi) {
68             MTrackACL::requireAllRights('Reports', 'create');
69             $rep = new MTrack_Report;
70         }
71         // only support id???
72         if ($pi) {
73             $rep = DB_DataObject::factory('reports');
74             $rep->get($pi);
75             MTrackACL::requireAllRights("report:" . $rep->rid, 'modify');
76         }
77         
78         $rep->summary = $_POST['name'];
79         $rep->description = $_POST['description'];
80         $rep->query = $_POST['query'];
81         if (isset($_POST['cancel'])) {
82             header("Location: {$this->baseURL}/Reports");
83             exit;
84         }
85         // in theory... everything else is a 'save'...
86         try {
87             $cs = MTrackChangeset::begin(
88                       "report:" . $rep->summary, $_POST['comment']);
89             $rep->save($cs);
90             $cs->commit();
91             return $this->get($pi);
92         } catch (Exception $e) {
93             $this->message = $e->getMessage();
94         }
95         return $this->get($pi);
96         
97         
98     }
99         
100         
101         
102  
103 }