Passed
Push — master ( 4c9f6d...9f98fb )
by Stanislav
03:51 queued 01:00
created

image::assign_block()   D

Complexity

Conditions 35
Paths 9

Size

Total Lines 71
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 35

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 57
c 2
b 1
f 0
dl 0
loc 71
ccs 56
cts 56
cp 1
rs 4.1666
cc 35
nc 9
nop 5
crap 35

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
/**
4
*
5
* @package phpBB Gallery Core
6
* @copyright (c) 2014 nickvergessen | Leinad4Mind 2025
7
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
*
9
*/
10
11
namespace phpbbgallery\core\image;
12
13
class image
14
{
15
	/** @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...
16
	protected $db;
17
18
	/** @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...
19
	protected $user;
20
21
	/** @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...
22
	protected $language;
23
24
	/** @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...
25
	protected $template;
26
27
	/** @var \phpbb\event\dispatcher_interface  */
0 ignored issues
show
Bug introduced by
The type phpbb\event\dispatcher_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...
28
	protected $phpbb_dispatcher;
29
30
	/** @var \phpbbgallery\core\auth\auth  */
31
	protected $gallery_auth;
32
33
	/** @var \phpbbgallery\core\album\album  */
34
	protected $album;
35
36
	/** @var \phpbbgallery\core\config  */
37
	protected $gallery_config;
38
39
	/** @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...
40
	protected $helper;
41
42
	/** @var \phpbbgallery\core\url  */
43
	protected $url;
44
45
	/** @var \phpbbgallery\core\log  */
46
	protected $gallery_log;
47
48
	/** @var \phpbbgallery\core\notification\helper  */
49
	protected $notification_helper;
50
51
	/** @var \phpbbgallery\core\cache  */
52
	protected $gallery_cache;
53
54
	/** @var \phpbbgallery\core\report  */
55
	protected $gallery_report;
56
57
	/** @var \phpbbgallery\core\user  */
58
	protected $gallery_user;
59
60
	/** @var \phpbbgallery\core\contest  */
61
	protected $contest;
62
63
	/** @var \phpbbgallery\core\file\file  */
64
	protected $file;
65
66
	/** @var   */
67
	protected $table_images;
68
69
	const IMAGE_SHOW_IP = 128;
70
	const IMAGE_SHOW_RATINGS = 64;
71
	const IMAGE_SHOW_USERNAME = 32;
72
	const IMAGE_SHOW_VIEWS = 16;
73
	const IMAGE_SHOW_TIME = 8;
74
	const IMAGE_SHOW_IMAGENAME = 4;
75
	const IMAGE_SHOW_COMMENTS = 2;
76
	const IMAGE_SHOW_ALBUM = 1;
77
78
	/**
79
	 * construct
80
	 *
81
	 * @param \phpbb\db\driver\driver_interface      $db
82
	 * @param \phpbb\user                            $user
83
	 * @param \phpbb\language\language               $language
84
	 * @param \phpbb\template\template               $template
85
	 * @param \phpbb\event\dispatcher_interface      $phpbb_dispatcher
86
	 * @param \phpbbgallery\core\auth\auth           $gallery_auth
87
	 * @param \phpbbgallery\core\album\album         $album
88
	 * @param \phpbbgallery\core\config              $gallery_config
89
	 * @param \phpbb\controller\helper               $helper
90
	 * @param \phpbbgallery\core\url                 $url
91
	 * @param \phpbbgallery\core\log                 $gallery_log
92
	 * @param \phpbbgallery\core\notification\helper $notification_helper
93
	 * @param \phpbbgallery\core\report              $report
94
	 * @param \phpbbgallery\core\cache               $gallery_cache
95
	 * @param \phpbbgallery\core\user                $gallery_user
96
	 * @param \phpbbgallery\core\contest             $contest
97
	 * @param \phpbbgallery\core\file\file           $file
98
	 * @param                                        $table_images
99
	 */
100 96
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, \phpbb\language\language $language,
101
		\phpbb\template\template $template, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbbgallery\core\auth\auth $gallery_auth,
102
		\phpbbgallery\core\album\album $album, \phpbbgallery\core\config $gallery_config, \phpbb\controller\helper $helper,
103
		\phpbbgallery\core\url $url, \phpbbgallery\core\log $gallery_log, \phpbbgallery\core\notification\helper $notification_helper,
104
		\phpbbgallery\core\report $report, \phpbbgallery\core\cache $gallery_cache, \phpbbgallery\core\user $gallery_user,
105
		\phpbbgallery\core\contest $contest, \phpbbgallery\core\file\file $file,
106
		$table_images)
