<?PHP
/**
 * For embedding from museum-digital functions.
 *
 * @file
 *
 * @author Joshua Ramon Enslin <joshua@jrenslin.de>
 */

/**
 * Function for checking a text for the existence of pseudocode
 * for embedding from museum-digital.
 *
 * @param string $text     Input string.
 * @param array  $settings General settings.
 *
 * @return string
 */
function checkForEmbeds(string $text, array $settings):string {

    $embedOptions = [
        "singleObjectTile",
        "singleObjectDetails",
        "singleCollectionTile",
        "singleCollectionDetails",
        "singleInstitutionTile",
        "singleInstitutionDetails",
        "singleExhibitionDetails",
        "singleEventDetails",
        "exhibitionCalendar",
        "eventCalendar"
    ];

    foreach ($embedOptions as $option) {

        if (strpos($text, $option) === false) continue;
        $position = strpos($text, $option) - 1;

        $nextTag = $nextWhitespace = strlen($text);
        if (strpos($text, "<", $position) !== false) $nextTag        = strpos($text, "<", $position);
        if (strpos($text, " ", $position) !== false) $nextWhitespace = strpos($text, " ", $position);

        $end = min($nextTag, $nextWhitespace);

        $pseudocode = substr($text, $position, $end - $position);

        $command   = substr($pseudocode, 1, strpos($pseudocode, "]") - 1);
        $arguments = [];
        if (strpos($pseudocode, "{") !== false) $arguments = explode("&", substr($pseudocode, strpos($pseudocode, "{") + 1, -1));

        switch ($command) {
        case "singleObjectTile":
            $text = str_replace($pseudocode, embedObject($arguments, $settings), $text);
            break;
        case "singleObjectDetails":
            $text = str_replace($pseudocode, embedObject($arguments, $settings, true), $text);
            break;
        case "singleCollectionTile":
            $text = str_replace($pseudocode, embedCollection($arguments, $settings), $text);
            break;
        case "singleCollectionDetails":
            $text = str_replace($pseudocode, embedCollection($arguments, $settings, true), $text);
            break;
        case "singleInstitutionTile":
            $text = str_replace($pseudocode, embedInstitution($arguments, $settings), $text);
            break;
        case "singleInstitutionDetails":
            $text = str_replace($pseudocode, embedInstitution($arguments, $settings, true), $text);
            break;
        case "exhibitionCalendar":
            $text = str_replace($pseudocode, embedExhibitionCalendar($arguments), $text);
            break;
        case "singleExhibitionDetails":
            $text = str_replace($pseudocode, embedExhibition($arguments, $settings, true), $text);
            break;
        case "eventCalendar":
            $text = str_replace($pseudocode, embedEventCalendar($arguments), $text);
            break;
        case "singleEventDetails":
            $text = str_replace($pseudocode, embedEvent($arguments, $settings, true), $text);
            break;
        }

    }

    return $text;

}

