2012-07-19 18:03:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhameBlogEditController
|
|
|
|
extends PhameController {
|
|
|
|
|
2012-10-15 23:50:12 +02:00
|
|
|
private $id;
|
2012-07-19 18:03:10 +02:00
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
2012-10-15 23:50:12 +02:00
|
|
|
$this->id = idx($data, 'id');
|
2012-07-19 18:03:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
2012-10-15 23:49:52 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2012-10-15 23:50:12 +02:00
|
|
|
if ($this->id) {
|
2012-10-15 23:49:52 +02:00
|
|
|
$blog = id(new PhameBlogQuery())
|
|
|
|
->setViewer($user)
|
2012-10-15 23:50:12 +02:00
|
|
|
->withIDs(array($this->id))
|
2012-10-15 23:49:52 +02:00
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
2014-10-07 15:01:04 +02:00
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
2012-10-15 23:49:52 +02:00
|
|
|
))
|
|
|
|
->executeOne();
|
|
|
|
if (!$blog) {
|
2012-07-19 18:03:10 +02:00
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:12 +02:00
|
|
|
$submit_button = pht('Save Changes');
|
|
|
|
$page_title = pht('Edit Blog');
|
|
|
|
$cancel_uri = $this->getApplicationURI('blog/view/'.$blog->getID().'/');
|
2012-07-19 18:03:10 +02:00
|
|
|
} else {
|
|
|
|
$blog = id(new PhameBlog())
|
|
|
|
->setCreatorPHID($user->getPHID());
|
2012-10-15 23:50:12 +02:00
|
|
|
|
|
|
|
$blog->setViewPolicy(PhabricatorPolicies::POLICY_USER);
|
|
|
|
$blog->setEditPolicy(PhabricatorPolicies::POLICY_USER);
|
|
|
|
$blog->setJoinPolicy(PhabricatorPolicies::POLICY_USER);
|
|
|
|
|
|
|
|
$submit_button = pht('Create Blog');
|
|
|
|
$page_title = pht('Create Blog');
|
|
|
|
$cancel_uri = $this->getApplicationURI();
|
2012-07-19 18:03:10 +02:00
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:12 +02:00
|
|
|
$e_name = true;
|
|
|
|
$e_custom_domain = null;
|
|
|
|
$errors = array();
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
if ($request->isFormPost()) {
|
2012-10-01 02:10:27 +02:00
|
|
|
$name = $request->getStr('name');
|
|
|
|
$description = $request->getStr('description');
|
|
|
|
$custom_domain = $request->getStr('custom_domain');
|
2012-10-13 01:01:33 +02:00
|
|
|
$skin = $request->getStr('skin');
|
2012-07-19 18:03:10 +02:00
|
|
|
|
|
|
|
if (empty($name)) {
|
2013-04-14 17:02:29 +02:00
|
|
|
$errors[] = pht('You must give the blog a name.');
|
|
|
|
$e_name = pht('Required');
|
2012-10-15 23:50:12 +02:00
|
|
|
} else {
|
|
|
|
$e_name = null;
|
2012-07-19 18:03:10 +02:00
|
|
|
}
|
2012-10-15 23:50:12 +02:00
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
$blog->setName($name);
|
|
|
|
$blog->setDescription($description);
|
2012-10-16 18:44:43 +02:00
|
|
|
$blog->setDomain(nonempty($custom_domain, null));
|
2012-10-15 23:51:12 +02:00
|
|
|
$blog->setSkin($skin);
|
2014-04-30 22:19:14 +02:00
|
|
|
$blog->setViewPolicy($request->getStr('can_view'));
|
|
|
|
$blog->setEditPolicy($request->getStr('can_edit'));
|
|
|
|
$blog->setJoinPolicy($request->getStr('can_join'));
|
2012-10-15 23:50:12 +02:00
|
|
|
|
2012-10-01 02:10:27 +02:00
|
|
|
if (!empty($custom_domain)) {
|
2014-04-30 22:19:14 +02:00
|
|
|
list($error_label, $error_text) =
|
|
|
|
$blog->validateCustomDomain($custom_domain);
|
|
|
|
if ($error_label) {
|
|
|
|
$errors[] = $error_text;
|
|
|
|
$e_custom_domain = $error_label;
|
|
|
|
}
|
2014-05-05 19:54:44 +02:00
|
|
|
if ($blog->getViewPolicy() != PhabricatorPolicies::POLICY_PUBLIC) {
|
2014-04-30 22:19:14 +02:00
|
|
|
$errors[] = pht(
|
|
|
|
'For custom domains to work, the blog must have a view policy of '.
|
|
|
|
'public.');
|
|
|
|
// Prefer earlier labels for the multiple error scenario.
|
|
|
|
if (!$e_custom_domain) {
|
|
|
|
$e_custom_domain = pht('Invalid Policy');
|
|
|
|
}
|
2012-10-01 02:10:27 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-19 18:03:10 +02:00
|
|
|
|
2012-10-15 23:49:52 +02:00
|
|
|
// Don't let users remove their ability to edit blogs.
|
|
|
|
PhabricatorPolicyFilter::mustRetainCapability(
|
|
|
|
$user,
|
|
|
|
$blog,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT);
|
2012-07-19 18:03:10 +02:00
|
|
|
|
2012-10-15 23:49:52 +02:00
|
|
|
if (!$errors) {
|
2012-10-15 23:51:12 +02:00
|
|
|
try {
|
|
|
|
$blog->save();
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI($this->getApplicationURI('blog/view/'.$blog->getID().'/'));
|
2014-08-05 23:51:21 +02:00
|
|
|
} catch (AphrontDuplicateKeyQueryException $ex) {
|
2013-04-14 17:02:29 +02:00
|
|
|
$errors[] = pht('Domain must be unique.');
|
|
|
|
$e_custom_domain = pht('Not Unique');
|
2012-10-15 23:51:12 +02:00
|
|
|
}
|
2012-07-19 18:03:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:49:52 +02:00
|
|
|
$policies = id(new PhabricatorPolicyQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->setObject($blog)
|
|
|
|
->execute();
|
|
|
|
|
2012-10-17 17:36:48 +02:00
|
|
|
$skins = PhameSkinSpecification::loadAllSkinSpecifications();
|
|
|
|
$skins = mpull($skins, 'getName');
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-04-14 17:02:29 +02:00
|
|
|
->setLabel(pht('Name'))
|
2012-07-19 18:03:10 +02:00
|
|
|
->setName('name')
|
|
|
|
->setValue($blog->getName())
|
|
|
|
->setID('blog-name')
|
2013-02-19 22:33:10 +01:00
|
|
|
->setError($e_name))
|
2012-07-19 18:03:10 +02:00
|
|
|
->appendChild(
|
2012-09-19 21:27:28 +02:00
|
|
|
id(new PhabricatorRemarkupControl())
|
2013-04-14 17:02:29 +02:00
|
|
|
->setLabel(pht('Description'))
|
2012-07-19 18:03:10 +02:00
|
|
|
->setName('description')
|
|
|
|
->setValue($blog->getDescription())
|
|
|
|
->setID('blog-description')
|
2013-02-04 18:54:01 +01:00
|
|
|
->setUser($user)
|
|
|
|
->setDisableMacros(true))
|
2012-07-19 18:03:10 +02:00
|
|
|
->appendChild(
|
2012-10-15 23:49:52 +02:00
|
|
|
id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
|
|
|
->setPolicyObject($blog)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_view'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
|
|
|
|
->setPolicyObject($blog)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_edit'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_JOIN)
|
|
|
|
->setPolicyObject($blog)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_join'))
|
2012-10-01 02:10:27 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-04-14 17:02:29 +02:00
|
|
|
->setLabel(pht('Custom Domain'))
|
2012-10-01 02:10:27 +02:00
|
|
|
->setName('custom_domain')
|
|
|
|
->setValue($blog->getDomain())
|
2013-04-14 17:02:29 +02:00
|
|
|
->setCaption(
|
|
|
|
pht('Must include at least one dot (.), e.g. blog.example.com'))
|
2013-02-19 22:33:10 +01:00
|
|
|
->setError($e_custom_domain))
|
2012-10-13 01:01:33 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
2013-04-14 17:02:29 +02:00
|
|
|
->setLabel(pht('Skin'))
|
2012-10-13 01:01:33 +02:00
|
|
|
->setName('skin')
|
|
|
|
->setValue($blog->getSkin())
|
2013-02-19 22:33:10 +01:00
|
|
|
->setOptions($skins))
|
2012-10-01 02:10:27 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2012-10-15 23:50:12 +02:00
|
|
|
->addCancelButton($cancel_uri)
|
2013-02-19 22:33:10 +01:00
|
|
|
->setValue($submit_button));
|
2012-07-19 18:03:10 +02:00
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 20:53:11 +02:00
|
|
|
->setHeaderText($page_title)
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 20:53:11 +02:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-04-14 17:02:29 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new'));
|
2013-04-14 17:02:29 +02:00
|
|
|
|
2012-10-16 02:55:57 +02:00
|
|
|
$nav = $this->renderSideNavFilterView();
|
|
|
|
$nav->selectFilter($this->id ? null : 'blog/new');
|
2012-10-15 23:50:12 +02:00
|
|
|
$nav->appendChild(
|
2012-07-19 18:03:10 +02:00
|
|
|
array(
|
2013-04-14 17:02:29 +02:00
|
|
|
$crumbs,
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box,
|
2012-10-15 23:50:12 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
2012-07-19 18:03:10 +02:00
|
|
|
array(
|
2013-04-14 17:02:29 +02:00
|
|
|
'title' => $page_title,
|
2012-07-19 18:03:10 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|