]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
dirfind: move pagination into template
[friendica.git] / mod / dirfind.php
1 <?php
2 require_once('include/contact_widgets.php');
3 require_once('include/socgraph.php');
4 require_once('include/Contact.php');
5
6 function dirfind_init(&$a) {
7
8         if(! local_user()) {
9                 notice( t('Permission denied.') . EOL );
10                 return;
11         }
12
13         if(! x($a->page,'aside'))
14                 $a->page['aside'] = '';
15
16         $a->page['aside'] .= follow_widget();
17
18         $a->page['aside'] .= findpeople_widget();
19 }
20
21
22
23 function dirfind_content(&$a, $prefix = "") {
24
25         $community = false;
26
27         $local = get_config('system','poco_local_search');
28
29         $search = $prefix.notags(trim($_REQUEST['search']));
30
31         if(strpos($search,'@') === 0)
32                 $search = substr($search,1);
33
34         if(strpos($search,'!') === 0) {
35                 $search = substr($search,1);
36                 $community = true;
37         }
38
39         $o = '';
40
41         if($search) {
42
43                 if ($local) {
44
45                         if ($community)
46                                 $extra_sql = " AND `community`";
47                         else
48                                 $extra_sql = "";
49
50                         $perpage = 80;
51                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
52
53                         $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND
54                                         (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR
55                                                 `about` REGEXP '%s' OR `keywords` REGEXP '%s')".$extra_sql,
56                                         dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
57                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
58                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)));
59
60                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`keywords`
61                                         FROM `gcontact`
62                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
63                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
64                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
65                                         WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND
66                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND
67                                         (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR
68                                                 `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') $extra_sql
69                                                 GROUP BY `gcontact`.`nurl`
70                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
71                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
72                                         dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
73                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
74                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)),
75                                         intval($startrec), intval($perpage));
76                         $j = new stdClass();
77                         $j->total = $count[0]["total"];
78                         $j->items_page = $perpage;
79                         $j->page = $a->pager['page'];
80                         foreach ($results AS $result) {
81                                 if (poco_alternate_ostatus_url($result["url"]))
82                                          continue;
83
84                                 if ($result["name"] == "") {
85                                         $urlparts = parse_url($result["url"]);
86                                         $result["name"] = end(explode("/", $urlparts["path"]));
87                                 }
88
89                                 $objresult = new stdClass();
90                                 $objresult->cid = $result["cid"];
91                                 $objresult->name = $result["name"];
92                                 $objresult->url = $result["url"];
93                                 $objresult->photo = $result["photo"];
94                                 $objresult->tags = $result["keywords"];
95
96                                 $j->results[] = $objresult;
97                         }
98
99                         // Add found profiles from the global directory to the local directory
100                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
101                 } else {
102
103                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
104
105                         if(strlen(get_config('system','directory')))
106                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
107
108                         $j = json_decode($x);
109                 }
110
111                 if($j->total) {
112                         $a->set_pager_total($j->total);
113                         $a->set_pager_itemspage($j->items_page);
114                 }
115
116                 if(count($j->results)) {
117
118                         $id = 0;
119
120                         foreach($j->results as $jj) {
121
122                                 // If We already know this contact then don't show the "connect" button
123                                 if ($jj->cid > 0) {
124                                         $connlnk = "";
125                                         $conntxt = "";
126                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
127                                                         intval($jj->cid));
128                                         if ($contact)
129                                                 $photo_menu = contact_photo_menu($contact[0]);
130                                         else
131                                                 $photo_menu = array();
132                                 } else {
133                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
134                                         $conntxt = t('Connect');
135                                         $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
136                                         $photo_menu[] = array(t("Connect/Follow"), $connlnk);
137                                 }
138
139                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
140
141                                 $entry = array(
142                                         'url' => zrl($jj->url),
143                                         'name' => htmlentities($jj->name),
144                                         'photo' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
145                                         'tags' => $jj->tags,
146                                         'conntxt' => $conntxt,
147                                         'connlnk' => $connlnk,
148                                         'photo_menu' => $photo_menu,
149                                         'id' => ++$id,
150                                 );
151                                 $entries[] = $entry;
152                         }
153
154                 $tpl = get_markup_template('match.tpl');
155
156                 $o .= replace_macros($tpl,array(
157                         'title' => sprintf( t('People Search - %s'), $search),
158                         '$entries' => $entries,
159                         '$paginate' => paginate($a),
160                 ));
161
162                 }
163                 else {
164                         info( t('No matches') . EOL);
165                 }
166
167         }
168
169         return $o;
170 }