1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-14 19:02:41 +01:00
phorge-phorge/src/applications/differential/controller/DifferentialRevisionEditController.php

184 lines
5.1 KiB
PHP
Raw Normal View History

2011-01-25 22:26:09 +01:00
<?php
final class DifferentialRevisionEditController
extends DifferentialController {
2011-01-25 22:26:09 +01:00
private $id;
public function willProcessRequest(array $data) {
$this->id = idx($data, 'id');
}
public function processRequest() {
2011-02-04 00:41:58 +01:00
$request = $this->getRequest();
$viewer = $request->getUser();
2011-02-04 00:41:58 +01:00
if (!$this->id) {
$this->id = $request->getInt('revisionID');
}
2011-01-25 22:26:09 +01:00
if ($this->id) {
$revision = id(new DifferentialRevisionQuery())
->setViewer($viewer)
->withIDs(array($this->id))
->needRelationships(true)
->needReviewerStatus(true)
->needActiveDiffs(true)
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
2011-01-25 22:26:09 +01:00
if (!$revision) {
return new Aphront404Response();
}
} else {
$revision = DifferentialRevision::initializeNewRevision($viewer);
$revision->attachReviewerStatus(array());
2011-01-25 22:26:09 +01:00
}
2011-01-26 02:17:19 +01:00
$diff_id = $request->getInt('diffID');
if ($diff_id) {
$diff = id(new DifferentialDiffQuery())
->setViewer($viewer)
->withIDs(array($diff_id))
->executeOne();
2011-01-26 02:17:19 +01:00
if (!$diff) {
return new Aphront404Response();
}
if ($diff->getRevisionID()) {
// TODO: Redirect?
throw new Exception('This diff is already attached to a revision!');
2011-01-26 02:17:19 +01:00
}
} else {
$diff = null;
}
if (!$diff) {
if (!$revision->getID()) {
throw new Exception(
pht('You can not create a new revision without a diff!'));
}
} else {
// TODO: It would be nice to show the diff being attached in the UI.
}
2011-02-03 04:38:43 +01:00
$field_list = PhabricatorCustomField::getObjectFields(
$revision,
PhabricatorCustomField::ROLE_EDIT);
$field_list
->setViewer($viewer)
->readFieldsFromStorage($revision);
2011-01-25 22:26:09 +01:00
$validation_exception = null;
2011-02-03 04:38:43 +01:00
if ($request->isFormPost() && !$request->getStr('viaDiffView')) {
$xactions = $field_list->buildFieldTransactionsFromRequest(
new DifferentialTransaction(),
$request);
if ($diff) {
$xactions[] = id(new DifferentialTransaction())
->setTransactionType(DifferentialTransaction::TYPE_UPDATE)
->setNewValue($diff->getPHID());
}
$comments = $request->getStr('comments');
if (strlen($comments)) {
$xactions[] = id(new DifferentialTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
->attachComment(
id(new DifferentialTransactionComment())
->setContent($comments));
2011-01-25 22:26:09 +01:00
}
2011-02-03 04:38:43 +01:00
$editor = id(new DifferentialTransactionEditor())
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true);
try {
$editor->applyTransactions($revision, $xactions);
$revision_uri = '/D'.$revision->getID();
return id(new AphrontRedirectResponse())->setURI($revision_uri);
} catch (PhabricatorApplicationTransactionValidationException $ex) {
$validation_exception = $ex;
}
}
2011-01-25 22:26:09 +01:00
2011-01-25 22:26:09 +01:00
$form = new AphrontFormView();
2011-01-31 03:52:29 +01:00
$form->setUser($request->getUser());
2011-01-26 02:17:19 +01:00
if ($diff) {
$form->addHiddenInput('diffID', $diff->getID());
}
2011-01-25 22:26:09 +01:00
if ($revision->getID()) {
2011-01-25 22:48:05 +01:00
$form->setAction('/differential/revision/edit/'.$revision->getID().'/');
2011-01-25 22:26:09 +01:00
} else {
$form->setAction('/differential/revision/edit/');
}
if ($diff && $revision->getID()) {
$form
->appendChild(
id(new AphrontFormTextAreaControl())
->setLabel(pht('Comments'))
->setName('comments')
->setCaption(pht("Explain what's new in this diff."))
->setValue($request->getStr('comments')))
->appendChild(
id(new AphrontFormSubmitControl())
->setValue(pht('Save')))
->appendChild(
id(new AphrontFormDividerControl()));
}
$field_list->appendFieldsToForm($form);
2011-01-26 02:17:19 +01:00
$submit = id(new AphrontFormSubmitControl())
->setValue('Save');
if ($diff) {
$submit->addCancelButton('/differential/diff/'.$diff->getID().'/');
} else {
$submit->addCancelButton('/D'.$revision->getID());
}
$form->appendChild($submit);
2011-01-25 22:26:09 +01:00
$crumbs = $this->buildApplicationCrumbs();
2011-01-25 22:26:09 +01:00
if ($revision->getID()) {
if ($diff) {
$title = pht('Update Differential Revision');
$crumbs->addTextCrumb(
'D'.$revision->getID(),
'/differential/diff/'.$diff->getID().'/');
} else {
$title = pht('Edit Differential Revision');
$crumbs->addTextCrumb(
'D'.$revision->getID(),
'/D'.$revision->getID());
}
2011-01-25 22:26:09 +01:00
} else {
$title = pht('Create New Differential Revision');
2011-01-25 22:26:09 +01:00
}
$form_box = id(new PHUIObjectBoxView())
->setHeaderText($title)
->setValidationException($validation_exception)
->setForm($form);
$crumbs->addTextCrumb($title);
2011-01-25 22:26:09 +01:00
return $this->buildApplicationPage(
2011-01-25 22:26:09 +01:00
array(
$crumbs,
$form_box,
),
array(
'title' => $title,
2011-01-25 22:26:09 +01:00
));
}
}