/**
 * Function drawObjectTile creates a tile with just the most basic information on an object.
 *
 * @param string[] $contents Input data fetched from the object API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawObjectTile(array $contents, array $settings):string {

    $output = '
    <div class="objTile">
    ';

    if (count($contents['object_images']) > 0) {
        foreach ($contents['object_images'] as $image) {
            if ($image['is_main'] != "j") continue;
            $output .= '
        <img src="' . $settings['mdImgFolder'] . $image['folder'] . '/' . $image['preview'] . '" />';
        }
    }

    $output .= '
        <div>

            <h4>' . $contents['object_name'] . '</h4>';
    if (!$settings['hideInstitution']) $output .= '
            <a>' . $contents['object_institution']['institution_name'] . '<a>';

    $output .= '
            <div>
                <a href="./object.php?id=' . $contents['object_id'] . '" class="toTranslate" data-content="More"></a>
                <a href="' . $settings['mdVersion'] . '?t=objekt&oges=' . $contents['object_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['object_name'] . '</a>
            </div>

        </div>

    </div>
    ';

    return $output;

}

/**
 * Function drawObjectDetails creates a tile with just the most basic information on an object.
 *
 * @param string[] $contents Input data fetched from the object API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawObjectDetails(array $contents, array $settings):string {

    $output = '
    <div class="objDetails">

        <h2>' . $contents['object_name'] . '</h2>
    ';

    if (count($contents['object_images']) > 0) {
        foreach ($contents['object_images'] as $image) {
            if ($image['is_main'] != "j") continue;
            $output .= '
        <img class="objMainImg" src="' . $settings['mdImgFolder'] . $image['folder'] . '/' . $image['filename_loc'] . '" />';
        }
    }

    $output .= '
        <div>
    ';

    $output .= '
            <p>
                ' . $contents['object_description'] . '
            </p>';

    $simpleDefinedConts = [
        "object_material_technique",
        "object_dimensions",
    ];

    $output .= "
            <dl>";
    if (count($contents['object_collection']) > 0) {
        $output .= '
                <dt class="toTranslate" data-content="Collection"></dt>';
        foreach ($contents['object_collection'] as $collection) {
            $output .= '
                <dd>' . $collection['collection_name'] . '</dd>
                ';
        }
    }

    foreach ($simpleDefinedConts as $value) {
        $output .= "
                <dt class='toTranslate' data-content='" . $value . "'></dt>
                <dd>" . $contents[$value] . "</dd>
        ";
    }

    if (count($contents['object_tags']) > 0) {
        $output .= '
                <dt class="toTranslate" data-content="Tags"></dt>';
        foreach ($contents['object_tags'] as $tag) {
            $output .= '
                <dd>' . $tag['tag_name'] . '</dd>
                ';
        }
    }

    if (count($contents['object_relation_places']) > 0) {
        $output .= '
                <dt class="toTranslate" data-content="Places"></dt>';
        foreach ($contents['object_relation_places'] as $place) {
            $output .= '
                <dd>' . $place['place']['place_name'] . '</dd>
                ';
        }
    }

    if (count($contents['object_relation_people']) > 0) {
        $output .= '
                <dt class="toTranslate" data-content="People"></dt>';
        foreach ($contents['object_relation_people'] as $people) {
            $output .= '
                <dd>' . $people['people']['displayname'] . '</dd>
                ';
        }
    }
    $output .= "
            </dl>";

    if (count($contents['object_events']) > 0) {
        $output .= "
            <div class='events'>";
        foreach ($contents['object_events'] as $event) {
            $output .= '

    <div class="objevent">
        <h5 class="toTranslate" data-content="eventType' . $event['event_type'] . '"></h5>
        <table>
        ';

            if (isset($event['people'])) $output .= '
        <tr>
            <th class="toTranslate" data-content="... who"></th>
            <td class="vcard h-card">
                <a rel="tag" class="u-url fn p-name url">' . $event['people']['displayname'] . '</a>
            </td>
        </tr>';

            if (isset($event['time'])) $output .= '
        <tr>
            <th class="toTranslate" data-content="... when"></th>
            <td>
                <a>' . $event['time']['time_name'] . '</a>
            </td>
        </tr>';

            if (isset($event['place'])) $output .= '
        <tr>
            <th class="toTranslate" data-content="... where"></th>
            <td class="h-geo geo">
                <a rel="tag">' . $event['place']['place_name'] . '</a>
            </td>
        </tr>';
            $output .= '
        </table>
    </div>
                ';
        }
        $output .= '
            </div>
        ';
    }

    $output .= '
            <p class="metadataLine">
                <span><span class="toTranslate" data-content="Metadata"></span></span>
                <span><span class="toTranslate" data-content="LastUpdated"></span>: ' . $contents['object_last_updated'] . '</span>
                <span><span class="toTranslate" data-content="Licence"></span>: ' . $contents['licence']['metadata_rights_status'] . '</span>
                <span><a class="toTranslate" data-content="ObjectAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=objekt&oges=' . $contents['object_id'] . '"></a></span>
            </p>
        </div>
    ';

    $output .= '
    </div>
    ';

    return $output;

}

/**
 * Function for displaying objects.
 *
 * @param array   $arguments   Arguments / GET parameters for urls to query.
 * @param array   $settings    Settings variable.
 * @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
 *
 * @return string
 */
function embedObject(array $arguments, array $settings, bool $showDetails = false):string {

    $toIgnore = ["t=", "output="];
    $srcArgs  = "t=objekt";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "object", $settings), true);

    if (!$showDetails) return drawObjectTile($contents, $settings);
    else return drawObjectDetails($contents, $settings);

}

