1<?php
2/**
3 * Copyright 2001-2002 Robert E. Coyle <robertecoyle@hotmail.com>
4 * Copyright 2001-2017 Horde LLC (http://www.horde.org/)
5 *
6 * See the enclosed file LICENSE for license information (BSD). If you
7 * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
8 *
9 * @author Chuck Hagenbuch <chuck@horde.org>
10 */
11
12/**
13 * Construct the URL back to a supplied search
14 */
15function _getSearchUrl($vars)
16{
17    $qUrl = new Horde_Url();
18
19    $queue = (int)$vars->get('queue');
20    $qUrl->add(array('queue' => $queue));
21
22    $summary = $vars->get('summary');
23    if ($summary) {
24        $qUrl->add('summary', $summary);
25    }
26
27    $states = $vars->get('states');
28    if (is_array($states)) {
29        foreach ($states as $type => $state) {
30            if (is_array($state)) {
31                foreach ($state as $s) {
32                    $qUrl->add("states[$type][]", $s);
33                }
34            } else {
35                $qUrl->add("states[$type]", $state);
36            }
37        }
38    }
39
40    return substr($qUrl, 1);
41}
42
43require_once __DIR__ . '/lib/Application.php';
44Horde_Registry::appInit('whups');
45
46$renderer = new Horde_Form_Renderer();
47$beendone = false;
48$vars = Horde_Variables::getDefaultVariables();
49
50Whups::addTopbarSearch();
51
52$form = new Whups_Form_Search($vars);
53$results = null;
54if (($vars->get('formname') || $vars->get('summary') || $vars->get('states') ||
55     Horde_Util::getFormData('haveSearch', false)) && $form->validate($vars, true)) {
56
57    $form->getInfo($vars, $info);
58    if ($vars->get('submitbutton') == _("Save as Query")) {
59        $qManager = new Whups_Query_Manager();
60        $whups_query = $qManager->newQuery();
61        if (strlen($info['summary'])) {
62            $whups_query->insertCriterion('', Whups_Query::CRITERION_SUMMARY, null,
63                                          Whups_Query::OPERATOR_CI_SUBSTRING, $info['summary']);
64        }
65        if ($vars->get('queue')) {
66            $whups_query->insertCriterion('', Whups_Query::CRITERION_QUEUE, null,
67                                          Whups_Query::OPERATOR_EQUAL, $info['queue'][0]);
68        }
69        foreach (array('ticket_timestamp', 'date_updated', 'date_resolved', 'date_assigned', 'date_due') as $date_field) {
70            if (!empty($info[$date_field]['from']) || !empty($info[$date_field]['to'])) {
71                $path = $whups_query->insertBranch('', Whups_Query::TYPE_AND);
72                break;
73            }
74        }
75        if (!empty($info['ticket_timestamp']['from'])) {
76            $whups_query->insertCriterion($path, Whups_Query::CRITERION_TIMESTAMP, null,
77                                          Whups_Query::OPERATOR_GREATER, $info['ticket_timestamp']['from']);
78        }
79        if (!empty($info['ticket_timestamp']['to'])) {
80            $whups_query->insertCriterion($path, Whups_Query::CRITERION_TIMESTAMP, null,
81                                          Whups_Query::OPERATOR_LESS, $info['ticket_timestamp']['to']);
82        }
83        if (!empty($info['date_updated']['from'])) {
84            $whups_query->insertCriterion($path, Whups_Query::CRITERION_UPDATED, null,
85                                          Whups_Query::OPERATOR_GREATER, $info['date_updated']['from']);
86        }
87        if (!empty($info['date_updated']['to'])) {
88            $whups_query->insertCriterion($path, Whups_Query::CRITERION_UPDATED, null,
89                                          Whups_Query::OPERATOR_LESS, $info['date_updated']['to']);
90        }
91        if (!empty($info['date_resolved']['from'])) {
92            $whups_query->insertCriterion($path, Whups_Query::CRITERION_RESOLVED, null,
93                                          Whups_Query::OPERATOR_GREATER, $info['date_resolved']['from']);
94        }
95        if (!empty($info['date_resolved']['to'])) {
96            $whups_query->insertCriterion($path, Whups_Query::CRITERION_RESOLVED, null,
97                                          Whups_Query::OPERATOR_LESS, $info['date_resolved']['to']);
98        }
99        if (!empty($info['date_assigned']['from'])) {
100            $whups_query->insertCriterion($path, Whups_Query::CRITERION_ASSIGNED, null,
101                                          Whups_Query::OPERATOR_GREATER, $info['date_assigned']['from']);
102        }
103        if (!empty($info['date_assigned']['to'])) {
104            $whups_query->insertCriterion($path, Whups_Query::CRITERION_ASSIGNED, null,
105                                          Whups_Query::OPERATOR_LESS, $info['date_assigned']['to']);
106        }
107        if (!empty($info['date_due']['from'])) {
108            $whups_query->insertCriterion($path, Whups_Query::CRITERION_DUE, null,
109                                          Whups_Query::OPERATOR_GREATER, $info['date_due']['from']);
110        }
111        if (!empty($info['date_due']['to'])) {
112            $whups_query->insertCriterion($path, Whups_Query::CRITERION_DUE, null,
113                                          Whups_Query::OPERATOR_LESS, $info['date_due']['to']);
114        }
115        if ($info['state_id']) {
116            $path = $whups_query->insertBranch('', Whups_Query::TYPE_OR);
117            foreach ($info['state_id'] as $state) {
118                $whups_query->insertCriterion($path, Whups_Query::CRITERION_STATE, null,
119                                              Whups_Query::OPERATOR_EQUAL, $state);
120            }
121        }
122        $session->set('whups', 'query', $whups_query);
123        Horde::url('query/index.php', true)
124            ->add('action', 'save')
125            ->redirect();
126    }
127    try {
128        $tickets = $whups_driver->getTicketsByProperties($info);
129        Whups::sortTickets($tickets);
130        $session->set('whups', 'last_search', Horde::url('search.php?' . _getSearchUrl($vars)));
131        $results = new Whups_View_Results(
132            array('title' => _("Search Results"),
133                  'results' => $tickets,
134                  'values' => Whups::getSearchResultColumns(),
135                  'url' => $session->get('whups', 'last_search')));
136        $beendone = true;
137    } catch (Whups_Exception $e) {
138        $notification->push(sprintf(_("There was an error performing your search: %s"), $tickets->getMessage()), 'horde.error');
139    }
140}
141
142Whups::addFeedLink();
143$page_output->ajax = true;
144$page_output->header(array(
145    'title' => _("Search")
146));
147$notification->notify(array('listeners' => 'status'));
148
149if ($results) {
150    $results->html();
151    if (is_object($form)) {
152        $form->setTitle(_("Refine Search"));
153        $form->renderActive($renderer, $vars, Horde::url('search.php'), 'get');
154    }
155}
156
157if (!$beendone) {
158    // Front search page.
159    $form->setTitle(_("Ticket Search"));
160    $form->renderActive($renderer, $vars, Horde::url('search.php'), 'get');
161}
162
163$qManager = new Whups_Query_Manager();
164$myqueries = new Whups_View_SavedQueries(
165    array('title' => $GLOBALS['registry']->getAuth() ? _("My Queries") : _("Public Queries"),
166          'results' => $qManager->listQueries($GLOBALS['registry']->getAuth(), true)));
167$myqueries->html();
168
169$page_output->footer();
170