[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class PhragmentSnapshotCreateController extends PhragmentController {
   4  
   5    private $dblob;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->dblob = idx($data, 'dblob', '');
   9    }
  10  
  11    public function processRequest() {
  12      $request = $this->getRequest();
  13      $viewer = $request->getUser();
  14  
  15      $parents = $this->loadParentFragments($this->dblob);
  16      if ($parents === null) {
  17        return new Aphront404Response();
  18      }
  19      $fragment = nonempty(last($parents), null);
  20      if ($fragment === null) {
  21        return new Aphront404Response();
  22      }
  23  
  24      PhabricatorPolicyFilter::requireCapability(
  25        $viewer,
  26        $fragment,
  27        PhabricatorPolicyCapability::CAN_EDIT);
  28  
  29      $children = id(new PhragmentFragmentQuery())
  30        ->setViewer($viewer)
  31        ->needLatestVersion(true)
  32        ->withLeadingPath($fragment->getPath().'/')
  33        ->execute();
  34  
  35      $errors = array();
  36      if ($request->isFormPost()) {
  37  
  38        $v_name = $request->getStr('name');
  39        if (strlen($v_name) === 0) {
  40          $errors[] = pht('You must specify a name.');
  41        }
  42        if (strpos($v_name, '/') !== false) {
  43          $errors[] = pht('Snapshot names can not contain "/".');
  44        }
  45  
  46        if (!count($errors)) {
  47          $snapshot = null;
  48  
  49          try {
  50            // Create the snapshot.
  51            $snapshot = id(new PhragmentSnapshot())
  52              ->setPrimaryFragmentPHID($fragment->getPHID())
  53              ->setName($v_name)
  54              ->save();
  55          } catch (AphrontDuplicateKeyQueryException $e) {
  56            $errors[] = pht('A snapshot with this name already exists.');
  57          }
  58  
  59          if (!count($errors)) {
  60            // Add the primary fragment.
  61            id(new PhragmentSnapshotChild())
  62              ->setSnapshotPHID($snapshot->getPHID())
  63              ->setFragmentPHID($fragment->getPHID())
  64              ->setFragmentVersionPHID($fragment->getLatestVersionPHID())
  65              ->save();
  66  
  67            // Add all of the child fragments.
  68            foreach ($children as $child) {
  69              id(new PhragmentSnapshotChild())
  70                ->setSnapshotPHID($snapshot->getPHID())
  71                ->setFragmentPHID($child->getPHID())
  72                ->setFragmentVersionPHID($child->getLatestVersionPHID())
  73                ->save();
  74            }
  75  
  76            return id(new AphrontRedirectResponse())
  77              ->setURI('/phragment/snapshot/view/'.$snapshot->getID());
  78          }
  79        }
  80      }
  81  
  82      $fragment_sequence = '-';
  83      if ($fragment->getLatestVersion() !== null) {
  84        $fragment_sequence = $fragment->getLatestVersion()->getSequence();
  85      }
  86  
  87      $rows = array();
  88      $rows[] = phutil_tag(
  89        'tr',
  90        array(),
  91        array(
  92          phutil_tag('th', array(), 'Fragment'),
  93          phutil_tag('th', array(), 'Version'),
  94        ));
  95      $rows[] = phutil_tag(
  96        'tr',
  97        array(),
  98        array(
  99          phutil_tag('td', array(), $fragment->getPath()),
 100          phutil_tag('td', array(), $fragment_sequence),
 101        ));
 102      foreach ($children as $child) {
 103        $sequence = '-';
 104        if ($child->getLatestVersion() !== null) {
 105          $sequence = $child->getLatestVersion()->getSequence();
 106        }
 107        $rows[] = phutil_tag(
 108          'tr',
 109          array(),
 110          array(
 111            phutil_tag('td', array(), $child->getPath()),
 112            phutil_tag('td', array(), $sequence),
 113          ));
 114      }
 115  
 116      $table = phutil_tag(
 117        'table',
 118        array('class' => 'remarkup-table'),
 119        $rows);
 120  
 121      $container = phutil_tag(
 122        'div',
 123        array('class' => 'phabricator-remarkup'),
 124        array(
 125          phutil_tag(
 126            'p',
 127            array(),
 128            pht(
 129              'The snapshot will contain the following fragments at '.
 130              'the specified versions: ')),
 131          $table,
 132        ));
 133  
 134      $form = id(new AphrontFormView())
 135        ->setUser($viewer)
 136        ->appendChild(
 137          id(new AphrontFormTextControl())
 138            ->setLabel(pht('Fragment Path'))
 139            ->setDisabled(true)
 140            ->setValue('/'.$fragment->getPath()))
 141        ->appendChild(
 142          id(new AphrontFormTextControl())
 143            ->setLabel(pht('Snapshot Name'))
 144            ->setName('name'))
 145        ->appendChild(
 146          id(new AphrontFormSubmitControl())
 147            ->setValue(pht('Create Snapshot'))
 148            ->addCancelButton(
 149              $this->getApplicationURI('browse/'.$fragment->getPath())))
 150        ->appendChild(
 151          id(new PHUIFormDividerControl()))
 152        ->appendInstructions($container);
 153  
 154      $crumbs = $this->buildApplicationCrumbsWithPath($parents);
 155      $crumbs->addTextCrumb(pht('Create Snapshot'));
 156  
 157      $box = id(new PHUIObjectBoxView())
 158        ->setHeaderText(pht('Create Snapshot of %s', $fragment->getName()))
 159        ->setFormErrors($errors)
 160        ->setForm($form);
 161  
 162      return $this->buildApplicationPage(
 163        array(
 164          $crumbs,
 165          $this->renderConfigurationWarningIfRequired(),
 166          $box,
 167        ),
 168        array(
 169          'title' => pht('Create Fragment'),
 170        ));
 171    }
 172  
 173  }


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