private function buildCommentForm(PhamePost $post)
 {
     $viewer = $this->getViewer();
     $draft = PhabricatorDraft::newFromUserAndKey($viewer, $post->getPHID());
     $box = id(new PhabricatorApplicationTransactionCommentView())->setUser($viewer)->setObjectPHID($post->getPHID())->setDraft($draft)->setHeaderText(pht('Add Comment'))->setAction($this->getApplicationURI('post/comment/' . $post->getID() . '/'))->setSubmitButtonName(pht('Add Comment'));
     return phutil_tag_div('phui-document-view-pro-box', $box);
 }
 public function execute()
 {
     $table = new PhamePost();
     $conn_r = $table->establishConnection('r');
     $where_clause = $this->buildWhereClause($conn_r);
     $order_clause = $this->buildOrderClause($conn_r);
     $limit_clause = $this->buildLimitClause($conn_r);
     $data = queryfx_all($conn_r, 'SELECT * FROM %T e %Q %Q %Q', $table->getTableName(), $where_clause, $order_clause, $limit_clause);
     $posts = $table->loadAllFromArray($data);
     return $posts;
 }
 public function handleRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $id = $request->getURIData('id');
     if ($id) {
         $post = id(new PhamePostQuery())->setViewer($user)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$post) {
             return new Aphront404Response();
         }
         $cancel_uri = $this->getApplicationURI('/post/view/' . $id . '/');
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Post');
     } else {
         $blog = id(new PhameBlogQuery())->setViewer($user)->withIDs(array($request->getInt('blog')))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_JOIN))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $post = PhamePost::initializePost($user, $blog);
         $cancel_uri = $this->getApplicationURI('/blog/view/' . $blog->getID() . '/');
         $submit_button = pht('Save Draft');
         $page_title = pht('Create Post');
     }
     $title = $post->getTitle();
     $phame_title = $post->getPhameTitle();
     $body = $post->getBody();
     $comments_widget = $post->getCommentsWidget();
     $e_title = true;
     $e_phame_title = true;
     $validation_exception = null;
     if ($request->isFormPost()) {
         $title = $request->getStr('title');
         $phame_title = $request->getStr('phame_title');
         $phame_title = PhabricatorSlug::normalize($phame_title);
         $body = $request->getStr('body');
         $comments_widget = $request->getStr('comments_widget');
         $xactions = array(id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_TITLE)->setNewValue($title), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_PHAME_TITLE)->setNewValue($phame_title), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_BODY)->setNewValue($body), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_COMMENTS_WIDGET)->setNewValue($comments_widget));
         $editor = id(new PhamePostEditor())->setActor($user)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $editor->applyTransactions($post, $xactions);
             $uri = $this->getApplicationURI('/post/view/' . $post->getID() . '/');
             return id(new AphrontRedirectResponse())->setURI($uri);
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
             $e_title = $validation_exception->getShortMessage(PhamePostTransaction::TYPE_TITLE);
             $e_phame_title = $validation_exception->getShortMessage(PhamePostTransaction::TYPE_PHAME_TITLE);
         }
     }
     $handle = id(new PhabricatorHandleQuery())->setViewer($user)->withPHIDs(array($post->getBlogPHID()))->executeOne();
     $form = id(new AphrontFormView())->setUser($user)->addHiddenInput('blog', $request->getInt('blog'))->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Blog'))->setValue($handle->renderLink()))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Title'))->setName('title')->setValue($title)->setID('post-title')->setError($e_title))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Phame Title'))->setName('phame_title')->setValue(rtrim($phame_title, '/'))->setID('post-phame-title')->setCaption(pht('Up to 64 alphanumeric characters ' . 'with underscores for spaces. ' . 'Formatting is enforced.'))->setError($e_phame_title))->appendChild(id(new PhabricatorRemarkupControl())->setLabel(pht('Body'))->setName('body')->setValue($body)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setID('post-body')->setUser($user)->setDisableMacros(true))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Comments Widget'))->setName('comments_widget')->setvalue($comments_widget)->setOptions($post->getCommentsWidgetOptionsForSelect()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $loading = phutil_tag_div('aphront-panel-preview-loading-text', pht('Loading preview...'));
     $preview_panel = phutil_tag_div('aphront-panel-preview', array(phutil_tag_div('phame-post-preview-header', pht('Post Preview')), phutil_tag('div', array('id' => 'post-preview'), $loading)));
     require_celerity_resource('phame-css');
     Javelin::initBehavior('phame-post-preview', array('preview' => 'post-preview', 'body' => 'post-body', 'title' => 'post-title', 'phame_title' => 'post-phame-title', 'uri' => '/phame/post/preview/'));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setValidationException($validation_exception)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('/post/view/' . $id . '/'));
     $nav = $this->renderSideNavFilterView(null);
     $nav->appendChild(array($crumbs, $form_box, $preview_panel));
     return $this->buildApplicationPage($nav, array('title' => $page_title));
 }
 protected function newEditableObject()
 {
     $viewer = $this->getViewer();
     if ($this->blog) {
         $blog = $this->blog;
     } else {
         $blog = PhameBlog::initializeNewBlog($viewer);
     }
     return PhamePost::initializePost($viewer, $blog);
 }
 protected function loadPage()
 {
     $table = new PhamePost();
     $conn_r = $table->establishConnection('r');
     $where_clause = $this->buildWhereClause($conn_r);
     $order_clause = $this->buildOrderClause($conn_r);
     $limit_clause = $this->buildLimitClause($conn_r);
     $data = queryfx_all($conn_r, 'SELECT * FROM %T p %Q %Q %Q', $table->getTableName(), $where_clause, $order_clause, $limit_clause);
     $posts = $table->loadAllFromArray($data);
     if ($posts) {
         // We require these to do visibility checks, so load them unconditionally.
         $blog_phids = mpull($posts, 'getBlogPHID');
         $blogs = id(new PhameBlogQuery())->setViewer($this->getViewer())->withPHIDs($blog_phids)->execute();
         $blogs = mpull($blogs, null, 'getPHID');
         foreach ($posts as $post) {
             if (isset($blogs[$post->getBlogPHID()])) {
                 $post->setBlog($blogs[$post->getBlogPHID()]);
             }
         }
     }
     return $posts;
 }
 protected function execute(ConduitAPIRequest $request)
 {
     $user = $request->getUser();
     $blog_phid = $request->getValue('blogPHID');
     $title = $request->getValue('title');
     $body = $request->getValue('body');
     $exception_description = array();
     if (!$blog_phid) {
         $exception_description[] = pht('No blog phid.');
     }
     if (!strlen($title)) {
         $exception_description[] = pht('No post title.');
     }
     if (!strlen($body)) {
         $exception_description[] = pht('No post body.');
     }
     if ($exception_description) {
         throw id(new ConduitException('ERR-INVALID-PARAMETER'))->setErrorDescription(implode("\n", $exception_description));
     }
     $blogger_phid = $request->getValue('bloggerPHID');
     if ($blogger_phid) {
         $blogger = id(new PhabricatorPeopleQuery())->setViewer($user)->withPHIDs(array($blogger_phid))->executeOne();
     } else {
         $blogger = $user;
     }
     $blog = id(new PhameBlogQuery())->setViewer($blogger)->withPHIDs(array($blog_phid))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_JOIN))->executeOne();
     if (!$blog) {
         throw new ConduitException('ERR-INVALID-BLOG');
     }
     $post = PhamePost::initializePost($blogger, $blog);
     $is_draft = $request->getValue('isDraft', false);
     if (!$is_draft) {
         $post->setDatePublished(time());
         $post->setVisibility(PhamePost::VISIBILITY_PUBLISHED);
     }
     $post->setTitle($title);
     $phame_title = $request->getValue('phameTitle', id(new PhutilUTF8StringTruncator())->setMaximumBytes(64)->truncateString($title));
     $post->setPhameTitle(PhabricatorSlug::normalize($phame_title));
     $post->setBody($body);
     $post->save();
     return $post->toDictionary();
 }
 private function loadAdjacentPosts(PhamePost $post)
 {
     $viewer = $this->getViewer();
     $query = id(new PhamePostQuery())->setViewer($viewer)->withVisibility(PhameConstants::VISIBILITY_PUBLISHED)->withBlogPHIDs(array($post->getBlog()->getPHID()))->setLimit(1);
     $prev = id(clone $query)->setAfterID($post->getID())->execute();
     $next = id(clone $query)->setBeforeID($post->getID())->execute();
     return array(head($prev), head($next));
 }
 public function getLiveURI(PhamePost $post = null)
 {
     if ($this->getDomain()) {
         $base = new PhutilURI('http://' . $this->getDomain() . '/');
     } else {
         $base = '/phame/live/' . $this->getID() . '/';
         $base = PhabricatorEnv::getURI($base);
     }
     if ($post) {
         $base .= '/post/' . $post->getPhameTitle();
     }
     return $base;
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $id = $request->getURIData('id');
     if ($id) {
         $post = id(new PhamePostQuery())->setViewer($viewer)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$post) {
             return new Aphront404Response();
         }
         $cancel_uri = $this->getApplicationURI('/post/view/' . $id . '/');
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Post');
         $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs($post->getPHID(), PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
         $v_projects = array_reverse($v_projects);
         $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID($post->getPHID());
     } else {
         $blog = id(new PhameBlogQuery())->setViewer($viewer)->withIDs(array($request->getInt('blog')))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $v_projects = array();
         $v_cc = array();
         $post = PhamePost::initializePost($viewer, $blog);
         $cancel_uri = $this->getApplicationURI('/blog/view/' . $blog->getID() . '/');
         $submit_button = pht('Create Post');
         $page_title = pht('Create Post');
     }
     $title = $post->getTitle();
     $phame_title = $post->getPhameTitle();
     $body = $post->getBody();
     $visibility = $post->getVisibility();
     $e_title = true;
     $e_phame_title = true;
     $validation_exception = null;
     if ($request->isFormPost()) {
         $title = $request->getStr('title');
         $phame_title = $request->getStr('phame_title');
         $phame_title = PhabricatorSlug::normalize($phame_title);
         $body = $request->getStr('body');
         $v_projects = $request->getArr('projects');
         $v_cc = $request->getArr('cc');
         $visibility = $request->getInt('visibility');
         $xactions = array(id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_TITLE)->setNewValue($title), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_PHAME_TITLE)->setNewValue($phame_title), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_BODY)->setNewValue($body), id(new PhamePostTransaction())->setTransactionType(PhamePostTransaction::TYPE_VISIBILITY)->setNewValue($visibility), id(new PhamePostTransaction())->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)->setNewValue(array('=' => $v_cc)));
         $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
         $xactions[] = id(new PhamePostTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $proj_edge_type)->setNewValue(array('=' => array_fuse($v_projects)));
         $editor = id(new PhamePostEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $editor->applyTransactions($post, $xactions);
             $uri = $this->getApplicationURI('/post/view/' . $post->getID() . '/');
             return id(new AphrontRedirectResponse())->setURI($uri);
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
             $e_title = $validation_exception->getShortMessage(PhamePostTransaction::TYPE_TITLE);
             $e_phame_title = $validation_exception->getShortMessage(PhamePostTransaction::TYPE_PHAME_TITLE);
         }
     }
     $handle = id(new PhabricatorHandleQuery())->setViewer($viewer)->withPHIDs(array($post->getBlogPHID()))->executeOne();
     $form = id(new AphrontFormView())->setUser($viewer)->addHiddenInput('blog', $request->getInt('blog'))->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Blog'))->setValue($handle->renderLink()))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Title'))->setName('title')->setValue($title)->setID('post-title')->setError($e_title))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Phame Title'))->setName('phame_title')->setValue(rtrim($phame_title, '/'))->setID('post-phame-title')->setCaption(pht('Up to 64 alphanumeric characters ' . 'with underscores for spaces. ' . 'Formatting is enforced.'))->setError($e_phame_title))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Visibility'))->setName('visibility')->setvalue($visibility)->setOptions(PhameConstants::getPhamePostStatusMap()))->appendChild(id(new PhabricatorRemarkupControl())->setLabel(pht('Body'))->setName('body')->setValue($body)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setID('post-body')->setUser($viewer)->setDisableMacros(true))->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Subscribers'))->setName('cc')->setValue($v_cc)->setUser($viewer)->setDatasource(new PhabricatorMetaMTAMailableDatasource()))->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($v_projects)->setDatasource(new PhabricatorProjectDatasource()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $preview = id(new PHUIRemarkupPreviewPanel())->setHeader($post->getTitle())->setPreviewURI($this->getApplicationURI('post/preview/'))->setControlID('post-body')->setPreviewType(PHUIRemarkupPreviewPanel::DOCUMENT);
     Javelin::initBehavior('phame-post-preview', array('title' => 'post-title', 'phame_title' => 'post-phame-title'));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setValidationException($validation_exception)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('/post/view/' . $id . '/'));
     return $this->newPage()->setTitle($page_title)->setCrumbs($crumbs)->appendChild(array($form_box, $preview));
 }
 private function renderPreviewFrame(PhamePost $post)
 {
     // TODO: Clean up this CSS.
     return phutil_tag('div', array('style' => 'text-align: center; padding: 1em;'), phutil_tag('iframe', array('style' => 'width: 100%; height: 600px; ' . 'border: 1px solid #303030;', 'src' => $this->getApplicationURI('/post/framed/' . $post->getID() . '/')), ''));
 }
 private function renderPreviewFrame(PhamePost $post)
 {
     return phutil_tag('div', array('style' => 'text-align: center; padding: 16px;'), phutil_tag('iframe', array('style' => 'width: 100%; height: 600px; ' . 'border: 1px solid #BFCFDA; ' . 'background-color: #fff; ' . 'border-radius: 3px; ', 'src' => $this->getApplicationURI('/post/framed/' . $post->getID() . '/')), ''));
 }
 private function getBlogCheckboxControl(PhamePost $post)
 {
     if ($post->getVisibility() == PhamePost::VISIBILITY_PUBLISHED) {
         $control_style = null;
     } else {
         $control_style = 'display: none';
     }
     $control = id(new AphrontFormCheckboxControl())->setLabel('Blogs')->setControlID('post-blogs')->setControlStyle($control_style);
     $post_blogs = $this->getPostBlogs();
     $user_blogs = $this->getUserBlogs();
     $all_blogs = $post_blogs + $user_blogs;
     $all_blogs = msort($all_blogs, 'getName');
     foreach ($all_blogs as $phid => $blog) {
         $control->addCheckbox('blogs[]', $blog->getPHID(), $blog->getName(), isset($post_blogs[$phid]));
     }
     return $control;
 }
