]> git.mxchange.org Git - friendica.git/blob - mod/msearch.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / msearch.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Database\DBA;
6
7 function msearch_post(App $a)
8 {
9         $search = defaults($_POST, 's', '');
10         $perpage  = intval(defaults($_POST, 'n', 80));
11         $page     = intval(defaults($_POST, 'p', 1));
12         $startrec = ($page - 1) * $perpage;
13
14         $total = 0;
15         $results = [];
16
17         if (!strlen($search)) {
18                 $output = ['total' => 0, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
19                 echo json_encode($output);
20                 exit();
21         }
22
23         $total = 0;
24
25         $count_stmt = DBA::p(
26                 "SELECT COUNT(*) AS `total`
27                         FROM `profile`
28                         JOIN `user` ON `user`.`uid` = `profile`.`uid`
29                         WHERE `is-default` = 1
30                         AND `user`.`hidewall` = 0
31                         AND MATCH(`pub_keywords`) AGAINST (?)",
32                 $search
33         );
34         if (DBA::isResult($count_stmt)) {
35                 $row = DBA::fetch($count_stmt);
36                 $total = $row['total'];
37         }
38
39         DBA::close($count_stmt);
40
41         $search_stmt = DBA::p(
42                 "SELECT `pub_keywords`, `username`, `nickname`, `user`.`uid`
43                         FROM `user`
44                         JOIN `profile` ON `user`.`uid` = `profile`.`uid`
45                         WHERE `is-default` = 1
46                         AND `user`.`hidewall` = 0
47                         AND MATCH(`pub_keywords`) AGAINST (?)
48                         LIMIT ?, ?",
49                 $search,
50                 $startrec,
51                 $perpage
52         );
53
54         while($search_result = DBA::fetch($search_stmt)) {
55                 $results[] = [
56                         'name'  => $search_result['name'],
57                         'url'   => System::baseUrl() . '/profile/' . $search_result['nickname'],
58                         'photo' => System::baseUrl() . '/photo/avatar/' . $search_result['uid'] . '.jpg',
59                         'tags'  => str_replace([',', '  '], [' ', ' '], $search_result['pub_keywords'])
60                 ];
61         }
62
63         $output = ['total' => $total, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
64
65         echo json_encode($output);
66
67         exit();
68 }