gitprep / templates / auto / _search.html.ep /
Yuki Kimoto cleanup methods
0e8555d 8 years ago
1 contributor
173 lines | 5.462kb
<%
  use Data::Page ();
  use Data::Page::Navigation ();
  
  # Parameters
  my $type = param('type');
  my $q = param('q');
  my $page = param('page');
  
  my $type_exists = $type ? 1 : 0;
  $type ||= 'repositories';
  my $q_exists = defined $q ? 1 : 0;
  
  # DBI
  my $dbi = app->dbi;

  # Limit
  $page ||= 1;
  my $count = 20;
  my $offset = ($page - 1) * $count;

  my $rows;
  my $tabel;
  my $table;
  my $where = $dbi->where;
  if ($q_exists) {
    if ($type eq 'users') {
      $table = 'user';
      $where->clause(':id{like}');
      $where->param({id => "%$q%"});
      $rows = $dbi->model($table)->select(
        where => $where,
        append => "order by id limit $offset, $count"
      )->all;
    } elsif ($type eq 'repositories') {
      $table = 'project';
      $where->clause(':name{like}');
      $where->param({name => "%$q%"});
      $rows = $dbi->model($table)->select(
        where => $where,
        append => "order by name, user_id limit $offset, $count"
      )->all;
    }
  }
  $rows ||= [];

  # Pager
  my $total = $dbi->model($table)->select(
    'count(*)',
    where => $where,
  )->value;
  my $pager = Data::Page->new($total, $count, $page);
  my @pages = $pager->pages_in_navigation(10);
%>
% layout 'common', title => 'Search';
  
  %= include '/include/header', title => 'Gitprep';
  <!-- Index page -->
  <div class="container">
    <div class="search-top">
      <div>Search</div>
      <form action="<%= url_for %>">
        <%= text_field 'q', style => "width:600px;margin-top:10px;margin-right:3px" %>
        <input class="btn" type="submit" value="Search">
        % if ($type_exists) {
          %= hidden_field type => $type;
        % }
      </form>
    </div>
  </div>
  <div class="container">
    <div class="search-result">
      <div class="left">
        <ul>
          <li class="<%= $type eq 'repositories' ? 'active' : '' %>">
            <a href="<%= url_with->query([type => 'repositories']) %>">Repositories</a>
          </li>
          <li class="<%= $type eq 'users' ? 'active' : '' %>">
            <a href="<%= url_with->query([type => 'users']) %>">Users</a>
          </li>
        </ul>
      </div>
      <div class="right">
        % if ($type eq 'users') {
          % if (@$rows) {
            <div>
              We've found <%= $total %> user results
            </div>
            <ul>
              % for my $user (@$rows) {
                % my $user = $user->{id};
                <li>
                  <div>
                    <a style="font-size:19px" href="<%= url_for("/$user") %>"><%= $user %></a>
                  </div>
                </li>
              % }
            </ul>
          % } else {
            <div>
              We couldn't find any users matching '<%= $q %>'
            </div>
          % }
        % } else {
          % if (@$rows) {
            <div>
              We've found <%= $total %> repository results
            </div>
            <ul>
              % for my $project (@$rows) {
                <%
                  my $user = $project->{user_id};
                  my $project = $project->{name};
                  my $rev = app->manager->default_branch($user, $project);
                  my $desc = app->git->description(app->rep_info($user, $project));
                  my $branches = app->git->branches($self->app->rep_info($user, $project));
                  my $commit;
                  if (@$branches) {
                    $commit = app->git->get_commit(app->rep_info($user, $project), $rev);
                  }
                %>
                
                <li>
                  <div>
                    <a style="font-size:19px" href="<%= url_for("/$user/$project") %>"><%= $user %>/<%= $project %></a>
                  </div>
                  <div>
                    <%= $desc %>
                  </div>
                  <div class="text-gray" style="font-size:13px;">
                    % if ($commit) {
                      <span title="<%= $commit->{age_string_datetime_local} %>"><%= $commit->{age_string} %>
                    % } else {
                      <span >Repositry is not yet created.
                    % }
                  </div>
                </li>
              % }
            </ul>
          % } else {
            <div>
              We couldn't find any repositories matching '<%= $q %>'
            </div>
          % }
        % }

        % if (@$rows && $pager->last_page != 1) {
          <div class="pagination-num">
            <ul>
              % if ($pager->previous_page) {
                <li><a href="<%= url_with->query([page => $pager->previous_page]) %>">&laquo;</a></li>
              % } else {
                <li class="disabled"><a href="#">&laquo;</a></li>
              % }
              % for my $p (@pages) {
                % if ($p eq $pager->current_page) {
                  <li class="active"><a href="#"><%= $p %></a></li>
                % } else {
                  <li><a href="<%= url_with->query([page => $p]) %>"><%= $p %></a></li>
                % }
              % }
              % if ($pager->next_page) {
                <li><a href="<%= url_with->query([page => $pager->next_page]) %>">&raquo;</a></li>
              % } else {
                <li class="disabled"><a href="#">&raquo;</a></li>
              % }
            </ul>
          </div>
        % }
      </div>
    </div>
  </div>
  %= include '/include/footer';