]> git.mxchange.org Git - friendica.git/blob - src/Module/Events/Json.php
[frio] Fix event time display
[friendica.git] / src / Module / Events / Json.php
1 <?php
2
3 namespace Friendica\Module\Events;
4
5 use Friendica\Database\DBA;
6 use Friendica\DI;
7 use Friendica\Model\Event;
8 use Friendica\Model\Item;
9 use Friendica\Model\Post;
10 use Friendica\Network\HTTPException;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Temporal;
13
14 class Json extends \Friendica\BaseModule
15 {
16         public static function rawContent(array $parameters = [])
17         {
18                 if (!local_user()) {
19                         throw new HTTPException\UnauthorizedException();
20                 }
21
22                 $y = intval(DateTimeFormat::localNow('Y'));
23                 $m = intval(DateTimeFormat::localNow('m'));
24
25                 // Put some limit on dates. The PHP date functions don't seem to do so well before 1900.
26                 if ($y < 1901) {
27                         $y = 1900;
28                 }
29
30                 $dim    = Temporal::getDaysInMonth($y, $m);
31                 $start  = sprintf('%d-%d-%d %d:%d:%d', $y, $m, 1, 0, 0, 0);
32                 $finish = sprintf('%d-%d-%d %d:%d:%d', $y, $m, $dim, 23, 59, 59);
33
34                 if (!empty($_GET['start'])) {
35                         $start = $_GET['start'];
36                 }
37
38                 if (!empty($_GET['end'])) {
39                         $finish = $_GET['end'];
40                 }
41
42                 // put the event parametes in an array so we can better transmit them
43                 $event_params = [
44                         'event_id' => intval($_GET['id'] ?? 0),
45                         'start'    => $start,
46                         'finish'   => $finish,
47                         'ignore'   => 0,
48                 ];
49
50                 // get events by id or by date
51                 if ($event_params['event_id']) {
52                         $r = Event::getListById(local_user(), $event_params['event_id']);
53                 } else {
54                         $r = Event::getListByDate(local_user(), $event_params);
55                 }
56
57                 $links = [];
58
59                 if (DBA::isResult($r)) {
60                         $r = Event::sortByDate($r);
61                         foreach ($r as $rr) {
62                                 $j = DateTimeFormat::utc($rr['start'], 'j');
63                                 if (empty($links[$j])) {
64                                         $links[$j] = DI::baseUrl() . '/' . DI::args()->getCommand() . '#link-' . $j;
65                                 }
66                         }
67                 }
68
69                 $events = [];
70
71                 // transform the event in a usable array
72                 if (DBA::isResult($r)) {
73                         $events = Event::sortByDate($r);
74
75                         $events = self::map($events);
76                 }
77
78                 header('Content-Type: application/json');
79                 echo json_encode($events);
80                 exit();
81         }
82
83         private static function map(array $events): array
84         {
85                 return array_map(function ($event) {
86                         $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private', 'uri-id'], ['id' => $event['itemid']]);
87                         if (!DBA::isResult($item)) {
88                                 // Using default values when no item had been found
89                                 $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC, 'uri-id' => ($event['uri-id'] ?? 0)];
90                         }
91
92                         return [
93                                 'id'       => $event['id'],
94                                 'title'    => $event['summary'],
95                                 'start'    => DateTimeFormat::local($event['start']),
96                                 'end'      => DateTimeFormat::local($event['finish']),
97                                 'nofinish' => $event['nofinish'],
98                                 'desc'     => $event['desc'],
99                                 'location' => $event['location'],
100                                 'item'     => $item,
101                         ];
102                 }, $events);
103         }
104 }