2011-01-25 22:26:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-10 00:46:25 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-01-25 22:26:09 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
if (!$this->id) {
|
|
|
|
$this->id = $request->getInt('revisionID');
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:26:09 +01:00
|
|
|
if ($this->id) {
|
|
|
|
$revision = id(new DifferentialRevision())->load($this->id);
|
|
|
|
if (!$revision) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$revision = new DifferentialRevision();
|
|
|
|
}
|
|
|
|
|
2011-08-14 23:28:08 +02:00
|
|
|
$revision->loadRelationships();
|
2011-08-10 22:46:01 +02:00
|
|
|
$aux_fields = $this->loadAuxiliaryFields($revision);
|
|
|
|
|
2011-01-26 02:17:19 +01:00
|
|
|
$diff_id = $request->getInt('diffID');
|
|
|
|
if ($diff_id) {
|
|
|
|
$diff = id(new DifferentialDiff())->load($diff_id);
|
|
|
|
if (!$diff) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
if ($diff->getRevisionID()) {
|
|
|
|
// TODO: Redirect?
|
|
|
|
throw new Exception("This diff is already attached to a revision!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$diff = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errors = array();
|
2011-02-03 04:38:43 +01:00
|
|
|
|
2011-01-25 22:26:09 +01:00
|
|
|
|
2011-02-03 04:38:43 +01:00
|
|
|
if ($request->isFormPost() && !$request->getStr('viaDiffView')) {
|
2011-01-26 02:17:19 +01:00
|
|
|
$user_phid = $request->getUser()->getPHID();
|
|
|
|
|
2011-08-10 22:46:01 +02:00
|
|
|
foreach ($aux_fields as $aux_field) {
|
|
|
|
$aux_field->setValueFromRequest($request);
|
|
|
|
try {
|
|
|
|
$aux_field->validateField();
|
|
|
|
} catch (DifferentialFieldValidationException $ex) {
|
|
|
|
$errors[] = $ex->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:26:09 +01:00
|
|
|
if (!$errors) {
|
2011-01-26 02:17:19 +01:00
|
|
|
$editor = new DifferentialRevisionEditor($revision, $user_phid);
|
|
|
|
if ($diff) {
|
|
|
|
$editor->addDiff($diff, $request->getStr('comments'));
|
|
|
|
}
|
2011-08-10 22:46:01 +02:00
|
|
|
$editor->setAuxiliaryFields($aux_fields);
|
2011-01-26 02:17:19 +01:00
|
|
|
$editor->save();
|
|
|
|
|
2011-01-30 22:20:56 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
2011-01-26 02:17:19 +01:00
|
|
|
->setURI('/D'.$revision->getID());
|
2011-01-25 22:26:09 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-03 04:38:43 +01:00
|
|
|
|
2011-08-14 23:28:08 +02:00
|
|
|
$aux_phids = array();
|
|
|
|
foreach ($aux_fields as $key => $aux_field) {
|
|
|
|
$aux_phids[$key] = $aux_field->getRequiredHandlePHIDsForRevisionEdit();
|
|
|
|
}
|
|
|
|
$phids = array_mergev($aux_phids);
|
2011-02-03 02:38:03 +01:00
|
|
|
$phids = array_unique($phids);
|
|
|
|
$handles = id(new PhabricatorObjectHandleData($phids))
|
|
|
|
->loadHandles();
|
2011-08-14 23:28:08 +02:00
|
|
|
foreach ($aux_fields as $key => $aux_field) {
|
|
|
|
$aux_field->setHandles(array_select_keys($handles, $aux_phids[$key]));
|
|
|
|
}
|
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/');
|
|
|
|
}
|
|
|
|
|
2011-01-26 02:17:19 +01:00
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
|
|
|
->setTitle('Form Errors')
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
2011-01-25 22:48:05 +01:00
|
|
|
|
2011-02-05 02:53:14 +01:00
|
|
|
if ($diff && $revision->getID()) {
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
|
|
|
->setLabel('Comments')
|
|
|
|
->setName('comments')
|
|
|
|
->setCaption("Explain what's new in this diff.")
|
|
|
|
->setValue($request->getStr('comments')))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue('Save'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormDividerControl()));
|
|
|
|
}
|
|
|
|
|
2011-08-10 22:46:01 +02:00
|
|
|
foreach ($aux_fields as $aux_field) {
|
|
|
|
$control = $aux_field->renderEditControl();
|
|
|
|
if ($control) {
|
|
|
|
$form->appendChild($control);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
$panel = new AphrontPanelView();
|
|
|
|
if ($revision->getID()) {
|
2011-02-05 02:53:14 +01:00
|
|
|
if ($diff) {
|
|
|
|
$panel->setHeader('Update Differential Revision');
|
|
|
|
} else {
|
|
|
|
$panel->setHeader('Edit Differential Revision');
|
|
|
|
}
|
2011-01-25 22:26:09 +01:00
|
|
|
} else {
|
|
|
|
$panel->setHeader('Create New Differential Revision');
|
|
|
|
}
|
|
|
|
|
|
|
|
$panel->appendChild($form);
|
|
|
|
$panel->setWidth(AphrontPanelView::WIDTH_FORM);
|
|
|
|
|
|
|
|
return $this->buildStandardPageResponse(
|
|
|
|
array($error_view, $panel),
|
|
|
|
array(
|
|
|
|
'title' => 'Edit Differential Revision',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-08-10 22:46:01 +02:00
|
|
|
private function loadAuxiliaryFields(DifferentialRevision $revision) {
|
|
|
|
|
2011-08-14 23:28:08 +02:00
|
|
|
$user = $this->getRequest()->getUser();
|
|
|
|
|
2011-08-10 22:46:01 +02:00
|
|
|
$aux_fields = DifferentialFieldSelector::newSelector()
|
|
|
|
->getFieldSpecifications();
|
|
|
|
foreach ($aux_fields as $key => $aux_field) {
|
2011-08-14 23:28:08 +02:00
|
|
|
$aux_field->setRevision($revision);
|
2011-08-10 22:46:01 +02:00
|
|
|
if (!$aux_field->shouldAppearOnEdit()) {
|
|
|
|
unset($aux_fields[$key]);
|
2011-08-14 23:28:08 +02:00
|
|
|
} else {
|
|
|
|
$aux_field->setUser($user);
|
2011-08-10 22:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DifferentialAuxiliaryField::loadFromStorage(
|
|
|
|
$revision,
|
|
|
|
$aux_fields);
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:26:09 +01:00
|
|
|
}
|