/**
  * Edit gitolite repository access levels
  * @throws ValidationErrors
  */
 function edit_git_repo()
 {
     $repo_id = array_var($_GET, 'project_source_repository_id');
     //project objects id
     $is_gitolite = GitoliteAdmin::is_gitolite();
     if (!ProjectSourceRepositories::canAdd($this->logged_user, $this->active_project)) {
         $this->response->forbidden();
     }
     // if
     $project = $this->active_project;
     $project_id = $project->getId();
     $logged_user = $this->logged_user;
     $user_id = $logged_user->getId();
     $no_key_warning = FALSE;
     // to give warning if logged in user has not added his public key
     if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'project_tracking_gitolite') {
         $do_continue = true;
     }
     if ($do_continue) {
         $users_details = $this->active_project->users()->describe($this->logged_user, true, true, STATE_VISIBLE);
         $repo_details = ProjectGitolite::get_repo_details($repo_id);
         /* print_r($repo_details);
            die(); */
         $repository_data = $this->request->post('repository');
         if (!is_array($repository_data)) {
             $repository_data = array('updatetype' => $this->active_repository->getFieldValue("update_type"), 'visibility' => $this->project_object_repository->getVisibility());
         }
         // if
         if (is_array($repo_details) && count($repo_details) > 0) {
             // repository id from integer_field_1 in project_objects , we are saving this id in our tables.
             $git_repo_id = $repo_details['repo_id'];
             $access_array = ProjectGitolite::get_access_levels($git_repo_id);
             //$result_access = DB::execute("SELECT * from $access_table_name where repo_id = '".$repo_details['repo_id']."'");
             if (is_array($access_array) && count($access_array) > 0) {
                 $access = $access_array['permissions'];
                 $permissions = @unserialize($access);
                 if ($permissions !== false || $permissions === 'b:0;') {
                     $permissions_array = $permissions;
                 } else {
                     $permissions_array = array();
                 }
             } else {
                 $this->response->forbidden();
             }
         } else {
             $this->response->forbidden();
         }
         //print_r($permissions_array);
         $user_detail_permissions = array();
         $view_url = false;
         if (is_foreachable($users_details)) {
             foreach ($users_details as $key => $value) {
                 // check key exists
                 $user_keys = GitoliteAc::check_keys_added($value['user']['id']);
                 if ($user_keys > 0) {
                     $user_detail_permissions[$value['user']['id']] = array('readaccess' => $permissions_array[$value['user']['id']] == "2" ? TRUE : FALSE, 'writeaccess' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'writeaccessplus' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'user_keys' => $user_keys);
                     $allowed_users[$value['user']['id']] = $value['user']['name'];
                 }
             }
         }
         if (!isset($user_detail_permissions[$user_id]) && ($this->logged_user->isAdministrator() || $this->logged_user->isProjectManager())) {
             $repoobj = new ProjectSourceRepositories();
             $objuser = new rtmUser($user_id);
             $user_keys = GitoliteAc::check_keys_added($user_id);
             if ($user_keys) {
                 $user_detail_permissions[$user_id] = array('readaccess' => $permissions_array[$user_id] == "2" ? TRUE : $repoobj->canAccess($objuser, $project), 'writeaccess' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canAdd($objuser, $project), 'writeaccessplus' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canManage($objuser, $project), 'user_keys' => $user_keys);
                 $allowed_users[$user_id] = $logged_user->getName();
             } else {
                 $no_key_warning = TRUE;
                 $view_url = $this->logged_user->getViewUrl();
             }
         }
         $this->response->assign(array('curr_users' => $allowed_users, 'repo_details' => $repo_details, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('edit_git_repository', array('project_slug' => $project->getSlug(), 'project_source_repository_id' => $repo_id)), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'repository_data' => $repository_data, 'view_url' => $view_url));
     } else {
         $this->response->assign(array('add_error' => TRUE));
     }
     if ($this->request->isSubmitted()) {
         // check for form submission
         try {
             /* Check form with validation error */
             $repository_data = $this->request->post('repository');
             $errors = new ValidationErrors();
             $post_data = $this->request->post();
             $settings = GitoliteAdmin::get_admin_settings();
             $sever_user_path = GitoliteAdmin::get_server_user_path();
             if (!$sever_user_path) {
                 //  $errors->addError ( 'Repository path on server invalid' );
             }
             $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git";
             $repo_name = trim($repository_data['name']);
             $access = $post_data['access'];
             if ($repo_name == "") {
                 $errors->addError('Please enter repository name', 'repo_name');
             }
             if (!is_array($access) && count($access) == 0) {
                 $errors->addError('Select access levels for user', 'access');
             }
             /* Check for duplications repository name and Key */
             if (!$errors->hasErrors()) {
                 if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) {
                     $errors->addError('Please enter valid repository name.', 'repo_name');
                 }
                 /* $dup_cnt = ProjectGitolite::check_duplication($project_id,$repository_data);
                                       if(count($dup_cnt) == 0)
                                       {
                                       $errors->addError('Problem occured while saving data, please try again.');
                                       }
                                       elseif(count($dup_cnt) > 0)
                                       {
                                       if($dup_cnt[0]['dup_name_cnt'] > 0)
                                       {
                                       $errors->addError('You have already added repository with same name.');
                 
                                       }
                 
                                       } */
             }
             // if errors found throw error exception
             if ($errors->hasErrors()) {
                 throw $errors;
             }
             /** save gitolite details in database * */
             // save reponame
             try {
                 DB::beginWork('Update repository @ ' . __CLASS__);
                 $this->active_repository->setAttributes($repository_data);
                 $this->project_object_repository->setVisibility($repository_data['visibility']);
                 $this->project_object_repository->setName($repository_data['name']);
                 $this->active_repository->save();
                 $this->project_object_repository->save();
                 $repo_fk = $this->active_repository->getId();
                 if ($repo_id) {
                     $notif_setting = isset($repository_data["disable_notifications"]) ? "yes" : "no";
                     $repo_table_name = TABLE_PREFIX . "rt_gitolite_repomaster";
                     DB::execute("update {$repo_table_name} set disable_notifications = '" . $notif_setting . "' where repo_fk = '" . $repo_fk . "'");
                     $update_access = ProjectGitolite::update_access_levels($git_repo_id, serialize($post_data['access']));
                     if ($update_access) {
                         $res = ProjectGitolite::render_conf_file();
                         $dir = $settings['gitoliteadminpath'] . "gitolite-admin";
                         $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push  || echo 'Not found'";
                         exec($command, $output, $return_var);
                         /* $git_server = $settings['gitoliteuser']."@".$settings['gitoliteserveradd'];
                            $command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name;
                            exec($command,$output,$return_var); */
                     } else {
                         $errors->addError('Error while saving access levels.');
                         throw $errors;
                     }
                 } else {
                     $errors->addError('Error while saving repository.');
                     throw $errors;
                 }
                 DB::commit('Repository created @ ' . __CLASS__);
                 $this->response->ok();
             } catch (Exception $e) {
                 DB::rollback('Failed to create a repository @ ' . __CLASS__);
                 $this->response->exception($e);
             }
         } catch (Exception $e) {
             DB::rollback('Failed to create a repository @ ' . __CLASS__);
             $this->response->exception($e);
         }
     }
 }