[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phragment/controller/ -> PhragmentSnapshotPromoteController.php (source)

   1  <?php
   2  
   3  final class PhragmentSnapshotPromoteController extends PhragmentController {
   4  
   5    private $dblob;
   6    private $id;
   7    private $targetSnapshot;
   8    private $targetFragment;
   9    private $snapshots;
  10    private $options;
  11  
  12    public function willProcessRequest(array $data) {
  13      $this->dblob = idx($data, 'dblob', null);
  14      $this->id = idx($data, 'id', null);
  15    }
  16  
  17    public function processRequest() {
  18      $request = $this->getRequest();
  19      $viewer = $request->getUser();
  20  
  21      // When the user is promoting a snapshot to the latest version, the
  22      // identifier is a fragment path.
  23      if ($this->dblob !== null) {
  24        $this->targetFragment = id(new PhragmentFragmentQuery())
  25          ->setViewer($viewer)
  26          ->requireCapabilities(array(
  27            PhabricatorPolicyCapability::CAN_VIEW,
  28            PhabricatorPolicyCapability::CAN_EDIT,
  29          ))
  30          ->withPaths(array($this->dblob))
  31          ->executeOne();
  32        if ($this->targetFragment === null) {
  33          return new Aphront404Response();
  34        }
  35  
  36        $this->snapshots = id(new PhragmentSnapshotQuery())
  37          ->setViewer($viewer)
  38          ->withPrimaryFragmentPHIDs(array($this->targetFragment->getPHID()))
  39          ->execute();
  40      }
  41  
  42      // When the user is promoting a snapshot to another snapshot, the
  43      // identifier is another snapshot ID.
  44      if ($this->id !== null) {
  45        $this->targetSnapshot = id(new PhragmentSnapshotQuery())
  46          ->setViewer($viewer)
  47          ->requireCapabilities(array(
  48            PhabricatorPolicyCapability::CAN_VIEW,
  49            PhabricatorPolicyCapability::CAN_EDIT,
  50          ))
  51          ->withIDs(array($this->id))
  52          ->executeOne();
  53        if ($this->targetSnapshot === null) {
  54          return new Aphront404Response();
  55        }
  56  
  57        $this->snapshots = id(new PhragmentSnapshotQuery())
  58          ->setViewer($viewer)
  59          ->withPrimaryFragmentPHIDs(array(
  60            $this->targetSnapshot->getPrimaryFragmentPHID(),
  61          ))
  62          ->execute();
  63      }
  64  
  65      // If there's no identifier, just 404.
  66      if ($this->snapshots === null) {
  67        return new Aphront404Response();
  68      }
  69  
  70      // Work out what options the user has.
  71      $this->options = mpull(
  72        $this->snapshots,
  73        'getName',
  74        'getID');
  75      if ($this->id !== null) {
  76        unset($this->options[$this->id]);
  77      }
  78  
  79      // If there's no options, show a dialog telling the
  80      // user there are no snapshots to promote.
  81      if (count($this->options) === 0) {
  82        return id(new AphrontDialogResponse())->setDialog(
  83          id(new AphrontDialogView())
  84            ->setTitle(pht('No snapshots to promote'))
  85            ->appendParagraph(pht(
  86              'There are no snapshots available to promote.'))
  87            ->setUser($request->getUser())
  88            ->addCancelButton(pht('Cancel')));
  89      }
  90  
  91      // Handle snapshot promotion.
  92      if ($request->isDialogFormPost()) {
  93        $snapshot = id(new PhragmentSnapshotQuery())
  94          ->setViewer($viewer)
  95          ->withIDs(array($request->getStr('snapshot')))
  96          ->executeOne();
  97        if ($snapshot === null) {
  98          return new Aphront404Response();
  99        }
 100  
 101        $snapshot->openTransaction();
 102          // Delete all existing child entries.
 103          $children = id(new PhragmentSnapshotChildQuery())
 104            ->setViewer(PhabricatorUser::getOmnipotentUser())
 105            ->withSnapshotPHIDs(array($snapshot->getPHID()))
 106            ->execute();
 107          foreach ($children as $child) {
 108            $child->delete();
 109          }
 110  
 111          if ($this->id === null) {
 112            // The user is promoting the snapshot to the latest version.
 113            $children = id(new PhragmentFragmentQuery())
 114              ->setViewer($viewer)
 115              ->needLatestVersion(true)
 116              ->withLeadingPath($this->targetFragment->getPath().'/')
 117              ->execute();
 118  
 119            // Add the primary fragment.
 120            id(new PhragmentSnapshotChild())
 121              ->setSnapshotPHID($snapshot->getPHID())
 122              ->setFragmentPHID($this->targetFragment->getPHID())
 123              ->setFragmentVersionPHID(
 124                $this->targetFragment->getLatestVersionPHID())
 125              ->save();
 126  
 127            // Add all of the child fragments.
 128            foreach ($children as $child) {
 129              id(new PhragmentSnapshotChild())
 130                ->setSnapshotPHID($snapshot->getPHID())
 131                ->setFragmentPHID($child->getPHID())
 132                ->setFragmentVersionPHID($child->getLatestVersionPHID())
 133                ->save();
 134            }
 135          } else {
 136            // The user is promoting the snapshot to another snapshot. We just
 137            // copy the other snapshot's child entries and change the snapshot
 138            // PHID to make it identical.
 139            $children = id(new PhragmentSnapshotChildQuery())
 140              ->setViewer($viewer)
 141              ->withSnapshotPHIDs(array($this->targetSnapshot->getPHID()))
 142              ->execute();
 143            foreach ($children as $child) {
 144              id(new PhragmentSnapshotChild())
 145                ->setSnapshotPHID($snapshot->getPHID())
 146                ->setFragmentPHID($child->getFragmentPHID())
 147                ->setFragmentVersionPHID($child->getFragmentVersionPHID())
 148                ->save();
 149            }
 150          }
 151        $snapshot->saveTransaction();
 152  
 153        if ($this->id === null) {
 154          return id(new AphrontRedirectResponse())
 155            ->setURI($this->targetFragment->getURI());
 156        } else {
 157          return id(new AphrontRedirectResponse())
 158            ->setURI($this->targetSnapshot->getURI());
 159        }
 160      }
 161  
 162      return $this->createDialog();
 163    }
 164  
 165    function createDialog() {
 166      $request = $this->getRequest();
 167      $viewer = $request->getUser();
 168  
 169      $dialog = id(new AphrontDialogView())
 170        ->setTitle(pht('Promote which snapshot?'))
 171        ->setUser($request->getUser())
 172        ->addSubmitButton(pht('Promote'))
 173        ->addCancelButton(pht('Cancel'));
 174  
 175      if ($this->id === null) {
 176        // The user is promoting a snapshot to the latest version.
 177        $dialog->appendParagraph(pht(
 178          'Select the snapshot you want to promote to the latest version:'));
 179      } else {
 180        // The user is promoting a snapshot to another snapshot.
 181        $dialog->appendParagraph(pht(
 182          "Select the snapshot you want to promote to '%s':",
 183          $this->targetSnapshot->getName()));
 184      }
 185  
 186      $dialog->appendChild(
 187        id(new AphrontFormSelectControl())
 188          ->setUser($viewer)
 189          ->setName('snapshot')
 190          ->setOptions($this->options));
 191  
 192      return id(new AphrontDialogResponse())->setDialog($dialog);
 193    }
 194  
 195  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1