import
[web.mtrack] / web / report.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 include '../inc/common.php';
5
6 $pi = mtrack_get_pathinfo();
7 $edit = isset($_REQUEST['edit']);
8
9 if (!strlen($pi)) {
10   if ($edit) {
11     MTrackACL::requireAllRights('Reports', 'create');
12     $rep = new MTrackReport;
13   } else {
14     throw new Exception("no report to render");
15   }
16 } elseif (ctype_digit($pi)) {
17   $rep = MTrackReport::loadByID($pi);
18   MTrackACL::requireAllRights("report:" . $rep->rid, $edit ? 'modify' : 'read');
19 } else {
20   $rep = MTrackReport::loadBySummary($pi);
21   MTrackACL::requireAllRights("report:" . $rep->rid, $edit ? 'modify' : 'read');
22 }
23
24 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
25   $rep->summary = $_POST['name'];
26   $rep->description = $_POST['description'];
27   $rep->query = $_POST['query'];
28
29   if (isset($_POST['cancel'])) {
30     header("Location: {$ABSWEB}reports.php");
31     exit;
32   }
33
34   if (isset($_POST['save'])) {
35     try {
36       $cs = MTrackChangeset::begin(
37               "report:" . $rep->summary, $_POST['comment']);
38       $rep->save($cs);
39       $cs->commit();
40       header("Location: {$ABSWEB}report.php/$rep->rid");
41       exit;
42     } catch (Exception $e) {
43       $message = $e->getMessage();
44     }
45   }
46 }
47
48 if (isset($_GET['format'])) {
49   // targeted report format; omit decoration
50   $params = $_GET;
51   unset($params['format']);
52   switch ($_GET['format']) {
53     case 'tab':
54       header('Content-Type: text/plain');
55       break;
56   }
57   echo $rep->renderReport($rep->query, $params, $_GET['format']);
58   exit;
59 }
60
61 if ($rep->rid) {
62   if ($edit) {
63     mtrack_head('{' . $rep->rid . '} ' . $rep->summary . " (edit)");
64   } else {
65     mtrack_head('{' . $rep->rid . '} ' . $rep->summary);
66   }
67 } else {
68   mtrack_head("Create Report");
69 }
70
71 if (!empty($message)) {
72   echo "<div class='error'>" . htmlentities($message, ENT_COMPAT, 'utf-8') . "</div>\n";
73 }
74
75 if (!$edit || isset($_POST['preview'])) {
76   echo "<h1>" . htmlentities($rep->summary, ENT_COMPAT, 'utf-8') . "</h1>";
77   echo MTrackWiki::format_to_html($rep->description);
78   echo $rep->renderReport($rep->query);
79
80   if ($edit) {
81     echo "<hr>";
82   } else if (MTrackACL::hasAllRights("report:" . $rep->rid, 'modify')) {
83     echo <<<HTML
84 <form name="editreport" method="GET" action="{$ABSWEB}report.php/$rep->rid">
85 <button type="submit" name="edit">Edit Report</button>
86 </form>
87 HTML;
88   }
89 }
90
91
92 if ($edit) {
93   echo <<<HTML
94 <form name="editreport" method="POST" action="{$ABSWEB}report.php/$rep->rid">
95 <input type="hidden" name="edit" value="1">
96 HTML;
97
98   if ($rep->rid) {
99     echo "<input type='hidden' name='rid' value='$rep->rid'/>\n";
100     echo '{' . $rep->rid . '} ';
101   }
102
103   $name = htmlentities($rep->summary, ENT_QUOTES, 'utf-8');
104   $desc = htmlentities($rep->description, ENT_QUOTES, 'utf-8');
105   $query = htmlentities($rep->query, ENT_QUOTES, 'utf-8');
106
107   echo <<<HTML
108 <label>Name: <input type="text" size="60" name='name' value="$name"></label><br/>
109 <label>Description:<br/>
110 <textarea name="description" rows="12" cols="76">$desc</textarea>
111 </label><br/>
112 <label>SQL Query:<br/>
113 <textarea name="query" class="code" rows="20" cols="76">$query</textarea>
114 </label>
115 <div class="buttons">
116   <button type="submit" name="preview">Preview</button>
117   <button type="submit" name="cancel">Cancel</button>
118 </div>
119   Reason for change: <input type="text" name="comment">
120   <button type="submit" name="save">Save changes</button>
121
122 </form>
123 HTML;
124
125 }
126 mtrack_foot();