moderate::album_overview()   F
last analyzed

Complexity

Conditions 15
Paths 322

Size

Total Lines 133
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
eloc 95
c 0
b 0
f 0
dl 0
loc 133
ccs 0
cts 93
cp 0
rs 2.9557
cc 15
nc 322
nop 3
crap 240

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * phpBB Gallery - Core Extension
4
 *
5
 * @package   phpbbgallery/core
6
 * @author    satanasov
7
 * @author    Leinad4Mind
8
 * @copyright 2014- satanasov, 2018- Leinad4Mind
9
 * @license   GPL-2.0-only
10
 */
11
12
namespace phpbbgallery\core;
13
14
class moderate
15
{
16
	/**
17
	 * @var \phpbb\db\driver\driver_interface
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
	 */
19
	protected $db;
20
21
	/**
22
	 * @var \phpbb\template\template
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
	 */
24
	protected $template;
25
26
	/**
27
	 * @var \phpbb\controller\helper
0 ignored issues
show
Bug introduced by
The type phpbb\controller\helper was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
	 */
29
	protected $helper;
30
31
	/**
32
	 * @var \phpbb\user
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
	 */
34
	protected $user;
35
36
	/**
37
	 * @var \phpbb\language\language
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
	 */
39
	protected $lang;
40
41
	/**
42
	 * @var \phpbb\user_loader
0 ignored issues
show
Bug introduced by
The type phpbb\user_loader was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
	 */
44
	protected $user_loader;
45
46
	/**
47
	 * @var \phpbbgallery\core\album\album
48
	 */
49
	protected $album;
50
51
	/**
52
	 * @var \phpbbgallery\core\auth\auth
53
	 */
54
	protected $gallery_auth;
55
56
	/**
57
	 * @var \phpbb\pagination
0 ignored issues
show
Bug introduced by
The type phpbb\pagination was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
	 */
59
	protected $pagination;
60
61
	/**
62
	 * @var \phpbbgallery\core\comment
63
	 */
64
	protected $comment;
65
66
	/**
67
	 * @var \phpbbgallery\core\report
68
	 */
69
	protected $report;
70
71
	/**
72
	 * @var \phpbbgallery\core\image\image
73
	 */
74
	protected $image;
75
76
	/**
77
	 * @var \phpbbgallery\core\config
78
	 */
79
	protected $gallery_config;
80
81
	/**
82
	 * @var \phpbbgallery\core\notification
83
	 */
84
	protected $gallery_notification;
85
86
	/**
87
	 * @var \phpbbgallery\core\rating
88
	 */
89
	protected $gallery_rating;
90
91
	/**
92
	 * @var string
93
	 */
94
	protected $images_table;
95
96
	/**
97
	 * moderate constructor.
98
	 *
99
	 * @param \phpbb\db\driver\driver_interface $db
100
	 * @param \phpbb\template\template          $template
101
	 * @param \phpbb\controller\helper          $helper
102
	 * @param \phpbb\user                       $user
103
	 * @param \phpbb\language\language          $lang
104
	 * @param \phpbb\user_loader                $user_loader
105
	 * @param album\album                       $album
106
	 * @param auth\auth                         $gallery_auth
107
	 * @param \phpbb\pagination                 $pagination
108
	 * @param comment                           $comment
109
	 * @param report                            $report
110
	 * @param image\image                       $image
111
	 * @param config                            $gallery_config
112
	 * @param notification                      $gallery_notification
113
	 * @param rating                            $gallery_rating
114
	 * @param                                   $images_table
115
	 */
116 15
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\controller\helper $helper, \phpbb\user $user,
117
		\phpbb\language\language $lang,
118
		\phpbb\user_loader $user_loader, \phpbbgallery\core\album\album $album, \phpbbgallery\core\auth\auth $gallery_auth, \phpbb\pagination $pagination,
119
		\phpbbgallery\core\comment $comment, \phpbbgallery\core\report $report, \phpbbgallery\core\image\image $image,
120
		\phpbbgallery\core\config $gallery_config, \phpbbgallery\core\notification $gallery_notification, \phpbbgallery\core\rating $gallery_rating,
121
		$images_table)
122
	{
123 15
		$this->db = $db;
124 15
		$this->template = $template;
125 15
		$this->helper = $helper;
126 15
		$this->user = $user;
127 15
		$this->lang = $lang;
128 15
		$this->user_loader = $user_loader;
129 15
		$this->album = $album;
130 15
		$this->gallery_auth = $gallery_auth;
131 15
		$this->pagination = $pagination;
132 15
		$this->comment = $comment;
133 15
		$this->report = $report;
134 15
		$this->image = $image;
135 15
		$this->gallery_config = $gallery_config;
136 15
		$this->gallery_notification = $gallery_notification;
137 15
		$this->gallery_rating = $gallery_rating;
138 15
		$this->images_table = $images_table;
139 15
	}