107
	{
108 96
		$this->db = $db;
109 96
		$this->user = $user;
110 96
		$this->language = $language;
111 96
		$this->template = $template;
112 96
		$this->phpbb_dispatcher = $phpbb_dispatcher;
113 96
		$this->gallery_auth = $gallery_auth;
114 96
		$this->album = $album;
115 96
		$this->gallery_config = $gallery_config;
116 96
		$this->helper = $helper;
117 96
		$this->url = $url;
118 96
		$this->gallery_log = $gallery_log;
119 96
		$this->notification_helper = $notification_helper;
120 96
		$this->gallery_cache = $gallery_cache;
121 96
		$this->gallery_report = $report;
122 96
		$this->gallery_user = $gallery_user;
123 96
		$this->contest = $contest;
124 96
		$this->file = $file;
125 96
		$this->table_images = $table_images;
126 96
	}
127
128 2
	public function get_new_author_info($username)
129
	{
130
		// Who is the new uploader?
131 2
		if (!$username)
132
		{
133
			return false;
134
		}
135 2
		$user_id = 0;
136 2
		if ($username)
137
		{
138 2
			if (!function_exists('user_get_id_name'))
139
			{
140 1
				$this->url->_include('functions_user', 'phpbb');
141
			}
142 2
			user_get_id_name($user_id, $username);
143
		}
144
145 2
		if (empty($user_id))
0 ignored issues
show
introduced by
The condition empty($user_id) is always true.
Loading history...
146
		{
147 1
			return false;
148
		}
149
150
		$sql = 'SELECT username, user_colour, user_id
151 1
			FROM ' . USERS_TABLE . '
0 ignored issues
show
Bug introduced by
The constant phpbbgallery\core\image\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
152 1
			WHERE user_id = ' . (int) $user_id[0];
153 1
		$result = $this->db->sql_query($sql);
154 1
		$row = $this->db->sql_fetchrow($result);
155 1
		$this->db->sql_freeresult($result);
156
157 1
		return $row;
158
	}
159
160
	/**
161
	 * Delete an image completely.
162
	 *
163
	 * @param    array $images Array with the image_id(s)
164
	 * @param    array $filenames Array with filenames for the image_ids. If a filename is missing it's queried from the database.
165
	 *                                    Format: $image_id => $filename
166
	 * @param bool $resync_albums
167
	 * @param    bool $skip_files If set to true, we won't try to delete the source files.
168
	 * @return bool
169
	 */
170
	public function delete_images($images, $filenames = array(), $resync_albums = true, $skip_files = false)
171
	{
172
		$phpbb_gallery_contest = $this->contest;
173
		if (empty($images))
174
		{
175
			return;
176
		}
177
		if (!$skip_files)
178
		{
179
			// Delete the files from the disc...
180
			$need_filenames = array();
181
			foreach ($images as $image)
182
			{
183
				if (!isset($filenames[$image]))
184
				{
185
					$need_filenames[] = $image;
186
				}
187
			}
188
			$filenames = array_merge($filenames, self::get_filenames($need_filenames));
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\image\image::get_filenames() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
			$filenames = array_merge($filenames, self::/** @scrutinizer ignore-call */ get_filenames($need_filenames));
Loading history...
189
			$this->file->delete($filenames);
190
		}
191
192
		/**
193
		* Event delete images
194
		*
195
		* @event phpbbgallery.core.image.delete_images
196
		* @var	array	images			array of the image ids we are deleting
197
		* @var	array	filenames		array of the image filenames
198
		* @since 1.2.0
199
		*/
200
		$vars = array('images', 'filenames');
201
		extract($this->phpbb_dispatcher->trigger_event('phpbbgallery.core.image.delete_images', compact($vars)));
202
203
		$sql = 'SELECT *
204
			FROM ' . $this->table_images . '
205
			WHERE ' . $this->db->sql_in_set('image_id', $images);
206
		$result = $this->db->sql_query($sql);
207
		$resync_album_ids = $resync_contests = $targets = array();
208
		while ($row = $this->db->sql_fetchrow($result))
209
		{
210
			if ($row['image_contest_rank'])
211
			{
212
				$resync_contests[] = (int) $row['image_album_id'];
213
			}
214
			$resync_album_ids[] = (int) $row['image_album_id'];
215
			if ($row['image_status'] == \phpbbgallery\core\block::STATUS_UNAPPROVED)
216
			{
217
				$targets[$row['image_album_id']][$row['image_id']] = $row['image_user_id'];
218
			}
219
		}
220
221
		// Let's prepare notifications
222
		if (!empty($targets))
223
		{
224
			foreach ($targets as $album => $target)
225
			{
226
227
				$data = array(
228
					'targets'	=> array((int) current($target)),
229
					'album_id'	=> $album,
230
					'last_image'	=> key($target),
231
				);
232
				$this->notification_helper->notify('not_approved', $data);
233
			}
234
235
		}
236
237
		$this->db->sql_freeresult($result);
238
		$resync_contests = array_unique($resync_contests);
239
		$resync_album_ids = array_unique($resync_album_ids);
240
241
		$sql = 'DELETE FROM ' . $this->table_images . '
242
			WHERE ' . $this->db->sql_in_set('image_id', $images);
243
		$this->db->sql_query($sql);
244
245
		// The images need to be deleted, before we grab the new winners.
246
		$phpbb_gallery_contest->resync_albums($resync_contests);
247
		if ($resync_albums)
248
		{
249
			foreach ($resync_album_ids as $album_id)
250
			{
251
				$this->album->update_info($album_id);
252
			}
253
		}
254
255
		return true;
256
	}
