quickmod::allow_topic_delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 12
rs 10
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 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\services;
11
12
class quickmod
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\template\template */
18
	protected $template;
19
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var string */
24
	protected $phpbb_root_path;
25
26
	/** @var string */
27
	protected $php_ext;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\auth\auth				$auth				Auth object
33
	 * @param \phpbb\template\template		$template			Template object
34
	 * @param \phpbb\user					$user				User object
35
	 * @param string						$phpbb_root_path	Path to the phpbb includes directory.
36
	 * @param string						$php_ext			php file extension
37
	*/
38
	public function __construct(\phpbb\auth\auth $auth, \phpbb\template\template $template, \phpbb\user $user, $phpbb_root_path, $php_ext)
39
	{
40
		$this->auth = $auth;
41
		$this->template = $template;
42
		$this->user = $user;
43
		$this->phpbb_root_path = $phpbb_root_path;
44
		$this->php_ext = $php_ext;
45
	}
46
47
	/**
48
	 * @param array $topic_data
49
	 * @return void
50
	 */
51
	public function show_tools(array $topic_data)
52
	{
53
		$s_quickmod_action = append_sid(
54
			"{$this->phpbb_root_path}mcp.$this->php_ext",
55
			array(
56
				'f'	=> $topic_data['forum_id'],
57
				't'	=> $topic_data['topic_id'],
58
				'quickmod'	=> 1,
59
				'redirect'	=> urlencode(str_replace('&amp;', '&', $topic_data['topic_url'])),
60
			),
61
			true,
62
			$this->user->session_id
63
		);
64
65
		$quickmod_array = $this->get_options($topic_data);
66
		foreach ($quickmod_array as $option => $qm_ary)
67
		{
68
			if (!empty($qm_ary[1]))
69
			{
70
				phpbb_add_quickmod_option($s_quickmod_action, $option, $qm_ary[0]);
71
			}
72
		}
73
74
		$this->template->assign_var('S_MOD_ACTION', $s_quickmod_action);
75
	}
76
77
	/**
78
	 * @param array $topic_data
79
	 * @return array
80
	 */
81
	protected function get_options(array $topic_data)
82
	{
83
		$s_quickmod_array = array(
84
			'lock'				=> array('LOCK_TOPIC', $this->allow_topic_lock($topic_data)),
85
			'unlock'			=> array('UNLOCK_TOPIC', $this->allow_topic_unlock($topic_data)),
86
			'delete_topic'		=> array('DELETE_TOPIC', $this->allow_topic_delete($topic_data)),
87
			'restore_topic'		=> array('RESTORE_TOPIC', $this->allow_topic_restore($topic_data)),
88
		);
89
90
		if ($this->auth->acl_get('m_', $topic_data['forum_id']) || $this->user_is_topic_poster($topic_data['topic_poster']))
91
		{
92
			$s_quickmod_array += array(
93
				'make_normal'		=> array('MAKE_NORMAL', $this->allow_make_normal($topic_data['forum_id'], $topic_data['topic_type'])),
94
				'make_sticky'		=> array('MAKE_STICKY', $this->allow_make_sticky($topic_data['forum_id'], $topic_data['topic_type'])),
95
				'make_announce'		=> array('MAKE_ANNOUNCE', $this->allow_make_announce($topic_data['forum_id'], $topic_data['topic_type'])),
96
				'make_global'		=> array('MAKE_GLOBAL', $this->allow_make_global($topic_data['forum_id'], $topic_data['topic_type'])),
97
			);
98
		}
99
100
		return array_merge($s_quickmod_array, array('topic_logs' => array('VIEW_TOPIC_LOGS', $this->auth->acl_get('m_', $topic_data['forum_id']))));
101
	}
102
103
	/**
104
	 * @param array $topic_data
105
	 * @return bool
106
	 */
107
	protected function allow_topic_lock(array $topic_data)
108
	{
109
		return ($topic_data['topic_status'] == ITEM_UNLOCKED) && $this->user_can_lock_topic($topic_data['forum_id'], $topic_data['topic_poster']);
110
	}
111
112
	/**
113
	 * @param array $topic_data
114
	 * @return bool
115
	 */
116
	protected function allow_topic_unlock(array $topic_data)
117
	{
118
		return ($topic_data['topic_status'] != ITEM_UNLOCKED) && ($this->auth->acl_get('m_lock', $topic_data['forum_id']));
119
	}
120
121
	/**
122
	 * @param array $topic_data
123
	 * @return bool
124
	 */
125
	protected function allow_topic_delete(array $topic_data)
126
	{
127
		return ($this->auth->acl_get('m_delete',  $topic_data['forum_id'])) || (($topic_data['topic_visibility'] != ITEM_DELETED) && $this->auth->acl_get('m_softdelete',  $topic_data['forum_id']));
128
	}
129
130
	/**
131
	 * @param array $topic_data
132
	 * @return bool
133
	 */
134
	protected function allow_topic_restore(array $topic_data)
135
	{
136
		return (($topic_data['topic_visibility'] == ITEM_DELETED) && $this->auth->acl_get('m_approve', $topic_data['forum_id']));
137
	}
138
139
	/**
140
	 * @param int $poster_id
141
	 * @return bool
142
	 */
143
	protected function user_is_topic_poster($poster_id)
144
	{
145
		return ($this->user->data['is_registered'] && $this->user->data['user_id'] == $poster_id) ? true : false;
146
	}
147
148
	/**
149
	 * @param int $forum_id
150
	 * @param int $poster_id
151
	 * @return bool
152
	 */
153
	protected function user_can_lock_topic($forum_id, $poster_id)
154
	{
155
		return ($this->auth->acl_get('m_lock', $forum_id) || ($this->auth->acl_get('f_user_lock', $forum_id) && $this->user_is_topic_poster($poster_id))) ? true : false;
156
	}
157
158
	/**
159
	 * @param int $forum_id
160
	 * @param int $topic_type
161
	 * @return bool
162
	 */
163
	protected function allow_make_normal($forum_id, $topic_type)
164
	{
165
		return ($this->auth->acl_gets('f_sticky', 'f_announce', 'f_announce_global', $forum_id) && $topic_type != POST_NORMAL);
166
	}
167
168
	/**
169
	 * @param int $forum_id
170
	 * @param int $topic_type
171
	 * @return bool
172
	 */
173
	protected function allow_make_sticky($forum_id, $topic_type)
174
	{
175
		return ($this->auth->acl_get('f_sticky', $forum_id) && $topic_type != POST_STICKY);
176
	}
177
178
	/**
179
	 * @param int $forum_id
180
	 * @param int $topic_type
181
	 * @return bool
182
	 */
183
	protected function allow_make_announce($forum_id, $topic_type)
184
	{
185
		return ($this->auth->acl_get('f_announce', $forum_id) && $topic_type != POST_ANNOUNCE);
186
	}
187
188
	/**
189
	 * @param int $forum_id
190
	 * @param int $topic_type
191
	 * @return bool
192
	 */
193
	protected function allow_make_global($forum_id, $topic_type)
194
	{
195
		return ($this->auth->acl_get('f_announce_global', $forum_id) && $topic_type != POST_GLOBAL);
196
	}
197
}
198