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     
10     function getAuth() 
11     {
12         parent::getAuth();
13        
14         return true;
15     }
16     
17     function get($pi=0)
18     {
19        
20        
21         if (!isset($_REQUEST['ajax_body'])) {
22             return;
23         }
24         $this->masterTemplate = 'report.html';
25         $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
26         $this->id  = $this->id  == 'new' ? 0 : $this->id;
27         $this->id  = (int) $this->id;
28         
29        
30         $pi = (int) $pi;
31         $this->edit = !empty($_REQUEST['edit']);
32         
33         
34         
35         
36         
37         if ($this->edit || !$pi) {
38             throw new Exception("no editing yet");
39         }
40         // only support id???
41         $rep = DB_DataObject::factory('mtrack_report');
42         $rep->get($pi); // eventually GET..
43         
44     
45     
46     
47         // select boxes for quickly changing stuff..
48     
49        
50         foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c)  {
51             $d = DB_DataObject::factory('core_enum');
52             $d->etype = $c;
53             $d->orderBy('seqid ASC, name ASC');
54             if (!$d->count()) {
55                 $d->createBaseEntries();
56                 
57             }
58             $ar = $d->fetchAll('id','name');
59             if (!$this->id) {
60                 $ar = array_merge( array(0 => '-- Select --'),$ar);
61             }
62             $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
63             $this->elements[$c.'_id']->setOptions($ar);
64             
65         }
66         
67        // DB_DataObject::debugLevel(1);
68         ///$rep = MTrack_Report::loadBySummary($pi);
69         ///MTrackACL::requireAllRights("report:" . $rep->rid, $edit ? 'modify' : 'read');
70
71         if (!empty($_GET['format']) && $pi) {
72           // targeted report format; omit decoration <<< ??? tab delimited only at present?
73             $params = $_GET;
74             $params['format'] = isset($params['format']) ? $_GET['format'] : 'html' ;
75             $format = $params['format'];
76             
77             unset($params['format']);
78             switch ($format) {
79                 case 'tab':
80                     header('Content-Type: text/plain');
81                     break;
82             }
83             echo $rep->render( $params, $format);
84             exit;
85         }
86         /*
87         $this->title = "Create Report";
88         if ($pi) {
89             $this->title = $this->edit ? 
90                 ('{' . $rep->id . '} ' . $rep->summary . " (edit)") : 
91                 '{' . $rep->id . '} ' . $rep->summary;
92         }
93         */
94        // $this->canModify = MTrackACL::hasAllRights("report:" . $rep->rid, 'modify');
95         $this->rep = $rep;
96         
97          
98
99     }
100     
101     function get2()
102     {
103         // the ajax version...
104         // just returns data which can be rendered using the jtemplate code..
105         
106         
107          if (!isset($_REQUEST['ajax_body'])) {
108             return;
109         }
110         $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
111         $this->id  = $this->id  == 'new' ? 0 : $this->id;
112         $this->id  = (int) $this->id;
113         
114        
115         $pi = (int) $pi;
116         
117         // only support id???
118         $rep = DB_DataObject::factory('mtrack_report');
119         
120         $rep->get($pi); // eventually GET..
121         
122         $this->jdata($rep->renderToJSON(), false,
123             array('report' => $rep->toArray())
124         );
125     
126         $this->rep = $rep;
127         
128          
129     }
130     
131     
132     
133     function post($pi=0)
134     {
135         die("not yet");
136         $pi = (int) $pi;
137         if (!$pi) {
138             MTrackACL::requireAllRights('Reports', 'create');
139             $rep = new MTrack_Report;
140         }
141         // only support id???
142         if ($pi) {
143             $rep = DB_DataObject::factory('mtrack_report');
144             $rep->get($pi);
145             MTrackACL::requireAllRights("report:" . $rep->rid, 'modify');
146         }
147         
148         $rep->summary = $_POST['name'];
149         $rep->description = $_POST['description'];
150         $rep->query = $_POST['query'];
151         if (isset($_POST['cancel'])) {
152             header("Location: {$this->baseURL}/Reports");
153             exit;
154         }
155         // in theory... everything else is a 'save'...
156         try {
157             $cs = DB_DataObject::Factory('mtrack_change');
158             $cs->begin( "report:" . $rep->summary, $_POST['comment']);
159             $rep->save($cs);
160             $cs->commit();
161             return $this->get($pi);
162         } catch (Exception $e) {
163             $this->message = $e->getMessage();
164         }
165         return $this->get($pi);
166         
167         
168     }
169         
170         
171         
172  
173 }