Passed
Push — master ( 7edf3e...70dac4 )
by Stanislav
31:43 queued 28:56
created

search::rating()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 61
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 61
ccs 0
cts 42
cp 0
rs 9.216
cc 4
nop 2
crap 20
nc 8

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
		$sql_where = $this->db->sql_in_set('i.image_id', $id_ary);
213
214
		$sql_array = array(
215 33
			'SELECT'		=> 'i.*, a.album_name, a.album_status, a.album_user_id, album_id',
216 33
			'FROM'			=> array($this->images_table => 'i'),
217
218
			'LEFT_JOIN'		=> array(
219
				array(
220 33
					'FROM'		=> array($this->albums_table => 'a'),
221 33
					'ON'		=> 'a.album_id = i.image_album_id',
222
				),
223
			),
224
225 33
			'WHERE'			=> 'i.image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . ' AND ' . $sql_where,
226 33
			'GROUP_BY'	=> 'i.image_id, a.album_name, a.album_status, a.album_user_id, a.album_id',
227 33
			'ORDER_BY'		=> $sql_order,
228
		);
229 33
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
230 33
		$result = $this->db->sql_query($sql);
231 33
		$rowset = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $rowset is dead and can be removed.
Loading history...
232
233 33
		$show_options = $this->gallery_config->get($fields);
234 33
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
235 33
		$imagename_link = $this->gallery_config->get('link_image_name');
236
237 33
		while ($row = $this->db->sql_fetchrow($result))
238
		{
239 33
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
240
		}
241
242 33
		$this->db->sql_freeresult($result);
243 33
	}
244
245
	/**
246
	* Get all recent images the user has access to
247
	* return (int) $images_count
248
	*/
249 2
	public function recent_count()
250
	{
251 2
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
252
253
		$sql = 'SELECT COUNT(image_id) as count
254 2
			FROM ' . $this->images_table . '
255 2
			WHERE image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN;
256 2
		$exclude_albums = array();
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 2
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
268 2
		$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

268
		$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...
269 2
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
270 2
		$result = $this->db->sql_query($sql);
271 2
		$row = $this->db->sql_fetchrow($result);
272 2
		return (int) $row['count'];
273
	}
274
275
	/**
276
	 * recent comments
277
	 * @param (int)    $limit How many imagese to query
278
	 * @param int $start
279
	 */
280 7
	public function recent_comments($limit, $start = 0)
281
	{
282 7
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
283 7
		$sql_limit = $limit;
284 7
		$exclude_albums = array();
285 7
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
286
		{
287
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
288
			$result = $this->db->sql_query($sql_no_user);
289
			while ($row = $this->db->sql_fetchrow($result))
290
			{
291
				$exclude_albums[] = (int) $row['album_id'];
292
			}
293
			$this->db->sql_freeresult($result);
294
		}
295 7
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
296
		$sql_array = array(
297
			'FROM' => array(
298 7
				$this->images_table => 'i',
299 7
				$this->comments_table => 'c',
300
			),
301 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),
302 7
			'ORDER_BY'	=> 'comment_time DESC'
303
		);
304 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 <> ' . \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

304
		$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 <> ' . \phpbbgallery\core\block::STATUS_UNAPPROVED . ')
Loading history...
305 7
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
306
307 7
		$sql_array['SELECT'] = 'COUNT(c.comment_id) as count';
308 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
309 7
		$result = $this->db->sql_query($sql);
310 7
		$row = $this->db->sql_fetchrow($result);
311 7
		$this->db->sql_freeresult($result);
312 7
		$count = 0;
313 7
		if ($row)
314
		{
315 7
			$count = $row['count'];
316
		}
317 7
		$sql_array['SELECT'] = '*';
318 7
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
319 7
		$result = $this->db->sql_query_limit($sql, $sql_limit, $start);
320 7
		$rowset = array();
321
322 7
		$users_array = array();
323 7
		while ($row = $this->db->sql_fetchrow($result))
324
		{
325 6
			$rowset[] = $row;
326 6
			$users_array[$row['comment_user_id']] = array('');
327 6
			$users_array[$row['image_user_id']] = array('');
328
		}
329 7
		$this->db->sql_freeresult($result);
330 7
		if (empty($rowset))
331
		{
332 1
			$this->template->assign_vars(array(
333 1
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RECENT_COMMENTS'),
334
			));
335 1
			return;
336
		}
337
338 6
		$this->user_loader->load_users(array_keys($users_array));
339 6
		foreach ($rowset as $var)
340
		{
341
342 6
			$album_tmp = $this->album->get_info($var['image_album_id']);
343 6
			$this->template->assign_block_vars('commentrow', array(
344 6
				'COMMENT_ID'	=> (int) $var['comment_id'],
345 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,
346 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,
347 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,
348 6
				'U_COMMENT'	=> $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '#comment_' . $var['comment_id'],
349 6
				'POST_AUTHOR_FULL'	=> (string) $this->user_loader->get_username($var['comment_user_id'], 'full'),
350 6
				'TIME'	=> $this->user->format_date($var['comment_time']),
351 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

351
				'TEXT'	=> /** @scrutinizer ignore-call */ generate_text_for_display($var['comment'], $var['comment_uid'], $var['comment_bitfield'], 7),
Loading history...
352 6
				'UC_IMAGE_NAME'	=> '<a href="' . $this->helper->route('phpbbgallery_core_image', array('image_id' => $var['comment_image_id'])) . '">' . $var['image_name'] . '</a>',
353
				// 'UC_THUMBNAIL'		=> $this->helper->route('phpbbgallery_core_image_file_mini', array('image_id' => $var['image_id'])),
354 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']),
355 6
				'IMAGE_AUTHOR'		=> $this->user_loader->get_username((int) $var['image_user_id'], 'full'),
356 6
				'IMAGE_TIME'		=> $this->user->format_date($var['image_time']),
357
			));
358
		}
359 6
		$this->template->assign_vars(array(
360 6
			'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_COMMENTS_SPRINTF', $count),
361 6
			'SEARCH_TITLE'		=> $this->language->lang('RECENT_COMMENTS'),
362
		));
363 6
		$this->pagination->generate_template_pagination(array(
364
			'routes' => array(
365 6
				'phpbbgallery_core_search_commented',
366
				'phpbbgallery_core_search_commented_page',),
367 6
				'params' => array()), 'pagination', 'page', $count, $limit, $start
368
		);
369 6
	}
370
371
	/**
372
	 * Generate recent images and populate template
373
	 * @param (int)    $limit How many imagese to query
374
	 * @param int $start
375
	 * @param int $user
376
	 * @param string $fields
377
	 * @param bool $block_name
378
	 * @param bool $u_block
379
	 */
380 34
	public function recent($limit, $start = 0, $user = 0, $fields = 'rrc_gindex_display', $block_name = false, $u_block = false)
381
	{
382
		// We will do small escape for not devising by 0
383 34
		if ($limit == 0)
384
		{
385
			return;
386
		}
387 34
		if ($limit < -1)
388
		{
389
			$limit = -1;
390
		}
391 34
		$pagination = true;
392 34
		if ($start == -1)
393
		{
394 1
			$start = 0;
395 1
			$pagination = false;
396
		}
397 34
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
398 34
		$sql_order = '';
399 34
		switch ($this->gallery_config->get('default_sort_key'))
400
		{
401 34
			case 't':
402 34
				$sql_order = 'image_time';
403 34
				break;
404
			case 'n':
405
				$sql_order = 'image_name_clean';
406
				break;
407
			case 'vc':
408
				$sql_order = 'image_view_count';
409
				break;
410
			case 'u':
411
				$sql_order = 'image_username_clean';
412
				break;
413
			case 'ra':
414
				$sql_order = 'image_rate_avg';
415
				break;
416
			case 'r':
417
				$sql_order = 'image_rates';
418
				break;
419
			case 'c':
420
				$sql_order = 'image_comments';
421
				break;
422
			case 'lc':
423
				$sql_order = 'image_last_comment';
424
				break;
425
		}
426 34
		$sql_order = $sql_order . ($this->gallery_config->get('default_sort_dir') == 'd' ? ' DESC' : ' ASC');
427 34
		$sql_limit = $limit;
428 34
		$exclude_albums = array();
429 34
		if (!$this->gallery_config->get('rrc_gindex_pegas'))
430
		{
431
			$sql_no_user = 'SELECT album_id FROM ' . $this->albums_table . ' WHERE album_user_id > 0';
432
			$result = $this->db->sql_query($sql_no_user);
433
			while ($row = $this->db->sql_fetchrow($result))
434
			{
435
				$exclude_albums[] = (int) $row['album_id'];
436
			}
437
			$this->db->sql_freeresult($result);
438
		}
439 34
		$exclude_albums = array_merge($exclude_albums, $this->gallery_auth->get_exclude_zebra());
440
		$sql_ary = array(
441
			'FROM'	=>	array(
442 34
				$this->images_table	=> 'i'
443
			),
444
			'WHERE'	=> 'image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN
445
		);
446 34
		if ($user > 0)
447
		{
448 30
			$sql_ary['WHERE'] .= ' and image_user_id = ' . (int) $user;
449
		}
450 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

450
		$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...
451 34
					OR ' . $this->db->sql_in_set('image_album_id', array_diff($this->gallery_auth->acl_album_ids('m_status'), $exclude_albums), false, true) . ')';
452
453 34
		$sql_ary['SELECT'] = 'COUNT(image_id) as count';
454 34
		$sql = $this->db->sql_build_query('SELECT', $sql_ary);
455 34
		$result = $this->db->sql_query($sql);
456 34
		$row = $this->db->sql_fetchrow($result);
457 34
		$this->db->sql_freeresult($result);
458 34
		$count = $row['count'];
459
460 34
		$sql_ary['SELECT'] = 'image_id';
461 34
		$sql_ary['ORDER_BY'] = $sql_order;
462 34
		$sql = $this->db->sql_build_query('SELECT', $sql_ary);
463 34
		$result = $this->db->sql_query_limit($sql, $sql_limit, $start);
464 34
		$id_ary = array();
465 34
		while ($row = $this->db->sql_fetchrow($result))
466
		{
467 34
			$id_ary[] = $row['image_id'];
468
		}
469
470 34
		$this->db->sql_freeresult($result);
471
472 34
		$total_match_count = sizeof($id_ary);
473
474 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...
475
476 34
		if ($user > 0)
477
		{
478 30
			$this->template->assign_block_vars('imageblock', array(
479 30
				'BLOCK_NAME'	=> $block_name ? $block_name : '' ,
480 30
				'U_BLOCK'	=> $u_block ? $u_block : $this->helper->route('phpbbgallery_core_search_egosearch'),
481
			));
482
		}
483
		else
484
		{
485 4
			$this->template->assign_block_vars('imageblock', array(
486 4
				'BLOCK_NAME'	=>  $block_name ? $block_name : $this->language->lang('RECENT_IMAGES'),
487 4
				'U_BLOCK'	=> $u_block ? $u_block : $this->helper->route('phpbbgallery_core_search_recent'),
488
			));
489
		}
490
491
		// For some searches we need to print out the "no results" page directly to allow re-sorting/refining the search options.
492 34
		if (!sizeof($id_ary))
493
		{
494
			$this->template->assign_block_vars('imageblock', array(
495
				'ERROR'	=> $this->language->lang('NO_SEARCH_RESULTS_RECENT')
496
			));
497
			return;
498
		}
499
500 34
		$sql_where = $this->db->sql_in_set('i.image_id', $id_ary);
501
502
		$sql_array = array(
503 34
			'SELECT'		=> 'i.*, a.album_name, a.album_status, a.album_user_id, a.album_id',
504 34
			'FROM'			=> array($this->images_table => 'i'),
505
506
			'LEFT_JOIN'		=> array(
507
				array(
508 34
					'FROM'		=> array($this->albums_table => 'a'),
509 34
					'ON'		=> 'a.album_id = i.image_album_id',
510
				),
511
			),
512
513 34
			'WHERE'			=> 'i.image_status <> ' . \phpbbgallery\core\block::STATUS_ORPHAN . ' AND ' . $sql_where,
514 34
			'ORDER_BY'		=> $sql_order,
515
		);
516 34
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
517 34
		$result = $this->db->sql_query($sql);
518
519 34
		$show_options = $this->gallery_config->get($fields);
520 34
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
521 34
		$imagename_link = $this->gallery_config->get('link_image_name');
522
523 34
		while ($row = $this->db->sql_fetchrow($result))
524
		{
525 34
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
526
		}
527 34
		$this->db->sql_freeresult($result);
528
529 34
		if ($user > 0)
530
		{
531 30
			$this->template->assign_vars(array(
532 30
				'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_IMAGES_SPRINTF', $count),
533 30
				'SEARCH_TITLE'		=> $this->language->lang('SEARCH_USER_IMAGES_OF', $this->user->data['username']),
534
			));
535 30
			$this->pagination->generate_template_pagination(array(
536
				'routes' => array(
537 30
					'phpbbgallery_core_search_egosearch',
538
					'phpbbgallery_core_search_egosearch_page',),
539 30
					'params' => array()), 'pagination', 'page', $count, $limit, $start
540
			);
541
		}
542
		else
543
		{
544 4
			$this->template->assign_vars(array(
545 4
				'TOTAL_IMAGES'				=> $this->language->lang('VIEW_ALBUM_IMAGES', $count),
546
			));
547 4
			if ($pagination)
548
			{
549 3
				$this->pagination->generate_template_pagination(array(
550
					'routes' => array(
551 3
						'phpbbgallery_core_search_recent',
552
						'phpbbgallery_core_search_recent_page',),
553 3
						'params' => array()), 'pagination', 'page', $count, $limit, $start
554
				);
555
			}
556
		}
557 34
	}
558
559
	/**
560
	 * Get top rated image
561
	 * @param $limit
562
	 * @param int $start
563
	 */
564
	public function rating($limit, $start = 0)
565
	{
566
		$this->gallery_auth->load_user_permissions($this->user->data['user_id']);
567
		$sql_array = array();
568
		$sql_array['FROM'] = array(
569
			$this->images_table	=> 'i'
570
		);
571
		$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';
572
		$sql_array['SELECT'] = 'COUNT(image_id) as count';
573
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
574
		$result = $this->db->sql_query($sql);
575
		$row = $this->db->sql_fetchrow($result);
576
		$this->db->sql_freeresult($result);
577
		$count = $row['count'];
578
		$sql_array['SELECT'] = '* , a.album_name, a.album_status, a.album_user_id, a.album_id';
579
		$sql_array['LEFT_JOIN']	= array(
580
			array(
581
				'FROM'		=> array($this->albums_table => 'a'),
582
				'ON'		=> 'a.album_id = i.image_album_id',
583
			)
584
		);
585
		$sql_array['ORDER_BY'] = 'image_rate_avg DESC, image_rates DESC';
586
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
587
		$result = $this->db->sql_query_limit($sql, $limit, $start);
588
		$rowset = array();
589
590
		while ($row = $this->db->sql_fetchrow($result))
591
		{
592
			$rowset[] = $row;
593
			$users_array[$row['image_user_id']] = array('');
594
		}
595
		$this->db->sql_freeresult($result);
596
		if (empty($rowset))
597
		{
598
			$this->template->assign_var('S_NO_SEARCH', true);
599
			trigger_error('NO_SEARCH');
600
		}
601
602
		$this->template->assign_block_vars('imageblock', array(
603
			'BLOCK_NAME'	=> $this->language->lang('SEARCH_TOPRATED'),
604
			'U_BLOCK'	=> $this->helper->route('phpbbgallery_core_search_toprated'),
605
		));
606
		$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...
607
		// Now let's get display options
608
		$show_options = $this->gallery_config->get('rrc_gindex_display');
609
		$thumbnail_link = $this->gallery_config->get('link_thumbnail');
610
		$imagename_link = $this->gallery_config->get('link_image_name');
611
		foreach ($rowset as $row)
612
		{
613
			$this->image->assign_block('imageblock.image', $row, $show_options, $thumbnail_link, $imagename_link);
614
		}
615
616
		$this->template->assign_vars(array(
617
			'SEARCH_MATCHES'	=> $this->language->lang('TOTAL_IMAGES_SPRINTF', $count),
618
			'SEARCH_TITLE'		=> $this->language->lang('SEARCH_TOPRATED'),
619
		));
620
		$this->pagination->generate_template_pagination(array(
621
			'routes' => array(
622
				'phpbbgallery_core_search_toprated',
623
				'phpbbgallery_core_search_toprated_page',),
624
				'params' => array()), 'pagination', 'page', $count, $limit, $start
625
		);
626
	}
627
}
628