Passed
Push — master ( 00f471...a34a93 )
by Stanislav
03:48 queued 14s
created

search::random()   F

Complexity

Conditions 16
Paths 481

Size

Total Lines 122
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 53
CRAP Score 18.7482

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 73
c 3
b 0
f 0
dl 0
loc 122
ccs 53
cts 68
cp 0.7794
rs 2.1208
cc 16
nc 481
nop 5
crap 18.7482

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
* @package phpBB Gallery Core
5
* @copyright (c) 2014 Lucifer
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace phpbbgallery\core;
11
12
class search
13
{
14
	/* @var \phpbb\db\driver\driver */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver 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...
15
	protected $db;
16
17
	/* @var \phpbb\request\request */
0 ignored issues
show
Bug introduced by
The type phpbb\request\request 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
	protected $request;
19
20
	/* @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...
21
	protected $template;
22
23
	/* @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...
24
	protected $user;
25
26
	/* @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...
27
	protected $language;
28
29
	/* @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...
30
	protected $helper;
31
32
	/* @var \phpbbgallery\core\config */
33
	protected $gallery_config;
34
35
	/* @var \phpbbgallery\core\auth\auth */
36
	protected $gallery_auth;
37
38
	/* @var \phpbbgallery\core\album\album */
39
	protected $album;
40
41
	/* @var \phpbbgallery\core\image\image */
42
	protected $image;
43
44
	/* @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...
45
	protected $pagination;
46
47
	/* @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...
48
	protected $user_loader;
49
50
	/* @var string */
51
	protected $images_table;
52
53
	/* @var string */
54
	protected $albums_table;
55
56
	/* @var string */
57
	protected $comments_table;
58
59
	/* @var \phpbbgallery\core\album\display */
60
	protected $display;
61
62
	/* @var string */
63
	protected $root_path;
64
65
	/* @var string */
66
	protected $php_ext;
67
68
	/**
69
	 * Constructor
70
	 *
71
	 * @param \phpbb\db\driver\driver|\phpbb\db\driver\driver_interface $db       Database object
72
	 * @param \phpbb\template\template                                  $template Template object
73
	 * @param \phpbb\user                                               $user     User object
74
	 * @param \phpbb\language\language                                  $language
75
	 * @param \phpbb\controller\helper                                  $helper   Controller helper object
76
	 * @param config                                                    $gallery_config
77
	 * @param auth\auth                                                 $gallery_auth
78
	 * @param album\album                                               $album
79
	 * @param image\image                                               $image
80
	 * @param \phpbb\pagination                                         $pagination
81
	 * @param \phpbb\user_loader                                        $user_loader
82
	 * @param                                                           $images_table
83
	 * @param                                                           $albums_table
84
	 * @param                                                           $comments_table
85
	 * @internal param \phpbb\auth\auth $auth Auth object
86
	 * @internal param \phpbb\config\config $config Config object
87
	 * @internal param \phpbb\request\request $request Request object
88
	 * @internal param album\display $display Albums display object
89
	 * @internal param string $root_path Root path
90
	 * @internal param string $php_ext php file extension
91
	 */
92 90
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user,
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...
93
		\phpbb\language\language $language, \phpbb\controller\helper $helper, \phpbbgallery\core\config $gallery_config,
94
		\phpbbgallery\core\auth\auth $gallery_auth, \phpbbgallery\core\album\album $album, \phpbbgallery\core\image\image $image,
95
		\phpbb\pagination $pagination, \phpbb\user_loader $user_loader,
96
		$images_table, $albums_table, $comments_table)
97
	{
98 90
		$this->db = $db;
99 90
		$this->template = $template;
100 90
		$this->user = $user;
101 90
		$this->language = $language;
102 90
		$this->helper = $helper;
103 90
		$this->gallery_config = $gallery_config;
104 90
		$this->gallery_auth = $gallery_auth;
105 90
		$this->album = $album;
106 90
		$this->image = $image;
107 90
		$this->pagination = $pagination;
108 90
		$this->user_loader = $user_loader;
109 90
		$this->images_table = $images_table;
110 90
		$this->albums_table = $albums_table;
111 90
		$this->comments_table = $comments_table;
112 90
	}
