[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/macro/controller/ -> PhabricatorMacroEditController.php (source)

   1  <?php
   2  
   3  final class PhabricatorMacroEditController extends PhabricatorMacroController {
   4  
   5    private $id;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->id = idx($data, 'id');
   9    }
  10  
  11    public function processRequest() {
  12      $this->requireApplicationCapability(
  13        PhabricatorMacroManageCapability::CAPABILITY);
  14  
  15      $request = $this->getRequest();
  16      $user = $request->getUser();
  17  
  18      if ($this->id) {
  19        $macro = id(new PhabricatorMacroQuery())
  20          ->setViewer($user)
  21          ->withIDs(array($this->id))
  22          ->needFiles(true)
  23          ->executeOne();
  24        if (!$macro) {
  25          return new Aphront404Response();
  26        }
  27      } else {
  28        $macro = new PhabricatorFileImageMacro();
  29        $macro->setAuthorPHID($user->getPHID());
  30      }
  31  
  32      $errors = array();
  33      $e_name = true;
  34      $e_file = null;
  35      $file = null;
  36      $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http');
  37  
  38      if ($request->isFormPost()) {
  39        $original = clone $macro;
  40  
  41        $new_name = null;
  42        if ($request->getBool('name_form') || !$macro->getID()) {
  43          $new_name = $request->getStr('name');
  44  
  45          $macro->setName($new_name);
  46  
  47          if (!strlen($macro->getName())) {
  48            $errors[] = pht('Macro name is required.');
  49            $e_name = pht('Required');
  50          } else if (!preg_match('/^[a-z0-9:_-]{3,}\z/', $macro->getName())) {
  51            $errors[] = pht(
  52              'Macro must be at least three characters long and contain only '.
  53              'lowercase letters, digits, hyphens, colons and underscores.');
  54            $e_name = pht('Invalid');
  55          } else {
  56            $e_name = null;
  57          }
  58        }
  59  
  60        $file = null;
  61        if ($request->getFileExists('file')) {
  62          $file = PhabricatorFile::newFromPHPUpload(
  63            $_FILES['file'],
  64            array(
  65              'name' => $request->getStr('name'),
  66              'authorPHID' => $user->getPHID(),
  67              'isExplicitUpload' => true,
  68              'canCDN' => true,
  69            ));
  70        } else if ($request->getStr('url')) {
  71          try {
  72            $file = PhabricatorFile::newFromFileDownload(
  73              $request->getStr('url'),
  74              array(
  75                'name' => $request->getStr('name'),
  76                'authorPHID' => $user->getPHID(),
  77                'isExplicitUpload' => true,
  78                'canCDN' => true,
  79              ));
  80          } catch (Exception $ex) {
  81            $errors[] = pht('Could not fetch URL: %s', $ex->getMessage());
  82          }
  83        } else if ($request->getStr('phid')) {
  84          $file = id(new PhabricatorFileQuery())
  85            ->setViewer($user)
  86            ->withPHIDs(array($request->getStr('phid')))
  87            ->executeOne();
  88        }
  89  
  90        if ($file) {
  91          if (!$file->isViewableInBrowser()) {
  92            $errors[] = pht('You must upload an image.');
  93            $e_file = pht('Invalid');
  94          } else {
  95            $macro->setFilePHID($file->getPHID());
  96            $macro->attachFile($file);
  97            $e_file = null;
  98          }
  99        }
 100  
 101        if (!$macro->getID() && !$file) {
 102          $errors[] = pht('You must upload an image to create a macro.');
 103          $e_file = pht('Required');
 104        }
 105  
 106        if (!$errors) {
 107          try {
 108            $xactions = array();
 109  
 110            if ($new_name !== null) {
 111              $xactions[] = id(new PhabricatorMacroTransaction())
 112                ->setTransactionType(PhabricatorMacroTransactionType::TYPE_NAME)
 113                ->setNewValue($new_name);
 114            }
 115  
 116            if ($file) {
 117              $xactions[] = id(new PhabricatorMacroTransaction())
 118                ->setTransactionType(PhabricatorMacroTransactionType::TYPE_FILE)
 119                ->setNewValue($file->getPHID());
 120            }
 121  
 122            $editor = id(new PhabricatorMacroEditor())
 123              ->setActor($user)
 124              ->setContinueOnNoEffect(true)
 125              ->setContentSourceFromRequest($request);
 126  
 127            $xactions = $editor->applyTransactions($original, $xactions);
 128  
 129            $view_uri = $this->getApplicationURI('/view/'.$original->getID().'/');
 130            return id(new AphrontRedirectResponse())->setURI($view_uri);
 131          } catch (AphrontDuplicateKeyQueryException $ex) {
 132            throw $ex;
 133            $errors[] = pht('Macro name is not unique!');
 134            $e_name = pht('Duplicate');
 135          }
 136        }
 137      }
 138  
 139      $current_file = null;
 140      if ($macro->getFilePHID()) {
 141        $current_file = $macro->getFile();
 142      }
 143  
 144      $form = new AphrontFormView();
 145      $form->addHiddenInput('name_form', 1);
 146      $form->setUser($request->getUser());
 147  
 148      $form
 149        ->setEncType('multipart/form-data')
 150        ->appendChild(
 151          id(new AphrontFormTextControl())
 152            ->setLabel(pht('Name'))
 153            ->setName('name')
 154            ->setValue($macro->getName())
 155            ->setCaption(
 156              pht('This word or phrase will be replaced with the image.'))
 157            ->setError($e_name));
 158  
 159      if (!$macro->getID()) {
 160        if ($current_file) {
 161          $current_file_view = id(new PhabricatorFileLinkView())
 162            ->setFilePHID($current_file->getPHID())
 163            ->setFileName($current_file->getName())
 164            ->setFileViewable(true)
 165            ->setFileViewURI($current_file->getBestURI())
 166            ->render();
 167          $form->addHiddenInput('phid', $current_file->getPHID());
 168          $form->appendChild(
 169            id(new AphrontFormMarkupControl())
 170              ->setLabel(pht('Selected File'))
 171              ->setValue($current_file_view));
 172  
 173          $other_label = pht('Change File');
 174        } else {
 175          $other_label = pht('File');
 176        }
 177  
 178        if ($can_fetch) {
 179          $form->appendChild(
 180            id(new AphrontFormTextControl())
 181              ->setLabel(pht('URL'))
 182              ->setName('url')
 183              ->setValue($request->getStr('url'))
 184              ->setError($request->getFileExists('file') ? false : $e_file));
 185        }
 186  
 187        $form->appendChild(
 188          id(new AphrontFormFileControl())
 189            ->setLabel($other_label)
 190            ->setName('file')
 191            ->setError($request->getStr('url') ? false : $e_file));
 192      }
 193  
 194  
 195      $view_uri = $this->getApplicationURI('/view/'.$macro->getID().'/');
 196  
 197      if ($macro->getID()) {
 198        $cancel_uri = $view_uri;
 199      } else {
 200        $cancel_uri = $this->getApplicationURI();
 201      }
 202  
 203      $form
 204        ->appendChild(
 205          id(new AphrontFormSubmitControl())
 206            ->setValue(pht('Save Image Macro'))
 207            ->addCancelButton($cancel_uri));
 208  
 209      $crumbs = $this->buildApplicationCrumbs();
 210  
 211      if ($macro->getID()) {
 212        $title = pht('Edit Image Macro');
 213        $crumb = pht('Edit Macro');
 214  
 215        $crumbs->addTextCrumb(pht('Macro "%s"', $macro->getName()), $view_uri);
 216      } else {
 217        $title = pht('Create Image Macro');
 218        $crumb = pht('Create Macro');
 219      }
 220  
 221      $crumbs->addTextCrumb($crumb, $request->getRequestURI());
 222  
 223      $upload = null;
 224      if ($macro->getID()) {
 225        $upload_form = id(new AphrontFormView())
 226          ->setEncType('multipart/form-data')
 227          ->setUser($request->getUser());
 228  
 229        if ($can_fetch) {
 230          $upload_form->appendChild(
 231            id(new AphrontFormTextControl())
 232              ->setLabel(pht('URL'))
 233              ->setName('url')
 234              ->setValue($request->getStr('url')));
 235        }
 236  
 237        $upload_form
 238          ->appendChild(
 239            id(new AphrontFormFileControl())
 240              ->setLabel(pht('File'))
 241              ->setName('file'))
 242          ->appendChild(
 243            id(new AphrontFormSubmitControl())
 244              ->setValue(pht('Upload File')));
 245  
 246        $upload = id(new PHUIObjectBoxView())
 247          ->setHeaderText(pht('Upload New File'))
 248          ->setForm($upload_form);
 249      }
 250  
 251      $form_box = id(new PHUIObjectBoxView())
 252        ->setHeaderText($title)
 253        ->setFormErrors($errors)
 254        ->setForm($form);
 255  
 256      return $this->buildApplicationPage(
 257        array(
 258          $crumbs,
 259          $form_box,
 260          $upload,
 261        ),
 262        array(
 263          'title' => $title,
 264        ));
 265    }
 266  
 267  }


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