Passed
Push — master ( fd4b92...932be2 )
by Stanislav
02:59
created

report::move_images()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
*
4
* @package phpBB Gallery
5
* @version $Id$
6
* @copyright (c) 2007 nickvergessen [email protected] http://www.flying-bits.org
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
15
16
namespace phpbbgallery\core;
17
18
class report
19
{
20
	const UNREPORTED = 0;
21
	const OPEN = 1;
22
	const LOCKED = 2;
23
24
	/** @var \phpbbgallery\core\log */
25
	protected $gallery_log;
26
27
	/** @var \phpbbgallery\core\auth\auth */
28
	protected $gallery_auth;
29
30
	/** @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...
31
	protected $user;
32
33
	/** @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...
34
	protected $language;
35
36
	/** @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...
37
	protected $db;
38
39
	/** @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...
40
	protected $user_loader;
41
42
	/** @var \phpbbgallery\core\album\album */
43
	protected $album;
44
45
	/** @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...
46
	protected $template;
47
48
	/** @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...
49
	protected $helper;
50
51
	/** @var \phpbbgallery\core\config */
52
	protected $gallery_config;
53
54
	/** @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...
55
	protected $pagination;
56
57
	/** @var \phpbbgallery\core\notification\helper */
58
	protected $notification_helper;
59
60
	/** @var string */
61
	protected $images_table;
62
63
	/** @var string */
64
	protected $reports_table;
65
66 106
	public function __construct(\phpbbgallery\core\log $gallery_log, \phpbbgallery\core\auth\auth $gallery_auth, \phpbb\user $user,
67
		\phpbb\language\language $language, \phpbb\db\driver\driver_interface $db,	\phpbb\user_loader $user_loader,
68
		\phpbbgallery\core\album\album $album, \phpbb\template\template $template, \phpbb\controller\helper $helper,
69
		\phpbbgallery\core\config $gallery_config, \phpbb\pagination $pagination, \phpbbgallery\core\notification\helper $notification_helper,
70
		$images_table, $reports_table)
71
	{
72 106
		$this->gallery_log = $gallery_log;
73 106
		$this->gallery_auth = $gallery_auth;
74 106
		$this->user = $user;
75 106
		$this->language = $language;
76 106
		$this->db = $db;
77 106
		$this->user_loader = $user_loader;
78 106
		$this->album = $album;
79 106
		$this->template = $template;
80 106
		$this->helper = $helper;
81 106
		$this->gallery_config = $gallery_config;
82 106
		$this->pagination = $pagination;
83 106
		$this->notification_helper = $notification_helper;
84 106
		$this->images_table = $images_table;
85 106
		$this->reports_table = $reports_table;
86 106
	}
87
88
	/**
89
	 * Report an image
90
	 *
91
	 * @param $data
92
	 */
93 2
	public function add($data)
94
	{
95 2
		if (!isset($data['report_album_id']) || !isset($data['report_image_id']) || !isset($data['report_note']))
96
		{
97 1
			return;
98
		}
99
		$data = $data + array(
100 1
			'reporter_id'				=> $this->user->data['user_id'],
101 1
			'report_time'				=> time(),
102 1
			'report_status'				=> self::OPEN,
103
		);
104 1
		$sql = 'INSERT INTO ' . $this->reports_table . ' ' . $this->db->sql_build_array('INSERT', $data);
105 1
		$this->db->sql_query($sql);
106
107 1
		$report_id = (int) $this->db->sql_nextid();
108
109 1
		$sql = 'UPDATE ' . $this->images_table . ' 
110 1
			SET image_reported = ' . $report_id . '
111 1
			WHERE image_id = ' . (int) $data['report_image_id'];
112 1
		$this->db->sql_query($sql);
113
114 1
		$this->gallery_log->add_log('moderator', 'reportopen', $data['report_album_id'], $data['report_image_id'], array('LOG_GALLERY_REPORT_OPENED', $data['report_note']));
115
		$data = array(
116 1
			'report_id'	=> $report_id,
117 1
			'reporter_id'	=> $this->user->data['user_id'],
118 1
			'reported_image_id'	=> $data['report_image_id'],
119 1
			'reported_album_id'	=> $data['report_album_id']
120
		);
121 1
		if (!isset($data['report_album_id']))
122
		{
123 1
			$this->notification_helper->notify('new_report', $data);
124
		}
125 1
	}
126
127
	/**
128
	 * Close report
129
	 * @param    array $report_ids array of report_ids to closedir
130
	 * @param bool|int $user_id User Id, if not set - use current user idate
131
	 */
132 1
	public function close_reports_by_image($report_ids, $user_id = false)
133
	{
134
		$sql_ary = array(
135 1
			'report_manager'		=> (int) (($user_id) ? $user_id : $this->user->data['user_id']),
136 1
			'report_status'			=> 0,
137
		);
138 1
		$sql = 'UPDATE ' . $this->reports_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
139 1
			WHERE ' . $this->db->sql_in_set('report_image_id', $report_ids);
140 1
		$this->db->sql_query($sql);
141
		// We will have to request some images so we can log closing reports
142 1
		$sql = 'SELECT * FROM ' . $this->images_table . ' WHERE image_reported <> 0 and ' . $this->db->sql_in_set('image_id', $report_ids);
143 1
		$result = $this->db->sql_query($sql);
144 1
		while ($row = $this->db->sql_fetchrow($result))
145
		{
146 1
			$this->gallery_log->add_log('moderator', 'reportclosed', (int) $row['image_album_id'], (int) $row['image_id'], array('LOG_GALLERY_REPORT_CLOSED', 'Closed'));
147
		}
148 1
		$this->db->sql_freeresult($result);
149 1
		$sql = 'UPDATE ' . $this->images_table . ' SET image_reported = 0 WHERE ' . $this->db->sql_in_set('image_id', $report_ids);
150 1
		$this->db->sql_query($sql);
151 1
	}
152
153
	/**
154
	 * Move an image from one album to another
155
	 *
156
	 * @param    mixed $image_ids Array or integer with image_id.
157
	 * @param $move_to
158
	 */
159 1
	public function move_images($image_ids, $move_to)
160
	{
161 1
		$image_ids = self::cast_mixed_int2array($image_ids);
162
163 1
		$sql = 'UPDATE ' . $this->reports_table . '
164 1
			SET report_album_id = ' . (int) $move_to . '
165 1
			WHERE ' . $this->db->sql_in_set('report_image_id', $image_ids);
166 1
		$this->db->sql_query($sql);
167 1
	}
168
169
	/**
170
	 * Move the content from one album to another
171
	 *
172
	 * @param $move_from
173
	 * @param $move_to
174
	 * @internal param mixed $image_ids Array or integer with image_id.
175
	 */
176 1
	public function move_album_content($move_from, $move_to)
177
	{
178 1
		$sql = 'UPDATE ' . $this->reports_table . '
179 1
			SET report_album_id = ' . (int) $move_to . '
180 1
			WHERE report_album_id = ' . (int) $move_from;
181 1
		$this->db->sql_query($sql);
182 1
	}
183
184
	/**
185
	* Delete reports for given report_ids
186
	*
187
	* @param	mixed	$report_ids		Array or integer with report_id.
188
	*/
189 1
	public function delete($report_ids)
190
	{
191 1
		$report_ids = self::cast_mixed_int2array($report_ids);
192
193 1
		$sql = 'DELETE FROM ' . $this->reports_table . '
194 1
			WHERE ' . $this->db->sql_in_set('report_id', $report_ids);
195 1
		$this->db->sql_query($sql);
196
197 1
		$sql = 'UPDATE ' . $this->images_table . '
198 1
			SET image_reported = ' . self::UNREPORTED . '
199 1
			WHERE ' . $this->db->sql_in_set('image_reported', $report_ids);
200 1
		$this->db->sql_query($sql);
201
202
		// Let's delete notifications
203 1
		$this->notification_helper->delete_notifications('report', $report_ids);
204 1
	}
205
206
207
	/**
208
	* Delete reports for given image_ids
209
	*
210
	* @param	mixed	$image_ids		Array or integer with image_id.
211
	*/
212 1
	public function delete_images($image_ids)
213
	{
214 1
		$image_ids = self::cast_mixed_int2array($image_ids);
215
216
		// Let's build array for report notifications for images we are deleting
217 1
		$sql = 'SELECT report_id FROM ' . $this->reports_table . '
218 1
			WHERE ' . $this->db->sql_in_set('report_image_id', $image_ids);
219 1
		$result = $this->db->sql_query($sql);
220 1
		$reports = array();
221 1
		while ($row = $this->db->sql_fetchrow($result))
222
		{
223 1
			$reports[] = $row['report_id'];
224
		}
225 1
		$this->db->sql_freeresult($result);
226 1
		if (!empty($reports))
227
		{
228 1
			$reports = self::cast_mixed_int2array($reports);
229 1
			$this->notification_helper->delete_notifications('report', $reports);
230
		}
231
232 1
		$sql = 'DELETE FROM ' . $this->reports_table . '
233 1
			WHERE ' . $this->db->sql_in_set('report_image_id', $image_ids);
234 1
		$this->db->sql_query($sql);
235 1
	}
236
237
238
	/**
239
	* Delete reports for given album_ids
240
	*
241
	* @param	mixed	$album_ids		Array or integer with album_id.
242
	*/
243 1
	public function delete_albums($album_ids)
244
	{
245 1
		$album_ids = self::cast_mixed_int2array($album_ids);
246
247
		// Let's build array for report notifications for albums we are deleting
248 1
		$sql = 'SELECT report_id FROM ' . $this->reports_table . '
249 1
			WHERE ' . $this->db->sql_in_set('report_album_id', $album_ids);
250 1
		$result = $this->db->sql_query($sql);
251 1
		$reports = array();
252 1
		while ($row = $this->db->sql_fetchrow($result))
253
		{
254 1
			$reports[] = $row['report_id'];
255
		}
256 1
		$this->db->sql_freeresult($result);
257 1
		if (!empty($reports))
258
		{
259 1
			$reports = self::cast_mixed_int2array($reports);
260 1
			$this->notification_helper->delete_notifications('report', $reports);
261
		}
262
263 1
		$sql = 'DELETE FROM ' . $this->reports_table . '
264 1
			WHERE ' . $this->db->sql_in_set('report_album_id', $album_ids);
265 1
		$this->db->sql_query($sql);
266 1
	}
267
268
	/**
269
	 * Helper function building queues
270
	 * @param    (string)    type    What type of queue are we building (short or full)
0 ignored issues
show
Bug introduced by
The type phpbbgallery\core\type 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...
271
	 * @param int $page
272
	 * @param int $per_page
273
	 * @param int $status
274
	 */
275 2
	public function build_list($album, $page = 1, $per_page = 0, $status = 1)
276
	{
277
		// So if we are not forcing par page get it from config
278 2
		if ($per_page == 0)
279
		{
280
			$per_page = $this->gallery_config->get('items_per_page');
281
		}
282
		// Let's get albums that user can moderate
283 2
		$this->gallery_auth->load_user_premissions($this->user->data['user_id']);
284
285
		// Get albums we can approve in
286 2
		$mod_array = array();
287 2
		if ($album === 0)
288
		{
289 2
			$mod_array = $this->gallery_auth->acl_album_ids('m_report');
290 2
			if (empty($mod_array))
291
			{
292 2
				$mod_array[] = 0;
293
			}
294
		}
295
		else
296
		{
297
			$mod_array = array($album);
298
		}
299
300
		$sql_array = array(
301
			'FROM'	=> array(
302 2
				$this->images_table => 'i',
303 2
				$this->reports_table	=> 'r',
304
			),
305 2
			'WHERE'	=> 'i.image_id = r.report_image_id and r.report_status = ' . (int) $status . ' and ' . $this->db->sql_in_set('i.image_album_id', $mod_array),
306 2
			'ORBER_BY'	=> 'r.report_id DESC'
307
		);
308
		// Get Count
309 2
		$sql_array['SELECT'] = 'COUNT(r.report_id) as count';
310 2
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
311 2
		$result = $this->db->sql_query($sql);
312 2
		$row = $this->db->sql_fetchrow($result);
313 2
		$this->db->sql_freeresult($result);
314 2
		$count = $row['count'];
315
		// Request reports
316 2
		$sql_array['SELECT'] = 'i.image_id, i.image_name, i.image_user_id, i.image_username, i.image_user_colour, i.image_time, i.image_album_id, r.report_id, r.reporter_id, r.report_time';
317 2
		$page = $page - 1;
318 2
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
319 2
		$result = $this->db->sql_query_limit($sql, $per_page, $page * $per_page);
320
		// Build few arrays
321 2
		while ($row = $this->db->sql_fetchrow($result))
322
		{
323
			$reported_images[] = array(
324
				'image_id'	=> $row['image_id'],
325
				'image_name'	=> $row['image_name'],
326
				'image_username'	=> $row['image_username'],
327
				'image_user_id'	=> $row['image_user_id'],
328
				'image_user_colour'	=> $row['image_user_colour'],
329
				'image_time'	=> $row['image_time'],
330
				'image_album_id'	=> $row['image_album_id'],
331
				'report_id'	=> $row['report_id'],
332
				'reporter_id'	=> $row['reporter_id'],
333
				'report_time'	=> $row['report_time'],
334
			);
335
			$users_array[$row['reporter_id']] = array('');
336
			$users_array[$row['image_user_id']] = array('');
337
		}
338 2
		$this->db->sql_freeresult($result);
339
340 2
		if (empty($users_array))
341
		{
342 2
			return;
343
		}
344
345
		// Load users
346
		$this->user_loader->load_users(array_keys($users_array));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $users_array does not seem to be defined for all execution paths leading up to this point.
Loading history...
347
348
		$reported_images_count = 0;
349
		foreach ($reported_images as $VAR)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reported_images does not seem to be defined for all execution paths leading up to this point.
Loading history...
350
		{
351
			$album_tmp = $this->album->get_info($VAR['image_album_id']);
352
			$this->template->assign_block_vars('report_image_open', array(
353
				'U_IMAGE_ID'	=> $VAR['image_id'],
354
				'U_IMAGE'	=> $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $VAR['image_id'])),
355
				'U_IMAGE_URL'	=> $this->helper->route('phpbbgallery_core_image', array('image_id'	=> $VAR['image_id'])),
356
				'U_IMAGE_NAME'	=> $VAR['image_name'],
357
				'IMAGE_AUTHOR'	=> $this->user_loader->get_username($VAR['image_user_id'], 'full'),
358
				'IMAGE_TIME'	=> $this->user->format_date($VAR['image_time']),
359
				'IMAGE_ALBUM'	=> $album_tmp['album_name'],
360
				'IMAGE_ALBUM_URL'	=> $this->helper->route('phpbbgallery_core_album', array('album_id' => $VAR['image_album_id'])),
361
				'REPORT_URL'	=> $this->helper->route('phpbbgallery_core_moderate_image', array('image_id' => $VAR['image_id'])),
362
				'REPORT_AUTHOR'	=> $this->user_loader->get_username($VAR['reporter_id'], 'full'),
363
				'REPORT_TIME'	=> $this->user->format_date($VAR['report_time']),
364
			));
365
			unset($album_tmp);
366
			$reported_images_count ++;
367
		}
