]> git.mxchange.org Git - friendica.git/blob - src/Module/Calendar/Export.php
The last PHPCS error ..
[friendica.git] / src / Module / Calendar / Export.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\Calendar;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Session\Capability\IHandleUserSessions;
28 use Friendica\Model\Event;
29 use Friendica\Model\User;
30 use Friendica\Module\Response;
31 use Friendica\Navigation\SystemMessages;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
35
36 /**
37  * Controller to export a calendar from a given user
38  */
39 class Export extends BaseModule
40 {
41         const EXPORT_ICAL = 'ical';
42         const EXPORT_CSV  = 'csv';
43
44         const DEFAULT_EXPORT = self::EXPORT_ICAL;
45
46         /** @var IHandleUserSessions */
47         protected $session;
48         /** @var SystemMessages */
49         protected $sysMessages;
50
51         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, array $server, array $parameters = [])
52         {
53                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->session     = $session;
56                 $this->sysMessages = $sysMessages;
57         }
58
59         protected function rawContent(array $request = [])
60         {
61                 if (!$this->session->getLocalUserId()) {
62                         throw new HTTPException\UnauthorizedException($this->t('Permission denied.'));
63                 }
64
65                 $owner = User::getByNickname($this->parameters['nickname'], ['uid']);
66                 if (empty($owner)) {
67                         throw new HTTPException\NotFoundException($this->t('User not found.'));
68                 }
69                 $ownerUid = $owner['uid'];
70                 $format   = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
71
72                 // Get the export data by uid
73                 $evexport = Event::exportListByUserId($ownerUid, $format);
74
75                 if (!$evexport["success"]) {
76                         if ($evexport["content"]) {
77                                 $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
78                         } else {
79                                 $this->sysMessages->addNotice($this->t('No exportable data found'));
80                         }
81
82                         // If it is the own calendar return to the events page
83                         // otherwise to the profile calendar page
84                         if ($this->session->getLocalUserId() === $ownerUid) {
85                                 $returnPath = 'calendar';
86                         } else {
87                                 $returnPath = 'calendar/show/' . $this->parameters['nickname'];
88                         }
89
90                         $this->baseUrl->redirect($returnPath);
91                 }
92
93                 // If nothing went wrong we can echo the export content
94                 if ($evexport["success"]) {
95                         $this->response->setHeader(sprintf('Content-Disposition: attachment; filename="%s-%s.%s"',
96                                 $this->t('calendar'),
97                                 $this->parameters['nickname'],
98                                 $evexport["extension"]
99                         ));
100
101                         switch ($format) {
102                                 case static::EXPORT_ICAL:
103                                         $this->response->setType(Response::TYPE_BLANK, 'text/ics');
104                                         break;
105                                 case static::EXPORT_CSV:
106                                         $this->response->setType(Response::TYPE_BLANK, 'text/csv');
107                                         break;
108                         }
109
110                         $this->response->addContent($evexport['content']);
111                 }
112         }
113 }