public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $project = id(new PhabricatorProject())->load($this->id);
     if (!$project) {
         return new Aphront404Response();
     }
     $affiliation = id(new PhabricatorProjectAffiliation())->loadOneWhere('projectPHID = %s AND userPHID = %s', $project->getPHID(), $user->getPHID());
     if (!$affiliation) {
         $affiliation = new PhabricatorProjectAffiliation();
         $affiliation->setUserPHID($user->getPHID());
         $affiliation->setProjectPHID($project->getPHID());
     }
     if ($request->isFormPost()) {
         $affiliation->setRole($request->getStr('role'));
         $affiliation->setStatus($request->getStr('status'));
         if (!strlen($affiliation->getRole())) {
             if ($affiliation->getID()) {
                 if ($affiliation->getIsOwner()) {
                     $affiliation->setRole('Owner');
                     $affiliation->save();
                 } else {
                     $affiliation->delete();
                 }
             }
         } else {
             $affiliation->save();
         }
         return id(new AphrontRedirectResponse())->setURI('/project/view/' . $project->getID() . '/');
     }
     $status_options = array('' => 'Current', 'former' => 'Former');
     $form = new AphrontFormView();
     $form->setUser($user)->setAction('/project/affiliation/' . $project->getID() . '/')->appendChild(id(new AphrontFormTextControl())->setLabel('Role')->setName('role')->setValue($affiliation->getRole()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Status')->setName('status')->setOptions($status_options)->setValue($affiliation->getStatus()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/project/view/' . $project->getID() . '/')->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Edit Project Affiliation');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse($panel, array('title' => 'Edit Project Affiliation'));
 }
 private function applyTransactionEffect(PhabricatorProject $project, PhabricatorProjectTransaction $xaction)
 {
     $type = $xaction->getTransactionType();
     switch ($type) {
         case PhabricatorProjectTransactionType::TYPE_NAME:
             $project->setName($xaction->getNewValue());
             $project->setPhrictionSlug($xaction->getNewValue());
             $this->validateName($project);
             break;
         case PhabricatorProjectTransactionType::TYPE_STATUS:
             $project->setStatus($xaction->getNewValue());
             break;
         case PhabricatorProjectTransactionType::TYPE_MEMBERS:
             $old = array_fill_keys($xaction->getOldValue(), true);
             $new = array_fill_keys($xaction->getNewValue(), true);
             $add = array();
             $rem = array();
             foreach ($project->getAffiliations() as $affil) {
                 if (empty($new[$affil->getUserPHID()])) {
                     $rem[] = $affil;
                 }
             }
             foreach ($new as $phid => $ignored) {
                 if (empty($old[$phid])) {
                     $affil = new PhabricatorProjectAffiliation();
                     $affil->setRole('');
                     $affil->setUserPHID($phid);
                     $add[] = $affil;
                 }
             }
             $this->addAffiliations = $add;
             $this->remAffiliations = $rem;
             break;
         default:
             throw new Exception("Unknown transaction type '{$type}'!");
     }
 }