/**
 * Function drawInstitutionDetails generates a detail area
 * containing data fetched from the institution API at museum-digital.
 *
 * @param string[] $contents Input data fetched from the institution API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawInstitutionDetails(array $contents, array $settings):string {

    $output = '
    <div class="objDetails institutionDetails">
    ';

    if (isset($contents['institution_image']) and $contents['institution_image']) {
        $output .= '
        <img class="objMainImg" src="' . $settings['mdImgFolder'] . $contents['institution_image'] . '" />';
    }

    $output .= '
        <h2>' . $contents['institution_name'] . '</h2>
        <p>' .  $contents['institution_description'] . '</p>
        ';

    $output .= '
    <fieldset>';
    if ($contents['institution_street']) $output .= $contents['institution_street'] . '<br />';
    if ($contents['institution_zipcode']) $output .= $contents['institution_zipcode'] . " " . $contents['institution_place'] . "<br />";
    if ($contents['institution_telnr']) $output .= "<a href='tel:" . $contents['institution_telnr'] . "'>" . $contents['institution_telnr'] . "</a><br />";
    $output .= '
    </fieldset>';

    if (count($contents['collections']) >= 1) {
        $output .= '
        <h3 class="toTranslate" data-content="Collections"></h3>
        <ul>';
        foreach ($contents['collections'] as $collection) {

            $output .= '
            <li><a href="collection.php?id=' . $collection['collection_id'] . '">' . $collection['collection_name'] . '</a></li>
            ';

        }
        $output .= '</ul>';
    }

    $output .= '
        <p class="metadataLine">
            <span><span class="toTranslate" data-content="Metadata"></span></span>
            <span><a class="toTranslate" data-content="InstitutionAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=institution&instnr=' . $contents['institution_id'] . '"></a></span>
        </p>

    </div>
    ';

    return $output;

}

/**
 * Function drawInstitutionTile creates a tile with just the most basic information on an institiution.
 *
 * @param string[] $contents Input data fetched from the collection API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawInstitutionTile(array $contents, array $settings):string {

    $output = '
    <div class="objTile institutionTile">
    ';

    if (isset($contents['institution_image']) and $contents['institution_image']) {
        $output .= '
        <img src="' . $settings['mdImgFolder'] . $contents['institution_image'] . '" />';
    }

    $output .= '
        <div>

            <h4>' . $contents['institution_name'] . '</h4>
            <p>
                ' . $contents['institution_street'] . '
                ' . $contents['institution_zipcode'] . " " . $contents['institution_place'] . '<br />
            </p>

            <div>
                <a href="./collection.php?id=' . $contents['institution_id'] . '" class="toTranslate" data-content="More"></a>
                <a href="' . $settings['mdVersion'] . '?t=institution&instnr=' . $contents['institution_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['institution_name'] . '</a>
            </div>

        </div>

    </div>
    ';

    return $output;

}

/**
 * Function for displaying institution.
 *
 * @param array   $arguments   Arguments / GET parameters for urls to query.
 * @param array   $settings    Settings variable.
 * @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
 *
 * @return string
 */
function embedInstitution(array $arguments, array $settings, bool $showDetails = false):string {

    $toIgnore = ["t=", "output="];
    $srcArgs  = "t=institution";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "institution", $settings), true);

    if (!$showDetails) return drawInstitutionTile($contents, $settings);
    else return drawInstitutionDetails($contents, $settings);

}

