2012-07-19 18:03:10 +02:00
|
|
|
<?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 {
|
|
|
|
|
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(
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT
|
|
|
|
))
|
|
|
|
->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)) {
|
2012-10-15 23:49:52 +02:00
|
|
|
$errors[] = 'You must give the blog a name.';
|
2012-10-15 23:50:12 +02:00
|
|
|
$e_name = 'Required';
|
|
|
|
} 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-15 23:51:12 +02:00
|
|
|
$blog->setDomain($custom_domain);
|
|
|
|
$blog->setSkin($skin);
|
2012-10-15 23:50:12 +02:00
|
|
|
|
2012-10-01 02:10:27 +02:00
|
|
|
if (!empty($custom_domain)) {
|
|
|
|
$error = $blog->validateCustomDomain($custom_domain);
|
|
|
|
if ($error) {
|
|
|
|
$errors[] = $error;
|
|
|
|
$e_custom_domain = 'Invalid';
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 18:03:10 +02:00
|
|
|
|
2012-10-15 23:49:52 +02:00
|
|
|
$blog->setViewPolicy($request->getStr('can_view'));
|
|
|
|
$blog->setEditPolicy($request->getStr('can_edit'));
|
|
|
|
$blog->setJoinPolicy($request->getStr('can_join'));
|
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().'/'));
|
|
|
|
} catch (AphrontQueryDuplicateKeyException $ex) {
|
|
|
|
$errors[] = 'Domain must be unique.';
|
|
|
|
$e_custom_domain = 'Not Unique';
|
|
|
|
}
|
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-07-19 18:03:10 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2012-10-15 23:50:12 +02:00
|
|
|
->setFlexible(true)
|
2012-07-19 18:03:10 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel('Name')
|
|
|
|
->setName('name')
|
|
|
|
->setValue($blog->getName())
|
|
|
|
->setID('blog-name')
|
|
|
|
->setError($e_name)
|
|
|
|
)
|
|
|
|
->appendChild(
|
2012-09-19 21:27:28 +02:00
|
|
|
id(new PhabricatorRemarkupControl())
|
2012-07-19 18:03:10 +02:00
|
|
|
->setLabel('Description')
|
|
|
|
->setName('description')
|
|
|
|
->setValue($blog->getDescription())
|
|
|
|
->setID('blog-description')
|
|
|
|
)
|
|
|
|
->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())
|
|
|
|
->setLabel('Custom Domain')
|
|
|
|
->setName('custom_domain')
|
|
|
|
->setValue($blog->getDomain())
|
|
|
|
->setCaption('Must include at least one dot (.), e.g. '.
|
|
|
|
'blog.example.com')
|
|
|
|
->setError($e_custom_domain)
|
|
|
|
)
|
2012-10-13 01:01:33 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel('Skin')
|
|
|
|
->setName('skin')
|
|
|
|
->setValue($blog->getSkin())
|
|
|
|
->setOptions(PhameBlog::getSkinOptionsForSelect())
|
|
|
|
)
|
2012-10-01 02:10:27 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2012-10-15 23:50:12 +02:00
|
|
|
->addCancelButton($cancel_uri)
|
2012-07-19 18:03:10 +02:00
|
|
|
->setValue($submit_button)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
2012-10-15 23:49:52 +02:00
|
|
|
->setTitle('Form Errors')
|
2012-07-19 18:03:10 +02:00
|
|
|
->setErrors($errors);
|
|
|
|
} else {
|
|
|
|
$error_view = null;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:12 +02:00
|
|
|
$nav = $this->renderSideNavFilterView(null);
|
|
|
|
$nav->appendChild(
|
2012-07-19 18:03:10 +02:00
|
|
|
array(
|
|
|
|
$error_view,
|
2012-10-15 23:50:12 +02:00
|
|
|
$form,
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
2012-07-19 18:03:10 +02:00
|
|
|
array(
|
|
|
|
'title' => $page_title,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|