posting::init_builder()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 3
b 0
f 0
nc 5
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 30
rs 9.6111
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.submit_post_modify_sql_data'			=> 'modify_sql_data',
61
			'core.posting_modify_submit_post_after'		=> array(array('save_fields'), array('set_redirect_url')),
62
			'core.topic_review_modify_post_list'		=> 'set_content_post_id',
63
			'core.topic_review_modify_row'				=> 'modify_topic_review',
64
			'core.posting_modify_submit_post_before'	=> 'force_visibility',
65
			'core.posting_modify_template_vars'			=> array(array('build_form'), array('get_form', -100)),
66
			'core.page_footer'							=> 'update_navbar',
67
		);
68
	}
69
70
	/**
71
	 * @param \phpbb\event\data $event
72
	 * @return void
73
	 */
74
	public function init_builder(\phpbb\event\data $event)
75
	{
76
		$type_info = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
77
78
		$this->content_type = (string) $type_info[0];
79
		$this->content_langname = $type_info[1];
80
81
		// are we adding/editing a content type post?
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_sql_data(\phpbb\event\data $event)
134
	{
135
		if ($this->build_content)
136
		{
137
			$sql_data = $event['sql_data'];
138
			$this->builder->modify_sql_data($sql_data[TOPICS_TABLE]['sql']);
139
140
			$event['sql_data'] = $sql_data;
141
		}
142
	}
143
144
	/**
145
	 * @param \phpbb\event\data $event
146
	 * @return void
147
	 */
148
	public function set_content_post_id(\phpbb\event\data $event)
149
	{
150
		if ($this->content_type)
151
		{
152
			$post_list = (array) $event['post_list'];
153
			$this->content_post_id = array_pop($post_list);
154
		}
155
	}
156
157
	/**
158
	 * @param \phpbb\event\data $event
159
	 * @return void
160
	 */
161
	public function modify_topic_review(\phpbb\event\data $event)
162
	{
163
		if ($this->content_type && $event['row']['post_id'] == $this->content_post_id)
164
		{
165
			$post_row = (array) $event['post_row'];
166
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
167
			$event['post_row'] = $post_row;
168
			unset($post_row);
169
		}
170
	}
171
172
	/**
173
	 * @param \phpbb\event\data $event
174
	 * @return void
175
	 */
176
	public function set_redirect_url(\phpbb\event\data $event)
177
	{
178
		// are we posting/editing a content type post?
179
		if ($this->build_content && ($redirect_url = $this->builder->get_redirect_url()) !== '')
180
		{
181
			$event['redirect_url'] = str_replace('&amp;', '&', urldecode($redirect_url));
182
		}
183
	}
184
185
	/**
186
	 * @param \phpbb\event\data $event
187
	 * @return void
188
	 */
189
	public function save_fields(\phpbb\event\data $event)
190
	{
191
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
192
		{
193
			$this->builder->save_db_fields(array_merge((array) $event['post_data'], (array) $event['data']));
194
		}
195
	}
196
197
	/**
198
	 * @param \phpbb\event\data $event
199
	 * @return void
200
	 */
201
	public function build_form(\phpbb\event\data $event)
202
	{
203
		if ($this->build_content)
204
		{
205
			$post_data = (array) $event['post_data'];
206
			$page_data = (array) $event['page_data'];
207
			$this->builder->generate_form((int) $event['topic_id'], $post_data, $page_data);
208
		}
209
	}
210
211
	/**
212
	 * @param \phpbb\event\data $event
213
	 * @return void
214
	 */
215
	public function get_form(\phpbb\event\data $event)
216
	{
217
		if ($this->build_content)
218
		{
219
			$post_data = (array) $event['post_data'];
220
			$page_data = (array) $event['page_data'];
221
222
			$post_data['TOPIC_URL'] = './';
223
			$page_data['SITEMAKER_FORM'] = $this->builder->get_form();
224
225
			if ($event['preview'] && $this->content_type)
226
			{
227
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
228
			}
229
230
			$event['page_data'] = $page_data;
231
			unset($post_data, $page_data);
232
		}
233
	}
234
235
	/**
236
	 * @return void
237
	 */
238
	public function update_navbar()
239
	{
240
		if ($this->content_type)
241
		{
242
			// remove 'Forum' nav added by Sitemaker when a startpage is specified
243
			if ($this->template->find_key_index('navlinks', 1))
244
			{
245
				$this->template->alter_block_array('navlinks', array(), 1, 'delete');
246
			}
247
248
			// update label & url that currently points to the forum to now point to the content type
249
			$this->template->alter_block_array('navlinks', array(
250
				'FORUM_NAME'	=> $this->content_langname,
251
				'U_VIEW_FORUM'	=> $this->helper->route('blitze_content_type', array('type' => $this->content_type)),
252
			), 0, 'change');
253
		}
254
	}
255
}
256