]> git.mxchange.org Git - friendica.git/commitdiff
Move event export to src/Module/Events/Export
authorPhilipp <admin@philipp.info>
Mon, 31 Oct 2022 23:01:37 +0000 (00:01 +0100)
committerPhilipp <admin@philipp.info>
Wed, 2 Nov 2022 14:16:23 +0000 (15:16 +0100)
mod/cal.php
src/Model/Event.php
src/Module/Events/Export.php [new file with mode: 0644]
static/routes.config.php
view/templates/widget/events.tpl

index 4c22232d279f9c526b191fb6553c890f80b0da17..d5b0487a4a958274651cab866d3ab3752d4f11e2 100644 (file)
@@ -34,7 +34,6 @@ use Friendica\Model\Event;
 use Friendica\Model\Item;
 use Friendica\Model\User;
 use Friendica\Module\BaseProfile;
-use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Temporal;
@@ -101,15 +100,8 @@ function cal_content(App $a)
        $m = 0;
        $ignored = (!empty($_REQUEST['ignored']) ? intval($_REQUEST['ignored']) : 0);
 
-       $format = 'ical';
-       if (DI::args()->getArgc() == 4 && DI::args()->getArgv()[2] == 'export') {
-               $mode = 'export';
-               $format = DI::args()->getArgv()[3];
-       }
-
        // Setup permissions structures
        $owner_uid = intval($owner['uid']);
-       $nick = $owner['nickname'];
 
        $contact_id = DI::userSession()->getRemoteContactID($owner['uid']);
 
@@ -258,40 +250,4 @@ function cal_content(App $a)
 
                return $o;
        }
-
-       if ($mode == 'export') {
-               if (!$owner_uid) {
-                       DI::sysmsg()->addNotice(DI::l10n()->t('User not found'));
-                       return;
-               }
-
-               // Get the export data by uid
-               $evexport = Event::exportListByUserId($owner_uid, $format);
-
-               if (!$evexport["success"]) {
-                       if ($evexport["content"]) {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('This calendar format is not supported'));
-                       } else {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('No exportable data found'));
-                       }
-
-                       // If it the own calendar return to the events page
-                       // otherwise to the profile calendar page
-                       if (DI::userSession()->getLocalUserId() === $owner_uid) {
-                               $return_path = "events";
-                       } else {
-                               $return_path = "cal/" . $nick;
-                       }
-
-                       DI::baseUrl()->redirect($return_path);
-               }
-
-               // If nothing went wrong we can echo the export content
-               if ($evexport["success"]) {
-                       header('content-disposition: attachment; filename="' . DI::l10n()->t('calendar') . '-' . $nick . '.' . $evexport["extension"] . '"');
-                       System::httpExit($evexport["content"], Response::TYPE_BLANK, 'text/calendar');
-               }
-
-               return;
-       }
 }
