silverstripe-cms/code/Controllers/CMSPageEditController.php

134 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Controllers;
2017-01-23 03:11:49 +01:00
use Page;
2017-05-25 07:29:32 +02:00
use SilverStripe\Admin\LeftAndMain;
2017-03-23 21:40:48 +01:00
use SilverStripe\CampaignAdmin\AddToCampaignHandler;
2017-01-23 03:11:49 +01:00
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
2016-09-09 01:26:24 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\ORM\FieldType\DBHTMLText;
2017-08-30 07:44:38 +02:00
use SilverStripe\ORM\ValidationResult;
/**
* @package cms
*/
2017-01-25 21:59:25 +01:00
class CMSPageEditController extends CMSMain
{
private static $url_segment = 'pages/edit';
private static $url_rule = '/$Action/$ID/$OtherID';
private static $url_priority = 41;
private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
private static $allowed_actions = [
2017-01-25 21:59:25 +01:00
'AddToCampaignForm',
];
2017-01-25 21:59:25 +01:00
public function getClientConfig()
{
return ArrayLib::array_merge_recursive(parent::getClientConfig(), [
2017-01-25 21:59:25 +01:00
'form' => [
'AddToCampaignForm' => [
2017-09-05 04:38:38 +02:00
'schemaUrl' => $this->Link('schema/AddToCampaignForm'),
2017-01-25 21:59:25 +01:00
],
2017-05-25 07:29:32 +02:00
'editorInternalLink' => [
'schemaUrl' => LeftAndMain::singleton()
->Link('methodSchema/Modals/editorInternalLink'),
],
2017-09-05 04:38:38 +02:00
'editorAnchorLink' => [
'schemaUrl' => LeftAndMain::singleton()
->Link('methodSchema/Modals/editorAnchorLink/:pageid'),
],
2017-01-25 21:59:25 +01:00
],
]);
}
/**
* Action handler for adding pages to a campaign
*
* @param array $data
* @param Form $form
* @return DBHTMLText|HTTPResponse
*/
public function addtocampaign($data, $form)
{
$id = $data['ID'];
$record = \Page::get()->byID($id);
$handler = AddToCampaignHandler::create($this, $record);
2017-08-30 07:44:38 +02:00
$results = $handler->addToCampaign($record, $data);
2017-01-25 21:59:25 +01:00
if (is_null($results)) {
return null;
}
if ($this->getSchemaRequested()) {
// Send extra "message" data with schema response
$extraData = ['message' => $results];
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
}
return $results;
}
/**
* Url handler for add to campaign form
*
* @param 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 = SiteTree::get()->byID($id);
if (!$record) {
$this->httpError(404, _t(
2017-08-30 07:44:38 +02:00
__CLASS__ . '.ErrorNotFound',
2017-01-25 21:59:25 +01:00
'That {Type} couldn\'t be found',
'',
['Type' => Page::singleton()->i18n_singular_name()]
));
return null;
}
if (!$record->canView()) {
$this->httpError(403, _t(
2017-08-30 07:44:38 +02:00
__CLASS__.'.ErrorItemPermissionDenied',
2017-01-25 21:59:25 +01:00
'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign',
'',
['ObjectTitle' => Page::singleton()->i18n_singular_name()]
));
return null;
}
$handler = AddToCampaignHandler::create($this, $record);
2017-08-30 07:44:38 +02:00
$form = $handler->Form($record);
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id) {
$schemaId = Controller::join_links($this->Link('schema/AddToCampaignForm'), $id);
return $this->getSchemaResponse($schemaId, $form, $errors);
});
return $form;
2017-01-25 21:59:25 +01:00
}
}