/**
 * Function drawCollectionDetails generates a detail area.
 *
 * @param string[] $contents Input data fetched from the collections API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawCollectionDetails(array $contents, array $settings):string {

    $output = '
    <div class="objDetails collectionDetails">
    ';

    if (isset($contents['collection_image']) and $contents['collection_image']) {
        $output .= '
        <img class="objMainImg" src="' . $settings['mdImgFolder'] . $contents['collection_image'] . '" />';
    }

    $output .= '
        <h2>' . $contents['collection_name'] . '</h2>
        <p>' .  $contents['collection_description'] . '</p>
        ';
    if (!$settings['hideInstitution']) $output .= '
        <a> ' . $contents['collection_institution']['institution_name'] . '</a>
    ';

    $output .= '
        <p class="metadataLine">
            <span><span class="toTranslate" data-content="Metadata"></span></span>
            <span><a class="toTranslate" data-content="CollectionAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=sammlung&gesusa=' . $contents['collection_id'] . '"></a></span>
        </p>

    </div>
    ';

    return $output;

}

/**
 * Function drawCollectionTile creates a tile with just the most basic information on a collection.
 *
 * @param string[] $contents Input data fetched from the collection API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawCollectionTile(array $contents, array $settings):string {

    $output = '
    <div class="objTile collectionTile">
    ';

    if (isset($contents['collection_image']) and $contents['collection_image']) {
        $output .= '
        <img src="' . $settings['mdImgFolder'] . $contents['collection_image'] . '" />';
    }

    $output .= '
        <div>

            <h4>' . $contents['collection_name'] . '</h4>
            <p>' .  createTextSnippet($contents['collection_description'], 180) . '</p>
        ';
    if (!$settings['hideInstitution']) $output .= '
            <a> ' . $contents['collection_institution']['institution_name'] . '</a>';

    $output .= '

            <div>
                <a href="./collection.php?id=' . $contents['collection_id'] . '" class="toTranslate" data-content="More"></a>
                <a href="' . $settings['mdVersion'] . '?t=sammlung&gesusa=' . $contents['collection_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['collection_name'] . '</a>
            </div>

        </div>

    </div>
    ';

    return $output;

}

/**
 * Function for displaying collections.
 *
 * @param array   $arguments   Arguments / GET parameters for urls to query.
 * @param array   $settings    Settings variable.
 * @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
 *
 * @return string
 */
function embedCollection(array $arguments, array $settings, bool $showDetails = false):string {

    $toIgnore = ["t=", "output="];
    $srcArgs  = "t=sammlung";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "collection", $settings), true);

    if (!$showDetails) return drawCollectionTile($contents, $settings);
    else return drawCollectionDetails($contents, $settings);

}

/**
 * Function drawExhibitionDetails creates a tile with just the most basic information on an exhibition.
 *
 * @param string[] $contents Input data fetched from the collection API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawExhibitionDetails(array $contents, array $settings):string {

    $output = '
    <div class="objDetails exhibitionDetails">
    ';

    if (isset($contents['image']) and $contents['image']) {
        $output .= '
        <img class="objMainImg" src="' . $settings['mdVersion'] . $contents['image'] . '" />';
    }

    $output .= '

        <h1>' . $contents['name'] . '</h1>

        <p class="timeNotifLine">
            <time>' . $contents['start'] . '</time> - <time>' . $contents['end'] . '</time>
        </p>

        <p>' .  $contents['description'] . '</p>
        ';
    $output .= '
        <p>
            <a href="institution.php?id=' . $contents['institution_id'] . '"> ' . $contents['institution_name'] . ' (' . $contents['institution_ort'] . ')</a>
        </p>';

    $output .= '
        <p class="metadataLine">
            <span><span class="toTranslate" data-content="Metadata"></span></span>
            <span><a class="toTranslate" data-content="ExhibitionAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=exhibition&id=' . $contents['exhibition_id'] . '"></a></span>
        </p>

    </div>
    ';

    return $output;

}

/**
 * Function drawExhibitionTile creates a tile with just the most basic information on an exhibition.
 *
 * @param string[] $contents Input data fetched from the collection API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawExhibitionTile(array $contents, array $settings):string {

    $output = '
    <div class="objTile exhibitionTile">
    ';

    if (isset($contents['image']) and $contents['image']) {
        $output .= '
        <img src="' . $settings['mdImgFolder'] . $contents['image'] . '" />';
    }

    $output .= '
        <div>

            <h4>' . $contents['name'] . '</h4>
        ';
    if (!$settings['hideInstitution']) $output .= '
            <a href="' . $contents['institution_id'] . '"> ' . $contents['institution_name'] . '</a>';

    $output .= '

            <div>
                <a href="./exhibition.php?id=' . $contents['exhibition_id'] . '" class="toTranslate" data-content="More"></a>
                <a href="' . $settings['mdVersion'] . '?t=exhibition&id=' . $contents['exhibition_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['name'] . '</a>
            </div>

        </div>

    </div>
    ';

    return $output;

}

/**
 * Function for displaying exhibition.
 *
 * @param array   $arguments   Arguments / GET parameters for urls to query.
 * @param array   $settings    Settings variable.
 * @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
 *
 * @return string
 */
function embedExhibition(array $arguments, array $settings, bool $showDetails = false):string {

    $toIgnore = ["t=", "output="];
    $srcArgs  = "t=exhibition";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "exhibition", $settings), true);

    if (!$showDetails) return drawExhibitionTile($contents, $settings);
    else return drawExhibitionDetails($contents, $settings);

}