257
258
	/**
259
	* Get the real filenames, so we can load/delete/edit the image-file.
260
	*
261
	* @param	mixed		$images		Array or integer with the image_id(s)
262
	* @return	array		Format: $image_id => $filename
263
	*/
264 4
	public function get_filenames($images)
265
	{
266 4
		if (empty($images))
267
		{
268
			return array();
269
		}
270
271 4
		$filenames = array();
272
		$sql = 'SELECT image_id, image_filename
273 4
			FROM ' . $this->table_images . '
274 4
			WHERE ' . $this->db->sql_in_set('image_id', $images);
275 4
		$result = $this->db->sql_query($sql);
276 4
		while ($row = $this->db->sql_fetchrow($result))
277
		{
278 3
			$filenames[(int) $row['image_id']] = $row['image_filename'];
279
		}
280 4
		$this->db->sql_freeresult($result);
281
282 4
		return $filenames;
283
	}
284
285
	/**
286
	 * Generate link to image
287
	 *
288
	 * @param    string $content what's in the link: image_name, thumbnail, fake_thumbnail, medium or lastimage_icon
289
	 * @param    string $mode where does the link leed to: highslide, lytebox, lytebox_slide_show, image_page, image, none
290
	 * @param    int $image_id
291
	 * @param    string $image_name
292
	 * @param    int $album_id
293
	 * @param    bool $is_gif we need to know whether we display a gif, so we can use a better medium-image
294
	 * @param    bool $count shall the image-link be counted as view? (Set to false from image_page.php to deny double increment)
295
	 * @param    string $additional_parameters additional parameters for the url, (starting with &amp;)
296
	 * @param int $next_image
297
	 * @return mixed
298
	 */
299 6
	public function generate_link($content, $mode, $image_id, $image_name, $album_id, $is_gif = false, $count = true, $additional_parameters = '', $next_image = 0)
