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

235 lines
7 KiB
PHP
Raw Normal View History

2011-01-25 22:26:09 +01:00
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* 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.
*/
class DifferentialRevisionEditController extends DifferentialController {
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-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;
}
$e_title = true;
$e_testplan = true;
$e_reviewers = null;
2011-01-26 02:17:19 +01:00
$errors = array();
2011-02-03 04:38:43 +01:00
$revision->loadRelationships();
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
$revision->setTitle($request->getStr('title'));
$revision->setSummary($request->getStr('summary'));
$revision->setTestPlan($request->getStr('testplan'));
$revision->setBlameRevision($request->getStr('blame'));
$revision->setRevertPlan($request->getStr('revert'));
if (!strlen(trim($revision->getTitle()))) {
$errors[] = 'You must provide a title.';
$e_title = 'Required';
} else {
$e_title = null;
2011-01-26 02:17:19 +01:00
}
if (!strlen(trim($revision->getTestPlan()))) {
$errors[] = 'You must provide a test plan.';
$e_testplan = 'Required';
} else {
$e_testplan = null;
2011-01-26 02:17:19 +01:00
}
$user_phid = $request->getUser()->getPHID();
if (in_array($user_phid, $request->getArr('reviewers'))) {
$errors[] = 'You may not review your own revision.';
$e_reviewers = 'Invalid';
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'));
}
$editor->setCCPHIDs($request->getArr('cc'));
$editor->setReviewers($request->getArr('reviewers'));
$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-01-26 02:17:19 +01:00
$reviewer_phids = $request->getArr('reviewers');
$cc_phids = $request->getArr('cc');
} else {
$reviewer_phids = $revision->getReviewers();
$cc_phids = $revision->getCCPHIDs();
2011-01-25 22:26:09 +01:00
}
2011-02-03 04:38:43 +01:00
$phids = array_merge($reviewer_phids, $cc_phids);
$phids = array_unique($phids);
2011-02-03 04:38:43 +01:00
$handles = id(new PhabricatorObjectHandleData($phids))
->loadHandles();
$handles = mpull($handles, 'getFullName', 'getPHID');
2011-02-03 04:38:43 +01:00
$reviewer_map = array_select_keys($handles, $reviewer_phids);
$cc_map = array_select_keys($handles, $cc_phids);
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
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-01-25 22:26:09 +01:00
$form
->appendChild(
id(new AphrontFormTextAreaControl())
2011-01-26 02:17:19 +01:00
->setLabel('Title')
->setName('title')
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
->setValue($revision->getTitle())
->setError($e_title))
2011-01-25 22:26:09 +01:00
->appendChild(
id(new AphrontFormTextAreaControl())
->setLabel('Summary')
->setName('summary')
->setValue($revision->getSummary()))
->appendChild(
id(new AphrontFormTextAreaControl())
->setLabel('Test Plan')
->setName('testplan')
->setValue($revision->getTestPlan())
->setError($e_testplan))
->appendChild(
2011-01-25 22:48:05 +01:00
id(new AphrontFormTokenizerControl())
2011-01-25 22:26:09 +01:00
->setLabel('Reviewers')
2011-01-25 22:48:05 +01:00
->setName('reviewers')
2011-01-26 02:17:19 +01:00
->setDatasource('/typeahead/common/users/')
->setError($e_reviewers)
2011-01-25 22:48:05 +01:00
->setValue($reviewer_map))
2011-01-25 22:26:09 +01:00
->appendChild(
2011-01-25 22:48:05 +01:00
id(new AphrontFormTokenizerControl())
2011-01-25 22:26:09 +01:00
->setLabel('CC')
2011-01-25 22:48:05 +01:00
->setName('cc')
2011-01-26 02:17:19 +01:00
->setDatasource('/typeahead/common/mailable/')
->setValue($cc_map))
2011-01-25 22:26:09 +01:00
->appendChild(
id(new AphrontFormTextControl())
->setLabel('Blame Revision')
->setName('blame')
->setValue($revision->getBlameRevision())
->setCaption('Revision which broke the stuff which this '.
'change fixes.'))
->appendChild(
id(new AphrontFormTextAreaControl())
2011-01-26 02:17:19 +01:00
->setLabel('Revert Plan')
2011-01-25 22:26:09 +01:00
->setName('revert')
->setValue($revision->getRevertPlan())
2011-01-26 02:17:19 +01:00
->setCaption('Special steps required to safely revert this change.'));
$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()) {
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',
));
}
}