|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Topic Prefixes extension for the phpBB Forum Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright (c) 2016 phpBB Limited <https://www.phpbb.com> |
|
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace phpbb\topicprefixes\event; |
|
12
|
|
|
|
|
13
|
|
|
use phpbb\language\language; |
|
|
|
|
|
|
14
|
|
|
use phpbb\request\request; |
|
|
|
|
|
|
15
|
|
|
use phpbb\topicprefixes\prefixes\manager; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Event listener |
|
20
|
|
|
*/ |
|
21
|
|
|
class listener implements EventSubscriberInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var manager Topic prefixes manager |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $manager; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var request Request object |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $request; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var language Language object |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $language; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array An array of topic prefixes |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $prefixes; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getSubscribedEvents() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
'core.posting_modify_template_vars' => 'add_to_posting_form', |
|
50
|
|
|
'core.posting_modify_submit_post_before' => 'submit_prefix_data', |
|
51
|
|
|
'core.submit_post_modify_sql_data' => 'save_prefix_to_topic', |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Listener constructor |
|
57
|
|
|
* |
|
58
|
|
|
* @param manager $manager Topic prefixes manager |
|
59
|
|
|
* @param request $request Request object |
|
60
|
|
|
* @param language $language Language object |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(manager $manager, request $request, language $language) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->manager = $manager; |
|
65
|
|
|
$this->request = $request; |
|
66
|
|
|
$this->language = $language; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Update the posting page form with template vars |
|
71
|
|
|
* for the topic prefix drop-down menu. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \phpbb\event\data $event Event data object |
|
|
|
|
|
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function add_to_posting_form($event) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!$this->is_new_topic($event)) |
|
79
|
|
|
{ |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->language->add_lang('topic_prefixes', 'phpbb/topicprefixes'); |
|
84
|
|
|
|
|
85
|
|
|
// Get prefixes for the current forum |
|
86
|
|
|
$this->prefixes = $this->manager->get_active_prefixes($event['forum_id']); |
|
87
|
|
|
|
|
88
|
|
|
// Get the current prefix selected |
|
89
|
|
|
$selected = $this->get_selected_prefix($event); |
|
90
|
|
|
|
|
91
|
|
|
$event['page_data'] = array_merge($event['page_data'], [ |
|
92
|
|
|
'PREFIXES' => $this->prefixes, |
|
93
|
|
|
'SELECTED_PREFIX' => array_key_exists($selected, $this->prefixes) ? $this->prefixes[$selected]['prefix_tag'] : '', |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Prepare topic prefix data for post submission |
|
99
|
|
|
* |
|
100
|
|
|
* @param \phpbb\event\data $event Event data object |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function submit_prefix_data($event) |
|
104
|
|
|
{ |
|
105
|
|
|
$selected = $this->request->variable('topic_prefix', 0); |
|
106
|
|
|
|
|
107
|
|
|
// Get data for the prefix selected by the user |
|
108
|
|
|
$prefix = $this->manager->get_prefix($selected); |
|
109
|
|
|
|
|
110
|
|
|
// First, add the topic prefix id to the data to be stored with the db |
|
111
|
|
|
$data = $event['data']; |
|
112
|
|
|
$data['topic_prefix_id'] = $prefix ? (int) $prefix['prefix_id'] : 0; |
|
113
|
|
|
$event['data'] = $data; |
|
114
|
|
|
|
|
115
|
|
|
// Next, prepend the topic prefix to the subject (if necessary) |
|
116
|
|
|
if (isset($prefix['prefix_tag'])) |
|
117
|
|
|
{ |
|
118
|
|
|
$post_data = $event['post_data']; |
|
119
|
|
|
$post_data['post_subject'] = $this->manager->prepend_prefix($prefix['prefix_tag'], $post_data['post_subject']); |
|
120
|
|
|
$event['post_data'] = $post_data; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Save the topic prefix id with the associated topic |
|
126
|
|
|
* |
|
127
|
|
|
* @param \phpbb\event\data $event Event data object |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
public function save_prefix_to_topic($event) |
|
131
|
|
|
{ |
|
132
|
|
|
if (!array_key_exists('topic_prefix_id', $event['data']) || !in_array($event['post_mode'], ['edit_first_post', 'edit_topic', 'post'], true)) |
|
133
|
|
|
{ |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$sql_data = $event['sql_data']; |
|
138
|
|
|
$sql_data[TOPICS_TABLE]['sql']['topic_prefix_id'] = $event['data']['topic_prefix_id']; |
|
|
|
|
|
|
139
|
|
|
$event['sql_data'] = $sql_data; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Is a new topic being posted/edited? |
|
144
|
|
|
* |
|
145
|
|
|
* @param \phpbb\event\data $event Event data object |
|
146
|
|
|
* @return bool Return true if starting a new post or editing the first post, false otherwise |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function is_new_topic($event) |
|
149
|
|
|
{ |
|
150
|
|
|
return ($event['mode'] === 'post' || ($event['mode'] === 'edit' && $event['post_data']['topic_first_post_id'] == $event['post_data']['post_id'])); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get the current prefix for the selection menu |
|
155
|
|
|
* |
|
156
|
|
|
* @param \phpbb\event\data $event Event data object |
|
157
|
|
|
* @return int Identifier for the selected prefix |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function get_selected_prefix($event) |
|
160
|
|
|
{ |
|
161
|
|
|
// Get the prefix from the select menu |
|
162
|
|
|
$prefix_id = $this->request->variable('topic_prefix', 0); |
|
163
|
|
|
|
|
164
|
|
|
// If we are in preview mode, send back the prefix from the form |
|
165
|
|
|
if (!empty($event['preview']) && $event['mode'] !== 'edit') |
|
166
|
|
|
{ |
|
167
|
|
|
return $prefix_id; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// If no prefix was selected, get one if it already exists (ie: editing a post) |
|
171
|
|
|
if (!$prefix_id && !empty($event['post_data']['topic_prefix_id'])) |
|
172
|
|
|
{ |
|
173
|
|
|
$prefix_id = (int) $event['post_data']['topic_prefix_id']; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// If still no prefix was identified, look in existing topic title (ie: editing a post) |
|
177
|
|
|
if (!$prefix_id && !empty($event['post_data']['topic_title'])) |
|
178
|
|
|
{ |
|
179
|
|
|
$prefix_id = $this->find_prefix_in_title($event['post_data']['topic_title']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $prefix_id; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Find an active topic prefix in the topic title |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $title The post title |
|
189
|
|
|
* @return int Identifier for the found prefix |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function find_prefix_in_title($title) |
|
192
|
|
|
{ |
|
193
|
|
|
foreach ($this->prefixes as $prefix_id => $prefix_data) |
|
194
|
|
|
{ |
|
195
|
|
|
if (strpos($title, $prefix_data['prefix_tag']) === 0) |
|
196
|
|
|
{ |
|
197
|
|
|
return (int) $prefix_id; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return 0; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths