PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/web/report.php

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