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

CMSPageEditController::AddToCampaignForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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
				$data = $this->getSchemaForForm($handler->Form($record));
55
				$data['message'] = $results;
56
57
				$response = new SS_HTTPResponse(Convert::raw2json($data));
58
				$response->addHeader('Content-Type', 'application/json');
59
				return $response;
60
			}
61
			return $results;
62
		}
63
	}
64
65
	/**
66
	 * Url handler for add to campaign form
67
	 *
68
	 * @param SS_HTTPRequest $request
69
	 * @return Form
70
	 */
71
	public function AddToCampaignForm($request)
72
	{
73
		// Get ID either from posted back value, or url parameter
74
		$id = $request->param('ID') ?: $request->postVar('ID');
75
		return $this->getAddToCampaignForm($id);
76
	}
77
78
	/**
79
	 * @param int $id
80
	 * @return Form
81
	 */
82
	public function getAddToCampaignForm($id)
83
	{
84
		// Get record-specific fields
85
		$record = \Page::get()->byID($id);
86
87
		if (!$record) {
88
			$this->httpError(404, _t(
89
				'AssetAdmin.ErrorNotFound',
90
				'That {Type} couldn\'t be found',
91
				'',
92
				['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...
93
			));
94
			return null;
95
		}
96
		if (!$record->canView()) {
97
			$this->httpError(403, _t(
98
				'AssetAdmin.ErrorItemPermissionDenied',
99
				'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign',
100
				'',
101
				['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...
102
			));
103
			return null;
104
		}
105
106
		$handler = AddToCampaignHandler::create($this, $record);
107
		return $handler->Form($record);
108
	}
109
110
}
111