Adjusted field names
[friendica.git/.git] / mod / msearch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Database\DBA;
24 use Friendica\DI;
25
26 function msearch_post(App $a)
27 {
28         $search = $_POST['s'] ?? '';
29         $perpage  = intval(($_POST['n'] ?? 0) ?: 80);
30         $page     = intval(($_POST['p'] ?? 0) ?: 1);
31         $startrec = ($page - 1) * $perpage;
32
33         $total = 0;
34         $results = [];
35
36         if (!strlen($search)) {
37                 $output = ['total' => 0, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
38                 echo json_encode($output);
39                 exit();
40         }
41
42         $total = 0;
43
44         $count_stmt = DBA::p(
45                 "SELECT COUNT(*) AS `total`
46                         FROM `profile`
47                         JOIN `user` ON `user`.`uid` = `profile`.`uid`
48                         WHERE `profile`.`net-publish`
49                         AND MATCH(`pub_keywords`) AGAINST (?)",
50                 $search
51         );
52         if (DBA::isResult($count_stmt)) {
53                 $row = DBA::fetch($count_stmt);
54                 $total = $row['total'];
55         }
56
57         DBA::close($count_stmt);
58
59         $search_stmt = DBA::p(
60                 "SELECT `pub_keywords`, `username`, `nickname`, `user`.`uid`
61                         FROM `user`
62                         JOIN `profile` ON `user`.`uid` = `profile`.`uid`
63                         WHERE `profile`.`net-publish`
64                         AND MATCH(`pub_keywords`) AGAINST (?)
65                         LIMIT ?, ?",
66                 $search,
67                 $startrec,
68                 $perpage
69         );
70
71         while ($search_result = DBA::fetch($search_stmt)) {
72                 $results[] = [
73                         'name'  => $search_result['name'],
74                         'url'   => DI::baseUrl() . '/profile/' . $search_result['nickname'],
75                         'photo' => DI::baseUrl() . '/photo/avatar/' . $search_result['uid'] . '.jpg',
76                         'tags'  => str_replace([',', '  '], [' ', ' '], $search_result['pub_keywords'])
77                 ];
78         }
79
80         DBA::close($search_stmt);
81
82         $output = ['total' => $total, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
83
84         echo json_encode($output);
85
86         exit();
87 }