gitprep / templates / settings / collaboration.html.ep /
Yuki Kimoto cleanup
e364325 8 years ago
1 contributor
154 lines | 4.415kb
<%
  # API
  my $api = gitprep_api;
  my $manager = app->manager;

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

% 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/$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") %>"><%= $collaborator %></a>
                      <form action="<%= url_for->query(op => 'remove') %>" method="post" style="display:inline-block">
                        <%= hidden_field 'collaborator' => $collaborator %>
                        (<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"" >
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

  %= include '/include/footer';