140
141
	/**
142
	 * Helper function building queues
143
	 *
144
	 * @param int $album    album we build queue for
145
	 * @param int $page     This queue builder should return objects for MCP queues, so page?
146
	 * @param int $per_page We need how many elements per page
147
	 */
148 2
	public function build_list($album, $page = 1, $per_page = 0)
149
	{
150
		// So if we are not forcing par page get it from config
151 2
		if ($per_page == 0)
152
		{
153
			$per_page = $this->gallery_config->get('items_per_page');
154
		}
155
		// Let's get albums that user can moderate
156 2
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
157
158
		// Get albums we can approve in
159 2
		$mod_array = array();
160 2
		if ($album === 0)
161
		{
162 2
			$mod_array = $this->gallery_auth->acl_album_ids('m_status');
163 2
			if (empty($mod_array))
164
			{
165 2
				$mod_array[] = 0;
166
			}
167
		}
168
		else
169
		{
170
			$mod_array = array($album);
171
		}
172
		// Let's get count of unapproved
173
		$sql = 'SELECT COUNT(DISTINCT image_id) as count 
174 2
			FROM ' . $this->images_table . ' 
175 2
			WHERE image_status = ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ' AND ' . $this->db->sql_in_set('image_album_id', $mod_array);
176 2
		$result = $this->db->sql_query($sql);
177 2
		$row = $this->db->sql_fetchrow($result);
178 2
		$this->db->sql_freeresult($result);
179 2
		$count = $row['count'];
180
		// If user has no albums to have e return him
181
		$sql = 'SELECT * 
182 2
			FROM ' . $this->images_table . ' 
183 2
			WHERE image_status = ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ' AND ' . $this->db->sql_in_set('image_album_id', $mod_array) . '
184
			ORDER BY image_id DESC';
185 2
		$page = $page - 1;
186 2
		$result = $this->db->sql_query_limit($sql, $per_page, $page * $per_page);
187
188 2
		$waiting_images = $users_array = array();
189 2
		while ($row = $this->db->sql_fetchrow($result))
190
		{
191
			$waiting_images[] = array(
192
				'image_id'       => $row['image_id'],
193
				'image_name'     => $row['image_name'],
194
				'image_author'   => (int) $row['image_user_id'],
195
				'image_time'     => $row['image_time'],
196
				'image_album_id' => $row['image_album_id'],
197
			);
198
			$users_array[$row['image_user_id']] = array('');
199
		}
200 2
		$this->db->sql_freeresult($result);
201
202 2
		if (empty($users_array))
203
		{
204 2
			return;
205
		}
206
207
		// Load users
208
		$this->user_loader->load_users(array_keys($users_array));
209
210
		foreach ($waiting_images as $VAR)
211
		{
212
			$album_tmp = $this->album->get_info($VAR['image_album_id']);
213
			$this->template->assign_block_vars('image_unapproved', array(
214
				'U_IMAGE_ID'           => $VAR['image_id'],
215
				'U_IMAGE'              => $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $VAR['image_id'])),
216
				'U_IMAGE_URL'          => $this->helper->route('phpbbgallery_core_image', array('image_id' => $VAR['image_id'])),
217
				'U_IMAGE_MODERATE_URL' => $this->helper->route('phpbbgallery_core_moderate_image', array('image_id' => $VAR['image_id'])),
218
				'U_IMAGE_NAME'         => $VAR['image_name'],
219
				'IMAGE_AUTHOR'         => $this->user_loader->get_username($VAR['image_author'], 'full'),
220
				'IMAGE_TIME'           => $this->user->format_date($VAR['image_time']),
221
				'IMAGE_ALBUM'          => $album_tmp['album_name'],
222
				'IMAGE_ALBUM_URL'      => $this->helper->route('phpbbgallery_core_album', array('album_id' => $VAR['image_album_id'])),
223
				'IMAGE_ALBUM_ID'       => $VAR['image_album_id'],
224
			));
225
			unset($album_tmp);
226
		}
227
		$this->template->assign_vars(array(
228
			'TOTAL_IMAGES_WAITING'     => $this->lang->lang('WAITING_UNAPPROVED_IMAGE', (int) $count),
229
			'S_GALLERY_APPROVE_ACTION' => $album > 0 ? $this->helper->route('phpbbgallery_core_moderate_queue_approve_album', array('album_id' => $album)) : $this->helper->route('phpbbgallery_core_moderate_queue_approve'),
230
		));
