gitprep / templates / auto / _search.html.ep /
0e302a3 7 years ago
1 contributor
180 lines | 5.643kb
<%
  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;
  my $total;
  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(':project.id{like}');
      $where->param({'project.id' => "%$q%"});
      $rows = $dbi->model($table)->select(
        [
          {__MY__ => '*'},
          {user => ['id']}
        ],
        where => $where,
        append => "order by project.id, user.id limit $offset, $count"
      )->all;
    }

    $total = $dbi->model($table)->select(
      'count(*)',
      where => $where,
    )->value;
  }
  $rows //= [];
  $total //= 0;

  # Pager
  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_id = $user->{id};
                <li>
                  <div>
                    <a style="font-size:19px" href="<%= url_for("/$user_id") %>"><%= $user_id %></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_id = $project->{'user.id'};
                  my $project_id = $project->{id};
                  my $rev = app->manager->default_branch($user_id, $project_id);
                  my $desc = app->git->description(app->rep_info($user_id, $project_id));
                  my $branches = app->git->branches($self->app->rep_info($user_id, $project_id));
                  my $commit;
                  if (@$branches) {
                    $commit = app->git->get_commit(app->rep_info($user_id, $project_id), $rev);
                  }
                %>
                
                <li>
                  <div>
                    <a style="font-size:19px" href="<%= url_for("/$user_id/$project_id") %>"><%= $user_id %>/<%= $project_id %></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';