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 if (!$this->session->getLocalUserId()) {
62 throw new HTTPException\UnauthorizedException($this->t('Permission denied.'));
65 $owner = User::getByNickname($this->parameters['nickname'], ['uid']);
67 throw new HTTPException\NotFoundException($this->t('User not found.'));
69 $ownerUid = $owner['uid'];
70 $format = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
72 // Get the export data by uid
73 $evexport = Event::exportListByUserId($ownerUid, $format);
75 if (!$evexport["success"]) {
76 if ($evexport["content"]) {
77 $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
79 $this->sysMessages->addNotice($this->t('No exportable data found'));
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';
87 $returnPath = 'calendar/show/' . $this->parameters['nickname'];
90 $this->baseUrl->redirect($returnPath);
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"',
97 $this->parameters['nickname'],
98 $evexport["extension"]
102 case static::EXPORT_ICAL:
103 $this->response->setType(Response::TYPE_BLANK, 'text/ics');
105 case static::EXPORT_CSV:
106 $this->response->setType(Response::TYPE_BLANK, 'text/csv');
110 $this->response->addContent($evexport['content']);