Add a search feature

This allows searching for projects, jobsets or jobs by name or
description.
This commit is contained in:
Eelco Dolstra 2013-02-22 15:45:10 +01:00
parent 4343781181
commit 07daff32b8
4 changed files with 113 additions and 9 deletions

View file

@ -300,4 +300,28 @@ sub evals :Local Args(0) {
} }
sub search :Local Args(0) {
my ($self, $c) = @_;
$c->stash->{template} = 'search.tt';
my $query = trim $c->request->params->{"query"};
error($c, "Query is empty.") if $query eq "";
error($c, "Invalid character in query.")
unless $query =~ /^[a-zA-Z0-9_\-]+$/;
$c->stash->{projects} = [ $c->model('DB::Projects')->search(
{ -or => [ name => { ilike => "%$query%" }, displayName => { ilike => "%$query%" }, description => { ilike => "%$query%" } ] },
{ order_by => ["name"] } ) ];
$c->stash->{jobsets} = [ $c->model('DB::Jobsets')->search(
{ -or => [ name => { ilike => "%$query%" }, description => { ilike => "%$query%" } ] },
{ order_by => ["project", "name"] } ) ];
$c->stash->{jobs} = [ $c->model('DB::Jobs')->search(
{ name => { ilike => "%$query%" } },
{ order_by => ["project", "jobset", "name"] } ) ];
}
1; 1;

View file

@ -85,7 +85,7 @@
<div class="navbar navbar-fixed-top navbar-inverse"> <div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner"> <div class="navbar-inner">
<div class="container"> <div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <a class="btn btn-navbar" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
@ -97,7 +97,7 @@
<img src="[% logo %]" alt="Hydra Logo" class="logo" /> <img src="[% logo %]" alt="Hydra Logo" class="logo" />
[% END %] [% END %]
</a> </a>
<div class="nav-collapse collapse"> <div class="nav-collapse collapse navbar-responsive-collapse">
[% PROCESS topbar.tt %] [% PROCESS topbar.tt %]
</div> </div>
</div> </div>

68
src/root/search.tt Normal file
View file

@ -0,0 +1,68 @@
[% WRAPPER layout.tt title="Search results" %]
[% PROCESS common.tt %]
[% IF projects.size > 0; matched = 1 %]
<p>The following projects match your query:</p>
<table class="table table-striped table-condensed">
<thead>
<tr><th>Project</th><th>Description</th></tr>
</thead>
<tbody>
[% FOREACH p IN projects %]
<tr>
<td>[% INCLUDE renderProjectName project=p.name %]</td>
<td>[% HTML.escape(p.description) %]</td>
</tr>
[% END %]
</tbody>
</table>
[% END %]
[% IF jobsets.size > 0; matched = 1 %]
<p>The following jobsets match your query:</p>
<table class="table table-striped table-condensed">
<thead>
<tr><th>Jobset</th><th>Description</th></tr>
</thead>
<tbody>
[% FOREACH j IN jobsets %]
<tr>
<td>[% INCLUDE renderFullJobsetName project=j.get_column('project') jobset=j.name %]</td>
<td>[% HTML.escape(j.description) %]</td>
</tr>
[% END %]
</tbody>
</table>
[% END %]
[% IF jobs.size > 0; matched = 1 %]
<p>The following jobs match your query:</p>
<table class="table table-striped table-condensed">
<thead>
<tr><th>Job</th></tr>
</thead>
<tbody>
[% FOREACH j IN jobs %]
<tr>
<td>[% INCLUDE renderFullJobName project=j.get_column('project') jobset=j.get_column('jobset') job=j.name %]</td>
</tr>
[% END %]
</tbody>
</table>
[% END %]
[% IF !matched %]
<div class="alert alert-warn">Sorry! Nothing matches your
query.</div>
[% END %]
[% END %]

View file

@ -24,7 +24,7 @@
[% INCLUDE makeLinkWrapped content="" %] [% INCLUDE makeLinkWrapped content="" %]
[% END %] [% END %]
<ul class="nav" id="top-menu"> <ul class="nav pull-left" id="top-menu">
[% WRAPPER makeSubMenu title="Status" %] [% WRAPPER makeSubMenu title="Status" %]
[% INCLUDE makeLink [% INCLUDE makeLink
@ -184,10 +184,22 @@
[% END %] [% END %]
[% END %] [% END %]
[% IF c.user_exists %]
[% INCLUDE makeLink uri = c.uri_for(c.controller('Root').action_for('logout')) title = "Sign out" %]
[% ELSE %]
[% INCLUDE makeLink uri = c.uri_for(c.controller('Root').action_for('login')) title = "Sign in" %]
[% END %]
</ul> </ul>
<div class="pull-right">
<form class="navbar-search" action="[% c.uri_for('/search') %]">
<input name="query" type="text" class="search-query span2" placeholder="Search"></input>
</form>
<ul class="nav" id="top-menu">
[% IF c.user_exists %]
[% INCLUDE makeLink uri = c.uri_for(c.controller('Root').action_for('logout')) title = "Sign out" %]
[% ELSE %]
[% INCLUDE makeLink uri = c.uri_for(c.controller('Root').action_for('login')) title = "Sign in" %]
[% END %]
</ul>
</div>