300
	{
301 6
		$image_page_url = $this->helper->route('phpbbgallery_core_image', array('image_id' => (int) $image_id));
302
		//$image_page_url = $phpbb_ext_gallery_url->append_sid('image_page', "album_id=$album_id&amp;image_id=$image_id{$additional_parameters}");
303
		//$image_url = $phpbb_ext_gallery_url->append_sid('image', "album_id=$album_id&amp;image_id=$image_id{$additional_parameters}" . ((!$count) ? '&amp;view=no_count' : ''));
304 6
		$image_url = $this->url->show_image($image_id, 'medium');
305 6
		$thumb_url = $this->url->show_image($image_id, 'mini');
306 6
		$medium_url = $this->url->show_image($image_id, 'medium');
307
		//$medium_url = $phpbb_ext_gallery_url->append_sid('image', "mode=medium&amp;album_id=$album_id&amp;image_id=$image_id{$additional_parameters}");
308 6
		switch ($content)
309
		{
310 6
			case 'image_name':
311
				$shorten_image_name = $image_name;
312
				$content = '<span style="font-weight: bold; display: inline;">' . $shorten_image_name . '</span>';
313
			break;
314 6
			case 'image_name_unbold':
315
				$shorten_image_name = $image_name;
316
				$content = $shorten_image_name;
317
			break;
318 6
			case 'thumbnail':
319 6
				$content = '<img src="{U_THUMBNAIL}" alt="{IMAGE_NAME}" title="{IMAGE_NAME}" style="max-width: 100%; max-height: 100%"/>';
320 6
				$content = str_replace(array('{U_THUMBNAIL}', '{IMAGE_NAME}'), array($thumb_url, $image_name), $content);
321 6
			break;
322
			case 'fake_thumbnail':
323
				$content = '<img src="{U_THUMBNAIL}" alt="{IMAGE_NAME}" title="{IMAGE_NAME}" style="max-width: {FAKE_THUMB_SIZE}px; max-height: {FAKE_THUMB_SIZE}px;" />';
324
				$content = str_replace(array('{U_THUMBNAIL}', '{IMAGE_NAME}', '{FAKE_THUMB_SIZE}'), array($thumb_url, $image_name, $this->gallery_config->get('mini_thumbnail_size')), $content);
325
			break;
326
			case 'medium':
327
				$content = '<img src="{U_MEDIUM}" alt="{IMAGE_NAME}" title="{IMAGE_NAME}" class="postimage" />';
328
				$content = str_replace(array('{U_MEDIUM}', '{IMAGE_NAME}'), array($medium_url, $image_name), $content);
329
				//cheat for animated/transparent gifs
330
				if ($is_gif)
331
				{
332
					$content = '<img src="{U_MEDIUM}" alt="{IMAGE_NAME}" title="{IMAGE_NAME}" style="max-width: {MEDIUM_WIDTH_SIZE}px; max-height: {MEDIUM_HEIGHT_SIZE}px;" />';
333
					$content = str_replace(array('{U_MEDIUM}', '{IMAGE_NAME}', '{MEDIUM_HEIGHT_SIZE}', '{MEDIUM_WIDTH_SIZE}'), array($image_url, $image_name, $this->gallery_config->get('medium_height'), $this->gallery_config->get('medium_width')), $content);
334
				}
335
			break;
336
			case 'lastimage_icon':
337
				$content = $this->user->img('icon_topic_latest', 'VIEW_LATEST_IMAGE');
338
			break;
339
		}
340
341 6
		$url = $image_page_url;
342
343 6
		switch ($mode)
344
		{
345 6
			case 'image_page':
346 6
				$tpl = '<a href="{IMAGE_URL}" title="{IMAGE_NAME}" style="display: inline;">{CONTENT}</a>';
347 6
			break;
348
			case 'image_page_next':
349
				$tpl = '<a href="{IMAGE_URL}" title="{IMAGE_NAME}" class="right-box right">{CONTENT}</a>';
350
			break;
351
			case 'image_page_prev':
352
				$tpl = '<a href="{IMAGE_URL}" title="{IMAGE_NAME}" class="left-box left">{CONTENT}</a>';
353
			break;
354
			case 'image':
355
				$url = $image_url;
356
				$tpl = '<a href="{IMAGE_URL}" title="{IMAGE_NAME}">{CONTENT}</a>';
357
			break;
358
			case 'none':
359
				$tpl = '{CONTENT}';
360
			break;
361
			case 'next':
362
				if ($next_image)
363
				{
364
					$url = $this->url->append_sid('image_page', "album_id=$album_id&amp;image_id=$next_image{$additional_parameters}");
365
					$tpl = '<a href="{IMAGE_URL}" title="{IMAGE_NAME}">{CONTENT}</a>';
366
				}
367
				else
368
				{
369
					$tpl = '{CONTENT}';
370
				}
371
			break;
372
			default:
373
				$url = $image_url;
374
375
				$tpl = '{CONTENT}';
376
377
				/**
378
				* Event generate link
379
				*
380
				* @event phpbbgallery.core.image.generate_link
381
				* @var	string	mode	type of link
382
				* @var	string	tpl		html to be outputed
383
				* @since 1.2.0
384
				*/
385
				$vars = array('mode', 'tpl');
386
				extract($this->phpbb_dispatcher->trigger_event('phpbbgallery.core.image.generate_link', compact($vars)));//@todo: Correctly identify the event
387
			break;
388
		}
389
390 6
		return str_replace(array('{IMAGE_URL}', '{IMAGE_NAME}', '{CONTENT}'), array($url, $image_name, $content), $tpl);
391
	}