113
114
	/**
115
	 * Generate random images and populate template
116
	 * @param (int)    $limit    how many images to generate_link
117
	 * @param int $user
118
	 * @param string $fields
119
	 * @param bool $block_name
120
	 * @param bool $u_block
121
	 */
122 33
	public function random($limit, $user = 0, $fields = 'rrc_gindex_display', $block_name = false, $u_block = false)
123
	{
124
		// We will do small escape for not devising by 0
125 33
		if ($limit == 0)
126
		{
127
			return;
128
		}
129 33
		if ($limit < -1)
130
		{
131
			$limit = -1;
132
		}
133
		// Define some vars
134 33
		$images_per_page = $limit;
135
136 33
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
137
138 33
		switch ($this->db->get_sql_layer())
139
		{
140 33
			case 'postgres':
141 33
			case 'sqlite3':
142
				$sql_order = 'RANDOM()';
143
			break;
144
145 33
			case 'mssql':
146 33
			case 'mssql_odbc':
147
				$sql_order = 'NEWID()';
148
			break;
149
150
			default:
151 33
				$sql_order = 'RAND()';
152 33
			break;
153
		}
154 33
		$sql_limit = $images_per_page;
155
		$sql = 'SELECT image_id
156 33
			FROM ' . $this->images_table . '
157 33
			WHERE image_status <> ' . (int) \phpbbgallery\core\block::STATUS_ORPHAN;
158 33
		if ($user > 0)
159
		{
160 30
			$sql .= ' and image_user_id = ' . (int) $user;
161
		}
162 33
		$exclude_albums = array();
163 33
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
164
		{
165
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
166
			$result = $this->db->sql_query($sql_no_user);
167
			while ($row = $this->db->sql_fetchrow($result))
168
			{
169
				$exclude_albums[] = (int) $row['album_id'];
170
			}
171
			$this->db->sql_freeresult($result);
172
		}
173 33
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
174 33
		$sql .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
0 ignored issues
show
Bug introduced by
It seems like $this->gallery_auth->acl_album_ids('i_view') can also be of type true; however, parameter $array of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

174
		$sql .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff(/** @scrutinizer ignore-type */ $this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
Loading history...
175 33
					OR (' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('a_list'), $exclude_albums), false, true) . ' AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
176 33
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')
177 33
			ORDER BY ' . $sql_order;
178
179 33
		if (!$sql_limit)
180
		{
181
			$result = $this->db->sql_query($sql);
182
		}
183
		else
184
		{
185 33
			$result = $this->db->sql_query_limit($sql, $sql_limit);
186
		}
187 33
		$id_ary = array();
188 33
		while ($row = $this->db->sql_fetchrow($result))
189
		{
190 33
			$id_ary[] = $row['image_id'];
191
		}
192 33
		$this->db->sql_freeresult($result);
193
194 33
		$total_match_count = sizeof($id_ary);
195
196 33
		$l_search_matches = $this->language->lang('FOUND_SEARCH_MATCHES', $total_match_count);
0 ignored issues
show
Unused Code introduced by
The assignment to $l_search_matches is dead and can be removed.
Loading history...
197
198 33
		$this->template->assign_block_vars('imageblock', array(
199 33
			'BLOCK_NAME'	=> $block_name ? $block_name : $this->language->lang('RANDOM_IMAGES'),
200 33
			'U_BLOCK'	=> $u_block ? $u_block : $this->helper->route('phpbbgallery_core_search_random'),
201
		));
202
203
		// For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
204 33
		if (!sizeof($id_ary))
205
		{
206
			$this->template->assign_block_vars('imageblock', array(
207
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RANDOM'),
208
			));
209
			return;
210
		}
211
212 33
		$id_ary = array_map('intval', $id_ary);
213
214 33
		$sql_where = $this->db->sql_in_set('i.image_id', $id_ary);
215
216
		$sql_array = array(
217 33
			'SELECT'		=> 'i.*, a.album_name, a.album_status, a.album_user_id, album_id',
218 33
			'FROM'			=> array($this->images_table => 'i'),
219
220
			'LEFT_JOIN'		=> array(
221
				array(
222 33
					'FROM'		=> array($this->albums_table => 'a'),
223 33
					'ON'		=> 'a.album_id = i.image_album_id',
224
				),
225
			),
226
227 33
			'WHERE'			=> 'i.image_status <> ' . (int) \phpbbgallery\core\block::STATUS_ORPHAN . ' AND ' . $sql_where,
228 33
			'GROUP_BY'	=> 'i.image_id, a.album_name, a.album_status, a.album_user_id, a.album_id',
229 33
			'ORDER_BY'		=> $sql_order,
230
		);
231 33
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
232 33
		$result = $this->db->sql_query($sql);
233
234 33
		$show_options = $this->gallery_config->get($fields);
235 33
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
236 33
		$imagename_link = $this->gallery_config->get('link_image_name');
237
238 33
		while ($row = $this->db->sql_fetchrow($result))
239
		{
240 33
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
241
		}
242
243 33
		$this->db->sql_freeresult($result);
244 33
	}
245
246
	/**
247
	* Get all recent images the user has access to
248
	* return (int) $images_count
249
	*/
250 2
	public function recent_count()
251
	{
252 2
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
253
254 2
		$exclude_albums = [];
255
256 2
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
257
		{
258
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
259
			$result = $this->db->sql_query($sql_no_user);
260
			while ($row = $this->db->sql_fetchrow($result))
261
			{
262
				$exclude_albums[] = (int) $row['album_id'];
263
			}
264
			$this->db->sql_freeresult($result);
265
		}
266
267 2
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
268
269
		// Get allowed album ids for view and mod permissions excluding excluded albums
270 2
		$view_album_ids = array_diff($this->gallery_auth->acl_album_ids('i_view'), $exclude_albums);
0 ignored issues
show
Bug introduced by
It seems like $this->gallery_auth->acl_album_ids('i_view') can also be of type true; however, parameter $array of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

270
		$view_album_ids = array_diff(/** @scrutinizer ignore-type */ $this->gallery_auth->acl_album_ids('i_view'), $exclude_albums);
Loading history...
271 2
		$mod_album_ids = array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums);
272
273 2
		if (empty($view_album_ids) && empty($mod_album_ids))
274
		{
275
			return 0;
276
		}
277
278
		$sql = 'SELECT COUNT(image_id) AS count
279 2
			FROM ' . $this->images_table . '
280 2
			WHERE image_status <> ' . (int) \phpbbgallery\core\block::STATUS_ORPHAN;
281
282 2
		$conditions = [];
283
284 2
		if (!empty($view_album_ids))
285
		{
286 2
			$conditions[] = '(' . $this->db->sql_in_set('image_album_id', $view_album_ids) . '
287 2
				AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')';
288
		}
289
290 2
		if (!empty($mod_album_ids))
291
		{
292
			$conditions[] = $this->db->sql_in_set('image_album_id', $mod_album_ids);
293
		}
294
295 2
		if (!empty($conditions))
296
		{
297 2
			$sql .= ' AND (' . implode(' OR ', $conditions) . ')';
298
		}
299
300 2
		$result = $this->db->sql_query($sql);
301 2
		$row = $this->db->sql_fetchrow($result);
302 2
		$this->db->sql_freeresult($result);
303
304 2
		return (int) $row['count'];
305
	}
306
307
308
309
	/**
310
	 * recent comments
311
	 * @param (int)    $limit How many images to query
312
	 * @param int $start
313
	 */
314 7
	public function recent_comments($limit, $start = 0, $pagination = true)
315
	{
316 7
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
317 7
		$sql_limit = $limit;
318 7
		$exclude_albums = array();
319 7
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
320
		{
321
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
322
			$result = $this->db->sql_query($sql_no_user);
323
			while ($row = $this->db->sql_fetchrow($result))
324
			{
325
				$exclude_albums[] = (int) $row['album_id'];
326
			}
327
			$this->db->sql_freeresult($result);
328
		}
329 7
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
330
		$sql_array = array(
331
			'FROM' => array(
332 7
				$this->images_table => 'i',
333 7
				$this->comments_table => 'c',
334
			),
335 7
			'WHERE'	=> 'i.image_id = c.comment_image_id and ' . $this->db->sql_in_set('image_album_id', $this->gallery_auth->acl_album_ids('c_read'), false, true),
336 7
			'GROUP_BY'	=> 'c.comment_id, c.comment_time, i.image_id',
337 7
			'ORDER_BY'	=> 'comment_time DESC'
338
		);
339 7
		$sql_array['WHERE'] .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
0 ignored issues
show
Bug introduced by
It seems like $this->gallery_auth->acl_album_ids('i_view') can also be of type true; however, parameter $array of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

339
		$sql_array['WHERE'] .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff(/** @scrutinizer ignore-type */ $this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND image_status <> ' . (int) \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
Loading history...
340 7
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
341
342 7
		$sql_array_count = $sql_array;
343 7
		$sql_array_count['SELECT'] = 'COUNT(c.comment_id) as count';
344 7
		unset($sql_array_count['GROUP_BY'], $sql_array_count['ORDER_BY']);
345 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array_count);
346 7
		$result = $this->db->sql_query($sql);
347 7
		$row = $this->db->sql_fetchrow($result);
348 7
		$this->db->sql_freeresult($result);
349
350 7
		$count = ($row) ? (int) $row['count'] : 0;
351
352 7
		$sql_array['SELECT'] = '*';
353 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
354 7
		$result = $this->db->sql_query_limit($sql, $sql_limit, $start);
355 7
		$rowset = [];
356
357 7
		$users_array = [];
358 7
		while ($row = $this->db->sql_fetchrow($result))
359
		{
360 6
			$rowset[] = $row;
361 6
			$users_array[$row['comment_user_id']] = array('');
362 6
			$users_array[$row['image_user_id']] = array('');
363
		}
364 7
		$this->db->sql_freeresult($result);
365 7
		if (empty($rowset))
366
		{
367 1
			$this->template->assign_vars(array(
368 1
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RECENT_COMMENTS'),
369
			));
370 1
			return;
371
		}
372
373 6
		$this->user_loader->load_users(array_keys($users_array));
374 6
		foreach ($rowset as $var)
375
		{
376 6
			$album_tmp = $this->album->get_info($var['image_album_id']);
377 6
			$this->template->assign_block_vars('commentrow', array(
378 6
				'COMMENT_ID'	=> (int) $var['comment_id'],
379 6
				'U_DELETE'	=> ($this->gallery_auth->acl_check('m_comments', $album_tmp['album_id'], $album_tmp['album_user_id']) || ($this->gallery_auth->acl_check('c_delete', $album_tmp['album_id'], $album_tmp['album_user_id']) && ($var['comment_user_id'] == $this->user->data['user_id']) && $this->user->data['is_registered'])) ? $this->helper->route('phpbbgallery_core_comment_delete', array('image_id' => $var['comment_image_id'], 'comment_id' => $var['comment_id'])) : false,
380 6
				'U_EDIT'	=> $this->gallery_auth->acl_check('c_edit', $album_tmp['album_id'], $album_tmp['album_user_id'])? $this->helper->route('phpbbgallery_core_comment_edit', array('image_id'	=> $var['comment_image_id'], 'comment_id'	=> $var['comment_id'])) : false,
381 6
				'U_QUOTE'	=> ($this->gallery_auth->acl_check('c_post', $album_tmp['album_id'], $album_tmp['album_user_id'])) ? $this->helper->route('phpbbgallery_core_comment_add', array('image_id'	=> $var['comment_image_id'], 'comment_id'	=> $var['comment_id'])) : false,
382 6
				'U_COMMENT'	=> $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '#comment_' . $var['comment_id'],
383 6
				'POST_AUTHOR_FULL'	=> (string) $this->user_loader->get_username($var['comment_user_id'], 'full'),
384 6
				'TIME'	=> $this->user->format_date($var['comment_time']),
385 6
				'TEXT'	=> generate_text_for_display($var['comment'], $var['comment_uid'], $var['comment_bitfield'], 7),
0 ignored issues
show
Bug introduced by
The function generate_text_for_display 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

385
				'TEXT'	=> /** @scrutinizer ignore-call */ generate_text_for_display($var['comment'], $var['comment_uid'], $var['comment_bitfield'], 7),
Loading history...
386 6
				'UC_IMAGE_NAME'	=> '<a href="' . $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '">' . $var['image_name'] . '</a>',
387
				// 'UC_THUMBNAIL'		=> $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $var['image_id'])),
388 6
				'UC_THUMBNAIL'		=> $this->image->generate_link('thumbnail', $this->gallery_config->get('link_thumbnail'), $var['comment_image_id'], $var['image_name'], $var['image_album_id']),
389 6
				'IMAGE_AUTHOR'		=> $this->user_loader->get_username((int) $var['image_user_id'], 'full'),
390 6
				'IMAGE_TIME'		=> $this->user->format_date($var['image_time']),
391
			));
392
		}
393 6
		$this->template->assign_vars([
394 6
			'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_COMMENTS_SPRINTF', $count),
395 6
			'SEARCH_TITLE'		=> $this->language->lang('RECENT_COMMENTS'),
396
		]);
397 6
		if ($pagination)
398
		{
399 6
			$this->pagination->generate_template_pagination([
400
				'routes' => [
401 6
						'phpbbgallery_core_search_commented',
402
						'phpbbgallery_core_search_commented_page'
403
					],
404
					'params' => []
405 6
				], 'pagination', 'page', $count, $limit, $start
406
			);
407
		}
408 6
	}
