]> git.mxchange.org Git - friendica.git/blob - src/Module/FriendSuggest.php
Revert "Replace Module::init() with Constructors"
[friendica.git] / src / Module / FriendSuggest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Worker;
28 use Friendica\DI;
29 use Friendica\Model\Contact as ContactModel;
30 use Friendica\Network\HTTPException\ForbiddenException;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\Strings;
34 use Friendica\Worker\Delivery;
35
36 /**
37  * Suggest friends to a known contact
38  */
39 class FriendSuggest extends BaseModule
40 {
41         public function init()
42         {
43                 if (!local_user()) {
44                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
45                 }
46         }
47
48         public function post()
49         {
50                 $cid = intval($this->parameters['contact']);
51
52                 // We do query the "uid" as well to ensure that it is our contact
53                 if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
54                         throw new NotFoundException(DI::l10n()->t('Contact not found.'));
55                 }
56
57                 $suggest_contact_id = intval($_POST['suggest']);
58                 if (empty($suggest_contact_id)) {
59                         return;
60                 }
61
62                 // We do query the "uid" as well to ensure that it is our contact
63                 $contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
64                 if (empty($contact)) {
65                         notice(DI::l10n()->t('Suggested contact not found.'));
66                         return;
67                 }
68
69                 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
70
71                 $suggest = DI::fsuggest()->save(DI::fsuggestFactory()->createNew(
72                         local_user(),
73                         $cid,
74                         $contact['name'],
75                         $contact['url'],
76                         $contact['request'],
77                         $contact['avatar'],
78                         $note
79                 ));
80
81                 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
82
83                 info(DI::l10n()->t('Friend suggestion sent.'));
84         }
85
86         public function content(): string
87         {
88                 $cid = intval($this->parameters['contact']);
89
90                 $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
91                 if (empty($contact)) {
92                         notice(DI::l10n()->t('Contact not found.'));
93                         DI::baseUrl()->redirect();
94                 }
95
96                 $suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
97                         '`uid` = ? 
98                         AND `id` != ? 
99                         AND `network` = ? 
100                         AND NOT `self` 
101                         AND NOT `blocked` 
102                         AND NOT `pending` 
103                         AND NOT `archive` 
104                         AND NOT `deleted` 
105                         AND `notify` != ""',
106                         local_user(),
107                         $cid,
108                         Protocol::DFRN,
109                 ]);
110
111                 $formattedContacts = [];
112
113                 foreach ($suggestableContacts as $suggestableContact) {
114                         $formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
115                 }
116
117                 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
118                 return Renderer::replaceMacros($tpl, [
119                         '$contact_id'      => $cid,
120                         '$fsuggest_title'  => DI::l10n()->t('Suggest Friends'),
121                         '$fsuggest_select' => [
122                                 'suggest',
123                                 DI::l10n()->t('Suggest a friend for %s', $contact['name']),
124                                 '',
125                                 '',
126                                 $formattedContacts,
127                         ],
128                         '$submit'          => DI::l10n()->t('Submit'),
129                 ]);
130         }
131 }