231
		if ($album === 0)
232
		{
233
			$this->pagination->generate_template_pagination(array(
234
				'routes' => array(
235
					'phpbbgallery_core_moderate_queue_approve',
236
					'phpbbgallery_core_moderate_queue_approve_page',
237
				),
238
				'params' => array(),
239
			), 'pagination', 'page', $count, $per_page, $page * $per_page);
240
			$this->template->assign_vars(array(
241
				'TOTAL_PAGES' => $this->lang->lang('PAGE_TITLE_NUMBER', $page + 1),
242
			));
243
		}
244
		else
245
		{
246
			$this->pagination->generate_template_pagination(array(
247
				'routes' => array(
248
					'phpbbgallery_core_moderate_queue_approve_album',
249
					'phpbbgallery_core_moderate_queue_approve_album_page',
250
				),
251
				'params' => array(
252
					'album_id' => $album,
253
				),
254
			), 'pagination', 'page', $count, $per_page, $page * $per_page);
255
			$this->template->assign_vars(array(
256
				'TOTAL_PAGES' => $this->lang->lang('PAGE_TITLE_NUMBER', $page + 1),
257
			));
258
		}
259
	}
260
261
	/**
262
	 * Build album overview
263
	 *
264
	 * @param int $album_id
265
	 * @param int $page     This queue builder should return objects for MCP queues, so page?
266
	 * @param int $per_page We need how many elements per page
267
	 * @internal param int $album album we build queue for
268
	 */
269
	public function album_overview($album_id, $page = 1, $per_page = 0)
270
	{
271
		// So if we are not forcing par page get it from config
272
		if ($per_page == 0)
273
		{
274
			$per_page = $this->gallery_config->get('items_per_page');
275
		}
276
		// Let's get albums that user can moderate
277
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
278
279
		// we have security in the controller, so no need to be paranoid ...
280
		// and we will build queue with only items user can review
281
		if (!isset($album_id))
282
		{
283
			return;
284
		}
285
		// Let's see what the user can do?
286
		$status[] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$status was never initialized. Although not strictly required by PHP, it is generally a good practice to add $status = array(); before regardless.
Loading history...
287
		$actions = array();
288
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
289
		$album = $this->album->get_info($album_id);
290
		if ($this->gallery_auth->acl_check('m_status', $album['album_id'], $album['album_user_id']))
291
		{
292
			$status[] = 0;
293
			$status[] = 2;
294
			$actions['approve'] = 'QUEUES_A_APPROVE';
295
			$actions['unapprove'] = 'QUEUES_A_UNAPPROVE';
296
			$actions['lock'] = 'QUEUES_A_LOCK';
297
		}
298
		if ($this->gallery_auth->acl_check('m_delete', $album['album_id'], $album['album_user_id']))
299
		{
300
			$actions['delete'] = 'QUEUES_A_DELETE';
301
		}
302
		if ($this->gallery_auth->acl_check('m_move', $album['album_id'], $album['album_user_id']))
303
		{
304
			$actions['move'] = 'QUEUES_A_MOVE';
305
		}
306
		if ($this->gallery_auth->acl_check('m_report', $album['album_id'], $album['album_user_id']))
307
		{
308
			$actions['report'] = 'REPORT_A_CLOSE';
309
		}
310
		$sql = 'SELECT COUNT(DISTINCT image_id) AS count FROM ' . $this->images_table . ' WHERE ' . $this->db->sql_in_set('image_status', $status) . ' AND image_album_id = ' . (int) $album_id;
311
		$result = $this->db->sql_query($sql);
312
		$row = $this->db->sql_fetchrow($result);
313
		$this->db->sql_freeresult($result);
314
		$count = $row['count'];
315
		$sql = 'SELECT * FROM ' . $this->images_table . ' WHERE ' . $this->db->sql_in_set('image_status', $status) . ' AND image_album_id = ' . (int) $album_id . ' ORDER BY image_id DESC';
316
317
		$result = $this->db->sql_query_limit($sql, $per_page, ($page - 1) * $per_page);
318
		$users_array = array();
319
		$images = array();
320
		while ($row = $this->db->sql_fetchrow($result))
321
		{
322
			$images[] = array(
323
				'image_id'             => $row['image_id'],
324
				'image_filename'       => $row['image_filename'],
325
				'image_name'           => $row['image_name'],
326
				'image_name_clean'     => $row['image_name_clean'],
327
				'image_desc'           => $row['image_desc'],
328
				'image_desc_uid'       => $row['image_desc_uid'],
329
				'image_desc_bitfield'  => $row['image_desc_bitfield'],
330
				'image_user_id'        => $row['image_user_id'],
331
				'image_username'       => $row['image_username'],
332
				'image_username_clean' => $row['image_username_clean'],
333
				'image_user_colour'    => $row['image_user_colour'],
334
				'image_user_ip'        => $row['image_user_ip'],
335
				'image_time'           => $row['image_time'],
336
				'image_album_id'       => $row['image_album_id'],
337
				'image_view_count'     => $row['image_view_count'],
338
				'image_status'         => $row['image_status'],
339
				'image_filemissing'    => $row['image_filemissing'],
340
				'image_rates'          => $row['image_rates'],
341
				'image_rate_points'    => $row['image_rate_points'],
342
				'image_rate_avg'       => $row['image_rate_avg'],
343
				'image_comments'       => $row['image_comments'],
344
				'image_last_comment'   => $row['image_last_comment'],
345
				'image_allow_comments' => $row['image_allow_comments'],
346
				'image_favorited'      => $row['image_favorited'],
347
				'image_reported'       => $row['image_reported'],
348
				'filesize_upload'      => $row['filesize_upload'],
349
				'filesize_medium'      => $row['filesize_medium'],
350
				'filesize_cache'       => $row['filesize_cache'],
351
			);
352
			$users_array[$row['image_user_id']] = array('');
353
		}
354
		$this->db->sql_freeresult($result);
355
356
		if (empty($users_array))
357
		{
358
			return;
359
		}
360
361
		// Load users
362
		$this->user_loader->load_users(array_keys($users_array));
363
		foreach ($images as $var)
364
		{
365
			$this->template->assign_block_vars('overview', array(
366
				'U_IMAGE_ID'           => $var['image_id'],
367
				'U_IMAGE'              => $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $var['image_id'])),
368
				'U_IMAGE_URL'          => $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['image_id'])),