409
410
	/**
411
	 * Generate recent images and populate template
412
	 * @param (int)    $limit How many images to query
413
	 * @param int $start
414
	 * @param int $user
415
	 * @param string $fields
416
	 * @param bool $block_name
417
	 * @param bool $u_block
418
	 */
419 34
	public function recent($limit, $start = 0, $user = 0, $fields = 'rrc_gindex_display', $block_name = false, $u_block = false)
420
	{
421
		// We will do small escape for not devising by 0
422 34
		if ($limit == 0)
423
		{
424
			return;
425
		}
426 34
		if ($limit < -1)
427
		{
428
			$limit = -1;
429
		}
430 34
		$pagination = true;
431 34
		if ($start == -1)
432
		{
433 1
			$start = 0;
434 1
			$pagination = false;
435
		}
436 34
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
437 34
		$sql_order = '';
438 34
		switch ($this->gallery_config->get('default_sort_key'))
439
		{
440 34
			case 't':
441 34
				$sql_order = 'image_time';
442 34
				break;
443
			case 'n':
444
				$sql_order = 'image_name_clean';
445
				break;
446
			case 'vc':
447
				$sql_order = 'image_view_count';
448
				break;
449
			case 'u':
450
				$sql_order = 'image_username_clean';
451
				break;
452
			case 'ra':
453
				$sql_order = 'image_rate_avg';
454
				break;
455
			case 'r':
456
				$sql_order = 'image_rates';
457
				break;
458
			case 'c':
459
				$sql_order = 'image_comments';
460
				break;
461
			case 'lc':
462
				$sql_order = 'image_last_comment';
463
				break;
464
		}
465 34
		$sql_order = $sql_order . ($this->gallery_config->get('default_sort_dir') == 'd' ? ' DESC' : ' ASC');
466 34
		$sql_limit = $limit;
467 34
		$exclude_albums = array();
468 34
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
469
		{
470
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
471
			$result = $this->db->sql_query($sql_no_user);
472
			while ($row = $this->db->sql_fetchrow($result))
473
			{
474
				$exclude_albums[] = (int) $row['album_id'];
475
			}
476
			$this->db->sql_freeresult($result);
477
		}
478 34
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
479
		$sql_ary = array(
480
			'FROM'	=>	array(
481 34
				$this->images_table	=> 'i'
482
			),
483 34
			'WHERE'	=> 'image_status <> ' . (int) \phpbbgallery\core\block::STATUS_ORPHAN
484
		);
485 34
		if ($user > 0)
486
		{
487 30
			$sql_ary['WHERE'] .= ' and image_user_id = ' . (int) $user;
488
		}
489 34
		$user_id = $this->user->data['user_id'];
490 34
		$sql_ary['WHERE'] .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND (image_status <> ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . ' OR image_user_id = ' . $user_id . '))
0 ignored issues
show
Bug introduced by
It seems like $this->gallery_auth->acl_album_ids('i_view') can also be of type true; however, parameter $array of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

490
		$sql_ary['WHERE'] .= ' AND ((' . $this->db->sql_in_set('image_album_id', array_diff(/** @scrutinizer ignore-type */ $this->gallery_auth->acl_album_ids('i_view'), $exclude_albums), false, true) . ' AND (image_status <> ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . ' OR image_user_id = ' . $user_id . '))
