From 36bfff389822dffbf0194cf03dc1774059ea1eb0 Mon Sep 17 00:00:00 2001 From: Chad Little Date: Wed, 16 Dec 2015 11:56:41 -0800 Subject: [PATCH] Give PhameBlog an EditEngine Summary: This seems to work, but I couldn't figure out how to pass over a Caption for a text field. Test Plan: New blog, Edit blog. Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D14770 --- src/__phutil_library_map__.php | 2 + .../blog/PhameBlogEditController.php | 195 +----------------- .../phame/editor/PhameBlogEditEngine.php | 85 ++++++++ src/applications/phame/storage/PhameBlog.php | 4 + 4 files changed, 96 insertions(+), 190 deletions(-) create mode 100644 src/applications/phame/editor/PhameBlogEditEngine.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 6ea90408e9..a006b7fcdf 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3352,6 +3352,7 @@ phutil_register_library_map(array( 'PhameBlogController' => 'applications/phame/controller/blog/PhameBlogController.php', 'PhameBlogCreateCapability' => 'applications/phame/capability/PhameBlogCreateCapability.php', 'PhameBlogEditController' => 'applications/phame/controller/blog/PhameBlogEditController.php', + 'PhameBlogEditEngine' => 'applications/phame/editor/PhameBlogEditEngine.php', 'PhameBlogEditor' => 'applications/phame/editor/PhameBlogEditor.php', 'PhameBlogFeedController' => 'applications/phame/controller/blog/PhameBlogFeedController.php', 'PhameBlogListController' => 'applications/phame/controller/blog/PhameBlogListController.php', @@ -7735,6 +7736,7 @@ phutil_register_library_map(array( 'PhameBlogController' => 'PhameController', 'PhameBlogCreateCapability' => 'PhabricatorPolicyCapability', 'PhameBlogEditController' => 'PhameBlogController', + 'PhameBlogEditEngine' => 'PhabricatorEditEngine', 'PhameBlogEditor' => 'PhabricatorApplicationTransactionEditor', 'PhameBlogFeedController' => 'PhameBlogController', 'PhameBlogListController' => 'PhameBlogController', diff --git a/src/applications/phame/controller/blog/PhameBlogEditController.php b/src/applications/phame/controller/blog/PhameBlogEditController.php index 0ad0b2bffd..7383876d6e 100644 --- a/src/applications/phame/controller/blog/PhameBlogEditController.php +++ b/src/applications/phame/controller/blog/PhameBlogEditController.php @@ -1,196 +1,11 @@ getViewer(); - $id = $request->getURIData('id'); - - if ($id) { - $blog = id(new PhameBlogQuery()) - ->setViewer($viewer) - ->withIDs(array($id)) - ->requireCapabilities( - array( - PhabricatorPolicyCapability::CAN_EDIT, - )) - ->executeOne(); - if (!$blog) { - return new Aphront404Response(); - } - - $submit_button = pht('Save Changes'); - $page_title = pht('Edit Blog'); - $cancel_uri = $this->getApplicationURI('blog/view/'.$blog->getID().'/'); - - $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs( - $blog->getPHID(), - PhabricatorProjectObjectHasProjectEdgeType::EDGECONST); - $v_projects = array_reverse($v_projects); - $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID( - $blog->getPHID()); - - } else { - $this->requireApplicationCapability( - PhameBlogCreateCapability::CAPABILITY); - - $blog = PhameBlog::initializeNewBlog($viewer); - - $submit_button = pht('Create Blog'); - $page_title = pht('Create Blog'); - $cancel_uri = $this->getApplicationURI(); - $v_projects = array(); - $v_cc = array(); - } - $name = $blog->getName(); - $description = $blog->getDescription(); - $custom_domain = $blog->getDomain(); - $can_view = $blog->getViewPolicy(); - $can_edit = $blog->getEditPolicy(); - - $e_name = true; - $e_custom_domain = null; - $e_view_policy = null; - $validation_exception = null; - if ($request->isFormPost()) { - $name = $request->getStr('name'); - $description = $request->getStr('description'); - $custom_domain = nonempty($request->getStr('custom_domain'), null); - $can_view = $request->getStr('can_view'); - $can_edit = $request->getStr('can_edit'); - $v_projects = $request->getArr('projects'); - $v_cc = $request->getArr('cc'); - - $xactions = array( - id(new PhameBlogTransaction()) - ->setTransactionType(PhameBlogTransaction::TYPE_NAME) - ->setNewValue($name), - id(new PhameBlogTransaction()) - ->setTransactionType(PhameBlogTransaction::TYPE_DESCRIPTION) - ->setNewValue($description), - id(new PhameBlogTransaction()) - ->setTransactionType(PhameBlogTransaction::TYPE_DOMAIN) - ->setNewValue($custom_domain), - id(new PhameBlogTransaction()) - ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY) - ->setNewValue($can_view), - id(new PhameBlogTransaction()) - ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY) - ->setNewValue($can_edit), - id(new PhameBlogTransaction()) - ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS) - ->setNewValue(array('=' => $v_cc)), - ); - - $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST; - $xactions[] = id(new PhameBlogTransaction()) - ->setTransactionType(PhabricatorTransactions::TYPE_EDGE) - ->setMetadataValue('edge:type', $proj_edge_type) - ->setNewValue(array('=' => array_fuse($v_projects))); - - $editor = id(new PhameBlogEditor()) - ->setActor($viewer) - ->setContentSourceFromRequest($request) - ->setContinueOnNoEffect(true); - - try { - $editor->applyTransactions($blog, $xactions); - return id(new AphrontRedirectResponse()) - ->setURI($this->getApplicationURI('blog/view/'.$blog->getID().'/')); - } catch (PhabricatorApplicationTransactionValidationException $ex) { - $validation_exception = $ex; - - $e_name = $validation_exception->getShortMessage( - PhameBlogTransaction::TYPE_NAME); - $e_custom_domain = $validation_exception->getShortMessage( - PhameBlogTransaction::TYPE_DOMAIN); - $e_view_policy = $validation_exception->getShortMessage( - PhabricatorTransactions::TYPE_VIEW_POLICY); - } - } - - $policies = id(new PhabricatorPolicyQuery()) - ->setViewer($viewer) - ->setObject($blog) - ->execute(); - - $form = id(new AphrontFormView()) - ->setUser($viewer) - ->appendChild( - id(new AphrontFormTextControl()) - ->setLabel(pht('Name')) - ->setName('name') - ->setValue($name) - ->setID('blog-name') - ->setError($e_name)) - ->appendChild( - id(new PhabricatorRemarkupControl()) - ->setUser($viewer) - ->setLabel(pht('Description')) - ->setName('description') - ->setValue($description) - ->setID('blog-description') - ->setUser($viewer) - ->setDisableMacros(true)) - ->appendControl( - id(new AphrontFormTokenizerControl()) - ->setLabel(pht('Subscribers')) - ->setName('cc') - ->setValue($v_cc) - ->setUser($viewer) - ->setDatasource(new PhabricatorMetaMTAMailableDatasource())) - ->appendChild( - id(new AphrontFormPolicyControl()) - ->setUser($viewer) - ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) - ->setPolicyObject($blog) - ->setPolicies($policies) - ->setError($e_view_policy) - ->setValue($can_view) - ->setName('can_view')) - ->appendChild( - id(new AphrontFormPolicyControl()) - ->setUser($viewer) - ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) - ->setPolicyObject($blog) - ->setPolicies($policies) - ->setValue($can_edit) - ->setName('can_edit')) - ->appendControl( - id(new AphrontFormTokenizerControl()) - ->setLabel(pht('Projects')) - ->setName('projects') - ->setValue($v_projects) - ->setDatasource(new PhabricatorProjectDatasource())) - ->appendChild( - id(new AphrontFormTextControl()) - ->setLabel(pht('Custom Domain')) - ->setName('custom_domain') - ->setValue($custom_domain) - ->setCaption( - pht('Must include at least one dot (.), e.g. %s', 'blog.example.com')) - ->setError($e_custom_domain)) - ->appendChild( - id(new AphrontFormSubmitControl()) - ->addCancelButton($cancel_uri) - ->setValue($submit_button)); - - $form_box = id(new PHUIObjectBoxView()) - ->setHeaderText($page_title) - ->setValidationException($validation_exception) - ->setForm($form); - - $crumbs = $this->buildApplicationCrumbs(); - $crumbs->addTextCrumb(pht('Blogs'), $this->getApplicationURI('blog/')); - $crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new')); - - return $this->newPage() - ->setTitle($page_title) - ->setCrumbs($crumbs) - ->appendChild( - array( - $form_box, - )); + return id(new PhameBlogEditEngine()) + ->setController($this) + ->buildResponse(); } + } diff --git a/src/applications/phame/editor/PhameBlogEditEngine.php b/src/applications/phame/editor/PhameBlogEditEngine.php new file mode 100644 index 0000000000..292a462ee2 --- /dev/null +++ b/src/applications/phame/editor/PhameBlogEditEngine.php @@ -0,0 +1,85 @@ +getViewer()); + } + + protected function newObjectQuery() { + return id(new PhameBlogQuery()) + ->needProfileImage(true); + } + + protected function getObjectCreateTitleText($object) { + return pht('Create New Blog'); + } + + protected function getObjectEditTitleText($object) { + return pht('Edit %s', $object->getName()); + } + + protected function getObjectEditShortText($object) { + return $object->getName(); + } + + protected function getObjectCreateShortText() { + return pht('Create Blog'); + } + + protected function getObjectViewURI($object) { + return $object->getManageURI(); + } + + protected function buildCustomEditFields($object) { + + return array( + id(new PhabricatorTextEditField()) + ->setKey('name') + ->setLabel(pht('Name')) + ->setDescription(pht('Blog name.')) + ->setConduitDescription(pht('Retitle the blog.')) + ->setConduitTypeDescription(pht('New blog title.')) + ->setTransactionType(PhameBlogTransaction::TYPE_NAME) + ->setValue($object->getName()), + id(new PhabricatorRemarkupEditField()) + ->setKey('description') + ->setLabel(pht('Description')) + ->setDescription(pht('Blog description.')) + ->setConduitDescription(pht('Change the blog description.')) + ->setConduitTypeDescription(pht('New blog description.')) + ->setTransactionType(PhameBlogTransaction::TYPE_DESCRIPTION) + ->setValue($object->getDescription()), + id(new PhabricatorTextEditField()) + ->setKey('domain') + ->setLabel(pht('Custom Domain')) + ->setDescription(pht('Blog domain name.')) + ->setConduitDescription(pht('Change the blog domain.')) + ->setConduitTypeDescription(pht('New blog domain.')) + ->setValue($object->getDomain()) + ->setTransactionType(PhameBlogTransaction::TYPE_DOMAIN), + id(new PhabricatorSelectEditField()) + ->setKey('status') + ->setLabel(pht('Status')) + ->setTransactionType(PhameBlogTransaction::TYPE_STATUS) + ->setIsConduitOnly(true) + ->setOptions(PhameBlog::getStatusNameMap()) + ->setDescription(pht('Active or archived status.')) + ->setConduitDescription(pht('Active or archive the blog.')) + ->setConduitTypeDescription(pht('New blog status constant.')) + ->setValue($object->getStatus()), + ); + } + +} diff --git a/src/applications/phame/storage/PhameBlog.php b/src/applications/phame/storage/PhameBlog.php index 2a2148b798..fb1d47ad5f 100644 --- a/src/applications/phame/storage/PhameBlog.php +++ b/src/applications/phame/storage/PhameBlog.php @@ -193,6 +193,10 @@ final class PhameBlog extends PhameDAO return '/phame/blog/view/'.$this->getID().'/'; } + public function getManageURI() { + return '/phame/blog/manage/'.$this->getID().'/'; + } + public function getProfileImageURI() { return $this->getProfileImageFile()->getBestURI(); }