Completed
Push — develop ( ff34f8...c5327c )
by Daniel
09:18
created

posting::modify_post_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
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('save_fields', '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_template_vars'			=> 'build_form',
52
		);
53
	}
54
55
	/**
56
	 * @param \phpbb\event\data $event
57
	 * @return void
58
	 */
59
	public function init_builder(\phpbb\event\data $event)
60
	{
61
		$this->content_type = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
62
63
		$topic_first_post_id = $event['post_data']['topic_first_post_id'];
64
		if (!$topic_first_post_id || $topic_first_post_id == $event['post_id'])
65
		{
66
			$this->build_content = ($this->content_type && $event['mode'] !== 'reply') ? true : false;
67
		}
68
	}
69
70
	/**
71
	 * @param \phpbb\event\data $event
72
	 * @return void
73
	 */
74
	public function build_message(\phpbb\event\data $event)
75
	{
76
		if ($this->build_content)
77
		{
78
			$message_parser = $event['message_parser'];
79
			$message_parser->message = $this->builder->generate_message();
80
			$event['message_parser'] = $message_parser;
81
		}
82
	}
83
84
	/**
85
	 * @param \phpbb\event\data $event
86
	 * @return void
87
	 */
88
	public function show_errors(\phpbb\event\data $event)
89
	{
90
		if ($this->build_content)
91
		{
92
			$event['error'] = $this->builder->get_errors();
93
		}
94
	}
95
96
	/**
97
	 * @param \phpbb\event\data $event
98
	 * @return void
99
	 */
100
	public function modify_sql_data(\phpbb\event\data $event)
101
	{
102
		if ($this->build_content)
103
		{
104
			$sql_data = $event['sql_data'];
105
			$this->builder->modify_posting_data($sql_data[TOPICS_TABLE]['sql']);
106
			$this->builder->force_visibility($event['post_mode'], $sql_data);
107
			$event['sql_data'] = $sql_data;
108
		}
109
	}
110
111
	/**
112
	 * @param \phpbb\event\data $event
113
	 * @return void
114
	 */
115
	public function set_content_post_id(\phpbb\event\data $event)
116
	{
117
		if ($this->content_type)
118
		{
119
			$this->content_post_id = array_pop($event['post_list']);
120
		}
121
	}
122
123
	/**
124
	 * @param \phpbb\event\data $event
125
	 * @return void
126
	 */
127
	public function modify_topic_review(\phpbb\event\data $event)
128
	{
129
		if ($this->content_type && $event['row']['post_id'] == $this->content_post_id)
130
		{
131
			$post_row = $event['post_row'];
132
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
133
			$event['post_row'] = $post_row;
134
			unset($post_row);
135
		}
136
	}
137
138
	/**
139
	 * @param \phpbb\event\data $event
140
	 * @return void
141
	 */
142
	public function set_redirect_url(\phpbb\event\data $event)
143
	{
144
		if ($this->content_type)
145
		{
146
			if ($this->build_content)
147
			{
148
				$event['redirect_url'] = $this->builder->get_cp_url();
149
			}
150
			else
151
			{
152
				$topic_url = $this->builder->get_post_url($this->content_type, $event['post_data']);
153
154
				$post_id = $event['post_id'];
155
				if ($post_id != $event['post_data']['topic_first_post_id'])
156
				{
157
					$topic_url .= "?p=$post_id#p$post_id";
158
				}
159
160
				$event['redirect_url'] = $topic_url;
161
			}
162
		}
163
	}
164
165
	/**
166
	 * @param \phpbb\event\data $event
167
	 * @return void
168
	 */
169
	public function save_fields(\phpbb\event\data $event)
170
	{
171
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
172
		{
173
			$this->builder->save_db_fields($event['topic_id']);
174
		}
175
	}
176
177
	/**
178
	 * @param \phpbb\event\data $event
179
	 * @return void
180
	 */
181
	public function build_form(\phpbb\event\data $event)
182
	{
183
		if ($this->build_content)
184
		{
185
			$post_data = $event['post_data'];
186
			$page_data = $event['page_data'];
187
188
			$post_data['TOPIC_URL'] = './';
189
			$page_data['SITEMAKER_FORM'] = $this->builder->generate_form($event['topic_id'], $post_data, $page_data);
190
191
			if ($event['preview'] && $this->content_type)
192
			{
193
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
194
			}
195
196
			$event['page_data'] = $page_data;
197
			unset($post_data, $page_data);
198
		}
199
	}
200
}
201