Loading history...
491 34
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
492
493 34
		$sql_ary['SELECT'] = 'COUNT(image_id) as count';
494 34
		$sql = $this->db->sql_build_query('SELECT', $sql_ary);
495 34
		$result = $this->db->sql_query($sql);
496 34
		$row = $this->db->sql_fetchrow($result);
497 34
		$this->db->sql_freeresult($result);
498 34
		$count = $row['count'];
499
500 34
		$sql_ary['SELECT'] = 'image_id';
501 34
		$sql_ary['ORDER_BY'] = $sql_order;
502 34
		$sql = $this->db->sql_build_query('SELECT', $sql_ary);
503 34
		$result = $this->db->sql_query_limit($sql, $sql_limit, $start);
504 34
		$id_ary = [];
505 34
		while ($row = $this->db->sql_fetchrow($result))
506
		{
507 34
			$id_ary[] = $row['image_id'];
508
		}
509
510 34
		$this->db->sql_freeresult($result);
511
512 34
		$total_match_count = sizeof($id_ary);
513
514 34
		$l_search_matches = $this->language->lang('FOUND_SEARCH_MATCHES', $total_match_count);
0 ignored issues
show
Unused Code introduced by
The assignment to $l_search_matches is dead and can be removed.
Loading history...
515
516 34
		if ($user > 0)