368
		$this->template->assign_vars(array(
369
			'TOTAL_IMAGES_REPORTED' => $status == 1 ? $this->language->lang('WAITING_REPORTED_IMAGE', (int) $count) : $this->language->lang('WAITING_REPORTED_DONE', (int) $count),
370
			'S_GALLERY_REPORT_ACTION'	=> $status == 1 ? ($album > 0 ? $this->helper->route('phpbbgallery_core_moderate_reports_album', array('album_id' => $album)) : $this->helper->route('phpbbgallery_core_moderate_reports')) : false,
371
		));
372
		if ($album === 0)
373
		{
374
			$this->pagination->generate_template_pagination(array(
375
				'routes' => array(
376
					$status == 1 ? 'phpbbgallery_core_moderate_reports' : 'phpbbgallery_core_moderate_reports_closed',
377
					$status == 1 ? 'phpbbgallery_core_moderate_reports_page' : 'phpbbgallery_core_moderate_reports_closed_page',
378
				),
379
				'params' => array(
380
				),
381
			), 'pagination', 'page', $count, $per_page, $page * $per_page);
382
			$this->template->assign_vars(array(
383
				'TOTAL_PAGES'				=> $this->language->lang('PAGE_TITLE_NUMBER', $page + 1),
384
			));
385
		}
