]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Event.php
Standards
[friendica.git] / src / Model / Event.php
index 145632fdfcefb8d34e0c3fbc22bb27b4aa2f2939..81e33013fb6d2b2606b656a7b9a191b407db330a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -173,32 +173,32 @@ class Event
        {
                $ev = [];
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-summary\](.*?)\[\/event\-summary\]/is", $text, $match)) {
                        $ev['summary'] = $match[1];
                }
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-description\](.*?)\[\/event\-description\]/is", $text, $match)) {
                        $ev['desc'] = $match[1];
                }
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-start\](.*?)\[\/event\-start\]/is", $text, $match)) {
                        $ev['start'] = $match[1];
                }
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-finish\](.*?)\[\/event\-finish\]/is", $text, $match)) {
                        $ev['finish'] = $match[1];
                }
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-location\](.*?)\[\/event\-location\]/is", $text, $match)) {
                        $ev['location'] = $match[1];
                }
 
-               $match = '';
+               $match = [];
                if (preg_match("/\[event\-adjust\](.*?)\[\/event\-adjust\]/is", $text, $match)) {
                        $ev['adjust'] = $match[1];
                }
@@ -242,7 +242,7 @@ class Event
                        return;
                }
 
-               DBA::delete('event', ['id' => $event_id], ['cascade' => false]);
+               DBA::delete('event', ['id' => $event_id]);
                Logger::log("Deleted event ".$event_id, Logger::DEBUG);
        }
 
@@ -257,6 +257,16 @@ class Event
         */
        public static function store($arr)
        {
+               $network = $arr['network'] ?? Protocol::DFRN;
+               $protocol = $arr['protocol'] ?? Conversation::PARCEL_UNKNOWN;
+               $direction = $arr['direction'] ?? Conversation::UNKNOWN;
+               $source = $arr['source'] ?? '';
+
+               unset($arr['network']);
+               unset($arr['protocol']);
+               unset($arr['direction']);
+               unset($arr['source']);
+
                $event = [];
                $event['id']        = intval($arr['id']        ?? 0);
                $event['uid']       = intval($arr['uid']       ?? 0);
@@ -283,14 +293,16 @@ class Event
                }
                $private = intval($arr['private'] ?? 0);
 
-               $conditions = ['uid' => $event['uid']];
                if ($event['cid']) {
-                       $conditions['id'] = $event['cid'];
+                       $conditions = ['id' => $event['cid']];
                } else {
-                       $conditions['self'] = true;
+                       $conditions = ['uid' => $event['uid'], 'self' => true];
                }
 
                $contact = DBA::selectFirst('contact', [], $conditions);
+               if (!DBA::isResult($contact)) {
+                       Logger::warning('Contact not found', ['condition' => $conditions, 'callstack' => System::callstack(20)]);
+               }
 
                // Existing event being modified.
                if ($event['id']) {
@@ -298,7 +310,7 @@ class Event
                        $existing_event = DBA::selectFirst('event', ['edited'], ['id' => $event['id'], 'uid' => $event['uid']]);
                        if (!DBA::isResult($existing_event) || ($existing_event['edited'] === $event['edited'])) {
 
-                               $item = Item::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]);
+                               $item = Post::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]);
 
                                return DBA::isResult($item) ? $item['id'] : 0;
                        }
@@ -317,7 +329,7 @@ class Event
 
                        DBA::update('event', $updated_fields, ['id' => $event['id'], 'uid' => $event['uid']]);
 
