Completed
Push — master ( 888ef4...61b434 )
by Marc
117:35 queued 52:30
created

listener_helper::check_forum_permissions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
/**
3
*
4
* @package Quickedit
5
* @copyright (c) 2015 - 2021 Marc Alexander ( www.m-a-styles.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace marc1706\quickedit\event;
11
12
use phpbb\auth\auth;
13
use phpbb\config\config;
14
use phpbb\request\request;
15
use phpbb\request\request_interface;
16
17
class listener_helper
18
{
19
	/** @var auth */
20
	protected $auth;
21
22
	/** @var config */
23
	protected $config;
24
25
	/** @var request */
26
	protected $request;
27
28
	/** @var int quickedit forums flag */
29
	const QUICKEDIT_FLAG = 128;
30
31
	/**
32
	 * Constructor for listener
33
	 *
34 29
	 * @param auth $auth phpBB auth
35
	 * @param config $config phpBB config
36 29
	 * @param request_interface $request $request phpBB request
37 29
	 */
38 29
	public function __construct(auth $auth, config $config, request_interface $request)
39 29
	{
40
		$this->auth = $auth;
41
		$this->config = $config;
42
		$this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type object<phpbb\request\request_interface> is incompatible with the declared type object<phpbb\request\request> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
	}
44
45
	/**
46
	* Check if request is a catchable request
47
	*
48 3
	* @param object $event The event object
49
	* @return bool True if it's a catchable request, false if not
50 3
	*/
51
	public function is_catchable_request($event) : bool
52
	{
53
		return $this->request->is_ajax() && !$event['submit'] && $event['mode'] == 'edit';
54
	}
55
56
	/**
57
	* Add hidden fields in order to prevent dropping the needed values upon
58
	* submission.
59
	*
60
	* @param object $event The event object
61 2
	* @return void
62
	*/
63 2
	public function add_hidden_fields(&$event)
64 2
	{
65 2
		$event['s_hidden_fields'] .= build_hidden_fields(array(
66 2
			'attachment_data' 		=> $event['message_parser']->attachment_data,
67 2
			'poll_vote_change'		=> $this->not_empty_or_default($event['post_data']['poll_vote_change'], ' checked="checked"', ''),
68 2
			'poll_title'			=> $this->isset_or_default($event['post_data']['poll_title'], ''),
69 2
			'poll_option_text'		=> $this->not_empty_or_default($event['post_data']['poll_options'], implode("\n", $event['post_data']['poll_options']), ''),
70 2
			'poll_max_options'		=> $this->isset_or_default((int) $event['post_data']['poll_max_options'], 1),
71 2
			'poll_length'			=> $event['post_data']['poll_length'],
72 2
			'attach_sig'			=> $event['post_data']['enable_sig'],
73
			'topic_status'			=> $event['post_data']['topic_status'],
74 2
		));
75 2
76 1
		if (!empty($event['post_data']['topic_status']))
77 1
		{
78 1
			$event['s_hidden_fields'] .= build_hidden_fields(array(
79 1
				'lock_topic'			=> true,
80
			));
81
		}
82 2
83 2
		// Add hidden fields for kinerity/topicdescriptions
84
		$event['s_hidden_fields'] = $this->add_hidden_if_exists($event['s_hidden_fields'], $event['post_data'], 'topic_desc');
85
	}
86
87
	/**
88
	* Returns value if it is set, otherwise the default
89
	*
90
	* @param mixed $value The variable to check
91
	* @param mixed $default The default value to use if variable is not set
92
	* @return mixed Value if variable is set, default value if not
93 2
	*/
94
	protected function isset_or_default($value, $default)
95 2
	{
96
		return (isset($value)) ? $value : $default;
97
	}
98
99
	/**
100
	* Returns value if it's not empty, otherwise the default
101
	*
102
	* @param mixed $check_value The variable to check
103
	* @param mixed $value The value if $check_value is not empty
104
	* @param mixed $default The default value to use if variable is empty
105
	* @return mixed Value if $check_value is not empty, default value if not
106
	*/
