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