]> git.mxchange.org Git - friendica.git/blob - src/Module/FriendSuggest.php
Remove deprecated code
[friendica.git] / src / Module / FriendSuggest.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\Worker;
9 use Friendica\DI;
10 use Friendica\Model\Contact as ContactModel;
11 use Friendica\Network\HTTPException\ForbiddenException;
12 use Friendica\Network\HTTPException\NotFoundException;
13 use Friendica\Util\DateTimeFormat;
14 use Friendica\Util\Strings;
15 use Friendica\Worker\Delivery;
16
17 /**
18  * Suggest friends to a known contact
19  */
20 class FriendSuggest extends BaseModule
21 {
22         public static function init(array $parameters = [])
23         {
24                 if (!local_user()) {
25                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
26                 }
27         }
28
29         public static function post(array $parameters = [])
30         {
31                 $cid = intval($parameters['contact']);
32
33                 // We do query the "uid" as well to ensure that it is our contact
34                 if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
35                         throw new NotFoundException(DI::l10n()->t('Contact not found.'));
36                 }
37
38                 $suggest_contact_id = intval($_POST['suggest']);
39                 if (empty($suggest_contact_id)) {
40                         return;
41                 }
42
43                 // We do query the "uid" as well to ensure that it is our contact
44                 $contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
45                 if (empty($contact)) {
46                         notice(DI::l10n()->t('Suggested contact not found.'));
47                         return;
48                 }
49
50                 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
51
52                 $suggest = DI::fsuggest()->insert([
53                         'uid'     => local_user(),
54                         'cid'     => $cid,
55                         'name'    => $contact['name'],
56                         'url'     => $contact['url'],
57                         'request' => $contact['request'],
58                         'photo'   => $contact['avatar'],
59                         'note'    => $note,
60                         'created' => DateTimeFormat::utcNow()
61                 ]);
62
63                 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
64
65                 info(DI::l10n()->t('Friend suggestion sent.'));
66         }
67
68         public static function content(array $parameters = [])
69         {
70                 $cid = intval($parameters['contact']);
71
72                 $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
73                 if (empty($contact)) {
74                         notice(DI::l10n()->t('Contact not found.'));
75                         DI::baseUrl()->redirect();
76                 }
77
78                 $contacts = ContactModel::selectToArray(['id', 'name'], [
79                         '`uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != "" AND `id` != ? AND `network` = ?',
80                         local_user(),
81                         $cid,
82                         Protocol::DFRN,
83                 ]);
84
85                 $formattedContacts = [];
86
87                 foreach ($contacts as $contact) {
88                         $formattedContacts[$contact['id']] = $contact['name'];
89                 }
90
91                 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
92                 return Renderer::replaceMacros($tpl, [
93                         '$contact_id'      => $cid,
94                         '$fsuggest_title'  => DI::l10n()->t('Suggest Friends'),
95                         '$fsuggest_select' => [
96                                 'suggest',
97                                 DI::l10n()->t('Suggest a friend for %s', $contact['name']),
98                                 '',
99                                 '',
100                                 $formattedContacts,
101                         ],
102                         '$submit'          => DI::l10n()->t('Submit'),
103                 ]);
104         }
105 }