X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FEvent.php;h=ec39cc7133d6efcdf1a555d87f71e7ba27f4a682;hb=6b8db5ad1333e2b430da3a771d9a962d44d4b6fc;hp=886f124153ac0a41bbbd1e7f58f6f4907aeba085;hpb=3f4636d490f11aafe354061fdcf2188c242ea4d3;p=friendica.git diff --git a/src/Model/Event.php b/src/Model/Event.php index 886f124153..ec39cc7133 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -5,28 +5,25 @@ namespace Friendica\Model; -use Friendica\BaseObject; use Friendica\Content\Text\BBCode; -use Friendica\Core\Addon; +use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Core\PConfig; use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\Database\DBA; -use Friendica\Model\Contact; +use Friendica\DI; +use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Friendica\Util\Map; +use Friendica\Util\Strings; use Friendica\Util\XML; -require_once 'boot.php'; -require_once 'include/dba.php'; -require_once 'include/items.php'; - /** * @brief functions for interacting with the event database table */ -class Event extends BaseObject +class Event { public static function getHTML(array $event, $simple = false) @@ -52,12 +49,14 @@ class Event extends BaseObject } if ($simple) { + $o = ''; + if (!empty($event['summary'])) { - $o = "

" . BBCode::convert($event['summary'], false, $simple) . "

"; + $o .= "

" . BBCode::convert(Strings::escapeHtml($event['summary']), false, $simple) . "

"; } if (!empty($event['desc'])) { - $o .= "
" . BBCode::convert($event['desc'], false, $simple) . "
"; + $o .= "
" . BBCode::convert(Strings::escapeHtml($event['desc']), false, $simple) . "
"; } $o .= "

" . L10n::t('Starts:') . "

" . $event_start . "

"; @@ -67,7 +66,7 @@ class Event extends BaseObject } if (!empty($event['location'])) { - $o .= "

" . L10n::t('Location:') . "

" . BBCode::convert($event['location'], false, $simple) . "

"; + $o .= "

" . L10n::t('Location:') . "

" . BBCode::convert(Strings::escapeHtml($event['location']), false, $simple) . "

"; } return $o; @@ -75,7 +74,7 @@ class Event extends BaseObject $o = '
' . "\r\n"; - $o .= '
' . BBCode::convert($event['summary'], false, $simple) . '
' . "\r\n"; + $o .= '
' . BBCode::convert(Strings::escapeHtml($event['summary']), false, $simple) . '
' . "\r\n"; $o .= '
' . L10n::t('Starts:') . ' ' . BBCode::convert($event['desc'], false, $simple) . '
' . "\r\n"; + $o .= '
' . BBCode::convert(Strings::escapeHtml($event['desc']), false, $simple) . '
' . "\r\n"; } if (!empty($event['location'])) { $o .= '
' . L10n::t('Location:') . ' ' - . BBCode::convert($event['location'], false, $simple) + . BBCode::convert(Strings::escapeHtml($event['location']), false, $simple) . '
' . "\r\n"; // Include a map of the location if the [map] BBCode is used. @@ -152,6 +151,7 @@ class Event extends BaseObject * @brief Extract bbcode formatted event data from a string. * * @params: string $s The string which should be parsed for event data. + * @param $text * @return array The array with the event information. */ public static function fromBBCode($text) @@ -219,6 +219,7 @@ class Event extends BaseObject * * @param int $event_id Event ID. * @return void + * @throws \Exception */ public static function delete($event_id) { @@ -226,7 +227,7 @@ class Event extends BaseObject return; } - DBA::delete('event', ['id' => $event_id]); + DBA::delete('event', ['id' => $event_id], ['cascade' => false]); Logger::log("Deleted event ".$event_id, Logger::DEBUG); } @@ -237,35 +238,35 @@ class Event extends BaseObject * * @param array $arr Array with event data. * @return int The new event id. + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function store($arr) { - $a = self::getApp(); - $event = []; - $event['id'] = intval(defaults($arr, 'id' , 0)); - $event['uid'] = intval(defaults($arr, 'uid' , 0)); - $event['cid'] = intval(defaults($arr, 'cid' , 0)); - $event['uri'] = defaults($arr, 'uri' , Item::newURI($event['uid'])); - $event['type'] = defaults($arr, 'type' , 'event'); - $event['summary'] = defaults($arr, 'summary' , ''); - $event['desc'] = defaults($arr, 'desc' , ''); - $event['location'] = defaults($arr, 'location' , ''); - $event['allow_cid'] = defaults($arr, 'allow_cid', ''); - $event['allow_gid'] = defaults($arr, 'allow_gid', ''); - $event['deny_cid'] = defaults($arr, 'deny_cid' , ''); - $event['deny_gid'] = defaults($arr, 'deny_gid' , ''); - $event['adjust'] = intval(defaults($arr, 'adjust' , 0)); - $event['nofinish'] = intval(defaults($arr, 'nofinish' , !empty($event['start']) && empty($event['finish']))); - - $event['created'] = DateTimeFormat::utc(defaults($arr, 'created' , 'now')); - $event['edited'] = DateTimeFormat::utc(defaults($arr, 'edited' , 'now')); - $event['start'] = DateTimeFormat::utc(defaults($arr, 'start' , DBA::NULL_DATETIME)); - $event['finish'] = DateTimeFormat::utc(defaults($arr, 'finish' , DBA::NULL_DATETIME)); + $event['id'] = intval($arr['id'] ?? 0); + $event['uid'] = intval($arr['uid'] ?? 0); + $event['cid'] = intval($arr['cid'] ?? 0); + $event['guid'] = ($arr['guid'] ?? '') ?: System::createUUID(); + $event['uri'] = ($arr['uri'] ?? '') ?: Item::newURI($event['uid'], $event['guid']); + $event['type'] = ($arr['type'] ?? '') ?: 'event'; + $event['summary'] = $arr['summary'] ?? ''; + $event['desc'] = $arr['desc'] ?? ''; + $event['location'] = $arr['location'] ?? ''; + $event['allow_cid'] = $arr['allow_cid'] ?? ''; + $event['allow_gid'] = $arr['allow_gid'] ?? ''; + $event['deny_cid'] = $arr['deny_cid'] ?? ''; + $event['deny_gid'] = $arr['deny_gid'] ?? ''; + $event['adjust'] = intval($arr['adjust'] ?? 0); + $event['nofinish'] = intval($arr['nofinish'] ?? (!empty($event['start']) && empty($event['finish']))); + + $event['created'] = DateTimeFormat::utc(($arr['created'] ?? '') ?: 'now'); + $event['edited'] = DateTimeFormat::utc(($arr['edited'] ?? '') ?: 'now'); + $event['start'] = DateTimeFormat::utc(($arr['start'] ?? '') ?: DBA::NULL_DATETIME); + $event['finish'] = DateTimeFormat::utc(($arr['finish'] ?? '') ?: DBA::NULL_DATETIME); if ($event['finish'] < DBA::NULL_DATETIME) { $event['finish'] = DBA::NULL_DATETIME; } - $private = intval(defaults($arr, 'private', 0)); + $private = intval($arr['private'] ?? 0); $conditions = ['uid' => $event['uid']]; if ($event['cid']) { @@ -303,7 +304,7 @@ class Event extends BaseObject $item = Item::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]); if (DBA::isResult($item)) { - $object = '' . XML::escape(ACTIVITY_OBJ_EVENT) . '' . XML::escape($event['uri']) . ''; + $object = '' . XML::escape(Activity\ObjectType::EVENT) . '' . XML::escape($event['uri']) . ''; $object .= '' . XML::escape(self::getBBCode($event)) . ''; $object .= '' . "\n"; @@ -315,10 +316,8 @@ class Event extends BaseObject $item_id = 0; } - Addon::callHooks('event_updated', $event['id']); + Hook::callAll('event_updated', $event['id']); } else { - $event['guid'] = defaults($arr, 'guid', System::createUUID()); - // New event. Store it. DBA::insert('event', $event); @@ -335,7 +334,7 @@ class Event extends BaseObject $item_arr['uri'] = $event['uri']; $item_arr['parent-uri'] = $event['uri']; $item_arr['guid'] = $event['guid']; - $item_arr['plink'] = defaults($arr, 'plink', ''); + $item_arr['plink'] = $arr['plink'] ?? ''; $item_arr['post-type'] = Item::PT_EVENT; $item_arr['wall'] = $event['cid'] ? 0 : 1; $item_arr['contact-id'] = $contact['id']; @@ -352,20 +351,20 @@ class Event extends BaseObject $item_arr['deny_gid'] = $event['deny_gid']; $item_arr['private'] = $private; $item_arr['visible'] = 1; - $item_arr['verb'] = ACTIVITY_POST; - $item_arr['object-type'] = ACTIVITY_OBJ_EVENT; + $item_arr['verb'] = Activity::POST; + $item_arr['object-type'] = Activity\ObjectType::EVENT; $item_arr['origin'] = $event['cid'] === 0 ? 1 : 0; $item_arr['body'] = self::getBBCode($event); $item_arr['event-id'] = $event['id']; - $item_arr['object'] = '' . XML::escape(ACTIVITY_OBJ_EVENT) . '' . XML::escape($event['uri']) . ''; + $item_arr['object'] = '' . XML::escape(Activity\ObjectType::EVENT) . '' . XML::escape($event['uri']) . ''; $item_arr['object'] .= '' . XML::escape(self::getBBCode($event)) . ''; $item_arr['object'] .= '' . "\n"; $item_id = Item::insert($item_arr); } - Addon::callHooks("event_created", $event['id']); + Hook::callAll("event_created", $event['id']); } return $item_id; @@ -375,6 +374,7 @@ class Event extends BaseObject * @brief Create an array with translation strings used for events. * * @return array Array with translations strings. + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function getStrings() { @@ -418,7 +418,6 @@ class Event extends BaseObject "February" => L10n::t("February"), "March" => L10n::t("March"), "April" => L10n::t("April"), - "May" => L10n::t("May"), "June" => L10n::t("June"), "July" => L10n::t("July"), "August" => L10n::t("August"), @@ -471,6 +470,7 @@ class Event extends BaseObject * @param int $event_id The ID of the event in the event table * @param string $sql_extra * @return array Query result + * @throws \Exception */ public static function getListById($owner_uid, $event_id, $sql_extra = '') { @@ -499,17 +499,18 @@ class Event extends BaseObject /** * @brief Get all events in a specific time frame. * - * @param int $owner_uid The User ID of the owner of the events. - * @param array $event_params An associative array with - * int 'ignore' => - * string 'start' => Start time of the timeframe. - * string 'finish' => Finish time of the timeframe. - * string 'adjust_start' => - * string 'adjust_finish' => + * @param int $owner_uid The User ID of the owner of the events. + * @param array $event_params An associative array with + * int 'ignore' => + * string 'start' => Start time of the timeframe. + * string 'finish' => Finish time of the timeframe. + * string 'adjust_start' => + * string 'adjust_finish' => * - * @param string $sql_extra Additional sql conditions (e.g. permission request). + * @param string $sql_extra Additional sql conditions (e.g. permission request). * * @return array Query results. + * @throws \Exception */ public static function getListByDate($owner_uid, $event_params, $sql_extra = '') { @@ -550,6 +551,8 @@ class Event extends BaseObject * * @param array $event_result Event query array. * @return array Event array for the template. + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException */ public static function prepareListForTemplate(array $event_result) { @@ -587,15 +590,14 @@ class Event extends BaseObject $copy = null; $drop = null; if (local_user() && local_user() == $event['uid'] && $event['type'] == 'event') { - $edit = !$event['cid'] ? [System::baseUrl() . '/events/event/' . $event['id'], L10n::t('Edit event') , '', ''] : null; - $copy = !$event['cid'] ? [System::baseUrl() . '/events/copy/' . $event['id'] , L10n::t('Duplicate event'), '', ''] : null; - $drop = [System::baseUrl() . '/events/drop/' . $event['id'] , L10n::t('Delete event') , '', '']; + $edit = !$event['cid'] ? [DI::baseUrl() . '/events/event/' . $event['id'], L10n::t('Edit event') , '', ''] : null; + $copy = !$event['cid'] ? [DI::baseUrl() . '/events/copy/' . $event['id'] , L10n::t('Duplicate event'), '', ''] : null; + $drop = [DI::baseUrl() . '/events/drop/' . $event['id'] , L10n::t('Delete event') , '', '']; } - $title = strip_tags(html_entity_decode(BBCode::convert($event['summary']), ENT_QUOTES, 'UTF-8')); + $title = BBCode::convert(Strings::escapeHtml($event['summary'])); if (!$title) { - list($title, $_trash) = explode(" $event['id'], 'start' => $start, @@ -631,25 +634,27 @@ class Event extends BaseObject /** * @brief Format event to export format (ical/csv). * - * @param array $events Query result for events. - * @param string $format The output format (ical/csv). - * @param string $timezone The timezone of the user (not implemented yet). + * @param array $events Query result for events. + * @param string $format The output format (ical/csv). * + * @param $timezone * @return string Content according to selected export format. * - * @todo Implement timezone support + * @todo Implement timezone support */ - private static function formatListForExport(array $events, $format, $timezone) + private static function formatListForExport(array $events, $format) { + $o = ''; + if (!count($events)) { - return ''; + return $o; } 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; + $o .= '"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "Location"' . PHP_EOL; foreach ($events as $event) { /// @todo The time / date entries don't include any information about the @@ -745,6 +750,7 @@ class Event extends BaseObject * @param int $uid The user ID. * * @return array Query results. + * @throws \Exception */ private static function getListByUserId($uid = 0) { @@ -775,33 +781,29 @@ class Event extends BaseObject /** * - * @param int $uid The user ID. + * @param int $uid The user ID. * @param string $format Output format (ical/csv). * @return array With the results: - * bool 'success' => True if the processing was successful,
- * string 'format' => The output format,
- * string 'extension' => The file extension of the output format,
- * string 'content' => The formatted output content.
+ * bool 'success' => True if the processing was successful,
+ * string 'format' => The output format,
+ * string 'extension' => The file extension of the output format,
+ * string 'content' => The formatted output content.
* + * @throws \Exception * @todo Respect authenticated users with events_by_uid(). */ public static function exportListByUserId($uid, $format = 'ical') { $process = false; - $user = DBA::selectFirst('user', ['timezone'], ['uid' => $uid]); - if (DBA::isResult($user)) { - $timezone = $user['timezone']; - } - // Get all events which are owned by a uid (respects permissions). $events = self::getListByUserId($uid); // We have the events that are available for the requestor. // Now format the output according to the requested format. - $res = self::formatListForExport($events, $format, $timezone); + $res = self::formatListForExport($events, $format); - // If there are results the precess was successfull. + // If there are results the precess was successful. if (!empty($res)) { $process = true; } @@ -835,6 +837,8 @@ class Event extends BaseObject * * @param array $item Array with item and event data. * @return string HTML output. + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException */ public static function getItemHTML(array $item) { $same_date = false; @@ -908,7 +912,7 @@ class Event extends BaseObject $tpl = Renderer::getMarkupTemplate('event_stream_item.tpl'); $return = Renderer::replaceMacros($tpl, [ '$id' => $item['event-id'], - '$title' => prepare_text($item['event-summary']), + '$title' => BBCode::convert($item['event-summary']), '$dtstart_label' => L10n::t('Starts:'), '$dtstart_title' => $dtstart_title, '$dtstart_dt' => $dtstart_dt, @@ -926,7 +930,7 @@ class Event extends BaseObject '$author_name' => $item['author-name'], '$author_link' => $profile_link, '$author_avatar' => $item['author-avatar'], - '$description' => prepare_text($item['event-desc']), + '$description' => BBCode::convert($item['event-desc']), '$location_label' => L10n::t('Location:'), '$show_map_label' => L10n::t('Show map'), '$hide_map_label' => L10n::t('Hide map'), @@ -949,6 +953,7 @@ class Event extends BaseObject * 'name' => The name of the location,
* 'address' => The address of the location,
* 'coordinates' => Latitude‎ and longitude‎ (e.g. '48.864716,2.349014').
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ private static function locationToArray($s = '') { if ($s == '') { @@ -975,7 +980,7 @@ class Event extends BaseObject } } - $location['name'] = prepare_text($location['name']); + $location['name'] = BBCode::convert($location['name']); // Construct the map HTML. if (isset($location['address'])) { @@ -993,6 +998,7 @@ class Event extends BaseObject * @param array $contact Contact array, expects: id, uid, url, name * @param string $birthday Birthday of the contact * @return bool + * @throws \Exception */ public static function createBirthday($contact, $birthday) {