107 2
	protected function not_empty_or_default($check_value, $value, $default)
108
	{
109 2
		return (!empty($check_value)) ? $value : $default;
110
	}
111
112
	/**
113
	* Enable quick edit
114
	*
115
	* @param object $event The event object
116
	* @return void
117
	*/
118
	public function enable_quick_edit($event)
119 1
	{
120
		$cfg_array = ($this->request->is_set('config')) ? $this->request->variable('config', array('' => '')) : '';
121 1
		if (isset($cfg_array['allow_quick_edit']))
122 1
		{
123 1
			$this->config->set('allow_quick_edit', (bool) $cfg_array['allow_quick_edit']);
124 1
			\enable_bitfield_column_flag(FORUMS_TABLE, 'forum_flags', log(self::QUICKEDIT_FLAG, 2));
125 1
		}
126 1
		$event->offsetSet('submit', true);
127 1
	}
128 1
129
	/**
130
	* Add quickedit settings to acp settings by modifying the display vars
131
	*
132
	* @param object $event The event object
133
	* @return void
134
	*/
135
	public function modify_acp_display_vars($event)
136
	{
137 1
		$new_display_var = array(
138
			'title'	=> $event['display_vars']['title'],
139
			'vars'	=> array(),
140 1
		);
141 1
142 1
		foreach ($event['display_vars']['vars'] as $key => $content)
143
		{
144 1
			$new_display_var['vars'][$key] = $content;
145
			if ($key == 'allow_quick_reply')
146 1
			{
147 1
				$new_display_var['vars']['allow_quick_edit'] = array(
148 1
					'lang'		=> 'ALLOW_QUICK_EDIT',
149 1
					'validate'	=> 'bool',
150 1
					'type'		=> 'custom',
151 1
					'function'	=> array('marc1706\quickedit\event\listener', 'quickedit_settings'),
152 1
					'explain' 	=> true,
153 1
				);
154 1
			}
155
		}
156 1
		$event->offsetSet('display_vars', $new_display_var);
157 1
	}
158 1
159 1
	/**
160
	* Check whether user can edit in this topic and forum
161
	*
162
	* @param object $event The event object
163
	* @return bool True if user can edit in this topic or forum, else false
164
	*/
165
	public function check_topic_edit($event) : bool
166
	{
167
		if (($event['topic_data']['forum_status'] == ITEM_UNLOCKED && $event['topic_data']['topic_status'] == ITEM_UNLOCKED) || $this->auth->acl_get('m_edit', $event['forum_id']))
168 2
		{
169
			return true;
170 2
		}
171 2
		else
172 1
		{
173
			return false;
174
		}
175
	}
176 1
177
	/**
178
	* Check forum_permissions and flag
179
	*
180
	* @param object $event The event object
181
	* @return bool True if quickedit is enabled and user can reply in forum,
182
	*		false if not
183
	*/
184
	public function check_forum_permissions($event) : bool
185
	{
186
		if (($event['topic_data']['forum_flags'] & self::QUICKEDIT_FLAG) && $this->auth->acl_get('f_reply', $event['forum_id']))
187
		{
188 2
			return true;
189
		}
190 2
		else
191 2
		{
192 1
			return false;
193
		}
194
	}
195
196 1
	/**
197
	 * Add data to hidden fields if column exists in post_data array
198
	 *
199
	 * @param string $hidden_fields Hidden fields data
200
	 * @param array $data_array post_data array
201
	 * @param string $column Column name
202
	 *
203
	 * @return string Hidden fields data
204
	 */
205
	protected function add_hidden_if_exists(string $hidden_fields, array $data_array, string $column) : string
206
	{
207
		if (isset($data_array[$column]))
208
		{
209 2
			$hidden_fields .= build_hidden_fields(array(
210
				$column		=> $data_array[$column],
211 2
			));
212 2
		}
213 1
214 1
		return $hidden_fields;
215 1
	}
216
}
217