/**
 * Function for embedding event calendar.
 *
 * @param array $arguments Arguments / GET parameters for urls to query.
 *
 * @return string
 */
function embedExhibitionCalendar(array $arguments):string {

    $toIgnore = ["t=", "calendar=", "output="];
    $srcArgs  = "t=exhibitions_overview&calendar=1";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $srcURL = "apiMirror.php?area=exhibitions&args=" . urlencode($srcArgs);

    if (isset($_GET['y'])) $y = $_GET['y'];
    else $y = date("Y");
    if (isset($_GET['m'])) $m = $_GET['m'];
    else $m = date("m");

    $prevMonth = strtotime('-1 month', strtotime("$y-$m-01"));
    $nextMonth = strtotime('+1 month', strtotime("$y-$m-01"));

    $output = '
        <section class="mdCalendar"
            data-year="'.$y.'" data-month="'.$m.'" data-colored="1"
            data-today="./index.php?id=' . $GLOBALS['id'] . '&' . date("Y").'&m='.date("m") . '"
            data-prev="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $prevMonth).'&m='.date("m", $prevMonth) . '"
            data-next="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '"
            data-src="' . $srcURL . '&y='.date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '">
        </section>
    ';

    return $output;
}

/**
 * Function drawEventDetails creates a tile with just the most basic information on an event.
 *
 * @param string[] $contents Input data fetched from the event API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawEventDetails(array $contents, array $settings):string {

    $output = '
    <div class="objDetails eventDetails">
    ';

    if (isset($contents['image']) and $contents['image']) {
        $output .= '
        <img class="objMainImg" src="' . $settings['mdVersion'] . $contents['image'] . '" />';
    }

    $output .= '

        <h1>' . $contents['name'] . '</h1>

        <p class="timeNotifLine">
            <time>' . $contents['start'] . '</time> - <time>' . $contents['end'] . '</time>
        </p>

        <p>' .  $contents['description'] . '</p>
        ';
    $output .= '
        <p>
            <a href="institution.php?id=' . $contents['institution_id'] . '"> ' . $contents['institution_name'] . ' (' . $contents['institution_ort'] . ')</a>
        </p>';

    $output .= '
        <p class="metadataLine">
            <span><span class="toTranslate" data-content="Metadata"></span></span>
            <span><a class="toTranslate" data-content="ExhibitionAtMuseumDigital" href="' . $settings['mdVersion'] . '?t=event&id=' . $contents['appointment_id'] . '"></a></span>
        </p>

    </div>
    ';

    return $output;

}

/**
 * Function drawEventTile creates a tile with just the most basic information on an event.
 *
 * @param string[] $contents Input data fetched from the event API at museum-digital.
 * @param array    $settings Settings variable.
 *
 * @return string
 */
function drawEventTile(array $contents, array $settings):string {

    $output = '
    <div class="objTile eventTile">
    ';

    if (isset($contents['image']) and $contents['image']) {
        $output .= '
        <img src="' . $settings['mdVersion'] . $contents['image'] . '" />';
    }

    $output .= '
        <div>

            <h4>' . $contents['name'] . '</h4>
        ';
    if (!$settings['hideInstitution']) $output .= '
            <a href="' . $contents['institution_id'] . '"> ' . $contents['institution_name'] . '</a>';

    $output .= '

            <div>
                <a href="./event.php?id=' . $contents['appointment_id'] . '" class="toTranslate" data-content="More"></a>
                <a href="' . $settings['mdVersion'] . '?t=event&id=' . $contents['appointment_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $contents['name'] . '</a>
            </div>

        </div>

    </div>
    ';

    return $output;

}

/**
 * Function for displaying events.
 *
 * @param array   $arguments   Arguments / GET parameters for urls to query.
 * @param array   $settings    Settings variable.
 * @param boolean $showDetails Optional. By default, only a tile with most basic information is displayed.
 *
 * @return string
 */
function embedEvent(array $arguments, array $settings, bool $showDetails = false):string {

    $toIgnore = ["t=", "output="];
    $srcArgs  = "t=event";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "event", $settings), true);

    if (!$showDetails) return drawEventTile($contents, $settings);
    else return drawEventDetails($contents, $settings);

}