517
		{
518 30
			$this->template->assign_block_vars('imageblock', array(
519 30
				'BLOCK_NAME'	=> $block_name ? $block_name : '' ,
520 30
				'U_BLOCK'	=> $u_block ? $u_block : $this->helper->route('phpbbgallery_core_search_egosearch'),
521
			));
522
		}
523
		else
524
		{
525 4
			$this->template->assign_block_vars('imageblock', array(
526 4
				'BLOCK_NAME'	=>  $block_name ? $block_name : $this->language->lang('RECENT_IMAGES'),
527 4
				'U_BLOCK'	=> $u_block ? $u_block : $this->helper->route('phpbbgallery_core_search_recent'),
528
			));
529
		}
530
531
		// For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
532 34
		if (!sizeof($id_ary))
533
		{
534
			$this->template->assign_block_vars('imageblock', array(
535
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RECENT')
536
			));
537
			return;
538
		}
539
540 34
		$id_ary = array_map('intval', $id_ary);
541
542 34
		$sql_where = $this->db->sql_in_set('i.image_id', $id_ary);
543
544
		$sql_array = array(
545 34
			'SELECT'		=> 'i.*, a.album_name, a.album_status, a.album_user_id, a.album_id',
546 34
			'FROM'			=> array($this->images_table => 'i'),
547
548
			'LEFT_JOIN'		=> array(
549
				array(
550 34
					'FROM'		=> array($this->albums_table => 'a'),
551 34
					'ON'		=> 'a.album_id = i.image_album_id',
552
				),
553
			),
554
555 34
			'WHERE'			=> 'i.image_status <> ' . (int) \phpbbgallery\core\block::STATUS_ORPHAN . ' AND ' . $sql_where,
556 34
			'ORDER_BY'		=> $sql_order,
557
		);