392
393
	/**
394
	* Handle user- & total image_counter
395
	*
396
	* @param	array	$image_id_ary	array with the image_ids which changed their status
397
	* @param	bool	$add			are we adding or removing the images
398
	* @param	bool	$readd			is it possible that there are images which aren't really changed
399
	*/
400
	public function handle_counter($image_id_ary, $add, $readd = false)
401
	{
402
		if (empty($image_id_ary))
403
		{
404
			return;
405
		}
406
407
		$num_images = $num_comments = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $num_comments is dead and can be removed.
Loading history...
408
		$sql = 'SELECT SUM(image_comments) as comments
409
			FROM ' . $this->table_images .'
410
			WHERE image_status ' . (($readd) ? '=' : '<>') . ' ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . '
411
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary) . '
412
			GROUP BY image_user_id';
413
		$result = $this->db->sql_query($sql);
414
		$num_comments = $this->db->sql_fetchfield('comments');
415
		$this->db->sql_freeresult($result);
416
417
		$sql = 'SELECT COUNT(image_id) images, image_user_id
418
			FROM ' . $this->table_images .' 
419
			WHERE image_status ' . (($readd) ? '=' : '<>') . ' ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . '
420
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary) . '
421
			GROUP BY image_user_id';
422
		$result = $this->db->sql_query($sql);
423
424
		while ($row = $this->db->sql_fetchrow($result))
425
		{
426
			$sql_ary = array(
0 ignored issues
show
Unused Code introduced by
The assignment to $sql_ary is dead and can be removed.
Loading history...
427
				'user_id'				=> (int) $row['image_user_id'],
428
				'user_images'			=> (int) $row['images'],
429
			);
430
			//@todo: phpbb_gallery_hookup::add_image($row['image_user_id'], (($add) ? $row['images'] : 0 - $row['images']));
431
432
			$num_images = $num_images + $row['images'];
433
434
			$this->gallery_user->set_user_id((int) $row['image_user_id'], false);
435
			$this->gallery_user->update_images((($add) ? $row['images'] : 0 - $row['images']));
436
		}
437
		$this->db->sql_freeresult($result);
438
439
		if ($add)
440
		{
441
			$this->gallery_config->inc('num_images', (int) $num_images);
442
			$this->gallery_config->inc('num_comments', (int) $num_comments);
443
		}
444
		else
445
		{
446
			$this->gallery_config->dec('num_images', (int) $num_images);
447
			$this->gallery_config->dec('num_comments', (int) $num_comments);
448
		}
449
	}
450
451 2
	public function get_image_data($image_id)
452
	{
453 2
		if (empty($image_id))
454
		{
455
			return;
456
		}
457
458 2
		$sql = 'SELECT * FROM ' . $this->table_images .' WHERE image_id = ' . (int) $image_id;
459 2
		$result = $this->db->sql_query($sql);
460 2
		$row = $this->db->sql_fetchrow($result);
461 2
		$this->db->sql_freeresult($result);
462
463 2
		if ($row)
464
		{
465 2
			return $row;
466
		}
467
	}
468
469
	/**
470
	* Approve image
471
	* @param (array)	$image_id_ary	The image ID array to be approved
472
	* @param (int)		$album_id	The album image is approved to (just save some queries for log)
473
	* return 0 on success
474
	*/
475
	public function approve_images($image_id_ary, $album_id)
476
	{
477
		$sql = 'SELECT image_id, image_name, image_user_id
478
			FROM ' . $this->table_images . ' 
479
			WHERE image_status = 0
480
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
481
		$result = $this->db->sql_query($sql);
482
		$targets = array();
483
		while ($row = $this->db->sql_fetchrow($result))
484
		{
485
			$this->gallery_log->add_log('moderator', 'approve', $album_id, $row['image_id'], array('LOG_GALLERY_APPROVED', $row['image_name']));
486
			$targets[] = $row['image_user_id'];
487
			$last_img = $row['image_id'];
488
		}
489
		$this->db->sql_freeresult($result);
490
		if (!empty($targets))
491
		{
492
			$data = array(
493
				'targets'	=> $targets,
494
				'album_id'	=> $album_id,
495
				'last_image'	=> $last_img,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $last_img does not seem to be defined for all execution paths leading up to this point.
Loading history...
496
			);
497
			$this->notification_helper->notify('approved', $data);
498
			$this->notification_helper->new_image($data);
499
		}
500
		$this->handle_counter($image_id_ary, true, true);
501
502
		$sql = 'UPDATE ' . $this->table_images . '
503
			SET image_status = ' . \phpbbgallery\core\block::STATUS_APPROVED . '
504
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
505
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
506
		$this->db->sql_query($sql);
507
	}
