Completed
Pull Request — master (#1587)
by Ingo
05:02
created

CMSPageEditController::getClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\CMS\Controllers;
4
5
use Convert;
6
use SilverStripe\Admin\AddToCampaignHandler;
7
use SS_HTTPResponse;
8
9
/**
10
 * @package cms
11
 */
12
class CMSPageEditController extends CMSMain {
0 ignored issues
show
Bug introduced by
There is one abstract method isCurrentPage in this class; you could implement it, or declare this class as abstract.
Loading history...
13
14
	private static $url_segment = 'pages/edit';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
16
	private static $url_rule = '/$Action/$ID/$OtherID';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
17
18
	private static $url_priority = 41;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
19
20
	private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
21
22
	private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
23
		'AddToCampaignForm',
24
	);
25
26
	public function getClientConfig()
27
	{
28
		return array_merge( parent::getClientConfig(), [
29
			'form' => [
30
				'AddToCampaignForm' => [
31
					'schemaUrl' => $this->Link('schema/AddToCampaignForm')
32
				],
33
			],
34
		]);
35
	}
36
37
	/**
38
	 * Action handler for adding pages to a campaign
39
	 *
40
	 * @param array $data
41
	 * @param Form $form
42
	 * @return DBHTMLText|SS_HTTPResponse
43
	 */
44
	public function addtocampaign($data, $form)
45
	{
46
		$id = $data['ID'];
47
		$record = \Page::get()->byID($id);
48
49
		$handler = AddToCampaignHandler::create($this, $record);
50
		$results = $handler->addToCampaign($record, $data['Campaign']);
51
		if (!is_null($results)) {
52
			$request = $this->getRequest();
53
			if($request->getHeader('X-Formschema-Request')) {
54
				$handler->setShowTitle(false);
55
				$data = $this->getSchemaForForm($handler->Form($record));
56
				$data['message'] = $results;
57
58
				$response = new SS_HTTPResponse(Convert::raw2json($data));
59
				$response->addHeader('Content-Type', 'application/json');
60
				return $response;
61
			}
62
			return $results;
63
		}
64
	}
65
66
	/**
67
	 * Url handler for add to campaign form
68
	 *
69
	 * @param SS_HTTPRequest $request
70
	 * @return Form
71
	 */
72
	public function AddToCampaignForm($request)
73
	{
74
		// Get ID either from posted back value, or url parameter
75
		$id = $request->param('ID') ?: $request->postVar('ID');
76
		return $this->getAddToCampaignForm($id);
77
	}
78
79
	/**
80
	 * @param int $id
81
	 * @return Form
82
	 */
83
	public function getAddToCampaignForm($id)
84
	{
85
		// Get record-specific fields
86
		$record = \Page::get()->byID($id);
87
88
		if (!$record) {
89
			$this->httpError(404, _t(
90
				'AssetAdmin.ErrorNotFound',
91
				'That {Type} couldn\'t be found',
92
				'',
93
				['Type' => _t('SiteTree.SINGULARNAME')]
0 ignored issues
show
Documentation introduced by
array('Type' => _t('SiteTree.SINGULARNAME')) is of type array<string,string,{"Type":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
			));
95
			return null;
96
		}
97
		if (!$record->canView()) {
98
			$this->httpError(403, _t(
99
				'AssetAdmin.ErrorItemPermissionDenied',
100
				'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign',
101
				'',
102
				['ObjectTitle' => _t('SiteTree.SINGULARNAME')]
0 ignored issues
show
Documentation introduced by
array('ObjectTitle' => _...iteTree.SINGULARNAME')) is of type array<string,string,{"ObjectTitle":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
			));
104
			return null;
105
		}
106
107
		$handler = AddToCampaignHandler::create($this, $record);
108
		$handler->setShowTitle(false);
109
		return $handler->Form($record);
110
	}
111
112
}
113