]> git.mxchange.org Git - friendica.git/blob - mod/search.php
add alt_pager function, add ability to use new pager logic on network, profile, commu...
[friendica.git] / mod / search.php
1 <?php
2
3 function search_saved_searches() {
4
5         $o = '';
6
7         $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
8                 intval(local_user())
9         );
10
11         if(count($r)) {
12                 $o .= '<div id="saved-search-list" class="widget">';
13                 $o .= '<h3>' . t('Saved Searches') . '</h3>' . "\r\n";
14                 $o .= '<ul id="saved-search-ul">' . "\r\n";
15                 foreach($r as $rr) {
16                         $o .= '<li class="saved-search-li clear"><a href="search/?f=&remove=1&search=' . $rr['term'] . '" class="icon drophide savedsearchdrop" title="' . t('Remove term') . '" onclick="return confirmDelete();" onmouseover="imgbright(this);" onmouseout="imgdull(this);" ></a> <a href="search/?f=&search=' . $rr['term'] . '" class="savedsearchterm" >' . $rr['term'] . '</a></li>' . "\r\n";
17                 }
18                 $o .= '</ul><div class="clear"></div></div>' . "\r\n";
19         }               
20
21         return $o;
22
23 }
24
25
26 function search_init(&$a) {
27
28         $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
29
30         if(local_user()) {
31                 if(x($_GET,'save') && $search) {
32                         $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
33                                 intval(local_user()),
34                                 dbesc($search)
35                         );
36                         if(! count($r)) {
37                                 q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
38                                         intval(local_user()),
39                                         dbesc($search)
40                                 );
41                         }
42                 }
43                 if(x($_GET,'remove') && $search) {
44                         q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
45                                 intval(local_user()),
46                                 dbesc($search)
47                         );
48                 }
49
50                 $a->page['aside'] .= search_saved_searches();
51
52         }
53         else
54                 unset($_SESSION['theme']);
55
56
57
58 }
59
60
61
62 function search_post(&$a) {
63         if(x($_POST,'search'))
64                 $a->data['search'] = $_POST['search'];
65 }
66
67
68 function search_content(&$a) {
69
70         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
71                 notice( t('Public access denied.') . EOL);
72                 return;
73         }
74
75         nav_set_selected('search');
76
77         require_once("include/bbcode.php");
78         require_once('include/security.php');
79         require_once('include/conversation.php');
80
81         $o = '<div id="live-search"></div>' . "\r\n";
82
83         $o .= '<h3>' . t('Search') . '</h3>';
84
85         if(x($a->data,'search'))
86                 $search = notags(trim($a->data['search']));
87         else
88                 $search = ((x($_GET,'search')) ? notags(trim(rawurldecode($_GET['search']))) : '');
89
90         $tag = false;
91         if(x($_GET,'tag')) {
92                 $tag = true;
93                 $search = ((x($_GET,'tag')) ? notags(trim(rawurldecode($_GET['tag']))) : '');
94         }
95
96
97         $o .= search($search,'search-box','/search',((local_user()) ? true : false));
98
99         if(strpos($search,'#') === 0) {
100                 $tag = true;
101                 $search = substr($search,1);
102         }
103         if(strpos($search,'@') === 0) {
104                 require_once('mod/dirfind.php');
105                 return dirfind_content($a);
106         }
107
108         if(! $search)
109                 return $o;
110
111         if (get_config('system','use_fulltext_engine')) {
112                 if($tag)
113                         $sql_extra = sprintf(" AND MATCH (`item`.`tag`) AGAINST ('".'"%s"'."' in boolean mode) ", '#'.dbesc(protect_sprintf($search)));
114                 else
115                         $sql_extra = sprintf(" AND MATCH (`item`.`body`) AGAINST ('".'"%s"'."' in boolean mode) ", dbesc(protect_sprintf($search)));
116         } else {
117                 if($tag)
118                         $sql_extra = sprintf(" AND `item`.`tag` REGEXP '%s' ",  dbesc('\\]' . protect_sprintf(preg_quote($search)) . '\\['));
119                 else
120                         $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
121         }
122
123
124
125
126         // Here is the way permissions work in the search module...
127         // Only public posts can be shown
128         // OR your own posts if you are a logged in member
129         // No items will be shown if the member has a blocked profile wall. 
130
131         if(! get_pconfig(local_user(),'system','alt_pager')) {
132                 $r = q("SELECT distinct(`item`.`uri`) as `total`
133                         FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
134                         WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
135                         AND (( `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0) 
136                                 OR `item`.`uid` = %d )
137                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
138                         $sql_extra group by `item`.`uri` ", 
139                         intval(local_user())
140                 );
141
142                 if(count($r))
143                         $a->set_pager_total(count($r));
144
145                 if(! count($r)) {
146                         info( t('No results.') . EOL);
147                         return $o;
148                 }
149         }
150
151         $r = q("SELECT distinct(`item`.`uri`), `item`.*, `item`.`id` AS `item_id`, 
152                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`alias`, `contact`.`rel`,
153                 `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, 
154                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
155                 `user`.`nickname`
156                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
157                 LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
158                 WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
159                 AND (( `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `item`.`private` = 0 AND `user`.`hidewall` = 0 ) 
160                         OR `item`.`uid` = %d )
161                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
162                 $sql_extra
163                 group by `item`.`uri`   
164                 ORDER BY `received` DESC LIMIT %d , %d ",
165                 intval(local_user()),
166                 intval($a->pager['start']),
167                 intval($a->pager['itemspage'])
168
169         );
170
171         if(! count($r)) {
172                 info( t('No results.') . EOL);
173                 return $o;
174         }
175
176
177         if($tag) 
178                 $o .= '<h2>Items tagged with: ' . $search . '</h2>';
179         else
180                 $o .= '<h2>Search results for: ' . $search . '</h2>';
181
182         $o .= conversation($a,$r,'search',false);
183
184         if(! get_pconfig(local_user(),'system','alt_pager')) {
185                 $o .= paginate($a);
186         }
187         else {
188                 $o .= alt_pager($a,count($r));
189         }
190
191         return $o;
192 }
193