369
				'U_IMAGE_MODERATE_URL' => $this->helper->route('phpbbgallery_core_moderate_image', array('image_id' => $var['image_id'])),
370
				'U_IMAGE_NAME'         => $var['image_name'],
371
				'IMAGE_AUTHOR'         => $this->user_loader->get_username($var['image_user_id'], 'full'),
372
				'IMAGE_TIME'           => $this->user->format_date($var['image_time']),
373
				'IMAGE_ALBUM'          => $album['album_name'],
374
				'IMAGE_ALBUM_URL'      => $this->helper->route('phpbbgallery_core_album', array('album_id' => $var['image_album_id'])),
375
				'IMAGE_ALBUM_ID'       => $var['image_album_id'],
376
				'U_IS_REPORTED'        => $this->gallery_auth->acl_check('m_report', $album['album_id'], $album['album_user_id']) && $var['image_reported'] > 0 ? true : false,
377
				'U_IS_UNAPPROVED'      => $var['image_status'] == 0 ? true : false,
378
				'U_IS_LOCKED'          => $var['image_status'] == 2 ? true : false,
379
			));
380
		}
381
382
		$this->pagination->generate_template_pagination(array(
383
			'routes' => array(
384
				'phpbbgallery_core_moderate_view',
385
				'phpbbgallery_core_moderate_view_page',
386
			),
387
			'params' => array(
388
				'album_id' => $album_id,
389
			),
390
		), 'pagination', 'page', $count, $per_page, ($page - 1) * $per_page);
391
392
		$select = '<select name="select_action">';
393
		foreach ($actions as $id => $var)
394
		{
395
			$select .= '<option value="' . $id . '">' . $this->lang->lang($var) . '</option>';
396
		}
397
		$select .= '</select>';
398
		$this->template->assign_vars(array(
399
			'TOTAL_PAGES'                        => $this->lang->lang('PAGE_TITLE_NUMBER', $page),
400
			'S_GALLERY_MODERATE_OVERVIEW_ACTION' => $this->helper->route('phpbbgallery_core_moderate_view', array('album_id' => $album_id)),
401
			'U_ACTION_SELECT'                    => $select,
402
		));
403
	}
404
405
	public function delete_images($images, $files = array())
406
	{
407
		// We are going to do some cleanup
408
		$this->gallery_rating->loader(0);
409
		$this->gallery_rating->delete_ratings($images);
410
		$this->comment->delete_images($images);
411
		$this->gallery_notification->delete_images($images);
412
		$this->report->delete_images($images);
413
		$this->image->delete_images($images, $files);
414
	}
415
}
416