]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Advanced.php
Adhere feedback
[friendica.git] / src / Module / Contact / Advanced.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Contact;
23
24 use Friendica\App;
25 use Friendica\App\Page;
26 use Friendica\BaseModule;
27 use Friendica\Content\Widget;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\Renderer;
31 use Friendica\Database\Database;
32 use Friendica\DI;
33 use Friendica\Model;
34 use Friendica\Module\Contact;
35 use Friendica\Module\Response;
36 use Friendica\Network\HTTPException\BadRequestException;
37 use Friendica\Network\HTTPException\ForbiddenException;
38 use Friendica\Util\Profiler;
39 use Friendica\Util\Strings;
40 use Psr\Log\LoggerInterface;
41
42 /**
43  * GUI for advanced contact details manipulation
44  */
45 class Advanced extends BaseModule
46 {
47         /** @var Database */
48         protected $dba;
49         /** @var Page */
50         protected $page;
51
52         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, App\Page $page, LoggerInterface $logger, Profiler $profiler, Response $response, Database $dba, array $server, array $parameters = [])
53         {
54                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55
56                 $this->dba  = $dba;
57                 $this->page = $page;
58
59                 if (!DI::userSession()->isAuthenticated()) {
60                         throw new ForbiddenException($this->t('Permission denied.'));
61                 }
62         }
63
64         protected function post(array $request = [])
65         {
66                 $cid = $this->parameters['id'];
67
68                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => DI::userSession()->getLocalUserId()]);
69                 if (empty($contact)) {
70                         throw new BadRequestException($this->t('Contact not found.'));
71                 }
72
73                 $name        = ($_POST['name'] ?? '') ?: $contact['name'];
74                 $nick        = $_POST['nick'] ?? '';
75                 $url         = $_POST['url'] ?? '';
76                 $poll        = $_POST['poll'] ?? '';
77                 $photo       = $_POST['photo'] ?? '';
78                 $nurl        = Strings::normaliseLink($url);
79
80                 $r = $this->dba->update(
81                         'contact',
82                         [
83                                 'name'        => $name,
84                                 'nick'        => $nick,
85                                 'url'         => $url,
86                                 'nurl'        => $nurl,
87                                 'poll'        => $poll,
88                         ],
89                         ['id' => $contact['id'], 'uid' => DI::userSession()->getLocalUserId()]
90                 );
91
92                 if ($photo) {
93                         $this->logger->notice('Updating photo.', ['photo' => $photo]);
94
95                         Model\Contact::updateAvatar($contact['id'], $photo, true);
96                 }
97
98                 if (!$r) {
99                         DI::sysmsg()->addNotice($this->t('Contact update failed.'));
100                 }
101         }
102
103         protected function content(array $request = []): string
104         {
105                 $cid = $this->parameters['id'];
106
107                 $contact = Model\Contact::selectFirst([], ['id' => $cid, 'uid' => DI::userSession()->getLocalUserId()]);
108                 if (empty($contact)) {
109                         throw new BadRequestException($this->t('Contact not found.'));
110                 }
111
112                 $this->page['aside'] = Widget\VCard::getHTML($contact);
113
114                 $returnaddr = "contact/$cid";
115
116                 // This data is fetched automatically for most networks.
117                 // Editing does only makes sense for mail and feed contacts.
118                 if (!in_array($contact['network'], [Protocol::FEED, Protocol::MAIL])) {
119                         $readonly = 'readonly';
120                 } else {
121                         $readonly = '';
122                 }
123
124                 $tab_str = Contact::getTabsHTML($contact, Contact::TAB_ADVANCED);
125
126                 $tpl = Renderer::getMarkupTemplate('contact/advanced.tpl');
127                 return Renderer::replaceMacros($tpl, [
128                         '$tab_str'           => $tab_str,
129                         '$returnaddr'        => $returnaddr,
130                         '$return'            => $this->t('Return to contact editor'),
131                         '$contact_id'        => $contact['id'],
132                         '$lbl_submit'        => $this->t('Submit'),
133
134                         '$name'    => ['name', $this->t('Name'), $contact['name'], '', '', $readonly],
135                         '$nick'    => ['nick', $this->t('Account Nickname'), $contact['nick'], '', '', 'readonly'],
136                         '$url'     => ['url', $this->t('Account URL'), $contact['url'], '', '', 'readonly'],
137                         'poll'     => ['poll', $this->t('Poll/Feed URL'), $contact['poll'], '', '', ($contact['network'] == Protocol::FEED) ? '' : 'readonly'],
138                         'photo'    => ['photo', $this->t('New photo from this URL'), '', '', '', $readonly],
139                 ]);
140         }
141 }