Phame - introduce blogs
Summary: blogs are collections of posts. a blog also has metadata like a name, description and "bloggers" that can edit the metadata of the blog and contribute posts. changes include the post edit flow where bloggers can now select which blogs to publish to. also made various small tweaks throughout the UI to make things sensical and clean as the concept of blogs is introduced. there's edges powering this stuff. bloggers <=> blogs and posts <=> blogs in particular. Test Plan: made blogs, deleted blogs, tried to make blogs with no bloggers. all went well. verified ui to publish only showed up for public posts, published posts to blogs, un-published posts to blogs, re-published posts to blogs, deleted posts and verified they disappeared from blogs. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T1373 Differential Revision: https://secure.phabricator.com/D3003
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group phame
|
||||
*/
|
||||
final class PhameBlogEditController
|
||||
extends PhameController {
|
||||
|
||||
private $phid;
|
||||
private $isBlogEdit;
|
||||
|
||||
private function setBlogPHID($phid) {
|
||||
$this->phid = $phid;
|
||||
return $this;
|
||||
}
|
||||
private function getBlogPHID() {
|
||||
return $this->phid;
|
||||
}
|
||||
private function setIsBlogEdit($is_blog_edit) {
|
||||
$this->isBlogEdit = $is_blog_edit;
|
||||
return $this;
|
||||
}
|
||||
private function isBlogEdit() {
|
||||
return $this->isBlogEdit;
|
||||
}
|
||||
|
||||
protected function getSideNavFilter() {
|
||||
if ($this->isBlogEdit()) {
|
||||
$filter = 'blog/edit/'.$this->getBlogPHID();
|
||||
} else {
|
||||
$filter = 'blog/new';
|
||||
}
|
||||
return $filter;
|
||||
}
|
||||
|
||||
protected function getSideNavBlogFilters() {
|
||||
$filters = parent::getSideNavBlogFilters();
|
||||
|
||||
if ($this->isBlogEdit()) {
|
||||
$filter =
|
||||
array('key' => 'blog/edit/'.$this->getBlogPHID(),
|
||||
'name' => 'Edit Blog');
|
||||
$filters[] = $filter;
|
||||
} else {
|
||||
$filter =
|
||||
array('key' => 'blog/new',
|
||||
'name' => 'New Blog');
|
||||
array_unshift($filters, $filter);
|
||||
}
|
||||
|
||||
return $filters;
|
||||
}
|
||||
|
||||
public function willProcessRequest(array $data) {
|
||||
$phid = idx($data, 'phid');
|
||||
$this->setBlogPHID($phid);
|
||||
$this->setIsBlogEdit((bool)$phid);
|
||||
}
|
||||
|
||||
public function processRequest() {
|
||||
$request = $this->getRequest();
|
||||
$user = $request->getUser();
|
||||
$e_name = null;
|
||||
$e_bloggers = null;
|
||||
$errors = array();
|
||||
|
||||
if ($this->isBlogEdit()) {
|
||||
$blogs = id(new PhameBlogQuery())
|
||||
->withPHIDs(array($this->getBlogPHID()))
|
||||
->execute();
|
||||
$blog = reset($blogs);
|
||||
if (empty($blog)) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
|
||||
$bloggers = $blog->loadBloggers()->getBloggers();
|
||||
|
||||
// TODO -- make this check use a policy
|
||||
if (!isset($bloggers[$user->getPHID()]) &&
|
||||
!$user->isAdmin()) {
|
||||
return new Aphront403Response();
|
||||
}
|
||||
$blogger_tokens = mpull($bloggers, 'getFullName', 'getPHID');
|
||||
$submit_button = 'Save Changes';
|
||||
$delete_button = javelin_render_tag(
|
||||
'a',
|
||||
array(
|
||||
'href' => $blog->getDeleteURI(),
|
||||
'class' => 'grey button',
|
||||
'sigil' => 'workflow',
|
||||
),
|
||||
'Delete Blog');
|
||||
$page_title = 'Edit Blog';
|
||||
} else {
|
||||
$blog = id(new PhameBlog())
|
||||
->setCreatorPHID($user->getPHID());
|
||||
$blogger_tokens = array($user->getPHID() => $user->getFullName());
|
||||
$submit_button = 'Create Blog';
|
||||
$delete_button = null;
|
||||
$page_title = 'Create Blog';
|
||||
}
|
||||
|
||||
if ($request->isFormPost()) {
|
||||
$saved = true;
|
||||
$name = $request->getStr('name');
|
||||
$description = $request->getStr('description');
|
||||
$blogger_arr = $request->getArr('bloggers');
|
||||
|
||||
if (empty($blogger_arr)) {
|
||||
$error = 'Bloggers must be nonempty.';
|
||||
if ($this->isBlogEdit()) {
|
||||
$error .= ' To delete the blog, use the delete button.';
|
||||
} else {
|
||||
$error .= ' A blog cannot exist without bloggers.';
|
||||
}
|
||||
$e_bloggers = 'Required';
|
||||
$errors[] = $error;
|
||||
}
|
||||
$new_bloggers = array_values($blogger_arr);
|
||||
if ($this->isBlogEdit()) {
|
||||
$old_bloggers = array_keys($blogger_tokens);
|
||||
} else {
|
||||
$old_bloggers = array();
|
||||
}
|
||||
|
||||
if (empty($name)) {
|
||||
$errors[] = 'Name must be nonempty.';
|
||||
$e_name = 'Required';
|
||||
}
|
||||
$blog->setName($name);
|
||||
$blog->setDescription($description);
|
||||
|
||||
if (empty($errors)) {
|
||||
$blog->save();
|
||||
|
||||
$add_phids = $new_bloggers;
|
||||
$rem_phids = array_diff($old_bloggers, $new_bloggers);
|
||||
$editor = new PhabricatorEdgeEditor();
|
||||
$edge_type = PhabricatorEdgeConfig::TYPE_BLOG_HAS_BLOGGER;
|
||||
$editor->setUser($user);
|
||||
foreach ($add_phids as $phid) {
|
||||
$editor->addEdge($blog->getPHID(), $edge_type, $phid);
|
||||
}
|
||||
foreach ($rem_phids as $phid) {
|
||||
$editor->removeEdge($blog->getPHID(), $edge_type, $phid);
|
||||
}
|
||||
$editor->save();
|
||||
|
||||
} else {
|
||||
$saved = false;
|
||||
}
|
||||
|
||||
if ($saved) {
|
||||
$uri = new PhutilURI($blog->getViewURI());
|
||||
$uri->setQueryParam('new', true);
|
||||
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('Name')
|
||||
->setName('name')
|
||||
->setValue($blog->getName())
|
||||
->setID('blog-name')
|
||||
->setError($e_name)
|
||||
)
|
||||
->appendChild(
|
||||
id(new AphrontFormTextAreaControl())
|
||||
->setLabel('Description')
|
||||
->setName('description')
|
||||
->setValue($blog->getDescription())
|
||||
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
||||
->setID('blog-description')
|
||||
->setCaption($remarkup_reference)
|
||||
)
|
||||
->appendChild(
|
||||
id(new AphrontFormTokenizerControl())
|
||||
->setLabel('Bloggers')
|
||||
->setName('bloggers')
|
||||
->setValue($blogger_tokens)
|
||||
->setUser($user)
|
||||
->setDatasource('/typeahead/common/users/')
|
||||
->setError($e_bloggers)
|
||||
)
|
||||
->appendChild(
|
||||
id(new AphrontFormSubmitControl())
|
||||
->addCancelButton('/phame/blog/')
|
||||
->setValue($submit_button)
|
||||
);
|
||||
|
||||
$panel->appendChild($form);
|
||||
|
||||
if ($errors) {
|
||||
$error_view = id(new AphrontErrorView())
|
||||
->setTitle('Errors saving blog.')
|
||||
->setErrors($errors);
|
||||
} else {
|
||||
$error_view = null;
|
||||
}
|
||||
|
||||
$this->setShowSideNav(true);
|
||||
return $this->buildStandardPageResponse(
|
||||
array(
|
||||
$error_view,
|
||||
$panel,
|
||||
),
|
||||
array(
|
||||
'title' => $page_title,
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user