PageRenderTime 343ms CodeModel.GetById 111ms app.highlight 110ms RepoModel.GetById 112ms app.codeStats 1ms

/web/search.php

https://bitbucket.org/yoander/mtrack
PHP | 257 lines | 232 code | 24 blank | 1 comment | 15 complexity | 74c27b2ba6354f929b9b464e936f775a MD5 | raw file
  1<?php # vim:ts=2:sw=2:et:
  2/* For licensing and copyright terms, see the file named LICENSE */
  3include '../inc/common.php';
  4
  5$q = $_GET['q'];
  6
  7$quick = MTrackSearchDB::expand_quick_link($q);
  8if ($quick) {
  9  header("Location: " . $quick[1]);
 10  exit;
 11}
 12
 13mtrack_head("Search");
 14?>
 15<h1>Search</h1>
 16
 17<input type="text" id="searchq" size="50" placeholder="Type here to search">
 18<div>
 19  <input type="checkbox" id="only_open_tickets" value="1"
 20  <?php
 21    if (isset($_GET['only_open_tickets'])) {
 22    echo "checked='checked'";
 23    }
 24  ?>
 25  >
 26    Restrict search to open tickets
 27</div>
 28<p>
 29  Search the full-text index.
 30  Read more about <a class='wikilink' href="<?php echo $ABSWEB ; ?>help.php/Searching">Searching</a>.
 31  <button data-toggle='collapse' data-target='#fieldsummary'
 32    type='button' class='btn'>Show Fields</button>
 33</p>
 34<p>
 35  You may also
 36  use the <a href="<?php echo $ABSWEB ?>query.php">Custom Ticket Query</a>
 37  page to create a report on the fly.
 38</p>
 39
 40<div id='fieldsummary' class='collapse'>
 41  <p>The following fields are available for targeted searching:</p>
 42  <table>
 43    <tr>
 44      <th>Item</th>
 45      <th>Field</th>
 46      <th>Description</th>
 47    </tr>
 48    <tr>
 49      <td>Ticket</td>
 50      <td>summary</td>
 51      <td>The one-line ticket summary</td>
 52    </tr>
 53    <tr>
 54      <td>Ticket</td>
 55      <td>description</td>
 56      <td>The ticket description</td>
 57    </tr>
 58    <tr>
 59      <td>Ticket</td>
 60      <td>changelog</td>
 61      <td>The changelog field</td>
 62    </tr>
 63    <tr>
 64      <td>Ticket</td>
 65      <td>keyword</td>
 66      <td>The keyword field</td>
 67    </tr>
 68    <tr>
 69      <td>Ticket</td>
 70      <td>date</td>
 71      <td>The last-changed date</td>
 72    </tr>
 73    <tr>
 74      <td>Ticket</td>
 75      <td>who</td>
 76      <td>who last changed the ticket</td>
 77    </tr>
 78    <tr>
 79      <td>Ticket</td>
 80      <td>creator</td>
 81      <td>who opened the ticket</td>
 82    </tr>
 83    <tr>
 84      <td>Ticket</td>
 85      <td>created</td>
 86      <td>The date that the ticket was created</td>
 87    </tr>
 88    <tr>
 89      <td>Ticket</td>
 90      <td>owner</td>
 91      <td>who is responsible for the ticket</td>
 92    </tr>
 93    <tr>
 94      <td>Comment</td>
 95      <td>description</td>
 96      <td>The comment text</td>
 97    </tr>
 98    <tr>
 99      <td>Comment</td>
100      <td>date</td>
101      <td>Date the comment was made</td>
102    </tr>
103    <tr>
104      <td>Comment</td>
105      <td>who</td>
106      <td>who made that comment</td>
107    </tr>
108    <tr>
109      <td>Wiki</td>
110      <td>wiki</td>
111      <td>The content from the wiki page</td>
112    </tr>
113    <tr>
114      <td>Wiki</td>
115      <td>who</td>
116      <td>Who last changed that wiki page</td>
117    </tr>
118    <tr>
119      <td>Wiki</td>
120      <td>date</td>
121      <td>Date the wiki page was last changed</td>
122    </tr>
123<?php
124$CF = MTrackTicket_CustomFields::getInstance();
125foreach ($CF->getFields() as $f) {
126  echo "<tr><td>Ticket</td><td>$f->name</td><td>",
127    htmlentities($f->label, ENT_QUOTES, 'utf-8'),
128    "</td></tr>\n";
129}
130?>
131  </table>
132
133</div>
134</form>
135
136<?php
137
138if (strlen($q)) {
139  $userq = $q;
140
141  if (isset($_GET['only_open_tickets'])) {
142    $q = "+type:ticket -status:closed +($q)";
143  }
144
145  $results = MTrackAPI::invoke('GET', '/search/query', null, array(
146    'q' => $q
147  ))->result;
148
149} else {
150  $results = null;
151  $userq = "";
152}
153$results = json_encode($results);
154$userq = json_encode($userq);
155?>
156
157<ul id="results" class="searchresults"></ul>
158
159<script type="text/template" id='obj-template'>
160  <%= link %>
161  <% _.each(hits, function (hit) { %>
162    <%= hit.excerpt %>
163  <% }); %>
164</script>
165
166<?php
167echo <<<HTML
168<script>
169$(document).ready(function () {
170  var results = $results;
171  var t = _.template($('#obj-template').html());
172
173  function show_list(res) {
174    var ul = $('#results');
175    ul.empty();
176    if (res) {
177      _.each(res.results, function (obj) {
178        var li = $("<li/>");
179        li.html(t(obj));
180        ul.append(li);
181      });
182    }
183    if (!res || !res.results.length) {
184      var li = $("<li/>");
185      li.text("No matching results");
186      ul.append(li);
187    }
188  }
189
190  show_list(results);
191
192  var ajax = null;
193  function search(q) {
194    if (!q.length) {
195      show_list({results:[]});
196      return;
197    }
198    if ($('#only_open_tickets').attr('checked')) {
199      q = "+type:ticket -status:closed +(" + q + ")";
200    }
201    ajax = $.ajax({
202      url: ABSWEB + 'api.php/search/query',
203      dataType: 'json',
204      data: {
205        q: q
206      },
207      success: function (data) {
208        show_list(data);
209      },
210      error: function (xhr, text, err) {
211        var li = $("<li/>");
212        li.text(err);
213        $('#results').empty().append(li);
214      },
215      complete: function (xhr, text) {
216        ajax = null;
217      }
218    });
219  }
220
221  var S = $('#searchq');
222  S.val($userq);
223  S.attr('autocomplete', 'off');
224  var timer = null;
225  var lastval = $userq;
226
227  function trigger_search() {
228    var q = S.val();
229
230    if (ajax) {
231      ajax.abort();
232      ajax = null;
233    }
234    clearTimeout(timer);
235    timer = setTimeout(function () {
236      search(q);
237    }, 250);
238  }
239
240  S.bind('keyup.mtrack', function () {
241    var q = $(this).val();
242    if (lastval == q) {
243      return;
244    }
245    lastval = q;
246    trigger_search();
247  });
248
249  $('#only_open_tickets').click(function () {
250    trigger_search();
251  });
252
253});
254</script>
255HTML;
256
257mtrack_foot();