PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/differential/controller/revisionedit/DifferentialRevisionEditController.php

http://github.com/facebook/phabricator
PHP | 193 lines | 156 code | 21 blank | 16 comment | 18 complexity | 264a84da6f028e6476e431b13a9d6fac MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. class DifferentialRevisionEditController extends DifferentialController {
  18. private $id;
  19. public function willProcessRequest(array $data) {
  20. $this->id = idx($data, 'id');
  21. }
  22. public function processRequest() {
  23. $request = $this->getRequest();
  24. if (!$this->id) {
  25. $this->id = $request->getInt('revisionID');
  26. }
  27. if ($this->id) {
  28. $revision = id(new DifferentialRevision())->load($this->id);
  29. if (!$revision) {
  30. return new Aphront404Response();
  31. }
  32. } else {
  33. $revision = new DifferentialRevision();
  34. }
  35. $revision->loadRelationships();
  36. $aux_fields = $this->loadAuxiliaryFields($revision);
  37. $diff_id = $request->getInt('diffID');
  38. if ($diff_id) {
  39. $diff = id(new DifferentialDiff())->load($diff_id);
  40. if (!$diff) {
  41. return new Aphront404Response();
  42. }
  43. if ($diff->getRevisionID()) {
  44. // TODO: Redirect?
  45. throw new Exception("This diff is already attached to a revision!");
  46. }
  47. } else {
  48. $diff = null;
  49. }
  50. $errors = array();
  51. if ($request->isFormPost() && !$request->getStr('viaDiffView')) {
  52. $user_phid = $request->getUser()->getPHID();
  53. foreach ($aux_fields as $aux_field) {
  54. $aux_field->setValueFromRequest($request);
  55. try {
  56. $aux_field->validateField();
  57. } catch (DifferentialFieldValidationException $ex) {
  58. $errors[] = $ex->getMessage();
  59. }
  60. }
  61. if (!$errors) {
  62. $editor = new DifferentialRevisionEditor($revision, $user_phid);
  63. if ($diff) {
  64. $editor->addDiff($diff, $request->getStr('comments'));
  65. }
  66. $editor->setAuxiliaryFields($aux_fields);
  67. $editor->save();
  68. return id(new AphrontRedirectResponse())
  69. ->setURI('/D'.$revision->getID());
  70. }
  71. }
  72. $aux_phids = array();
  73. foreach ($aux_fields as $key => $aux_field) {
  74. $aux_phids[$key] = $aux_field->getRequiredHandlePHIDsForRevisionEdit();
  75. }
  76. $phids = array_mergev($aux_phids);
  77. $phids = array_unique($phids);
  78. $handles = id(new PhabricatorObjectHandleData($phids))
  79. ->loadHandles();
  80. foreach ($aux_fields as $key => $aux_field) {
  81. $aux_field->setHandles(array_select_keys($handles, $aux_phids[$key]));
  82. }
  83. $form = new AphrontFormView();
  84. $form->setUser($request->getUser());
  85. if ($diff) {
  86. $form->addHiddenInput('diffID', $diff->getID());
  87. }
  88. if ($revision->getID()) {
  89. $form->setAction('/differential/revision/edit/'.$revision->getID().'/');
  90. } else {
  91. $form->setAction('/differential/revision/edit/');
  92. }
  93. $error_view = null;
  94. if ($errors) {
  95. $error_view = id(new AphrontErrorView())
  96. ->setTitle('Form Errors')
  97. ->setErrors($errors);
  98. }
  99. if ($diff && $revision->getID()) {
  100. $form
  101. ->appendChild(
  102. id(new AphrontFormTextAreaControl())
  103. ->setLabel('Comments')
  104. ->setName('comments')
  105. ->setCaption("Explain what's new in this diff.")
  106. ->setValue($request->getStr('comments')))
  107. ->appendChild(
  108. id(new AphrontFormSubmitControl())
  109. ->setValue('Save'))
  110. ->appendChild(
  111. id(new AphrontFormDividerControl()));
  112. }
  113. foreach ($aux_fields as $aux_field) {
  114. $control = $aux_field->renderEditControl();
  115. if ($control) {
  116. $form->appendChild($control);
  117. }
  118. }
  119. $submit = id(new AphrontFormSubmitControl())
  120. ->setValue('Save');
  121. if ($diff) {
  122. $submit->addCancelButton('/differential/diff/'.$diff->getID().'/');
  123. } else {
  124. $submit->addCancelButton('/D'.$revision->getID());
  125. }
  126. $form->appendChild($submit);
  127. $panel = new AphrontPanelView();
  128. if ($revision->getID()) {
  129. if ($diff) {
  130. $panel->setHeader('Update Differential Revision');
  131. } else {
  132. $panel->setHeader('Edit Differential Revision');
  133. }
  134. } else {
  135. $panel->setHeader('Create New Differential Revision');
  136. }
  137. $panel->appendChild($form);
  138. $panel->setWidth(AphrontPanelView::WIDTH_FORM);
  139. return $this->buildStandardPageResponse(
  140. array($error_view, $panel),
  141. array(
  142. 'title' => 'Edit Differential Revision',
  143. ));
  144. }
  145. private function loadAuxiliaryFields(DifferentialRevision $revision) {
  146. $user = $this->getRequest()->getUser();
  147. $aux_fields = DifferentialFieldSelector::newSelector()
  148. ->getFieldSpecifications();
  149. foreach ($aux_fields as $key => $aux_field) {
  150. $aux_field->setRevision($revision);
  151. if (!$aux_field->shouldAppearOnEdit()) {
  152. unset($aux_fields[$key]);
  153. } else {
  154. $aux_field->setUser($user);
  155. }
  156. }
  157. return DifferentialAuxiliaryField::loadFromStorage(
  158. $revision,
  159. $aux_fields);
  160. }
  161. }