/**
 * Function for embedding event calendar.
 *
 * @param array $arguments Arguments / GET parameters for urls to query.
 *
 * @return string
 */
function embedEventCalendar(array $arguments):string {

    $toIgnore = ["t=", "calendar=", "output="];
    $srcArgs  = "t=events&calendar=1";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json";

    $srcURL = "apiMirror.php?area=events&args=" . urlencode($srcArgs);

    if (isset($_GET['y'])) $y = $_GET['y'];
    else $y = date("Y");
    if (isset($_GET['m'])) $m = $_GET['m'];
    else $m = date("m");

    $prevMonth = strtotime('-1 month', strtotime("$y-$m-01"));
    $nextMonth = strtotime('+1 month', strtotime("$y-$m-01"));

    $output = '
        <section class="mdCalendar"
            data-year="'.$y.'" data-month="'.$m.'" data-colored="1"
            data-today="./index.php?id=' . $GLOBALS['id'] . '&' . date("Y").'&m='.date("m") . '"
            data-prev="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $prevMonth).'&m='.date("m", $prevMonth) . '"
            data-next="./index.php?id=' . $GLOBALS['id'] . '&y=' . date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '"
            data-src="' . $srcURL . '&y='.date("Y", $nextMonth).'&m='.date("m", $nextMonth) . '">
        </section>
    ';

    return $output;
}

/**
 * Function for embedding search results from MD.
 *
 * @param array $arguments Arguments / GET parameters for urls to query.
 * @param array $settings  Settings variable.
 *
 * @return string
 */
function searchMDObjects(array $arguments, array $settings):string {

    $toIgnore = ["t=", "output=", "mod="];
    $srcArgs  = "done=yes";
    foreach ($arguments as $arg) {
        if (startsWithAny($arg, $toIgnore)) continue;
        $srcArgs .= "&" . $arg;
    }
    $srcArgs .= "&output=json&mod=complete";

    $contents = json_decode(queryCachePage($settings['mdVersion'] . "?$srcArgs", "search", $settings), true);

    if (!isset($contents[0])) return "";

    $output  = '
    <div class="searchGrid">
        <div class="paginationBar">
    ';

    if (isset($_GET['startwert']) and $_GET['startwert'] % 24 === 0) $current = $_GET['startwert'];
    else $current = 0;

    $toDisplay = [];
    for ($i = 0; $i <= 7; $i++) {
        $toDisplay[] = $current + (-72 + 24 * $i);
    }

    $i = 0;
    $lastToDisplay = end($toDisplay);

    // Display option to go to start
    if (!in_array(0, $toDisplay)) $output .= '<a href="search.php?startwert=0' . write_get_vars(['q', 'id']) . '#mdSearchObjs">1</a>';

    while ($i < $contents[0]['total']) {

        if (!in_array($i, $toDisplay)) {
            $i += 24; continue;
        }

        $output .= '
            <a ';
        if ($i == $current) $output .= ' class="navicurrent"';
        $output .= ' href="search.php?startwert=' . (string)$i . write_get_vars(['q', 'id']) . '#mdSearchObjs">' . strval(($i / 24) + 1) . '</a>
        ';

        $i += 24;
        if ($i == $lastToDisplay) break;
    }

    // Display option to go to start
    $last = $contents[0]["total"] - ($contents[0]["total"] % 24);
    if (!in_array($last, $toDisplay)) $output .= '<a href="search.php?startwert=' . (string)$last . write_get_vars(['q', 'id']) . '#mdSearchObjs">' . (string)(($last / 24) + 1) . '</a>';

    // Generate pagination
    $output .= '

        </div>
    ';

    foreach ($contents as $object) {
        $output .= '
        <div class="objTile">
        ';

        if (isset($object['image']) > 0) {
            $output .= '
            <img src="' . $settings['mdVersion'] . $object['image'] . '" />';
        }

        $output .= '
            <div>

                <h4>' . $object['objekt_name'] . '</h4>

                <div>
                    <a href="./object.php?id=' . $object['objekt_id'] . '" class="toTranslate" data-content="More"></a>
                    <a href="' . $settings['mdVersion'] . '?t=objekt&oges=' . $object['objekt_id'] . '" class="toTranslate" data-content="MoreAtMuseumDigital">' . $object['objekt_name'] . '</a>
                </div>

            </div>

        </div>
        ';
    }

    $output .= '
    </div>
    ';

    return $output;
}

?>