508
509
	/**
510
	* UnApprove image
511
	* @param (array)	$image_id_ary	The image ID array to be unapproved
512
	* @param (int)		$album_id	The album image is approved to (just save some queries for log)
513
	*/
514
	public function unapprove_images($image_id_ary, $album_id)
515
	{
516
		self::handle_counter($image_id_ary, false);
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\image\image::handle_counter() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

516
		self::/** @scrutinizer ignore-call */ 
517
        handle_counter($image_id_ary, false);
Loading history...
517
518
		$sql = 'UPDATE ' . $this->table_images .' 
519
			SET image_status = ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . '
520
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
521
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
522
		$this->db->sql_query($sql);
523
524
		$sql = 'SELECT image_id, image_name
525
			FROM ' . $this->table_images .' 
526
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
527
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
528
		$result = $this->db->sql_query($sql);
529
		while ($row = $this->db->sql_fetchrow($result))
530
		{
531
			$this->gallery_log->add_log('moderator', 'unapprove', $album_id, $row['image_id'], array('LOG_GALLERY_UNAPPROVED', $row['image_name']));
532
		}
533
		$this->db->sql_freeresult($result);
534
	}
535
536
	/**
537
	 * Move image
538
	 * @oaram (int)    $image_id    The image that we want to move_uploaded_file
539
	 * @param $image_id_ary
540
	 * @param $album_id
541
	 * @internal param $ (int)    $album_id    The album we want to move image to
542
	 */
543
	public function move_image($image_id_ary, $album_id)
544
	{
545
		$target_data = $this->album->get_info($album_id);
546
547
		// Store images to cache (so we can log them)
548
		$image_cache = $this->gallery_cache->get_images($image_id_ary);
549
		//TO DO - Contests
550
		$sql = 'UPDATE ' . $this->table_images . '
551
			SET image_album_id = ' . (int) $album_id . '
552
			WHERE ' . $this->db->sql_in_set('image_id', $image_id_ary);
553
		$this->db->sql_query($sql);
554
555
		$this->gallery_report->move_images($image_id_ary, $album_id);
556
557
		foreach ($image_id_ary as $image)
558
		{
559
			$this->gallery_log->add_log('moderator', 'move', 0, $image, array('LOG_GALLERY_MOVED', $image_cache[$image]['image_name'], $target_data['album_name']));
560
		}
561
		$this->gallery_cache->destroy_images();
562
		//You will need to take care for album sync for the target and source
563
	}
564
565
	/**
566
	* Lock images
567
	* @param (array)	$image_id_ary	Array of images we want to lock
568
	* @param (int)		$album_id		Album id, so we can log the action
569
	*/
570
	public function lock_images($image_id_ary, $album_id)
571
	{
572
		self::handle_counter($image_id_ary, false);
0 ignored issues
show
Bug Best Practice introduced by
The method phpbbgallery\core\image\image::handle_counter() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

572
		self::/** @scrutinizer ignore-call */ 
573
        handle_counter($image_id_ary, false);
Loading history...
573
574
		$sql = 'UPDATE ' . $this->table_images . ' 
575
			SET image_status = ' . \phpbbgallery\core\block::STATUS_LOCKED . '
576
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
577
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
578
		$this->db->sql_query($sql);
579
580
		$sql = 'SELECT image_id, image_name
581
			FROM ' . $this->table_images . ' 
582
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
583
				AND ' . $this->db->sql_in_set('image_id', $image_id_ary);
584
		$result = $this->db->sql_query($sql);
585
		while ($row = $this->db->sql_fetchrow($result))
586
		{
587
			$this->gallery_log->add_log('moderator', 'lock', $album_id, $row['image_id'], array('LOG_GALLERY_LOCKED', $row['image_name']));
588
		}
589
		$this->db->sql_freeresult($result);
590
	}
591
592
	/**
593
	* Get last image id
594
	* Return (int) image_id
595
	**/
596 3
	public function get_last_image()
