Phriction - move "move" business logic to the editor
Summary: Ref T4029. Even more code consolidation and cleanup for the long term benefits! Test Plan: moved a page successfully. tried to move a page to an existing page and got an error. did the two tab trick to try to move a deleted page and got an error. Reviewers: chad, epriestley Reviewed By: epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T4029 Differential Revision: https://secure.phabricator.com/D10812
This commit is contained in:
		@@ -56,75 +56,36 @@ final class PhrictionMoveController extends PhrictionController {
 | 
			
		||||
    $submit_uri = $request->getRequestURI()->getPath();
 | 
			
		||||
    $cancel_uri = PhrictionDocument::getSlugURI($slug);
 | 
			
		||||
 | 
			
		||||
    $errors = array();
 | 
			
		||||
    $error_view = null;
 | 
			
		||||
    $e_url = null;
 | 
			
		||||
 | 
			
		||||
    $disallowed_statuses = array(
 | 
			
		||||
      PhrictionDocumentStatus::STATUS_DELETED => true, // Silly
 | 
			
		||||
      PhrictionDocumentStatus::STATUS_MOVED => true, // Plain silly
 | 
			
		||||
      PhrictionDocumentStatus::STATUS_STUB => true, // Utterly silly
 | 
			
		||||
    );
 | 
			
		||||
    if (isset($disallowed_statuses[$document->getStatus()])) {
 | 
			
		||||
      $error_dialog = id(new AphrontDialogView())
 | 
			
		||||
        ->setUser($user)
 | 
			
		||||
        ->setTitle('Can not move page!')
 | 
			
		||||
        ->appendChild(pht('An already moved or deleted document '.
 | 
			
		||||
          'can not be moved again.'))
 | 
			
		||||
        ->addCancelButton($cancel_uri);
 | 
			
		||||
 | 
			
		||||
      return id(new AphrontDialogResponse())->setDialog($error_dialog);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $e_url = true;
 | 
			
		||||
    $validation_exception = null;
 | 
			
		||||
    $content = $document->getContent();
 | 
			
		||||
 | 
			
		||||
    if ($request->isFormPost() && !count($errors)) {
 | 
			
		||||
    if ($request->isFormPost()) {
 | 
			
		||||
 | 
			
		||||
      // NOTE: We use the ominpotent user because we can't let users overwrite
 | 
			
		||||
      // documents even if they can't see them.
 | 
			
		||||
      $target_document = id(new PhrictionDocumentQuery())
 | 
			
		||||
        ->setViewer(PhabricatorUser::getOmnipotentUser())
 | 
			
		||||
        ->withSlugs(array($target_slug))
 | 
			
		||||
        ->needContent(true)
 | 
			
		||||
        ->executeOne();
 | 
			
		||||
      $editor = id(new PhrictionTransactionEditor())
 | 
			
		||||
        ->setActor($user)
 | 
			
		||||
        ->setContentSourceFromRequest($request)
 | 
			
		||||
        ->setContinueOnNoEffect(true)
 | 
			
		||||
        ->setDescription($request->getStr('description'));
 | 
			
		||||
 | 
			
		||||
      // Considering to overwrite existing docs? Nuke this!
 | 
			
		||||
      $exists = PhrictionDocumentStatus::STATUS_EXISTS;
 | 
			
		||||
      if ($target_document && $target_document->getStatus() == $exists) {
 | 
			
		||||
        $errors[] = pht('Can not overwrite existing target document.');
 | 
			
		||||
        $e_url = pht('Already exists.');
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (!count($errors)) {
 | 
			
		||||
 | 
			
		||||
        $editor = id(new PhrictionTransactionEditor())
 | 
			
		||||
          ->setActor($user)
 | 
			
		||||
          ->setContentSourceFromRequest($request)
 | 
			
		||||
          ->setContinueOnNoEffect(true)
 | 
			
		||||
          ->setDescription($request->getStr('description'));
 | 
			
		||||
 | 
			
		||||
        $xactions = array();
 | 
			
		||||
        $xactions[] = id(new PhrictionTransaction())
 | 
			
		||||
          ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO)
 | 
			
		||||
          ->setNewValue($document);
 | 
			
		||||
        if (!$target_document) {
 | 
			
		||||
          $target_document = PhrictionDocument::initializeNewDocument(
 | 
			
		||||
            $user,
 | 
			
		||||
            $target_slug);
 | 
			
		||||
        }
 | 
			
		||||
      $xactions = array();
 | 
			
		||||
      $xactions[] = id(new PhrictionTransaction())
 | 
			
		||||
        ->setTransactionType(PhrictionTransaction::TYPE_MOVE_TO)
 | 
			
		||||
        ->setNewValue($document);
 | 
			
		||||
      $target_document = PhrictionDocument::initializeNewDocument(
 | 
			
		||||
        $user,
 | 
			
		||||
        $target_slug);
 | 
			
		||||
      try {
 | 
			
		||||
        $editor->applyTransactions($target_document, $xactions);
 | 
			
		||||
 | 
			
		||||
        $redir_uri = PhrictionDocument::getSlugURI(
 | 
			
		||||
          $target_document->getSlug());
 | 
			
		||||
        return id(new AphrontRedirectResponse())->setURI($redir_uri);
 | 
			
		||||
      } catch (PhabricatorApplicationTransactionValidationException $ex) {
 | 
			
		||||
        $validation_exception = $ex;
 | 
			
		||||
        $e_url = $ex->getShortMessage(PhrictionTransaction::TYPE_MOVE_TO);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ($errors) {
 | 
			
		||||
      $error_view = id(new AphrontErrorView())
 | 
			
		||||
        ->setErrors($errors);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $form = id(new PHUIFormLayoutView())
 | 
			
		||||
      ->setUser($user)
 | 
			
		||||
      ->appendChild(
 | 
			
		||||
@@ -141,12 +102,13 @@ final class PhrictionMoveController extends PhrictionController {
 | 
			
		||||
      ->appendChild(
 | 
			
		||||
        id(new AphrontFormTextControl())
 | 
			
		||||
        ->setLabel(pht('Edit Notes'))
 | 
			
		||||
        ->setValue($content->getDescription())
 | 
			
		||||
        ->setValue(pht('Moving document to a new location.'))
 | 
			
		||||
        ->setError(null)
 | 
			
		||||
        ->setName('description'));
 | 
			
		||||
 | 
			
		||||
    $dialog = id(new AphrontDialogView())
 | 
			
		||||
      ->setUser($user)
 | 
			
		||||
      ->setValidationException($validation_exception)
 | 
			
		||||
      ->setTitle(pht('Move Document'))
 | 
			
		||||
      ->appendChild($form)
 | 
			
		||||
      ->setSubmitURI($submit_uri)
 | 
			
		||||
 
 | 
			
		||||
@@ -477,6 +477,53 @@ final class PhrictionTransactionEditor
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          break;
 | 
			
		||||
 | 
			
		||||
        case PhrictionTransaction::TYPE_MOVE_TO:
 | 
			
		||||
          $source_document = $xaction->getNewValue();
 | 
			
		||||
          switch ($source_document->getStatus()) {
 | 
			
		||||
            case PhrictionDocumentStatus::STATUS_DELETED:
 | 
			
		||||
              $e_text = pht('A deleted document can not be moved.');
 | 
			
		||||
              break;
 | 
			
		||||
            case PhrictionDocumentStatus::STATUS_MOVED:
 | 
			
		||||
              $e_text = pht('A moved document can not be moved again.');
 | 
			
		||||
              break;
 | 
			
		||||
            case PhrictionDocumentStatus::STATUS_STUB:
 | 
			
		||||
              $e_text = pht('A stub document can not be moved.');
 | 
			
		||||
              break;
 | 
			
		||||
            default:
 | 
			
		||||
              $e_text = null;
 | 
			
		||||
              break;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          if ($e_text) {
 | 
			
		||||
            $error = new PhabricatorApplicationTransactionValidationError(
 | 
			
		||||
              $type,
 | 
			
		||||
              pht('Can not move document.'),
 | 
			
		||||
              $e_text,
 | 
			
		||||
              $xaction);
 | 
			
		||||
            $errors[] = $error;
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          // NOTE: We use the ominpotent user because we can't let users
 | 
			
		||||
          // overwrite documents even if they can't see them.
 | 
			
		||||
          $target_document = id(new PhrictionDocumentQuery())
 | 
			
		||||
            ->setViewer(PhabricatorUser::getOmnipotentUser())
 | 
			
		||||
            ->withSlugs(array($object->getSlug()))
 | 
			
		||||
            ->needContent(true)
 | 
			
		||||
            ->executeOne();
 | 
			
		||||
 | 
			
		||||
          // Considering to overwrite existing docs? Nuke this!
 | 
			
		||||
          $exists = PhrictionDocumentStatus::STATUS_EXISTS;
 | 
			
		||||
          if ($target_document && $target_document->getStatus() == $exists) {
 | 
			
		||||
            $error = new PhabricatorApplicationTransactionValidationError(
 | 
			
		||||
              $type,
 | 
			
		||||
              pht('Can not move document.'),
 | 
			
		||||
              pht('Can not overwrite existing target document.'),
 | 
			
		||||
              $xaction);
 | 
			
		||||
            $errors[] = $error;
 | 
			
		||||
          }
 | 
			
		||||
          break;
 | 
			
		||||
 | 
			
		||||
        case PhrictionTransaction::TYPE_DELETE:
 | 
			
		||||
          switch ($object->getStatus()) {
 | 
			
		||||
            case PhrictionDocumentStatus::STATUS_DELETED:
 | 
			
		||||
@@ -498,7 +545,6 @@ final class PhrictionTransactionEditor
 | 
			
		||||
            $e_text,
 | 
			
		||||
            $xaction);
 | 
			
		||||
          $errors[] = $error;
 | 
			
		||||
 | 
			
		||||
          break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user