Passed
Push — release-3.0.0 ( 5e25ca...6e018a )
by Daniel
04:45
created

posting::modify_posting_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class posting implements EventSubscriberInterface
15
{
16
	/** @var \phpbb\controller\helper */
17
	protected $helper;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \blitze\content\services\form\builder */
23
	protected $builder;
24
25
	/** @var string */
26
	protected $content_langname = '';
27
28
	/** @var string */
29
	protected $content_type = '';
30
31
	/** @var bool */
32
	protected $build_content = false;
33
34
	/** @var int */
35
	protected $content_post_id = 0;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \phpbb\controller\helper					$helper			Controller helper class
41
	 * @param \phpbb\template\template					$template		Template object
42
	 * @param \blitze\content\services\form\builder		$builder		Form builder object
43
	*/
44
	public function __construct(\phpbb\controller\helper $helper, \phpbb\template\template $template, \blitze\content\services\form\builder $builder)
45
	{
46
		$this->helper = $helper;
47
		$this->template = $template;
48
		$this->builder = $builder;
49
	}
50
51
	/**
52
	 * @return array
53
	 */
54
	public static function getSubscribedEvents()
55
	{
56
		return array(
57
			'core.modify_posting_auth'					=> 'init_builder',
58
			'core.posting_modify_message_text'			=> 'build_message',
59
			'core.posting_modify_submission_errors'		=> 'show_errors',
60
			'core.modify_submit_post_data'				=> 'modify_posting_data',
61
			'core.submit_post_modify_sql_data'			=> 'modify_sql_data',
62
			'core.posting_modify_submit_post_after'		=> array(array('save_fields'), array('set_redirect_url')),
63
			'core.topic_review_modify_post_list'		=> 'set_content_post_id',
64
			'core.topic_review_modify_row'				=> 'modify_topic_review',
65
			'core.posting_modify_submit_post_before'	=> 'force_visibility',
66
			'core.posting_modify_template_vars'			=> 'build_form',
67
			'core.page_footer'							=> 'update_navbar',
68
		);
69
	}
70
71
	/**
72
	 * @param \phpbb\event\data $event
73
	 * @return void
74
	 */
75
	public function init_builder(\phpbb\event\data $event)
76
	{
77
		$type_info = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
78
79
		$this->content_type = (string) $type_info[0];
80
		$this->content_langname = $type_info[1];
81
82
		if (empty($event['post_data']['topic_first_post_id']) || $event['post_data']['topic_first_post_id'] == $event['post_id'])
83
		{
84
			$this->build_content = ($this->content_type && $event['mode'] !== 'reply') ? true : false;
85
		}
86
	}
87
88
	/**
89
	 * @param \phpbb\event\data $event
90
	 * @return void
91
	 */
92
	public function build_message(\phpbb\event\data $event)
93
	{
94
		if ($this->build_content)
95
		{
96
			$message_parser = $event['message_parser'];
97
			$message_parser->message = $this->builder->generate_message();
98
			$event['message_parser'] = $message_parser;
99
		}
100
	}
101
102
	/**
103
	 * @param \phpbb\event\data $event
104
	 * @return void
105
	 */
106
	public function show_errors(\phpbb\event\data $event)
107
	{
108
		if ($this->build_content)
109
		{
110
			$event['error'] = $this->builder->get_errors();
111
		}
112
	}
113
114
	/**
115
	 * @param \phpbb\event\data $event
116
	 * @return void
117
	 */
118
	public function force_visibility(\phpbb\event\data $event)
119
	{
120
		if ($this->build_content)
121
		{
122
			$data = (array) $event['data'];
123
			$this->builder->force_visibility($event['mode'], $data);
124
125
			$event['data'] = $data;
126
		}
127
	}
128
129
	/**
130
	 * @param \phpbb\event\data $event
131
	 * @return void
132
	 */
133
	public function modify_posting_data(\phpbb\event\data $event)
134
	{
135
		if ($this->build_content && $event['mode'] === 'post')
136
		{
137
			$data = $event['data'];
138
			$this->builder->modify_posting_data($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of blitze\content\services\...::modify_posting_data() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
			$this->builder->modify_posting_data(/** @scrutinizer ignore-type */ $data);
Loading history...
139
			$event['data'] = $data;
140
		}
141
	}
142
143
	/**
144
	 * @param \phpbb\event\data $event
145
	 * @return void
146
	 */
147
	public function modify_sql_data(\phpbb\event\data $event)
148
	{
149
		if ($this->build_content)
150
		{
151
			$sql_data = $event['sql_data'];
152
			$this->builder->modify_sql_data($sql_data[TOPICS_TABLE]['sql']);
153
			$event['sql_data'] = $sql_data;
154
		}
155
	}
156
157
	/**
158
	 * @param \phpbb\event\data $event
159
	 * @return void
160
	 */
161
	public function set_content_post_id(\phpbb\event\data $event)
162
	{
163
		if ($this->content_type)
164
		{
165
			$post_list = (array) $event['post_list'];
166
			$this->content_post_id = array_pop($post_list);
167
		}
168
	}
169
170
	/**
171
	 * @param \phpbb\event\data $event
172
	 * @return void
173
	 */
174
	public function modify_topic_review(\phpbb\event\data $event)
175
	{
176
		if ($this->content_type && $event['row']['post_id'] == $this->content_post_id)
177
		{
178
			$post_row = (array) $event['post_row'];
179
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
180
			$event['post_row'] = $post_row;
181
			unset($post_row);
182
		}
183
	}
184
185
	/**
186
	 * @param \phpbb\event\data $event
187
	 * @return void
188
	 */
189
	public function set_redirect_url(\phpbb\event\data $event)
190
	{
191
		if ($this->content_type)
192
		{
193
			if ($this->build_content)
194
			{
195
				$event['redirect_url'] = $this->builder->get_cp_url();
196
			}
197
			else
198
			{
199
				$topic_url = $this->builder->get_post_url($this->content_type, (array) $event['post_data']);
200
201
				$post_id = $event['data']['post_id'];
202
				if ($post_id && $post_id != $event['post_data']['topic_first_post_id'])
203
				{
204
					$topic_url .= "?p=$post_id#p$post_id";
205
				}
206
207
				$event['redirect_url'] = $topic_url;
208
			}
209
		}
210
	}
211
212
	/**
213
	 * @param \phpbb\event\data $event
214
	 * @return void
215
	 */
216
	public function save_fields(\phpbb\event\data $event)
217
	{
218
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
219
		{
220
			$this->builder->save_db_fields(array_merge((array) $event['post_data'], (array) $event['data']));
221
		}
222
	}
223
224
	/**
225
	 * @param \phpbb\event\data $event
226
	 * @return void
227
	 */
228
	public function build_form(\phpbb\event\data $event)
229
	{
230
		if ($this->build_content)
231
		{
232
			$post_data = (array) $event['post_data'];
233
			$page_data = (array) $event['page_data'];
234
235
			$post_data['TOPIC_URL'] = './';
236
			$page_data['SITEMAKER_FORM'] = $this->builder->generate_form((int) $event['topic_id'], $post_data, $page_data);
237
238
			if ($event['preview'] && $this->content_type)
239
			{
240
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
241
			}
242
243
			$event['page_data'] = $page_data;
244
			unset($post_data, $page_data);
245
		}
246
	}
247
248
	/**
249
	 * @return void
250
	 */
251
	public function update_navbar()
252
	{
253
		if ($this->content_type)
254
		{
255
			// remove 'Forum' nav added by Sitemaker when a startpage is specified
256
			if ($this->template->find_key_index('navlinks', 1))
257
			{
258
				$this->template->alter_block_array('navlinks', array(), 1, 'delete');
259
			}
260
261
			// update label & url that currently points to the forum to now point to the content type
262
			$this->template->alter_block_array('navlinks', array(
263
				'FORUM_NAME'	=> $this->content_langname,
264
				'U_VIEW_FORUM'	=> $this->helper->route('blitze_content_type', array('type' => $this->content_type)),
265
			), 0, 'change');
266
		}
267
	}
268
}
269