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