From c46490ea3a6b63788d0bf037d0af13b22a2da356 Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Mon, 22 Aug 2016 15:16:27 +1200 Subject: [PATCH 1/4] ErrorPage no longer generates HTML response for ajax requests --- code/Controllers/ErrorPageControllerExtension.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/Controllers/ErrorPageControllerExtension.php b/code/Controllers/ErrorPageControllerExtension.php index 2f5fce7f..26715d82 100644 --- a/code/Controllers/ErrorPageControllerExtension.php +++ b/code/Controllers/ErrorPageControllerExtension.php @@ -24,6 +24,9 @@ class ErrorPageControllerExtension extends Extension { * @throws SS_HTTPResponse_Exception */ public function onBeforeHTTPError($statusCode, $request) { + if (\Director::is_ajax()) { + return; + } $response = ErrorPage::response_for($statusCode); if($response) { throw new SS_HTTPResponse_Exception($response, $statusCode); From c411c500a5325af1a5d07cb4e7d2106b4598f91d Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Tue, 23 Aug 2016 12:39:01 +1200 Subject: [PATCH 2/4] Fix for pages admin add to campaign modal --- code/Controllers/CMSMain.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index e370b881..7f55d338 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -102,6 +102,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr private static $page_length = 15; private static $allowed_actions = array( + 'AddToCampaignForm', 'archive', 'deleteitems', 'DeleteItemsForm', @@ -1309,6 +1310,26 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr return $this->getResponseNegotiator()->respond($this->getRequest()); } + /** + * Action handler for adding pages to a campaign + * + * @param SS_HTTPRequest + * @return DBHTMLText|SS_HTTPResponse + */ + public function AddToCampaignForm($request) { + $data = $request->postVars(); + + if (!$data['Campaign']) { + $this->httpError(400, _t( + 'AddToCampaign.ErrorCampaignNotSelected', + 'There was no campaign selected to be added to' + )); + return null; + } + $handler = AddToCampaignHandler::create($this, $data); + return $handler->handle(); + } + /** * Action handler for adding pages to a campaign * @@ -1317,7 +1338,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr * @return DBHTMLText|SS_HTTPResponse */ public function addtocampaign($data, $form) { - $handler = AddToCampaignHandler::create($form, $data); + $handler = AddToCampaignHandler::create($this, $data); return $handler->handle(); } From 93d7197aad6abe1bd534ff5e90e46d255ebc6124 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 25 Aug 2016 21:30:53 +1200 Subject: [PATCH 3/4] Support for React-driven "add to campaign" --- code/Controllers/CMSMain.php | 34 -------- code/Controllers/CMSPageEditController.php | 94 ++++++++++++++++++++++ 2 files changed, 94 insertions(+), 34 deletions(-) diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index 7f55d338..fa889664 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -22,7 +22,6 @@ use SilverStripe\Admin\AdminRootController; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Admin\CMSBatchActionHandler; use SilverStripe\Admin\CMSPreviewable; -use SilverStripe\Admin\AddToCampaignHandler; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\RedirectorPage; use SilverStripe\CMS\Model\CurrentPageIdentifier; @@ -102,7 +101,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr private static $page_length = 15; private static $allowed_actions = array( - 'AddToCampaignForm', 'archive', 'deleteitems', 'DeleteItemsForm', @@ -1310,38 +1308,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr return $this->getResponseNegotiator()->respond($this->getRequest()); } - /** - * Action handler for adding pages to a campaign - * - * @param SS_HTTPRequest - * @return DBHTMLText|SS_HTTPResponse - */ - public function AddToCampaignForm($request) { - $data = $request->postVars(); - - if (!$data['Campaign']) { - $this->httpError(400, _t( - 'AddToCampaign.ErrorCampaignNotSelected', - 'There was no campaign selected to be added to' - )); - return null; - } - $handler = AddToCampaignHandler::create($this, $data); - return $handler->handle(); - } - - /** - * Action handler for adding pages to a campaign - * - * @param array $data - * @param Form $form - * @return DBHTMLText|SS_HTTPResponse - */ - public function addtocampaign($data, $form) { - $handler = AddToCampaignHandler::create($this, $data); - return $handler->handle(); - } - /** * Batch Actions Handler */ diff --git a/code/Controllers/CMSPageEditController.php b/code/Controllers/CMSPageEditController.php index d05cee14..cd056597 100644 --- a/code/Controllers/CMSPageEditController.php +++ b/code/Controllers/CMSPageEditController.php @@ -2,6 +2,10 @@ namespace SilverStripe\CMS\Controllers; +use Convert; +use SilverStripe\Admin\AddToCampaignHandler; +use SS_HTTPResponse; + /** * @package cms */ @@ -15,4 +19,94 @@ class CMSPageEditController extends CMSMain { private static $required_permission_codes = 'CMS_ACCESS_CMSMain'; + private static $allowed_actions = array( + 'AddToCampaignForm', + ); + + public function getClientConfig() + { + return array_merge( parent::getClientConfig(), [ + 'form' => [ + 'AddToCampaignForm' => [ + 'schemaUrl' => $this->Link('schema/AddToCampaignForm') + ], + ], + ]); + } + + /** + * Action handler for adding pages to a campaign + * + * @param array $data + * @param Form $form + * @return DBHTMLText|SS_HTTPResponse + */ + public function addtocampaign($data, $form) + { + $id = $data['ID']; + $record = \Page::get()->byID($id); + + $handler = AddToCampaignHandler::create($this, $record); + $results = $handler->addToCampaign($record, $data['Campaign']); + if (!is_null($results)) { + $request = $this->getRequest(); + if($request->getHeader('X-Formschema-Request')) { + $handler->setShowTitle(false); + $data = $this->getSchemaForForm($handler->Form($record)); + $data['message'] = $results; + + $response = new SS_HTTPResponse(Convert::raw2json($data)); + $response->addHeader('Content-Type', 'application/json'); + return $response; + } + return $results; + } + } + + /** + * Url handler for add to campaign form + * + * @param SS_HTTPRequest $request + * @return Form + */ + public function AddToCampaignForm($request) + { + // Get ID either from posted back value, or url parameter + $id = $request->param('ID') ?: $request->postVar('ID'); + return $this->getAddToCampaignForm($id); + } + + /** + * @param int $id + * @return Form + */ + public function getAddToCampaignForm($id) + { + // Get record-specific fields + $record = \Page::get()->byID($id); + + if (!$record) { + $this->httpError(404, _t( + 'AssetAdmin.ErrorNotFound', + 'That {Type} couldn\'t be found', + '', + ['Type' => _t('SiteTree.SINGULARNAME')] + )); + return null; + } + if (!$record->canView()) { + $this->httpError(403, _t( + 'AssetAdmin.ErrorItemPermissionDenied', + 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', + '', + ['ObjectTitle' => _t('SiteTree.SINGULARNAME')] + )); + return null; + } + + $handler = AddToCampaignHandler::create($this, $record); + $handler->setShowTitle(false); + return $handler->Form($record); + } + } From 40904686a4749c28b4ead3fffffd9b6f302075a5 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sun, 28 Aug 2016 13:49:25 +1200 Subject: [PATCH 4/4] Removed AddToCampaignHandler->setShowTitle() It was used to support different display variations in admin/assets and admin/pages, while those display variations should've been removed in the first place (and have been now). --- code/Controllers/CMSPageEditController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/code/Controllers/CMSPageEditController.php b/code/Controllers/CMSPageEditController.php index cd056597..bcbcdb69 100644 --- a/code/Controllers/CMSPageEditController.php +++ b/code/Controllers/CMSPageEditController.php @@ -51,7 +51,6 @@ class CMSPageEditController extends CMSMain { if (!is_null($results)) { $request = $this->getRequest(); if($request->getHeader('X-Formschema-Request')) { - $handler->setShowTitle(false); $data = $this->getSchemaForForm($handler->Form($record)); $data['message'] = $results; @@ -105,7 +104,6 @@ class CMSPageEditController extends CMSMain { } $handler = AddToCampaignHandler::create($this, $record); - $handler->setShowTitle(false); return $handler->Form($record); }