2012-01-25 11:51:20 -08:00
|
|
|
<?php
|
|
|
|
|
2012-03-09 15:46:25 -08:00
|
|
|
final class PhabricatorProjectUpdateController
|
2012-01-25 11:51:20 -08:00
|
|
|
extends PhabricatorProjectController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
private $action;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
$this->action = $data['action'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2012-08-15 10:44:58 -07:00
|
|
|
$capabilities = array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
);
|
2012-01-25 11:51:20 -08:00
|
|
|
|
|
|
|
$process_action = false;
|
|
|
|
switch ($this->action) {
|
|
|
|
case 'join':
|
2012-08-15 10:44:58 -07:00
|
|
|
$capabilities[] = PhabricatorPolicyCapability::CAN_JOIN;
|
2012-01-25 11:51:20 -08:00
|
|
|
$process_action = $request->isFormPost();
|
|
|
|
break;
|
|
|
|
case 'leave':
|
|
|
|
$process_action = $request->isDialogFormPost();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2012-08-15 10:44:58 -07:00
|
|
|
$project = id(new PhabricatorProjectQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->needMembers(true)
|
|
|
|
->requireCapabilities($capabilities)
|
|
|
|
->executeOne();
|
|
|
|
if (!$project) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:04:01 -08:00
|
|
|
$project_uri = $this->getApplicationURI('profile/'.$project->getID().'/');
|
2012-01-25 11:51:20 -08:00
|
|
|
|
|
|
|
if ($process_action) {
|
2014-02-10 14:30:00 -08:00
|
|
|
|
|
|
|
$edge_action = null;
|
2012-01-25 11:51:20 -08:00
|
|
|
switch ($this->action) {
|
|
|
|
case 'join':
|
2014-02-10 14:30:00 -08:00
|
|
|
$edge_action = '+';
|
2012-01-25 11:51:20 -08:00
|
|
|
break;
|
|
|
|
case 'leave':
|
2014-02-10 14:30:00 -08:00
|
|
|
$edge_action = '-';
|
2012-01-25 11:51:20 -08:00
|
|
|
break;
|
|
|
|
}
|
2014-02-10 14:30:00 -08:00
|
|
|
|
2015-01-02 10:10:58 +11:00
|
|
|
$type_member = PhabricatorProjectProjectHasMemberEdgeType::EDGECONST;
|
2014-02-10 14:30:00 -08:00
|
|
|
$member_spec = array(
|
|
|
|
$edge_action => array($user->getPHID() => $user->getPHID()),
|
|
|
|
);
|
|
|
|
|
|
|
|
$xactions = array();
|
|
|
|
$xactions[] = id(new PhabricatorProjectTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
|
|
|
|
->setMetadataValue('edge:type', $type_member)
|
|
|
|
->setNewValue($member_spec);
|
|
|
|
|
|
|
|
$editor = id(new PhabricatorProjectTransactionEditor($project))
|
|
|
|
->setActor($user)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true)
|
|
|
|
->setContinueOnMissingFields(true)
|
|
|
|
->applyTransactions($project, $xactions);
|
|
|
|
|
2012-01-25 11:51:20 -08:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($project_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dialog = null;
|
|
|
|
switch ($this->action) {
|
|
|
|
case 'leave':
|
|
|
|
$dialog = new AphrontDialogView();
|
|
|
|
$dialog->setUser($user);
|
2014-09-18 11:00:50 -07:00
|
|
|
if ($this->userCannotLeave($project)) {
|
|
|
|
$dialog->setTitle(pht('You can not leave this project.'));
|
|
|
|
$body = pht('The membership is locked for this project.');
|
|
|
|
} else {
|
|
|
|
$dialog->setTitle(pht('Really leave project?'));
|
|
|
|
$body = pht(
|
|
|
|
'Your tremendous contributions to this project will be sorely '.
|
|
|
|
'missed. Are you sure you want to leave?');
|
|
|
|
$dialog->addSubmitButton(pht('Leave Project'));
|
|
|
|
}
|
|
|
|
$dialog->appendParagraph($body);
|
2012-01-25 11:51:20 -08:00
|
|
|
$dialog->addCancelButton($project_uri);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:00:50 -07:00
|
|
|
/**
|
|
|
|
* This is enforced in @{class:PhabricatorProjectTransactionEditor}. We use
|
|
|
|
* this logic to render a better form for users hitting this case.
|
|
|
|
*/
|
|
|
|
private function userCannotLeave(PhabricatorProject $project) {
|
|
|
|
$user = $this->getRequest()->getUser();
|
|
|
|
|
|
|
|
return
|
|
|
|
$project->getIsMembershipLocked() &&
|
|
|
|
!PhabricatorPolicyFilter::hasCapability(
|
|
|
|
$user,
|
|
|
|
$project,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT);
|
|
|
|
}
|
2012-01-25 11:51:20 -08:00
|
|
|
}
|