558 34
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
559 34
		$result = $this->db->sql_query($sql);
560
561 34
		$show_options = $this->gallery_config->get($fields);
562 34
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
563 34
		$imagename_link = $this->gallery_config->get('link_image_name');
564
565 34
		while ($row = $this->db->sql_fetchrow($result))
566
		{
567 34
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
568
		}
569 34
		$this->db->sql_freeresult($result);
570
571 34
		if ($user > 0)
572
		{
573 30
			$this->template->assign_vars(array(
574 30
				'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_IMAGES_SPRINTF', $count),
575 30
				'SEARCH_TITLE'		=> $this->language->lang('SEARCH_USER_IMAGES_OF', $this->user->data['username']),
576
			));
577 30
			$this->pagination->generate_template_pagination(array(
578
				'routes' => array(
579 30
					'phpbbgallery_core_search_egosearch',
580
					'phpbbgallery_core_search_egosearch_page',),
581 30
					'params' => array()), 'pagination', 'page', $count, $limit, $start
582
			);
583
		}
584
		else
585
		{
586 4
			$this->template->assign_vars(array(
587 4
				'TOTAL_IMAGES'				=> $this->language->lang('VIEW_ALBUM_IMAGES', $count),
588
			));
589 4
			if ($pagination)
590
			{
591 3
				$this->pagination->generate_template_pagination(array(
592
					'routes' => array(
593 3
						'phpbbgallery_core_search_recent',
594
						'phpbbgallery_core_search_recent_page',),
595 3
						'params' => array()), 'pagination', 'page', $count, $limit, $start
596
				);
597
			}
598
		}
