]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Automatically updating the searched contacts while searching it.
[friendica.git] / mod / dirfind.php
1 <?php
2
3 function dirfind_init(&$a) {
4
5         require_once('include/contact_widgets.php');
6
7         if(! x($a->page,'aside'))
8                 $a->page['aside'] = '';
9
10         $a->page['aside'] .= follow_widget();
11
12         $a->page['aside'] .= findpeople_widget();
13 }
14
15
16
17 function dirfind_content(&$a) {
18
19         $local = get_config('system','poco_local_search');
20
21         $search = notags(trim($_REQUEST['search']));
22
23         if(strpos($search,'@') === 0)
24                 $search = substr($search,1);
25
26         $o = '';
27
28         $o .= replace_macros(get_markup_template("section_title.tpl"),array(
29                 '$title' => sprintf( t('People Search - %s'), $search)
30         ));
31
32         if($search) {
33
34                 if ($local) {
35
36                         $perpage = 80;
37                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
38
39                         $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND
40                                         (`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR
41                                                 `about` REGEXP '%s' OR `keywords` REGEXP '%s')",
42                                         dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
43                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
44                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)));
45
46                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`keywords`
47                                         FROM `gcontact`
48                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
49                                         WHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND `gcontact`.`last_contact` >= `gcontact`.`last_failure` AND
50                                         (`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR
51                                                 `gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s')
52                                                 GROUP BY `gcontact`.`nurl`
53                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
54                                         intval(local_user()), dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA),
55                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)),
56                                         dbesc(escape_tags($search)), dbesc(escape_tags($search)),
57                                         intval($startrec), intval($perpage));
58                         $j = new stdClass();
59                         $j->total = $count[0]["total"];
60                         $j->items_page = $perpage;
61                         $j->page = $a->pager['page'];
62                         foreach ($results AS $result) {
63                                 if ($result["name"] == "") {
64                                         $urlparts = parse_url($result["url"]);
65                                         $result["name"] = end(explode("/", $urlparts["path"]));
66                                 }
67
68                                 $objresult = new stdClass();
69                                 $objresult->cid = $result["cid"];
70                                 $objresult->name = $result["name"];
71                                 $objresult->url = $result["url"];
72                                 $objresult->photo = $result["photo"];
73                                 $objresult->tags = $result["keywords"];
74
75                                 $j->results[] = $objresult;
76                         }
77
78                         // Add found profiles from the global directory to the local directory
79                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
80                 } else {
81
82                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
83
84                         if(strlen(get_config('system','directory_submit_url')))
85                                 $x = fetch_url('http://dir.friendica.com/lsearch?f=' . $p .  '&search=' . urlencode($search));
86
87                         $j = json_decode($x);
88                 }
89
90                 if($j->total) {
91                         $a->set_pager_total($j->total);
92                         $a->set_pager_itemspage($j->items_page);
93                 }
94
95                 if(count($j->results)) {
96
97                         $tpl = get_markup_template('match.tpl');
98                         foreach($j->results as $jj) {
99
100                                 // If We already know this contact then don't show the "connect" button
101                                 if ($jj->cid > 0) {
102                                         $connlnk = "";
103                                         $conntxt = "";
104                                 } else {
105                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
106                                         $conntxt = t('Connect');
107                                 }
108
109                                 $o .= replace_macros($tpl,array(
110                                         '$url' => zrl($jj->url),
111                                         '$name' => $jj->name,
112                                         '$photo' => proxy_url($jj->photo),
113                                         '$tags' => $jj->tags,
114                                         '$conntxt' => $conntxt,
115                                         '$connlnk' => $connlnk,
116                                 ));
117                         }
118                 }
119                 else {
120                         info( t('No matches') . EOL);
121                 }
122
123         }
124
125         $o .= '<div class="clear"></div>';
126         $o .= paginate($a);
127         return $o;
128 }