-                       $item = Item::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]);
+                       $item = Post::selectFirst(['id', 'uri-id'], ['event-id' => $event['id'], 'uid' => $event['uid']]);
                        if (DBA::isResult($item)) {
                                $object = '<object><type>' . XML::escape(Activity\ObjectType::EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
                                $object .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
@@ -326,9 +338,9 @@ class Event
                                $fields = ['body' => self::getBBCode($event), 'object' => $object, 'edited' => $event['edited']];
                                Item::update($fields, ['id' => $item['id']]);
 
-                               $item_id = $item['id'];
+                               $uriid = $item['uri-id'];
                        } else {
-                               $item_id = 0;
+                               $uriid = 0;
                        }
 
                        Hook::callAll('event_updated', $event['id']);
@@ -336,7 +348,7 @@ class Event
                        // New event. Store it.
                        DBA::insert('event', $event);
 
-                       $item_id = 0;
+                       $uriid = 0;
 
                        // Don't create an item for birthday events
                        if ($event['type'] == 'event') {
@@ -347,7 +359,7 @@ class Event
                                $item_arr['uid']           = $event['uid'];
                                $item_arr['contact-id']    = $event['cid'];
                                $item_arr['uri']           = $event['uri'];
-                               $item_arr['parent-uri']    = $event['uri'];
+                               $item_arr['uri-id']        = ItemURI::getIdByURI($event['uri']);
                                $item_arr['guid']          = $event['guid'];
                                $item_arr['plink']         = $arr['plink'] ?? '';
                                $item_arr['post-type']     = Item::PT_EVENT;
@@ -368,22 +380,28 @@ class Event
                                $item_arr['visible']       = 1;
                                $item_arr['verb']          = Activity::POST;
                                $item_arr['object-type']   = Activity\ObjectType::EVENT;
+                               $item_arr['post-type']     = Item::PT_EVENT;
                                $item_arr['origin']        = $event['cid'] === 0 ? 1 : 0;
                                $item_arr['body']          = self::getBBCode($event);
                                $item_arr['event-id']      = $event['id'];
-                               $item_arr['network']       = Protocol::DFRN;
+                               $item_arr['network']       = $network;
+                               $item_arr['protocol']      = $protocol;
+                               $item_arr['direction']     = $direction;
+                               $item_arr['source']        = $source;
 
                                $item_arr['object']  = '<object><type>' . XML::escape(Activity\ObjectType::EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
                                $item_arr['object'] .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
                                $item_arr['object'] .= '</object>' . "\n";
 
-                               $item_id = Item::insert($item_arr);
+                               if (Item::insert($item_arr)) {
+                                       $uriid = $item_arr['uri-id'];
+                               }
                        }
 
                        Hook::callAll("event_created", $event['id']);
                }
 
-               return $item_id;
+               return $uriid;
        }
 
        /**
@@ -498,15 +516,13 @@ class Event
                }
 
                // Query for the event by event id
-               $r = q("SELECT `event`.*, `item`.`id` AS `itemid` FROM `event`
-                       LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
-                       WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
-                       intval($owner_uid),
-                       intval($event_id)
-               );
+               $events = DBA::toArray(DBA::p("SELECT `event`.*, `post-user-view`.`id` AS `itemid` FROM `event`
+                       LEFT JOIN `post-user-view` ON `post-user-view`.`event-id` = `event`.`id` AND `post-user-view`.`uid` = `event`.`uid`
+                       WHERE `event`.`uid` = ? AND `event`.`id` = ? $sql_extra",
+                       $owner_uid, $event_id));
 
-               if (DBA::isResult($r)) {
-                       $return = self::removeDuplicates($r);
+               if (DBA::isResult($events)) {
+                       $return = self::removeDuplicates($events);
                }
 
                return $return;
@@ -539,24 +555,17 @@ class Event
 
                // Query for the event by date.
                // @todo Slow query (518 seconds to run), to be optimzed
-               $r = q("SELECT `event`.*, `item`.`id` AS `itemid` FROM `event`
-                               LEFT JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
-                               WHERE `event`.`uid` = %d AND event.ignore = %d
-                               AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
-                               OR  (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
-                               $sql_extra ",
-                               intval($owner_uid),
-                               intval($event_params["ignore"]),
-                               DBA::escape($event_params["start"]),
-                               DBA::escape($event_params["start"]),
-                               DBA::escape($event_params["finish"]),
-                               DBA::escape($event_params["adjust_start"]),
-                               DBA::escape($event_params["adjust_start"]),
-                               DBA::escape($event_params["adjust_finish"])
-               );
+               $events = DBA::toArray(DBA::p("SELECT `event`.*, `post-user-view`.`id` AS `itemid` FROM `event`
+                               LEFT JOIN `post-user-view` ON `post-user-view`.`event-id` = `event`.`id` AND `post-user-view`.`uid` = `event`.`uid`
+                               WHERE `event`.`uid` = ? AND `event`.`ignore` = ?
+                               AND ((NOT `adjust` AND (`finish` >= ? OR (`nofinish` AND `start` >= ?)) AND `start` <= ?)
+                               OR  (`adjust` AND (`finish` >= ? OR (`nofinish` AND `start` >= ?)) AND `start` <= ?))" . $sql_extra,
+                               $owner_uid, $event_params["ignore"],
+                               $event_params["start"], $event_params["start"], $event_params["finish"],
+                               $event_params["adjust_start"], $event_params["adjust_start"], $event_params["adjust_finish"]));
 
-               if (DBA::isResult($r)) {
-                       $return = self::removeDuplicates($r);
+               if (DBA::isResult($events)) {
+                       $return = self::removeDuplicates($events);
                }
 
                return $return;
@@ -577,10 +586,10 @@ class Event
                $last_date = '';
                $fmt = DI::l10n()->t('l, F j');
                foreach ($event_result as $event) {
-                       $item = Item::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link'], ['id' => $event['itemid']]);
+                       $item = Post::selectFirst(['plink', 'author-name', 'author-avatar', 'author-link', 'private'], ['id' => $event['itemid']]);
                        if (!DBA::isResult($item)) {
                                // Using default values when no item had been found
-                               $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => ''];
+                               $item = ['plink' => '', 'author-name' => '', 'author-avatar' => '', 'author-link' => '', 'private' => Item::PUBLIC];
                        }
 
                        $event = array_merge($event, $item);
@@ -617,10 +626,8 @@ class Event
                        }
 
                        $author_link = $event['author-link'];
-                       $plink       = $event['plink'];
 
                        $event['author-link'] = Contact::magicLink($author_link);
-                       $event['plink']       = Contact::magicLink($author_link, $plink);
 
                        $html = self::getHTML($event);
                        $event['summary']  = BBCode::convert(Strings::escapeHtml($event['summary']));
@@ -640,7 +647,7 @@ class Event
                                'is_first' => $is_first,
                                'item'     => $event,
                                'html'     => $html,
-                               'plink'    => [$event['plink'], DI::l10n()->t('link to source'), '', ''],
+                               'plink'    => Item::getPlink($event),
                        ];
                }
 
@@ -923,7 +930,9 @@ class Event
                $location = self::locationToArray($item['event-location']);
 
                // Construct the profile link (magic-auth).
-               $profile_link = Contact::magicLinkById($item['author-id']);
+               $author = ['uid' => 0, 'id' => $item['author-id'],
+                               'network' => $item['author-network'], 'url' => $item['author-link']];
+               $profile_link = Contact::magicLinkByContact($author);
 
                $tpl = Renderer::getMarkupTemplate('event_stream_item.tpl');
                $return = Renderer::replaceMacros($tpl, [