<?php

$table = new PhamePost();
$conn_w = $table->establishConnection('w');
$iterator = new LiskMigrationIterator($table);
foreach ($iterator as $post) {
    $id = $post->getID();
    echo pht('Adding mail key for Post %d...', $id);
    echo "\n";
    queryfx($conn_w, 'UPDATE %T SET mailKey = %s WHERE id = %d', $table->getTableName(), Filesystem::readRandomCharacters(20), $id);
}
 private function buildPhamePostHeader(PhamePost $post)
 {
     $image = null;
     if ($post->getHeaderImagePHID()) {
         $image = phutil_tag('div', array('class' => 'phame-header-hero'), phutil_tag('img', array('src' => $post->getHeaderImageURI(), 'class' => 'phame-header-image')));
     }
     $title = phutil_tag_div('phame-header-title', $post->getTitle());
     $subtitle = null;
     if ($post->getSubtitle()) {
         $subtitle = phutil_tag_div('phame-header-subtitle', $post->getSubtitle());
     }
     return phutil_tag_div('phame-mega-header', array($image, $title, $subtitle));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $e_phame_title = null;
     $e_title = null;
     $errors = array();
     if ($this->isPostEdit()) {
         $post = id(new PhamePost())->loadOneWhere('phid = %s', $this->getPostPHID());
         if (empty($post)) {
             return new Aphront404Response();
         }
         if ($post->getBloggerPHID() != $user->getPHID()) {
             return new Aphront403Response();
         }
         $cancel_uri = $post->getViewURI($user->getUsername());
         $submit_button = 'Save Changes';
         $delete_button = javelin_render_tag('a', array('href' => $post->getDeleteURI(), 'class' => 'grey button', 'sigil' => 'workflow'), 'Delete Post');
         $page_title = 'Edit Post';
     } else {
         $post = id(new PhamePost())->setBloggerPHID($user->getPHID())->setVisibility(PhamePost::VISIBILITY_DRAFT);
         $cancel_uri = '/phame';
         $submit_button = 'Create Post';
         $delete_button = null;
         $page_title = 'Create Post';
     }
     if ($request->isFormPost()) {
         $saved = true;
         $visibility = $request->getInt('visibility');
         $comments = $request->getStr('comments_widget');
         $data = array('comments_widget' => $comments);
         $phame_title = $request->getStr('phame_title');
         $phame_title = PhabricatorSlug::normalize($phame_title);
         $title = $request->getStr('title');
         $post->setTitle($title);
         $post->setPhameTitle($phame_title);
         $post->setBody($request->getStr('body'));
         $post->setVisibility($visibility);
         $post->setConfigData($data);
         // only publish once...!
         if ($visibility == PhamePost::VISIBILITY_PUBLISHED) {
             if (!$post->getDatePublished()) {
                 $post->setDatePublished(time());
             }
             // this is basically a cast of null to 0 if its a new post
         } else {
             if (!$post->getDatePublished()) {
                 $post->setDatePublished(0);
             }
         }
         if ($phame_title == '/') {
             $errors[] = 'Phame title must be nonempty.';
             $e_phame_title = 'Required';
         }
         if (empty($title)) {
             $errors[] = 'Title must be nonempty.';
             $e_title = 'Required';
         }
         if (empty($errors)) {
             try {
                 $post->save();
             } catch (AphrontQueryDuplicateKeyException $e) {
                 $saved = false;
                 $e_phame_title = 'Not Unique';
                 $errors[] = 'Another post already uses this slug. ' . 'Each post must have a unique slug.';
             }
         } else {
             $saved = false;
         }
         if ($saved) {
             $uri = new PhutilURI($post->getViewURI($user->getUsername()));
             return id(new AphrontRedirectResponse())->setURI($uri);
         }
     }
     $panel = new AphrontPanelView();
     $panel->setHeader($page_title);
     $panel->setWidth(AphrontPanelView::WIDTH_FULL);
     if ($delete_button) {
         $panel->addButton($delete_button);
     }
     $remarkup_reference = phutil_render_tag('a', array('href' => PhabricatorEnv::getDoclink('article/Remarkup_Reference.html'), 'tabindex' => '-1', 'target' => '_blank'), 'Formatting Reference');
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setName('title')->setValue($post->getTitle())->setID('post-title')->setError($e_title))->appendChild(id(new AphrontFormTextControl())->setLabel('Phame Title')->setName('phame_title')->setValue(rtrim($post->getPhameTitle(), '/'))->setID('post-phame-title')->setCaption('Up to 64 alphanumeric characters ' . 'with underscores for spaces. ' . 'Formatting is enforced.')->setError($e_phame_title))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Body')->setName('body')->setValue($post->getBody())->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setEnableDragAndDropFileUploads(true)->setID('post-body')->setCaption($remarkup_reference))->appendChild(id(new AphrontFormSelectControl())->setLabel('Visibility')->setName('visibility')->setValue($post->getVisibility())->setOptions(PhamePost::getVisibilityOptionsForSelect()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Comments Widget')->setName('comments_widget')->setvalue($post->getCommentsWidget())->setOptions($post->getCommentsWidgetOptionsForSelect()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $panel->appendChild($form);
     $preview_panel = '<div class="aphront-panel-preview ">
      <div class="phame-post-preview-header">
        Post Preview
      </div>
      <div id="post-preview">
        <div class="aphront-panel-preview-loading-text">
          Loading preview...
        </div>
      </div>
    </div>';
     Javelin::initBehavior('phame-post-preview', array('preview' => 'post-preview', 'body' => 'post-body', 'title' => 'post-title', 'phame_title' => 'post-phame-title', 'uri' => '/phame/post/preview/'));
     if ($errors) {
         $error_view = id(new AphrontErrorView())->setTitle('Errors saving post.')->setErrors($errors);
     } else {
         $error_view = null;
     }
     $this->setShowSideNav(true);
     return $this->buildStandardPageResponse(array($error_view, $panel, $preview_panel), array('title' => $page_title));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->id) {
         $post = id(new PhamePostQuery())->setViewer($user)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$post) {
             return new Aphront404Response();
         }
         $cancel_uri = $this->getApplicationURI('/post/view/' . $this->id . '/');
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Post');
     } else {
         $blog = id(new PhameBlogQuery())->setViewer($user)->withIDs(array($request->getInt('blog')))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_JOIN))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $post = PhamePost::initializePost($user, $blog);
         $cancel_uri = $this->getApplicationURI('/blog/view/' . $blog->getID() . '/');
         $submit_button = pht('Save Draft');
         $page_title = pht('Create Post');
     }
     $e_phame_title = null;
     $e_title = true;
     $errors = array();
     if ($request->isFormPost()) {
         $comments = $request->getStr('comments_widget');
         $data = array('comments_widget' => $comments);
         $phame_title = $request->getStr('phame_title');
         $phame_title = PhabricatorSlug::normalize($phame_title);
         $title = $request->getStr('title');
         $post->setTitle($title);
         $post->setPhameTitle($phame_title);
         $post->setBody($request->getStr('body'));
         $post->setConfigData($data);
         if ($phame_title == '/') {
             $errors[] = pht('Phame title must be nonempty.');
             $e_phame_title = pht('Required');
         }
         if (!strlen($title)) {
             $errors[] = pht('Title must be nonempty.');
             $e_title = pht('Required');
         } else {
             $e_title = null;
         }
         if (!$errors) {
             try {
                 $post->save();
                 $uri = $this->getApplicationURI('/post/view/' . $post->getID() . '/');
                 return id(new AphrontRedirectResponse())->setURI($uri);
             } catch (AphrontDuplicateKeyQueryException $e) {
                 $e_phame_title = pht('Not Unique');
                 $errors[] = pht('Another post already uses this slug. ' . 'Each post must have a unique slug.');
             }
         }
     }
     $handle = id(new PhabricatorHandleQuery())->setViewer($user)->withPHIDs(array($post->getBlogPHID()))->executeOne();
     $form = id(new AphrontFormView())->setUser($user)->addHiddenInput('blog', $request->getInt('blog'))->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Blog'))->setValue($handle->renderLink()))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Title'))->setName('title')->setValue($post->getTitle())->setID('post-title')->setError($e_title))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Phame Title'))->setName('phame_title')->setValue(rtrim($post->getPhameTitle(), '/'))->setID('post-phame-title')->setCaption(pht('Up to 64 alphanumeric characters ' . 'with underscores for spaces. ' . 'Formatting is enforced.'))->setError($e_phame_title))->appendChild(id(new PhabricatorRemarkupControl())->setLabel(pht('Body'))->setName('body')->setValue($post->getBody())->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setID('post-body')->setUser($user)->setDisableMacros(true))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Comments Widget'))->setName('comments_widget')->setvalue($post->getCommentsWidget())->setOptions($post->getCommentsWidgetOptionsForSelect()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $loading = phutil_tag_div('aphront-panel-preview-loading-text', pht('Loading preview...'));
     $preview_panel = phutil_tag_div('aphront-panel-preview', array(phutil_tag_div('phame-post-preview-header', pht('Post Preview')), phutil_tag('div', array('id' => 'post-preview'), $loading)));
     require_celerity_resource('phame-css');
     Javelin::initBehavior('phame-post-preview', array('preview' => 'post-preview', 'body' => 'post-body', 'title' => 'post-title', 'phame_title' => 'post-phame-title', 'uri' => '/phame/post/preview/'));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setFormErrors($errors)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('/post/view/' . $this->id . '/'));
     $nav = $this->renderSideNavFilterView(null);
     $nav->appendChild(array($crumbs, $form_box, $preview_panel));
     return $this->buildApplicationPage($nav, array('title' => $page_title));
 }