id = filterId($id); $this->date = DateTime::createFromFormat("Y-m-d", $date); $this->shiaiId = filterId($shiaiId); $this->deadline = DateTime::createFromFormat("Y-m-d", $deadline); $this->remarks = $remarks; } // Getter /** Getter for the id * * @return (int) Id of the event */ public function getId() { return $this->id; } /** Getter for the date * * @return DateTime date of the event */ public function getDate() { return $this->date; } /** Getter for the shiaiId * * @return int>0 id for the shiai in the db */ public function getShiaiId() { return $this->shiaiId; } /** Getter for the deadline * * @return DateTime deadline for the event */ public function getDeadLine() { return $this->deadline; } /** Getter for the linked Shiai * * - lazy loading: only load if not already loaded (overridable by the $forceLoading param) * * @param boolean $forceLoading if true, the loading is enforced even if there already is a shiai linked * * @return Shiai reference to the linked Shiai */ public function shiai($forceLoading = false): Shiai { // We want to load if it isn't loaded yet or we want to enforce it. But in either case we need an id to load if ((!isset($this->shiai) || $forceLoading) && isset($this->shiaiId)) { $this->shiai = Shiai::loadFromDb($this->shiaiId); } if ($this->shiai == null) { $this->shiai = Shiai::fromDbArray( json_decode($this->remarks, true), ); } return $this->shiai; } /** load the Event by id from the db * * - Frontend function: sanitizes the input and calls the backend function * * @param integer>0 $id id of the Event to load from the data base * @return Event Event from the db with this id, null in case the Event couldn't be loaded */ public static function loadFromDb($id) { $id = filterId($id); if ($id) { return self::loadFromDbById($id); } return null; } /** * loads an event from the db by id * * - Backend function, input is supposed to be sanitized * * @param integer>0 $id sanitized id of the event to load * @return Event Event from the db with this id, null in case the Event couldn't be loaded */ private static function loadFromDbById(int $id) { $query = "SELECT * FROM `cwsvjudo_main`.`wkParticipo_Events` WHERE `id` = :id;"; $params = [":id" => ["value" => $id, "data_type" => PDO::PARAM_INT]]; $response = dbConnector::query($query, $params); // ids are considered unique. so every other count then 1 is treated as error to prevent unprivileged access if (count($response) != 1) { return null; } return self::fromDbArray($response[0]); } public function htmlDate($format = "Y-m-d") { return $this->date->format($format); } public function htmlDeadLine($format = "Y-m-d") { return $this->deadline->format($format); } /** Representation of an event as (materializeCss) card * * @return string string with the html code of the event */ public function asHtmlCard() { $shiai = $this->shiai(); return '
' . '
' . '' . $shiai->getHtmlName() . "" . "
" . "
Datum
" . "
" . $this->htmlDate() . "
" . "
Einschreibefrist
" . "
" . $this->htmlDeadLine() . "
" . "
Altersklassen
" . "
" . $this->shiai()->getHtmlDescriptiveAgeClasses() . "
" . "
" . "
" . "
"; } public function getHtmlEventDetails() { $html = "
"; $html .= "
Datum
" . $this->htmlDate() . "
"; $html .= "
Deadline zum Einschreiben:
" . $this->htmlDeadLine() . "
"; $html .= "
"; $html .= $this->shiai()->getHtml(); $html .= $this->getHtmlStarterStatistic(); $html .= $this->getHtmlStarterList(); } public function htmlTableRow(): string { $shiai = $this->shiai(); return "" . "" . $this->htmlDate() . "" . '' . $shiai->getHtmlName() . "" . "" . $shiai->getHtmlDescriptiveAgeClasses() . "" . '' . ""; } public function htmlModal(): void { $modal = '"; echo $modal; } /** Get the list of starters as html list */ public function getHtmlStarterList() { $listOfStarter = $this->getStarter(); if (!isset($listOfStarter) || count($listOfStarter) == 0) { return ""; } $starterList = 'Bereits eingetragen:
'; foreach ($listOfStarter as $start) { $startingUser = $start->loadStarter(); $starterList .= '
' . '
' . $startingUser->getName() . ", " . $startingUser->getFirstname() . " (" . StartingType::$AsString[$start->getTypeId()] . "):
" . '
' . $start->getHtmlFormRemove() . "
" . "
"; } $starterList .= "
"; return $starterList; } /** Returns the currents users starter to this event * * @todo docu */ public function getStarter() { $userId = $_SESSION["user"]["userId"] ?? null; $query = "SELECT `wkParticipo_Starter`.`id` as `starterId` FROM `wkParticipo_Starter` " . "LEFT JOIN `vormundschaft` ON `vormundschaft`.`kidId` = `wkParticipo_Starter`.`userId` " . " WHERE `wkParticipo_Starter`.`eventId` = :eventId AND `vormundschaft`.`userId` = :userId;"; $params = [ ":eventId" => ["value" => $this->id, "data_type" => PDO::PARAM_INT], ":userId" => ["value" => $userId, "data_type" => PDO::PARAM_INT], ]; $response = dbConnector::query($query, $params); $starter = []; foreach ($response as $r) { $starter[$r["starterId"]] = Starter::loadFromDb($r["starterId"]); } return $starter; } /** get number of starters of a certain type for this event * * @param int $startingType it representation of the StartingType to count * @return int count of starters */ public function getStarterCount(int $startingType) { $query = "SELECT COUNT(`wkParticipo_Starter`.`id`) AS starterCount FROM `wkParticipo_Starter` " . " WHERE `wkParticipo_Starter`.`eventId` = :eventId AND `wkParticipo_Starter`.`type` = :typeId;"; $params = [ ":eventId" => ["value" => $this->id, "data_type" => PDO::PARAM_INT], ":typeId" => [ "value" => $startingType, "data_type" => PDO::PARAM_INT, ], ]; $response = dbConnector::query($query, $params); return intval($response[0]["starterCount"]); } public function getSeatCount() { return self::getSeatCountOf($this->id); } public function getHtmlStarterStatistic() { $isEmpty = true; $retHtml = "
"; foreach ( [ StartingType::Fighter, StartingType::NoParticipation, StartingType::Audience, ] as $type ) { $count = $this->getStarterCount($type); if ($count > 0) { $isEmpty = false; $retHtml .= "
" . StartingType::$AsString[$type] . "
" . $count . "
"; } } if ($this->getSeatCount() > 0) { $isEmpty = false; $retHtml .= "
Mitfahrgelegenheiten
" . $this->getSeatCount() . "
"; } $retHtml .= "
"; return $isEmpty ? "" : $retHtml; } public static function fromDbArray($member) { return new Event( $member["id"] ?? null, $member["date"] ?? null, $member["wkId"] ?? null, $member["meldefrist"] ?? null, $member["bemerkungen"] ?? null, ); } /// Einen Starter per userId mit typeId zu einem Event per eventId hinzufügen /// Es erfolgt keine Überprüfung der Meldeberechtigung! public static function addStarter($dbConnection, $starter) { $query = "INSERT INTO `wkParticipo_Starter` (eventId, userId, type) values (:eventId, :userId, :typeId);"; $params = [ ":eventId" => [ "value" => $starter->getEventId(), "data_type" => PDO::PARAM_INT, ], ":userId" => [ "value" => $starter->getUserId(), "data_type" => PDO::PARAM_INT, ], ":typeId" => [ "value" => $starter->getTypeId(), "data_type" => PDO::PARAM_INT, ], ]; return dbConnector::query($query, $params); } public static function getSeatCountOf(int $id) { $query = "SELECT SUM(plaetze) AS sumSeats FROM `cwsvjudo_main`.`wkParticipo_Fahrten` WHERE eventId = :eventId;"; $params = [ "eventId" => ["value" => $id, "data_type" => PDO::PARAM_INT], ]; $response = dbConnector::query($query, $params); $sumSeats = filterCount($response[0]["sumSeats"]); return $sumSeats; } public function getHtmlAddStarterForm($user, $options = []) { $defaults = [ "formClass" => "s12 m6 xl3", "inputClass" => "col s12", "buttonClass" => "btn", ]; $options = array_merge($defaults, $options); $returnToUrl = $options["returnToUrl"] ?? urlencode(getCurPagesUrl()); $key = $_SESSION["apiKey"] ?? null; $selectId = "form-selectType-event-" . $this->id . "-user-" . $user->getId(); $form = '
' . '' . '' . '' . '
' . '
" . '" . "

" . '" . "

" . "

" . '" . "

" . "

" . '" . "

" . '' . "
" . "
" . "
"; return $form; } } // end class event