Completed
Push — develop ( 4a255f...c77227 )
by Daniel
07:47
created

posting::show_errors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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 \blitze\content\services\form\builder */
17
	protected $builder;
18
19
	/** @var string|bool */
20
	protected $content_type = false;
21
22
	/** @var bool */
23
	protected $build_content = false;
24
25
	/** @var int */
26
	protected $content_post_id = 0;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param \blitze\content\services\form\builder		$builder		Form builder object
32
	*/
33
	public function __construct(\blitze\content\services\form\builder $builder)
34
	{
35
		$this->builder = $builder;
36
	}
37
38
	/**
39
	 * @return array
40
	 */
41
	public static function getSubscribedEvents()
42
	{
43
		return array(
44
			'core.modify_posting_auth'					=> 'init_builder',
45
			'core.posting_modify_message_text'			=> 'build_message',
46
			'core.posting_modify_submission_errors'		=> 'show_errors',
47
			'core.submit_post_modify_sql_data'			=> 'modify_sql_data',
48
			'core.posting_modify_submit_post_after'		=> array(array('save_fields'), array('set_redirect_url')),
49
			'core.topic_review_modify_post_list'		=> 'set_content_post_id',
50
			'core.topic_review_modify_row'				=> 'modify_topic_review',
51
			'core.posting_modify_submit_post_before'	=> 'force_visibility',
52
			'core.posting_modify_template_vars'			=> 'build_form',
53
		);
54
	}
55
56
	/**
57
	 * @param \phpbb\event\data $event
58
	 * @return void
59
	 */
60
	public function init_builder(\phpbb\event\data $event)
61
	{
62
		$this->content_type = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
63
64
		$topic_first_post_id = $event['post_data']['topic_first_post_id'];
65
		if (!$topic_first_post_id || $topic_first_post_id == $event['post_id'])
66
		{
67
			$this->build_content = ($this->content_type && $event['mode'] !== 'reply') ? true : false;
68
		}
69
	}
70
71
	/**
72
	 * @param \phpbb\event\data $event
73
	 * @return void
74
	 */
75
	public function build_message(\phpbb\event\data $event)
76
	{
77
		if ($this->build_content)
78
		{
79
			$message_parser = $event['message_parser'];
80
			$message_parser->message = $this->builder->generate_message();
81
			$event['message_parser'] = $message_parser;
82
		}
83
	}
84
85
	/**
86
	 * @param \phpbb\event\data $event
87
	 * @return void
88
	 */
89
	public function show_errors(\phpbb\event\data $event)
90
	{
91
		if ($this->build_content)
92
		{
93
			$event['error'] = $this->builder->get_errors();
94
		}
95
	}
96
97
	/**
98
	 * @param \phpbb\event\data $event
99
	 * @return void
100
	 */
101
	public function force_visibility(\phpbb\event\data $event)
102
	{
103
		if ($this->build_content)
104
		{
105
			$data = $event['data'];
106
			$this->builder->force_visibility($event['mode'], $data);
107
108
			$event['data'] = $data;
109
		}
110
	}
111
112
	/**
113
	 * @param \phpbb\event\data $event
114
	 * @return void
115
	 */
116
	public function modify_sql_data(\phpbb\event\data $event)
117
	{
118
		if ($this->build_content)
119
		{
120
			$sql_data = $event['sql_data'];
121
			$this->builder->modify_posting_data($sql_data[TOPICS_TABLE]['sql']);
122
			$event['sql_data'] = $sql_data;
123
		}
124
	}
125
126
	/**
127
	 * @param \phpbb\event\data $event
128
	 * @return void
129
	 */
130
	public function set_content_post_id(\phpbb\event\data $event)
131
	{
132
		if ($this->content_type)
133
		{
134
			$this->content_post_id = array_pop($event['post_list']);
135
		}
136
	}
137
138
	/**
139
	 * @param \phpbb\event\data $event
140
	 * @return void
141
	 */
142
	public function modify_topic_review(\phpbb\event\data $event)
143
	{
144
		if ($this->content_type && $event['row']['post_id'] == $this->content_post_id)
145
		{
146
			$post_row = $event['post_row'];
147
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
148
			$event['post_row'] = $post_row;
149
			unset($post_row);
150
		}
151
	}
152
153
	/**
154
	 * @param \phpbb\event\data $event
155
	 * @return void
156
	 */
157
	public function set_redirect_url(\phpbb\event\data $event)
158
	{
159
		if ($this->content_type)
160
		{
161
			if ($this->build_content)
162
			{
163
				$event['redirect_url'] = $this->builder->get_cp_url();
164
			}
165
			else
166
			{
167
				$topic_url = $this->builder->get_post_url($this->content_type, $event['post_data']);
168
169
				$post_id = $event['post_id'];
170
				if ($post_id != $event['post_data']['topic_first_post_id'])
171
				{
172
					$topic_url .= "?p=$post_id#p$post_id";
173
				}
174
175
				$event['redirect_url'] = $topic_url;
176
			}
177
		}
178
	}
179
180
	/**
181
	 * @param \phpbb\event\data $event
182
	 * @return void
183
	 */
184
	public function save_fields(\phpbb\event\data $event)
185
	{
186
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
187
		{
188
			$this->builder->save_db_fields($event['topic_id']);
189
		}
190
	}
191
192
	/**
193
	 * @param \phpbb\event\data $event
194
	 * @return void
195
	 */
196
	public function build_form(\phpbb\event\data $event)
197
	{
198
		if ($this->build_content)
199
		{
200
			$post_data = $event['post_data'];
201
			$page_data = $event['page_data'];
202
203
			$post_data['TOPIC_URL'] = './';
204
			$page_data['SITEMAKER_FORM'] = $this->builder->generate_form($event['topic_id'], $post_data, $page_data);
205
206
			if ($event['preview'] && $this->content_type)
207
			{
208
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
209
			}
210
211
			$event['page_data'] = $page_data;
212
			unset($post_data, $page_data);
213
		}
214
	}
215
}
216