Completed
Push — develop ( 613db1...73b4e9 )
by Daniel
05:02
created

posting   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 1
dl 0
loc 237
ccs 0
cts 138
cp 0
rs 9.2
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSubscribedEvents() 0 15 1
B init_builder() 0 10 5
A build_message() 0 9 2
A show_errors() 0 7 2
A force_visibility() 0 10 2
A modify_sql_data() 0 9 2
A set_content_post_id() 0 7 2
A modify_topic_review() 0 10 3
B set_redirect_url() 0 22 4
A save_fields() 0 7 3
A build_form() 0 19 4
A update_navbar() 0 17 3
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|bool */
29
	protected $content_type = false;
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'			=> 'build_form',
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
		list($this->content_type, $this->content_langname) = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
77
78
		$topic_first_post_id = $event['post_data']['topic_first_post_id'];
79
		if (!$topic_first_post_id || $topic_first_post_id == $event['post_id'])
80
		{
81
			$this->build_content = ($this->content_type && $event['mode'] !== 'reply') ? true : false;
82
		}
83
	}
84
85
	/**
86
	 * @param \phpbb\event\data $event
87
	 * @return void
88
	 */
89
	public function build_message(\phpbb\event\data $event)
90
	{
91
		if ($this->build_content)
92
		{
93
			$message_parser = $event['message_parser'];
94
			$message_parser->message = $this->builder->generate_message();
95
			$event['message_parser'] = $message_parser;
96
		}
97
	}
98
99
	/**
100
	 * @param \phpbb\event\data $event
101
	 * @return void
102
	 */
103
	public function show_errors(\phpbb\event\data $event)
104
	{
105
		if ($this->build_content)
106
		{
107
			$event['error'] = $this->builder->get_errors();
108
		}
109
	}
110
111
	/**
112
	 * @param \phpbb\event\data $event
113
	 * @return void
114
	 */
115
	public function force_visibility(\phpbb\event\data $event)
116
	{
117
		if ($this->build_content)
118
		{
119
			$data = $event['data'];
120
			$this->builder->force_visibility($event['mode'], $data);
121
122
			$event['data'] = $data;
123
		}
124
	}
125
126
	/**
127
	 * @param \phpbb\event\data $event
128
	 * @return void
129
	 */
130
	public function modify_sql_data(\phpbb\event\data $event)
131
	{
132
		if ($this->build_content)
133
		{
134
			$sql_data = $event['sql_data'];
135
			$this->builder->modify_posting_data($sql_data[TOPICS_TABLE]['sql']);
136
			$event['sql_data'] = $sql_data;
137
		}
138
	}
139
140
	/**
141
	 * @param \phpbb\event\data $event
142
	 * @return void
143
	 */
144
	public function set_content_post_id(\phpbb\event\data $event)
145
	{
146
		if ($this->content_type)
147
		{
148
			$this->content_post_id = array_pop($event['post_list']);
149
		}
150
	}
151
152
	/**
153
	 * @param \phpbb\event\data $event
154
	 * @return void
155
	 */
156
	public function modify_topic_review(\phpbb\event\data $event)
157
	{
158
		if ($this->content_type && $event['row']['post_id'] == $this->content_post_id)
159
		{
160
			$post_row = $event['post_row'];
161
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
162
			$event['post_row'] = $post_row;
163
			unset($post_row);
164
		}
165
	}
166
167
	/**
168
	 * @param \phpbb\event\data $event
169
	 * @return void
170
	 */
171
	public function set_redirect_url(\phpbb\event\data $event)
172
	{
173
		if ($this->content_type)
174
		{
175
			if ($this->build_content)
176
			{
177
				$event['redirect_url'] = $this->builder->get_cp_url();
178
			}
179
			else
180
			{
181
				$topic_url = $this->builder->get_post_url($this->content_type, $event['post_data']);
182
183
				$post_id = $event['post_id'];
184
				if ($post_id != $event['post_data']['topic_first_post_id'])
185
				{
186
					$topic_url .= "?p=$post_id#p$post_id";
187
				}
188
189
				$event['redirect_url'] = $topic_url;
190
			}
191
		}
192
	}
193
194
	/**
195
	 * @param \phpbb\event\data $event
196
	 * @return void
197
	 */
198
	public function save_fields(\phpbb\event\data $event)
199
	{
200
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
201
		{
202
			$this->builder->save_db_fields(array_merge($event['post_data'], $event['data']));
203
		}
204
	}
205
206
	/**
207
	 * @param \phpbb\event\data $event
208
	 * @return void
209
	 */
210
	public function build_form(\phpbb\event\data $event)
211
	{
212
		if ($this->build_content)
213
		{
214
			$post_data = $event['post_data'];
215
			$page_data = $event['page_data'];
216
217
			$post_data['TOPIC_URL'] = './';
218
			$page_data['SITEMAKER_FORM'] = $this->builder->generate_form($event['topic_id'], $post_data, $page_data);
219
220
			if ($event['preview'] && $this->content_type)
221
			{
222
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
223
			}
224
225
			$event['page_data'] = $page_data;
226
			unset($post_data, $page_data);
227
		}
228
	}
229
230
	/**
231
	 * @return void
232
	 */
233
	public function update_navbar()
234
	{
235
		if ($this->content_type)
236
		{
237
			// remove 'Forum' nav added by Sitemaker when a startpage is specified
238
			if ($this->template->find_key_index('navlinks', 0))
239
			{
240
				$this->template->alter_block_array('navlinks', array(), 0, 'delete');
241
			}
242
243
			// update label & url that currently points to the forum to now point to the content type
244
			$this->template->alter_block_array('navlinks', array(
245
				'FORUM_NAME'	=> $this->content_langname,
246
				'U_VIEW_FORUM'	=> $this->helper->route('blitze_content_type', array('type' => $this->content_type)),
247
			), 0, 'change');
248
		}
249
	}
250
}
251