index a738428eaab7071520a3e3900ff57279c9fe4943..9a75152724759cad464cd21a77d9963e589049aa 100644 (file)
@@ -670,7 +670,6 @@ class Event
                switch ($format) {
                        // Format the exported data as a CSV file.
                        case "csv":
-                               header("Content-type: text/csv");
                                $o .= '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL;
 
                                foreach ($events as $event) {
@@ -691,7 +690,6 @@ class Event
 
                        // Format the exported data as a ics file.
                        case "ical":
-                               header("Content-type: text/ics");
                                $o = 'BEGIN:VCALENDAR' . PHP_EOL
                                        . 'VERSION:2.0' . PHP_EOL
                                        . 'PRODID:-//friendica calendar export//0.1//EN' . PHP_EOL;
@@ -705,6 +703,8 @@ class Event
                                //       also long lines SHOULD be split at 75 characters length
                                foreach ($events as $event) {
                                        $o .= 'BEGIN:VEVENT' . PHP_EOL;
+                                       $o .= 'UID:' . $event['id'] . PHP_EOL;
+                                       $o .= 'DTSTAMP:' . DateTimeFormat::utc($event['created'], 'Ymd\THis\Z') . PHP_EOL;
 
                                        if ($event['start']) {
                                                $o .= 'DTSTART:' . DateTimeFormat::utc($event['start'], 'Ymd\THis\Z') . PHP_EOL;
@@ -736,7 +736,6 @@ class Event
                                        }
 
                                        $o .= 'END:VEVENT' . PHP_EOL;
-                                       $o .= PHP_EOL;
                                }
 
                                $o .= 'END:VCALENDAR' . PHP_EOL;
@@ -768,7 +767,7 @@ class Event
                        return $return;
                }
 
-               $fields = ['start', 'finish', 'summary', 'desc', 'location', 'nofinish'];
+               $fields = ['id', 'created', 'start', 'finish', 'summary', 'desc', 'location', 'nofinish'];
 
                $conditions = ['uid' => $uid, 'cid' => 0];
 
diff --git a/src/Module/Events/Export.php b/src/Module/Events/Export.php
new file mode 100644 (file)
index 0000000..fef3ed8
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Events;
+
+use Friendica\App;
+use Friendica\BaseModule;
+use Friendica\Core\L10n;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Model\Event;
+use Friendica\Model\User;
+use Friendica\Module\Response;
+use Friendica\Navigation\SystemMessages;
+use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Controller to export calendar
+ */
+class Export extends BaseModule
+{
+       const EXPORT_ICAL = 'ical';
+       const EXPORT_CSV  = 'csv';
+
+       const DEFAULT_EXPORT = self::EXPORT_ICAL;
+
+       /** @var IHandleUserSessions */
+       protected $session;
+       /** @var SystemMessages */
+       protected $sysMessages;
+
+       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 = [])
+       {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->session     = $session;
+               $this->sysMessages = $sysMessages;
+       }
+
+       protected function rawContent(array $request = [])
+       {
+               $owner = User::getByNickname($this->parameters['nickname'], ['uid']);
+               if (empty($owner)) {
+                       throw new HTTPException\NotFoundException($this->t('User not found.'));
+               }
+               $ownerUid = $owner['uid'];
+               $format   = $this->parameters['format'] ?: static::DEFAULT_EXPORT;
+
+               // Get the export data by uid
+               $evexport = Event::exportListByUserId($ownerUid, $format);
+
+               if (!$evexport["success"]) {
+                       if ($evexport["content"]) {
+                               $this->sysMessages->addNotice($this->t('This calendar format is not supported'));
+                       } else {
+                               $this->sysMessages->addNotice($this->t('No exportable data found'));
+                       }
+
+                       // If it is the own calendar return to the events page
+                       // otherwise to the profile calendar page
+                       if ($this->session->getLocalUserId() === $ownerUid) {
+                               $returnPath = 'events';
+                       } else {
+                               $returnPath = 'events/' . $this->parameters['nickname'];
+                       }
+
+                       $this->baseUrl->redirect($returnPath);
+               }
+
+               // If nothing went wrong we can echo the export content
+               if ($evexport["success"]) {
+                       $this->response->setHeader(sprintf('content-disposition: attachment; filename="%s-%s.%s"',
+                               $this->t('calendar'),
+                               $this->parameters['nickname'],
+                               $evexport["extension"]
+                       ));
+
+                       switch ($format) {
+                               case static::EXPORT_ICAL:
+                                       $this->response->setType(Response::TYPE_BLANK, 'text/ics');
+                                       break;
+                               case static::EXPORT_CSV:
+                                       $this->response->setType(Response::TYPE_BLANK, 'text/csv');
+                                       break;
+                       }
+
+                       $this->response->addContent($evexport['content']);
+               }
+       }
+}
index 6f05faee2d788de6536838035f1df06afb675775..e344de75cc1c174e9df305db8f3c3348d1b7c9e5 100644 (file)
@@ -398,7 +398,8 @@ return [
        '/dirfind'                  => [Module\Search\Directory::class, [R::GET]],
        '/directory'                => [Module\Directory::class,        [R::GET]],
 
-       '/events/json'              => [Module\Events\Json::class,      [R::GET]],
+       '/events/{nickname}/export[/{format}]' => [Module\Events\Export::class, [R::GET]],
+       '/events/json'                         => [Module\Events\Json::class,   [R::GET]],
 
        '/featured/{nickname}'      => [Module\ActivityPub\Featured::class, [R::GET]],
 
index 81dde70463c9fc5a137dfe4c21da4813cd5c5c47..c83542778ea236850b93383c5bd0fc68f324cc65 100644 (file)
@@ -3,7 +3,7 @@
        <h3>{{$etitle}}</h3>
 
        <ul class="sidebar-calendar-export-ul">
-               <li role="menuitem" class="sidebar-calendar-export-li"><a href="{{$baseurl}}/cal/{{$user}}/export/ical">{{$export_ical}}</a></li>
-               <li role="menuitem" class="sidebar-calendar-export-li"><a href="{{$baseurl}}/cal/{{$user}}/export/csv">{{$export_csv}}</a></li>
+               <li role="menuitem" class="sidebar-calendar-export-li"><a href="{{$baseurl}}/events/{{$user}}/export/ical">{{$export_ical}}</a></li>
+               <li role="menuitem" class="sidebar-calendar-export-li"><a href="{{$baseurl}}/events/{{$user}}/export/csv">{{$export_csv}}</a></li>
        </ul>
 </div>