597
	{
598 3
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
599 3
		$public = $this->album->get_public_albums();
600 3
		$sql_order = 'image_id DESC';
601 3
		$sql_limit = 1;
602
		$sql = 'SELECT * 
603 3
			FROM ' . $this->table_images . '
604 3
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . '
605 3
				AND ((' . $this->db->sql_in_set('image_album_id', $this->gallery_auth->acl_album_ids('i_view'), false, true) . ' AND image_status <> ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
606 3
					OR ' . $this->db->sql_in_set('image_album_id', $this->gallery_auth->acl_album_ids('m_status'), false, true) . ') AND ' . $this->db->sql_in_set('image_album_id', $public, true, true) . '
607 3
			ORDER BY ' . $sql_order;
608 3
		$result = $this->db->sql_query_limit($sql, $sql_limit);
609
610 3
		$row = $this->db->sql_fetchrow($result);
611
612 3
		$this->db->sql_freeresult($result);
613
614 3
		return $row;
615
	}
616 67
	public function assign_block($image_block_name, $image_data, $display_option = 0, $thumbnail_link = 'image_page', $imagename_link = 'image_page')
617
	{
618
		// Now let's get display options
619 67
		$show_ip         = ($display_option & self::IMAGE_SHOW_IP) !== 0;
620 67
		$show_ratings    = ($display_option & self::IMAGE_SHOW_RATINGS) !== 0;
621 67
		$show_username   = ($display_option & self::IMAGE_SHOW_USERNAME) !== 0;
622 67
		$show_views      = ($display_option & self::IMAGE_SHOW_VIEWS) !== 0;
623 67
		$show_time       = ($display_option & self::IMAGE_SHOW_TIME) !== 0;
624 67
		$show_imagename  = ($display_option & self::IMAGE_SHOW_IMAGENAME) !== 0;
625 67
		$show_comments   = ($display_option & self::IMAGE_SHOW_COMMENTS) !== 0;
626 67
		$show_album      = ($display_option & self::IMAGE_SHOW_ALBUM) !== 0;
627
628 67
		switch ($thumbnail_link)
629
		{
630 67
			case 'image_page':
631 63
				$action = $this->helper->route('phpbbgallery_core_image', array('image_id' => (int) $image_data['image_id']));
632 63
				break;
633 4
			case 'image':
634 2
				$action = $this->helper->route('phpbbgallery_core_image_file_source', array('image_id' => (int) $image_data['image_id']));
635 2
				break;
636
			default:
637 2
				$action = false;
638 2
				break;
639
		}
640
641 67
		switch ($imagename_link)
642
		{
643 67
			case 'image_page':
644 63
				$action_image = $this->helper->route('phpbbgallery_core_image', array('image_id' => (int) $image_data['image_id']));
645 63
				break;
646 4
			case 'image':
647 2
				$action_image = $this->helper->route('phpbbgallery_core_image_file_source', array('image_id' => (int) $image_data['image_id']));
648 2
				break;
649
			default:
650 2
				$action_image = false;
651 2
				break;
652
		}
653
654 67
		$this->template->assign_block_vars($image_block_name, array(
655 67
			'IMAGE_ID'		=> $image_data['image_id'],
656 67
			'U_IMAGE'		=> $show_imagename ? $action_image : false,
657 67
			'UC_IMAGE_NAME'	=> $show_imagename ? $image_data['image_name'] : false,
658 67
			'U_ALBUM'	=> $show_album ? $this->helper->route('phpbbgallery_core_album', array('album_id' => (int) $image_data['album_id'])) : false,
659 67
			'ALBUM_NAME'	=> $show_album ? $image_data['album_name'] : false,
660 67
			'IMAGE_VIEWS'	=> $show_views ? $image_data['image_view_count'] : -1,
661
			//'UC_THUMBNAIL'	=> 'self::generate_link('thumbnail', $phpbb_ext_gallery->config->get('link_thumbnail'), $image_data['image_id'], $image_data['image_name'], $image_data['image_album_id']),
662 67
			'UC_THUMBNAIL'		=> $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => (int) $image_data['image_id'])),
663 67
			'UC_THUMBNAIL_ACTION'	=> $action,
664 67
			'S_UNAPPROVED'	=> ($this->gallery_auth->acl_check('m_status', $image_data['image_album_id'], $image_data['album_user_id']) && ($image_data['image_status'] == \phpbbgallery\core\block::STATUS_UNAPPROVED)) ? true : false,
665 67
			'S_LOCKED'		=> ($image_data['image_status'] == \phpbbgallery\core\block::STATUS_LOCKED) ? true : false,
666 67
			'S_REPORTED'	=> ($this->gallery_auth->acl_check('m_report', $image_data['image_album_id'], $image_data['album_user_id']) && $image_data['image_reported']) ? true : false,
667 67
			'POSTER'		=> $show_username ? get_username_string('full', $image_data['image_user_id'], $image_data['image_username'], $image_data['image_user_colour']) : false,
0 ignored issues
show
Bug introduced by
The function get_username_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

667
			'POSTER'		=> $show_username ? /** @scrutinizer ignore-call */ get_username_string('full', $image_data['image_user_id'], $image_data['image_username'], $image_data['image_user_colour']) : false,
Loading history...
668 67
			'TIME'			=> $show_time ? $this->user->format_date($image_data['image_time']) : false,
669
670 67
			'S_RATINGS'		=> ($this->gallery_config->get('allow_rates') == 1 && $show_ratings) ? ($image_data['image_rates'] > 0 ? $image_data['image_rate_avg'] / 100 : $this->language->lang('NOT_RATED')) : false,
671 67
			'U_RATINGS'		=> $this->helper->route('phpbbgallery_core_image', array('image_id' => (int) $image_data['image_id'])) . '#rating',
672 67
			'L_COMMENTS'	=> ($image_data['image_comments'] == 1) ? $this->language->lang('COMMENT') : $this->language->lang('COMMENTS'),
673 67
			'S_COMMENTS'	=> $show_comments ? (($this->gallery_config->get('allow_comments') && $this->gallery_auth->acl_check('c_read', $image_data['image_album_id'], $image_data['album_user_id'])) ? (($image_data['image_comments']) ? $image_data['image_comments'] : $this->language->lang('NO_COMMENTS')) : '') : false,
674 67
			'U_COMMENTS'	=> $this->helper->route('phpbbgallery_core_image', array('image_id' => (int) $image_data['image_id'])) . '#comments',
675 67
			'U_USER_IP'		=> $show_ip && $this->gallery_auth->acl_check('m_status', $image_data['image_album_id'], $image_data['album_user_id']) ? $image_data['image_user_ip'] : false,
676
677 67
			'S_IMAGE_REPORTED'		=> $image_data['image_reported'],
678 67
			'U_IMAGE_REPORTED'		=> '',//($image_data['image_reported']) ? $phpbb_ext_gallery->url->append_sid('mcp', "mode=report_details&amp;album_id={$image_data['image_album_id']}&amp;option_id=" . $image_data['image_reported']) : '',
679 67
			'S_STATUS_APPROVED'		=> ($image_data['image_status'] == \phpbbgallery\core\block::STATUS_APPROVED),
680 67
			'S_STATUS_UNAPPROVED'	=> ($this->gallery_auth->acl_check('m_status', $image_data['image_album_id'], $image_data['album_user_id']) && $image_data['image_status'] == \phpbbgallery\core\block::STATUS_UNAPPROVED) ? true : false,
681 67
			'S_STATUS_UNAPPROVED_ACTION'	=> ($this->gallery_auth->acl_check('m_status', $image_data['image_album_id'], $image_data['album_user_id']) && $image_data['image_status'] == \phpbbgallery\core\block::STATUS_UNAPPROVED) ? $this->helper->route('phpbbgallery_core_moderate_image_approve', array('image_id' => (int) $image_data['image_id'])) : '',
682 67
			'S_STATUS_LOCKED'		=> ($image_data['image_status'] == \phpbbgallery\core\block::STATUS_LOCKED),
683
684 67
			'U_REPORT'	=> ($this->gallery_auth->acl_check('m_report', $image_data['image_album_id'], $image_data['album_user_id']) && $image_data['image_reported']) ? '123'/*$this->url->append_sid('mcp', "mode=report_details&amp;album_id={$image_data['image_album_id']}&amp;option_id=" . $image_data['image_reported'])*/ : '',
685 67
			'U_STATUS'	=> '',//($this->auth->acl_check('m_status', $image_data['image_album_id'], $album_user_id)) ? $phpbb_ext_gallery->url->append_sid('mcp', "mode=queue_details&amp;album_id={$image_data['image_album_id']}&amp;option_id=" . $image_data['image_id']) : '',
686 67
			'L_STATUS'	=> ($image_data['image_status'] == \phpbbgallery\core\block::STATUS_UNAPPROVED) ? $this->language->lang('APPROVE_IMAGE') : (($image_data['image_status'] == \phpbbgallery\core\block::STATUS_APPROVED) ? $this->language->lang('CHANGE_IMAGE_STATUS') : $this->language->lang('UNLOCK_IMAGE')),
687
		));
688 67
	}
689
}
690