1: <?php
2: 3: 4:
5: class Whups_Block_Query extends Horde_Core_Block
6: {
7: 8:
9: public function __construct($app, $params = array())
10: {
11: parent::__construct($app, $params);
12:
13: $this->_name = _("Query Results");
14: }
15:
16: 17:
18: protected function _params()
19: {
20: $qManager = new Whups_Query_Manager();
21: $qDefault = null;
22: $qParams = $qManager->listQueries($GLOBALS['registry']->getAuth());
23: if (count($qParams)) {
24: $qType = 'enum';
25: } else {
26: $qDefault = _("You have no saved queries.");
27: $qType = 'error';
28: }
29:
30: return array(
31: 'query' => array(
32: 'type' => $qType,
33: 'name' => _("Query to run"),
34: 'default' => $qDefault,
35: 'values' => $qParams
36: )
37: );
38: }
39:
40: 41:
42: protected function _title()
43: {
44: if (($query = $this->_getQuery()) && $query->name) {
45: return Horde::link(Whups::urlFor('query', empty($query->slug) ? array('id' => $query->id) : array('slug' => $query->slug)))
46: . htmlspecialchars($query->name) . '</a>';
47: }
48:
49: return $this->getName();
50: }
51:
52: 53:
54: protected function _content()
55: {
56: global $whups_driver, $prefs;
57:
58: if (!($query = $this->_getQuery())) {
59: return '<p><em>' . _("No query to run") . '</em></p>';
60: }
61:
62: $vars = Horde_Variables::getDefaultVariables();
63: $tickets = $whups_driver->executeQuery($query, $vars);
64: $html = '<thead><tr>';
65: $sortby = $prefs->getValue('sortby');
66: $sortdirclass = ' class="' . ($prefs->getValue('sortdir') ? 'sortup' : 'sortdown') . '"';
67: foreach (Whups::getSearchResultColumns('block') as $name => $column) {
68: $html .= '<th' . ($sortby == $column ? $sortdirclass : '') . '>' . $name . '</th>';
69: }
70: $html .= '</tr></thead><tbody>';
71:
72: Whups::sortTickets($tickets);
73: foreach ($tickets as $ticket) {
74: $link = Horde::link(Whups::urlFor('ticket', $ticket['id'], true));
75: $html .= '<tr><td>' . $link . htmlspecialchars($ticket['id']) . '</a></td>' .
76: '<td>' . $link . htmlspecialchars($ticket['summary']) . '</a></td>' .
77: '<td>' . htmlspecialchars($ticket['priority_name']) . '</td>' .
78: '<td>' . htmlspecialchars($ticket['state_name']) . '</td></tr>';
79: }
80:
81: Horde::addScriptFile('tables.js', 'horde', true);
82:
83: return '<table id="whups_block_query_' . $query->id . '" cellspacing="0" class="tickets striped sortable">' . $html . '</tbody></table>';
84: }
85:
86: 87:
88: private function _getQuery()
89: {
90: if (empty($this->_params['query'])) {
91: return false;
92: }
93:
94: $qManager = new Whups_Query_Manager();
95: try {
96: $query = $qManager->getQuery($this->_params['query']);
97: } catch (Whups_Exception $e) {
98: return false;
99: }
100: if (!$query->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
101: return false;
102: }
103:
104: return $query;
105: }
106:
107: }
108: