ucp_pmsearch_module   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 17
Bugs 0 Features 0
Metric Value
eloc 102
c 17
b 0
f 0
dl 0
loc 161
ccs 0
cts 134
cp 0
rs 10
wmc 16

1 Method

Rating   Name   Duplication   Size   Complexity  
D main() 0 151 16
1
<?php
2
3
/**
4
*
5
* @package Anavaro.com PM Search
6
* @copyright (c) 2013 Lucifer
7
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
namespace anavaro\pmsearch\ucp;
15
16
class ucp_pmsearch_module
17
{
18
	var $u_action;
19
	private $search_helper;
20
	private $config;
21
	private $terms_ary = array(
22
		'all'	=> 1,
23
		'any'	=> 2,
24
		'nick'	=> 3,
25
	);
26
	function main($id, $mode)
27
	{
28
		global $db, $user, $auth, $template, $request, $phpbb_container, $config;
29
		$this->config = $config;
30
		$this->search_helper = $phpbb_container->get('anavaro.pmsearch.search.helper');
31
		if (!$auth->acl_get('u_pmsearch'))
32
		{
33
			trigger_error('ACCESS_DENIED');
34
		}
35
		switch ($mode)
36
		{
37
			case 'search':
38
				$this->tpl_name	= 'ucp_pmsearch';
0 ignored issues
show
Bug Best Practice introduced by
The property tpl_name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
				$template->assign_vars(array(
40
					'S_UCP_ACTION'	=>	append_sid("ucp.php?i=".$id."&mode=".$mode)
0 ignored issues
show
Bug introduced by
The function append_sid 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

40
					'S_UCP_ACTION'	=>	/** @scrutinizer ignore-call */ append_sid("ucp.php?i=".$id."&mode=".$mode)
Loading history...
41
				));
42
43
				$terms = $request->variable('terms', 'any');
44
				$keywords = utf8_normalize_nfc($request->variable('keywords', '', true));
0 ignored issues
show
Bug introduced by
The function utf8_normalize_nfc 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

44
				$keywords = /** @scrutinizer ignore-call */ utf8_normalize_nfc($request->variable('keywords', '', true));
Loading history...
45
46
				if ($keywords)
47
				{
48
					$this->search = null;
0 ignored issues
show
Bug Best Practice introduced by
The property search does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
					$error = false;
50
					$search_types = $this->search_helper->get_search_types();
51
					if ($this->search_helper->init_search($search_types[0], $this->search, $error))
52
					{
53
						trigger_error($error . adm_back_link($this->u_action), E_USER_WARNING);
0 ignored issues
show
Bug introduced by
Are you sure $error of type false can be used in concatenation? ( Ignorable by Annotation )

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

53
						trigger_error(/** @scrutinizer ignore-type */ $error . adm_back_link($this->u_action), E_USER_WARNING);
Loading history...
Bug introduced by
The function adm_back_link 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

53
						trigger_error($error . /** @scrutinizer ignore-call */ adm_back_link($this->u_action), E_USER_WARNING);
Loading history...
54
					}
55
					$search_count = 0;
56
					$startFrom = $request->variable('start', 0);
57
					$id_ary = array();
58
					$user_id = array(
59
						'' => (int) $user->data['user_id']
60
					);
61
					//What are we searching for?
62
					if ($terms != 'nick')
63
					{
64
						$this->search->split_keywords($keywords, $terms);
0 ignored issues
show
Bug introduced by
The method split_keywords() does not exist on null. ( Ignorable by Annotation )

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

64
						$this->search->/** @scrutinizer ignore-call */ 
65
                     split_keywords($keywords, $terms);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
						//$search_count = $this->search->keyword_search('norma', 'all', 'all', array('msg_id' => 'a'), 'msg_id', 'd', 0, array($user_id), '', $id_ary, $startFrom, 25);
66
						$search_count = $this->search->keyword_search('norma', 'all', 'all', array('msg_id' => 'a'), 'msg_id', 'd', 0, array(), '', '', $user_id, '', $id_ary, $startFrom, $this->config['search_block_size']);
67
68
					}
69
					else
70
					{
71
						// So do we have user_id or username?
72
						if (!is_numeric($keywords))
73
						{
74
							// It's username ... let's get ID
75
							$sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE username_clean LIKE \'' . utf8_clean_string($keywords) . '\'';
0 ignored issues
show
Bug introduced by
The function utf8_clean_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

75
							$sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE username_clean LIKE \'' . /** @scrutinizer ignore-call */ utf8_clean_string($keywords) . '\'';
Loading history...
Bug introduced by
The constant anavaro\pmsearch\ucp\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
							$result = $db->sql_query($sql);
77
							$user_id = $db->sql_fetchrow($result);
78
							$keywords = $user_id['user_id'];
79
						}
80
81
						$search_count = $this->search->user_search($keywords, $id_ary, $startFrom, $this->config['search_block_size']);
82
					}
83
84
					if ($search_count > 0 )
85
					{
86
						// Let's get additional info
87
						$page_array = array();
88
						// As there seems to be some problem with POSTGRES SQL I'll split the info query in two separate queries - one for the message ...
89
						$sql_array = array(
90
							'SELECT'	=> 'msg.msg_id as msg_id, msg.message_subject as msg_subject, msg.message_time as msg_time, msg.author_id as msg_author, tmsg.msg_id, MAX(tmsg.pm_unread) as unread, MAX(tmsg.pm_replied) as replied',
91
							'FROM'	=> array(
92
								PRIVMSGS_TABLE => 'msg',
0 ignored issues
show
Bug introduced by
The constant anavaro\pmsearch\ucp\PRIVMSGS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
93
								PRIVMSGS_TO_TABLE => 'tmsg'
0 ignored issues
show
Bug introduced by
The constant anavaro\pmsearch\ucp\PRIVMSGS_TO_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
							),
95
							'WHERE'	=> 'msg.msg_id = tmsg.msg_id and msg.author_id = tmsg.author_id and ' . $db->sql_in_set('msg.msg_id', $id_ary),
96
							'GROUP_BY'	=> 'msg.msg_id, tmsg.msg_id',
97
							'ORDER_BY'	=> 'msg.msg_id DESC'
98
						);
99
						$sql = $db->sql_build_query('SELECT', $sql_array);
100
						//$this->var_display($sql);
101
						$result = $db->sql_query($sql);
102
						// Let's populate template
103
						$author_uid_arrray = array();
104
						while ($row = $db->sql_fetchrow($result))
105
						{
106
							$author_uid_arrray[] = (int) $row['msg_author'];
107
							$page_array[$row['msg_id']] = array(
108
								'msg_id'	=> $row['msg_id'],
109
								'msg_subject'	=>	$row['msg_subject'],
110
								'msg_author'	=>	$row['msg_author'],
111
								'msg_time'	=>	(int) $row['msg_time'],
112
								'unread'	=> $row['unread'],
113
								'replied'	=> $row['replied']
114
							);
115
						}
116
						if (is_numeric($keywords))
117
						{
118
							$author_uid_arrray[] = (int) $keywords;
119
						}
120
						$db->sql_freeresult($result);
121
						// ... one for the authors on this page
122
						$authors_array = array();
123
						$sql = 'SELECT user_id, username, user_colour FROM ' . USERS_TABLE . ' WHERE ' .  $db->sql_in_set('user_id', $author_uid_arrray);
124
						$result = $db->sql_query($sql);
125
						while ($row = $db->sql_fetchrow($result))
126
						{
127
							$authors_array[$row['user_id']] = array(
128
								'username'	=> $row['username'],
129
								'user_colour'	=> $row['user_colour']
130
							);
131
						}
132
						$count = 1;
133
						foreach ($page_array as $VAR) {
134
							$template->assign_block_vars('pm_results', array(
135
								'S_ROW_COUNT'	=> $count,
136
								'FOLDER_IMG_STYLE'	=> ($VAR['unread'] ? 'pm_unread' : 'pm_read'),
137
								'PM_CLASS'	=> ($VAR['replied'] ? 'pm_replied_colour' : ''),
138
								'U_VIEW_PM'	=> './ucp.php?i=pm&mode=view&p=' . $VAR['msg_id'],
139
								'SUBJECT'	=> $VAR['msg_subject'],
140
								'SENT_TIME'	=>	$user->format_date($VAR['msg_time']),
141
								'MESSAGE_AUTHOR_FULL'	=> ($authors_array[$VAR['msg_author']]['user_colour'] ? '<a href="./memberlist.php?mode=viewprofile&u=' . $VAR['msg_author'] . '" class="username-coloured" style="color: #' . $authors_array[$VAR['msg_author']]['user_colour'] . ';">' . $authors_array[$VAR['msg_author']]['username'] . '</a>' : '<a href="./memberlist.php?mode=viewprofile&u=' . $VAR['msg_author'] . '" class="username">' . $authors_array[$VAR['msg_author']]['username'] . '</a>'),
142
							));
143
							$count ++;
144
						}
145
146
						$pagination = $phpbb_container->get('pagination');
147
						$base_url = append_sid('ucp.php?i=' . $id . '&mode=' . $mode . '&keywords=' . $keywords . '&terms=' . $terms);
148
						$pagination->generate_template_pagination($base_url, 'pagination', 'start', $search_count, $this->config['search_block_size'], $startFrom);
149
						$pageNumber = $pagination->get_on_page($this->config['search_block_size'], $startFrom);
0 ignored issues
show
Unused Code introduced by
The assignment to $pageNumber is dead and can be removed.
Loading history...
150
						if (is_numeric($keywords))
151
						{
152
							$template->assign_vars(array(
153
								'S_KEYWORDS'	=> $authors_array[$keywords]['username']
154
							));
155
						}
156
						else
157
						{
158
							$template->assign_vars(array(
159
								'S_KEYWORDS'	=>	$keywords
160
							));
161
						}
162
						$template->assign_vars(array(
163
							'PAGE_NUMBER'	=> $pagination->on_page($search_count, $this->config['search_block_size'], $startFrom),
164
							'TOTAL_MESSAGES'	=> $search_count,
165
							'HAS_RESULTS'	=> 1,
166
						));
167
					}
168
169
					else
170
					{
171
						trigger_error('NO_RESULTS_FOUND');
172
					}
173
					// After we got the the search count we go deeper
174
				}
175
				$template->assign_vars(array(
176
					'SEARCH_TEARM_TYPE' => $this->terms_ary[$terms]
177
				));
178
		}
179
	}
180
}
181