[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phame/controller/post/ -> PhamePostEditController.php (source)

   1  <?php
   2  
   3  final class PhamePostEditController extends PhameController {
   4  
   5    private $id;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->id = idx($data, 'id');
   9    }
  10  
  11    public function processRequest() {
  12      $request       = $this->getRequest();
  13      $user          = $request->getUser();
  14  
  15      if ($this->id) {
  16        $post = id(new PhamePostQuery())
  17          ->setViewer($user)
  18          ->withIDs(array($this->id))
  19          ->requireCapabilities(
  20            array(
  21              PhabricatorPolicyCapability::CAN_EDIT,
  22            ))
  23          ->executeOne();
  24        if (!$post) {
  25          return new Aphront404Response();
  26        }
  27  
  28        $cancel_uri = $this->getApplicationURI('/post/view/'.$this->id.'/');
  29        $submit_button = pht('Save Changes');
  30        $page_title = pht('Edit Post');
  31      } else {
  32        $blog = id(new PhameBlogQuery())
  33          ->setViewer($user)
  34          ->withIDs(array($request->getInt('blog')))
  35          ->requireCapabilities(
  36            array(
  37              PhabricatorPolicyCapability::CAN_VIEW,
  38              PhabricatorPolicyCapability::CAN_JOIN,
  39            ))
  40          ->executeOne();
  41        if (!$blog) {
  42          return new Aphront404Response();
  43        }
  44  
  45        $post = PhamePost::initializePost($user, $blog);
  46        $cancel_uri = $this->getApplicationURI('/blog/view/'.$blog->getID().'/');
  47  
  48        $submit_button = pht('Save Draft');
  49        $page_title    = pht('Create Post');
  50      }
  51  
  52      $e_phame_title = null;
  53      $e_title       = true;
  54      $errors        = array();
  55  
  56      if ($request->isFormPost()) {
  57        $comments    = $request->getStr('comments_widget');
  58        $data        = array('comments_widget' => $comments);
  59        $phame_title = $request->getStr('phame_title');
  60        $phame_title = PhabricatorSlug::normalize($phame_title);
  61        $title       = $request->getStr('title');
  62        $post->setTitle($title);
  63        $post->setPhameTitle($phame_title);
  64        $post->setBody($request->getStr('body'));
  65        $post->setConfigData($data);
  66  
  67        if ($phame_title == '/') {
  68          $errors[]      = pht('Phame title must be nonempty.');
  69          $e_phame_title = pht('Required');
  70        }
  71  
  72        if (!strlen($title)) {
  73          $errors[] = pht('Title must be nonempty.');
  74          $e_title  = pht('Required');
  75        } else {
  76          $e_title = null;
  77        }
  78  
  79        if (!$errors) {
  80          try {
  81            $post->save();
  82  
  83            $uri = $this->getApplicationURI('/post/view/'.$post->getID().'/');
  84            return id(new AphrontRedirectResponse())->setURI($uri);
  85          } catch (AphrontDuplicateKeyQueryException $e) {
  86            $e_phame_title = pht('Not Unique');
  87            $errors[]      = pht('Another post already uses this slug. '.
  88                             'Each post must have a unique slug.');
  89          }
  90        }
  91      }
  92  
  93      $handle = id(new PhabricatorHandleQuery())
  94        ->setViewer($user)
  95        ->withPHIDs(array($post->getBlogPHID()))
  96        ->executeOne();
  97  
  98      $form = id(new AphrontFormView())
  99        ->setUser($user)
 100        ->addHiddenInput('blog', $request->getInt('blog'))
 101        ->appendChild(
 102          id(new AphrontFormMarkupControl())
 103            ->setLabel(pht('Blog'))
 104            ->setValue($handle->renderLink()))
 105        ->appendChild(
 106          id(new AphrontFormTextControl())
 107          ->setLabel(pht('Title'))
 108          ->setName('title')
 109          ->setValue($post->getTitle())
 110          ->setID('post-title')
 111          ->setError($e_title))
 112        ->appendChild(
 113          id(new AphrontFormTextControl())
 114          ->setLabel(pht('Phame Title'))
 115          ->setName('phame_title')
 116          ->setValue(rtrim($post->getPhameTitle(), '/'))
 117          ->setID('post-phame-title')
 118          ->setCaption(pht('Up to 64 alphanumeric characters '.
 119                       'with underscores for spaces. '.
 120                       'Formatting is enforced.'))
 121          ->setError($e_phame_title))
 122        ->appendChild(
 123          id(new PhabricatorRemarkupControl())
 124          ->setLabel(pht('Body'))
 125          ->setName('body')
 126          ->setValue($post->getBody())
 127          ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
 128          ->setID('post-body')
 129          ->setUser($user)
 130          ->setDisableMacros(true))
 131        ->appendChild(
 132          id(new AphrontFormSelectControl())
 133          ->setLabel(pht('Comments Widget'))
 134          ->setName('comments_widget')
 135          ->setvalue($post->getCommentsWidget())
 136          ->setOptions($post->getCommentsWidgetOptionsForSelect()))
 137        ->appendChild(
 138          id(new AphrontFormSubmitControl())
 139          ->addCancelButton($cancel_uri)
 140          ->setValue($submit_button));
 141  
 142      $loading = phutil_tag_div(
 143        'aphront-panel-preview-loading-text',
 144        pht('Loading preview...'));
 145  
 146      $preview_panel = phutil_tag_div('aphront-panel-preview', array(
 147        phutil_tag_div('phame-post-preview-header', pht('Post Preview')),
 148        phutil_tag('div', array('id' => 'post-preview'), $loading),
 149      ));
 150  
 151      require_celerity_resource('phame-css');
 152      Javelin::initBehavior(
 153        'phame-post-preview',
 154        array(
 155          'preview'     => 'post-preview',
 156          'body'        => 'post-body',
 157          'title'       => 'post-title',
 158          'phame_title' => 'post-phame-title',
 159          'uri'         => '/phame/post/preview/',
 160        ));
 161  
 162      $form_box = id(new PHUIObjectBoxView())
 163        ->setHeaderText($page_title)
 164        ->setFormErrors($errors)
 165        ->setForm($form);
 166  
 167      $crumbs = $this->buildApplicationCrumbs();
 168      $crumbs->addTextCrumb(
 169        $page_title,
 170        $this->getApplicationURI('/post/view/'.$this->id.'/'));
 171  
 172      $nav = $this->renderSideNavFilterView(null);
 173      $nav->appendChild(
 174        array(
 175          $crumbs,
 176          $form_box,
 177          $preview_panel,
 178        ));
 179  
 180      return $this->buildApplicationPage(
 181        $nav,
 182        array(
 183          'title' => $page_title,
 184        ));
 185    }
 186  
 187  }


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