gitprep / templates / settings / collaboration.html.ep /
b51214a 7 years ago
1 contributor
164 lines | 4.834kb
<%
  # API
  my $api = gitprep_api;
  my $manager = app->manager;

  # Parameters
  my $op = param('op') || '';
  my $user_id = param('user') || '';
  my $project_id = param('project');
  
  # Authentication
  unless ($api->logined($user_id)) {
    $self->redirect_to('/');
    return;
  }
  
  my $project_row_id = app->dbi->model('project')->select(
    'project.row_id',
    where => {'user.id' => $user_id, 'project.id' => $project_id}
  )->value;
  
  # Rename project
  my $git = app->git;
  my $errors;
  if (lc $self->req->method eq 'post') {
    if ($op eq 'add') {
      my $collaborator_id = param('collaborator');
      
      # Validator
      my $vc = app->vc;
      
      # Validation result
      my $validation = $vc->validation;
      
      # collaborator check
      if (!length $collaborator_id) {
        $validation->add_failed(collaborator => "collaborator is empty");
      }
      elsif ($collaborator_id eq $user_id) {
        $validation->add_failed(collaborator => "User $collaborator_id is yourself");
      }
      else {
        my $row = app->dbi->model('user')->select(
          where => {id => $collaborator_id}
        )->one;
        if (!$row) {
          $validation->add_failed(collaborator => "User $collaborator_id don't exists");
        }
        else {
          my $row = app->dbi->model('collaboration')->select(
            where => {project => $project_row_id, 'user.id' => $collaborator_id}
          )->one;
          if ($row) {
            $validation->add_failed(collaborator => "Collaborator $collaborator_id already exists");
          }
        }
      }
      
      if ($validation->is_valid) {
        
        my $collaborator_row_id = $api->get_user_row_id($collaborator_id);
        
        # Insert
        eval {
          app->dbi->model('collaboration')->insert(
            {
              project => $project_row_id,
              user => $collaborator_row_id
            }
          );
        };
        if (my $e = $@) {
          app->log->error(url_with . ": $e");
          $errors = ['Internal Error'];
        }
        else {
          flash(message => "Collaborator $collaborator_id is added.");
          $self->redirect_to('current');
          return;
        }
      }
      else {
        $errors = $validation->messages;
      }
    }
    elsif ($op eq 'remove') {
      my $collaborator_id = param('collaborator');
      
      my $collaborator_row_id = $api->get_user_row_id($collaborator_id);
      
      # Delete
      eval {
        app->dbi->model('collaboration')->delete(
          where => {
            project => $project_row_id,
            user => $collaborator_row_id
          }
        );
      };
      if (my $e = $@) {
        app->log->error(url_with . ": $e");
        $errors = ['Internal Error'];
      }
      else {
        flash(message => "Collaborator $collaborator_id is removed.");
        $self->redirect_to('current');
        return;
      }
    }
  }
  
  my $collaborators = app->dbi->model('collaboration')->select(
    {user => ['id']},
    where => {project => $project_row_id},
    append => 'order by collaboration.user'
  )->all;
%>

% layout 'common', title => 'Collaboration';
  
  %= include '/include/header';
  
  <div class="container">
    %= include '/include/errors', errors => $errors;
    %= include '/include/message', message => flash('message');
    
    <div class="project-settings">
      <div class="left">
        <ul>
          <li><a href="<%= url_for("/$user_id/$project_id/settings") %>">Options</a></li>
          <li><b>Collaborators</b></li>
        </ul>
      </div>
      <div class="right">
        <div class="collaboration">
          <div>Manage Collaborators</div>
            % if (@$collaborators) {
              <table>
                % for my $collaborator (@$collaborators) {
                  <tr>
                    <td>
                      <a href="<%= url_for("/$collaborator->{'user.id'}") %>"><%= $collaborator->{'user.id'} %></a>
                      <form action="<%= url_for->query(op => 'remove') %>" method="post" style="display:inline-block">
                        <%= hidden_field 'collaborator' => $collaborator->{'user.id'} %>
                        (<a href="javascript:void(0)" onclick="$(this).closest('form').submit();" style="color:red">remove</a>)
                      </form>
                    </td>
                  </tr>
                % }
              </table>
            % }
            <form action="<%= url_for->query(op => 'add') %>" method="post">
              <div>
                <%= text_field 'collaborator' %>
                <input type="submit" value="Add" class="btn" >
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

  %= include '/include/footer';