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         
134         
135         
136     }
137     
138     
139     
140     function post($pi=0)
141     {
142         die("not yet");
143         $pi = (int) $pi;
144         if (!$pi) {
145             MTrackACL::requireAllRights('Reports', 'create');
146             $rep = new MTrack_Report;
147         }
148         // only support id???
149         if ($pi) {
150             $rep = DB_DataObject::factory('mtrack_report');
151             $rep->get($pi);
152             MTrackACL::requireAllRights("report:" . $rep->rid, 'modify');
153         }
154         
155         $rep->summary = $_POST['name'];
156         $rep->description = $_POST['description'];
157         $rep->query = $_POST['query'];
158         if (isset($_POST['cancel'])) {
159             header("Location: {$this->baseURL}/Reports");
160             exit;
161         }
162         // in theory... everything else is a 'save'...
163         try {
164             $cs = DB_DataObject::Factory('mtrack_change');
165             $cs->begin( "report:" . $rep->summary, $_POST['comment']);
166             $rep->save($cs);
167             $cs->commit();
168             return $this->get($pi);
169         } catch (Exception $e) {
170             $this->message = $e->getMessage();
171         }
172         return $this->get($pi);
173         
174         
175     }
176         
177         
178         
179  
180 }