1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-03-12 20:34:54 +01:00
phorge-phorge/src/applications/transactions/editengine/PhabricatorApplicationEditEngine.php

304 lines
8.8 KiB
PHP
Raw Normal View History

Implement a basic version of ApplicationEditor in Paste Summary: Ref T9132. Ref T4768. This is a rough v0 of ApplicationEditor, which replaces the edit workflow in Paste. This mostly looks and works like ApplicationSearch, and is heavily modeled on it. Roughly, we define a set of editable fields and the ApplicationEditor stuff builds everything else. This has no functional changes, except: - I removed "Fork Paste" since I don't think it's particularly useful now that pastes are editable. We could restore it if users miss it. - Subscribers are now editable. - Form field order is a little goofy (this will be fixed in a future diff). - Subscribers and projects are now race-resistant. The race-resistance works like this: instead of submitting just the new value ("subscribers=apple, dog") and doing a set operation ("set subscribers = apple, dog"), we submit the old and new values ("original=apple" + "new=apple, dog") then apply the user's changes as an add + remove ("add=dog", "remove=<none>"). This means that two users who do "Edit Paste" at around the same time and each add or remove a couple of subscribers won't overwrite each other, unless they actually add or remove the exact same subscribers (in which case their edits legitimately conflict). Previously, the last user to save would win, and whatever was in their field would overwrite the prior state, potentially losing the first user's edits. Test Plan: - Created pastes. - Created pastes via API. - Edited pastes. - Edited every field. - Opened a paste in two windows and did project/subscriber edits in each, saved in arbitrary order, had edits respected. Reviewers: chad Reviewed By: chad Maniphest Tasks: T4768, T9132 Differential Revision: https://secure.phabricator.com/D14390
2015-11-02 18:58:32 -08:00
<?php
abstract class PhabricatorApplicationEditEngine extends Phobject {
private $viewer;
private $controller;
final public function setViewer(PhabricatorUser $viewer) {
$this->viewer = $viewer;
return $this;
}
final public function getViewer() {
return $this->viewer;
}
final public function setController(PhabricatorController $controller) {
$this->controller = $controller;
$this->setViewer($controller->getViewer());
return $this;
}
final public function getController() {
return $this->controller;
}
final protected function buildEditFields($object) {
$viewer = $this->getViewer();
$editor = $object->getApplicationTransactionEditor();
$types = $editor->getTransactionTypesForObject($object);
$types = array_fuse($types);
$fields = $this->buildCustomEditFields($object);
if ($object instanceof PhabricatorPolicyInterface) {
$policies = id(new PhabricatorPolicyQuery())
->setViewer($viewer)
->setObject($object)
->execute();
$map = array(
PhabricatorTransactions::TYPE_VIEW_POLICY => array(
'key' => 'policy.view',
'aliases' => array('view'),
'capability' => PhabricatorPolicyCapability::CAN_VIEW,
),
PhabricatorTransactions::TYPE_EDIT_POLICY => array(
'key' => 'policy.edit',
'aliases' => array('edit'),
'capability' => PhabricatorPolicyCapability::CAN_EDIT,
),
PhabricatorTransactions::TYPE_JOIN_POLICY => array(
'key' => 'policy.join',
'aliases' => array('join'),
'capability' => PhabricatorPolicyCapability::CAN_JOIN,
),
);
foreach ($map as $type => $spec) {
if (empty($types[$type])) {
continue;
}
$capability = $spec['capability'];
$key = $spec['key'];
$aliases = $spec['aliases'];
$policy_field = id(new PhabricatorPolicyEditField())
->setKey($key)
->setAliases($aliases)
->setCapability($capability)
->setPolicies($policies)
->setTransactionType($type)
->setValue($object->getPolicy($capability));
$fields[] = $policy_field;
if ($object instanceof PhabricatorSpacesInterface) {
if ($capability == PhabricatorPolicyCapability::CAN_VIEW) {
$type_space = PhabricatorTransactions::TYPE_SPACE;
if (isset($types[$type_space])) {
$space_field = id(new PhabricatorSpaceEditField())
->setKey('spacePHID')
->setAliases(array('space', 'policy.space'))
->setTransactionType($type_space)
->setValue($object->getSpacePHID());
$fields[] = $space_field;
$policy_field->setSpaceField($space_field);
}
}
}
}
}
$edge_type = PhabricatorTransactions::TYPE_EDGE;
$object_phid = $object->getPHID();
$project_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
if ($object instanceof PhabricatorProjectInterface) {
if (isset($types[$edge_type])) {
if ($object_phid) {
$project_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
$object_phid,
$project_edge_type);
$project_phids = array_reverse($project_phids);
} else {
$project_phids = array();
}
$edge_field = id(new PhabricatorDatasourceEditField())
->setKey('projectPHIDs')
->setLabel(pht('Projects'))
->setDatasource(new PhabricatorProjectDatasource())
->setAliases(array('project', 'projects'))
->setTransactionType($edge_type)
->setMetadataValue('edge:type', $project_edge_type)
->setValue($project_phids);
$fields[] = $edge_field;
}
}
$subscribers_type = PhabricatorTransactions::TYPE_SUBSCRIBERS;
if ($object instanceof PhabricatorSubscribableInterface) {
if (isset($types[$subscribers_type])) {
if ($object_phid) {
$sub_phids = PhabricatorSubscribersQuery::loadSubscribersForPHID(
$object_phid);
} else {
// TODO: Allow applications to provide default subscribers; Maniphest
// does this at a minimum.
$sub_phids = array();
}
$subscribers_field = id(new PhabricatorDatasourceEditField())
->setKey('subscriberPHIDs')
->setLabel(pht('Subscribers'))
->setDatasource(new PhabricatorMetaMTAMailableDatasource())
->setAliases(array('subscriber', 'subscribers'))
->setTransactionType($subscribers_type)
->setValue($sub_phids);
$fields[] = $subscribers_field;
}
}
return $fields;
}
abstract protected function newEditableObject();
abstract protected function newObjectQuery();
abstract protected function buildCustomEditFields($object);
abstract protected function getObjectCreateTitleText($object);
abstract protected function getObjectEditTitleText($object);
abstract protected function getObjectCreateShortText($object);
abstract protected function getObjectEditShortText($object);
abstract protected function getObjectViewURI($object);
protected function getObjectCreateCancelURI($object) {
return $this->getController()->getApplicationURI();
}
protected function getObjectEditCancelURI($object) {
return $this->getObjectViewURI($object);
}
protected function getObjectCreateButtonText($object) {
return $this->getObjectCreateTitleText($object);
}
protected function getObjectEditButtonText($object) {
return pht('Save Changes');
}
final public function buildResponse() {
$controller = $this->getController();
$viewer = $this->getViewer();
$request = $controller->getRequest();
$id = $request->getURIData('id');
if ($id) {
$object = $this->newObjectQuery()
->setViewer($viewer)
->withIDs(array($id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$object) {
return new Aphront404Response();
}
$is_create = false;
} else {
$object = $this->newEditableObject();
$is_create = true;
}
$fields = $this->buildEditFields($object);
foreach ($fields as $field) {
$field
->setViewer($viewer)
->setObject($object);
}
$validation_exception = null;
if ($request->isFormPost()) {
foreach ($fields as $field) {
$field->readValueFromSubmit($request);
}
$template = $object->getApplicationTransactionTemplate();
$xactions = array();
foreach ($fields as $field) {
$xactions[] = $field->generateTransaction(clone $template);
}
$editor = $object->getApplicationTransactionEditor()
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true)
->setContinueOnMissingFields(false);
try {
$editor->applyTransactions($object, $xactions);
return id(new AphrontRedirectResponse())
->setURI($this->getObjectViewURI($object));
} catch (PhabricatorApplicationTransactionValidationException $ex) {
$validation_exception = $ex;
}
} else {
if ($is_create) {
foreach ($fields as $field) {
$field->readValueFromRequest($request);
}
} else {
foreach ($fields as $field) {
$field->readValueFromObject($object);
}
}
}
$box = id(new PHUIObjectBoxView())
->setUser($viewer);
$crumbs = $controller->buildApplicationCrumbsForEditEngine();
if ($is_create) {
$header_text = $this->getObjectCreateTitleText($object);
$crumbs->addTextCrumb(
$this->getObjectCreateShortText($object));
$cancel_uri = $this->getObjectCreateCancelURI($object);
$submit_button = $this->getObjectCreateButtonText($object);
} else {
$header_text = $this->getObjectEditTitleText($object);
$crumbs->addTextCrumb(
$this->getObjectEditShortText($object),
$this->getObjectViewURI($object));
$cancel_uri = $this->getObjectEditCancelURI($object);
$submit_button = $this->getObjectEditButtonText($object);
}
$box->setHeaderText($header_text);
$form = id(new AphrontFormView())
->setUser($viewer);
foreach ($fields as $field) {
$field->appendToForm($form);
}
$form->appendControl(
id(new AphrontFormSubmitControl())
->addCancelButton($cancel_uri)
->setValue($submit_button));
$box->appendChild($form);
if ($validation_exception) {
$box->setValidationException($validation_exception);
}
return $controller->newPage()
->setTitle($header_text)
->setCrumbs($crumbs)
->appendChild($box);
}
}