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