Passed
Push — master ( de1c6e...8badc5 )
by Stanislav
14:22 queued 11:27
created

search::recent_count()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 55
ccs 21
cts 28
cp 0.75
rs 8.4444
cc 8
nc 18
nop 0
crap 9

How to fix   Long Method   

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 Luifer
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 <> ' . \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 <> ' . \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 <> ' . \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 <> ' . \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 33
		$rowset = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $rowset is dead and can be removed.
Loading history...
234
235 33
		$show_options = $this->gallery_config->get($fields);
236 33
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
237 33
		$imagename_link = $this->gallery_config->get('link_image_name');
238
239 33
		while ($row = $this->db->sql_fetchrow($result))
240
		{
241 33
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
242
		}
243
244 33
		$this->db->sql_freeresult($result);
245 33
	}
246
247
	/**
248
	* Get all recent images the user has access to
249
	* return (int) $images_count
250
	*/
251 2
	public function recent_count()
252
	{
253 2
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
254
255 2
		$exclude_albums = [];
256
257 2
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
258
		{
259
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
260
			$result = $this->db->sql_query($sql_no_user);
261
			while ($row = $this->db->sql_fetchrow($result))
262
			{
263
				$exclude_albums[] = (int) $row['album_id'];
264
			}
265
			$this->db->sql_freeresult($result);
266
		}
267
268 2
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
269
270
		// Get allowed album ids for view and mod permissions excluding excluded albums
271 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

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

340
		$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...
341 7
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
342
343 7
		$sql_array['SELECT'] = 'COUNT(c.comment_id) as count';
344 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
345 7
		$result = $this->db->sql_query($sql);
346 7
		$row = $this->db->sql_fetchrow($result);
347 7
		$this->db->sql_freeresult($result);
348 7
		$count = 0;
349 7
		if ($row)
350
		{
351 7
			$count = $row['count'];
352
		}
353 7
		$sql_array['SELECT'] = '*';
354 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
355 7
		$result = $this->db->sql_query_limit($sql, $sql_limit, $start);
356 7
		$rowset = array();
357
358 7
		$users_array = array();
359 7
		while ($row = $this->db->sql_fetchrow($result))
360
		{
361 6
			$rowset[] = $row;
362 6
			$users_array[$row['comment_user_id']] = array('');
363 6
			$users_array[$row['image_user_id']] = array('');
364
		}
365 7
		$this->db->sql_freeresult($result);
366 7
		if (empty($rowset))
367
		{
368 1
			$this->template->assign_vars(array(
369 1
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RECENT_COMMENTS'),
370
			));
371 1
			return;
372
		}
373
374 6
		$this->user_loader->load_users(array_keys($users_array));
375 6
		foreach ($rowset as $var)
376
		{
377
378 6
			$album_tmp = $this->album->get_info($var['image_album_id']);
379 6
			$this->template->assign_block_vars('commentrow', array(
380 6
				'COMMENT_ID'	=> (int) $var['comment_id'],
381 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,
382 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,
383 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,
384 6
				'U_COMMENT'	=> $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '#comment_' . $var['comment_id'],
385 6
				'POST_AUTHOR_FULL'	=> (string) $this->user_loader->get_username($var['comment_user_id'], 'full'),
386 6
				'TIME'	=> $this->user->format_date($var['comment_time']),
387 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

387
				'TEXT'	=> /** @scrutinizer ignore-call */ generate_text_for_display($var['comment'], $var['comment_uid'], $var['comment_bitfield'], 7),
Loading history...
388 6
				'UC_IMAGE_NAME'	=> '<a href="' . $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '">' . $var['image_name'] . '</a>',
389
				// 'UC_THUMBNAIL'		=> $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $var['image_id'])),
390 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']),
391 6
				'IMAGE_AUTHOR'		=> $this->user_loader->get_username((int) $var['image_user_id'], 'full'),
392 6
				'IMAGE_TIME'		=> $this->user->format_date($var['image_time']),
393
			));
394
		}
395 6
		$this->template->assign_vars(array(
396 6
			'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_COMMENTS_SPRINTF', $count),
397 6
			'SEARCH_TITLE'		=> $this->language->lang('RECENT_COMMENTS'),
398
		));
399 6
		if ($pagination)
400
		{
401 6
			$this->pagination->generate_template_pagination(array(
402
				'routes' => array(
403 6
					'phpbbgallery_core_search_commented',
404
					'phpbbgallery_core_search_commented_page',),
405 6
					'params' => array()), '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
			'WHERE'	=> 'image_status <> ' . \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
		$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 . ')
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

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