3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Calendar;
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;
37 * Controller to export a calendar from a given user
39 class Export extends BaseModule
41 const EXPORT_ICAL = 'ical';
42 const EXPORT_CSV = 'csv';
44 const DEFAULT_EXPORT = self::EXPORT_ICAL;
46 /** @var IHandleUserSessions */
48 /** @var SystemMessages */
49 protected $sysMessages;
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 = [])
53 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55 $this->session = $session;
56 $this->sysMessages = $sysMessages;
59 protected function rawContent(array $request = [])
61 $owner = User::getByNickname($this->parameters['nickname'], ['uid']);
63 throw new HTTPException\NotFoundException($this->t('User not found.'));
65 $ownerUid = $owner['uid'];
66 $format = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
68 // Get the export data by uid
69 $evexport = Event::exportListByUserId($ownerUid, $format);
71 if (!$evexport["success"]) {
72 if ($evexport["content"]) {
73 $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
75 $this->sysMessages->addNotice($this->t('No exportable data found'));
78 // If it is the own calendar return to the events page
79 // otherwise to the profile calendar page
80 if ($this->session->getLocalUserId() === $ownerUid) {
81 $returnPath = 'events';
83 $returnPath = 'events/' . $this->parameters['nickname'];
86 $this->baseUrl->redirect($returnPath);
89 // If nothing went wrong we can echo the export content
90 if ($evexport["success"]) {
91 $this->response->setHeader(sprintf('Content-Disposition: attachment; filename="%s-%s.%s"',
93 $this->parameters['nickname'],
94 $evexport["extension"]
98 case static::EXPORT_ICAL:
99 $this->response->setType(Response::TYPE_BLANK, 'text/ics');
101 case static::EXPORT_CSV:
102 $this->response->setType(Response::TYPE_BLANK, 'text/csv');
106 $this->response->addContent($evexport['content']);