599 34
	}
600
601
	/**
602
	 * Get top rated image
603
	 * @param $limit
604
	 * @param int $start
605
	 */
606
	public function rating($limit, $start = 0)
607
	{
608
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
609
		$sql_array = array();
610
		$sql_array['FROM'] = array(
611
			$this->images_table	=> 'i'
612
		);
613
		$sql_array['WHERE'] = $this->db->sql_in_set('image_album_id', $this->gallery_auth->acl_album_ids('i_view'), false, true) . ' and image_rate_avg <> 0';
614
		$sql_array['SELECT'] = 'COUNT(image_id) as count';
615
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
616
		$result = $this->db->sql_query($sql);
617
		$row = $this->db->sql_fetchrow($result);
618
		$this->db->sql_freeresult($result);
619
		$count = $row['count'];
620
		$sql_array['SELECT'] = '* , a.album_name, a.album_status, a.album_user_id, a.album_id';
621
		$sql_array['LEFT_JOIN']	= array(
622
			array(
623
				'FROM'		=> array($this->albums_table => 'a'),
624
				'ON'		=> 'a.album_id = i.image_album_id',
625
			)
626
		);
627
		$sql_array['ORDER_BY'] = 'image_rate_avg DESC, image_rates DESC';
628
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
629
		$result = $this->db->sql_query_limit($sql, $limit, $start);
630
		$rowset = [];
631
632
		while ($row = $this->db->sql_fetchrow($result))
633
		{
634
			$rowset[] = $row;
635
			$users_array[$row['image_user_id']] = array('');
636
		}
637
		$this->db->sql_freeresult($result);
638
		if (empty($rowset))
639
		{
640
			$this->template->assign_var('S_NO_SEARCH', true);
641
			trigger_error('NO_SEARCH');
642
		}
643
644
		$this->template->assign_block_vars('imageblock', array(
645
			'BLOCK_NAME'	=> $this->language->lang('SEARCH_TOPRATED'),
646
			'U_BLOCK'	=> $this->helper->route('phpbbgallery_core_search_toprated'),
647
		));
648
		$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...
649
		// Now let's get display options
650
		$show_options = $this->gallery_config->get('rrc_gindex_display');
651
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
652
		$imagename_link = $this->gallery_config->get('link_image_name');
653
		foreach ($rowset as $row)
654
		{
655
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
656
		}
657
658
		$this->template->assign_vars(array(
659
			'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_IMAGES_SPRINTF', $count),
660
			'SEARCH_TITLE'		=> $this->language->lang('SEARCH_TOPRATED'),
661
		));
662
		$this->pagination->generate_template_pagination(array(
663
			'routes' => array(
664
				'phpbbgallery_core_search_toprated',
665
				'phpbbgallery_core_search_toprated_page',),
666
				'params' => array()), 'pagination', 'page', $count, $limit, $start
667
		);
668
	}
669
}
670