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