386
		else
387
		{
388
			$this->pagination->generate_template_pagination(array(
389
				'routes' => array(
390
					$status == 1 ? 'phpbbgallery_core_moderate_reports_album' : 'phpbbgallery_core_moderate_reports_closed_album',
391
					$status == 1 ? 'phpbbgallery_core_moderate_reports_album_page' : 'phpbbgallery_core_moderate_reports_closed_album_page',
392
				),
393
				'params' => array(
394
					'album_id'	=> $album,
395
				),
396
			), 'pagination', 'page', $count, $per_page, $page * $per_page);
397
			$this->template->assign_vars(array(
398
				'TOTAL_PAGES'				=> $this->language->lang('PAGE_TITLE_NUMBER', $page + 1),
399
			));
400
		}
401
	}
402
403
	/**
404
	 * Get report data by image id
405
	 *
406
	 * @param    (int)    $image_id    Image id for which we will get info about
407
	 * return    array    $report_data    array with all report info\
408
	 * @return array|void
409
	 */
410 1
	public function get_data_by_image($image_id)
411
	{
412 1
		if (empty($image_id))
413
		{
414
			return;
415
		}
416
417 1
		$sql = 'SELECT * FROM ' . $this->reports_table . ' WHERE report_image_id = ' . (int) $image_id;
418 1
		$result = $this->db->sql_query($sql);
419 1
		$report_data = array();
420 1
		while ($row = $this->db->sql_fetchrow($result))
421
		{
422 1
			$report_data[$row['report_id']] = array(
423 1
				'report_id'			=> $row['report_id'],
424 1
				'report_album_id'	=> $row['report_album_id'],
425 1
				'reporter_id'		=> $row['reporter_id'],
426 1
				'report_manager'	=> $row['report_manager'],
427 1
				'report_note'		=> $row['report_note'],
428 1
				'report_time'		=> $row['report_time'],
429 1
				'report_status'		=> $row['report_status'],
430
			);
431
		}
432 1
		$this->db->sql_freeresult($result);
433
434 1
		return $report_data;
435
	}
436 5
	static public function cast_mixed_int2array($ids)
437
	{
438 5
		if (is_array($ids))
439
		{
440 5
			return array_map('intval', $ids);
441
		}
442
		else
443
		{
444 1
			return array((int) $ids);
445
		}
446
	}
447
}
448