dfasdf - PHP Online
Form of PHP Sandbox
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of dfasdf.php
- <?php
- class RestOnlineCollection extends modRestController
- {
- /*
- api properties
- general
- &action={search|suggest|object|filterdata|collection}
- &limit=10
- &objecttypesindex=kunst
- &debug=true
- &lang=en-US
- search
- &search=Jawlensky
- &filter{ID}[]=1
- suggest
- &suggest=Jaw
- object
- &object=medien/medien__online_collections/system_object_id/95353
- filter data
- &filterdata=all
- **/
- # vars
- public $allowedMethods = ["GET"];
- public $action = "search";
- public $easydbRequest = null;
- public $easydbUrl = null;
- public $easydbUser = null;
- public $easydbPassword = null;
- public $easydbPath = "v1/";
- public $easydbPost = 1;
- public $easydbOutput = null;
- public $output = [];
- public $token = null;
- public $requestbody = null;
- public $limit = 25;
- public $offset = 0;
- public $count = 0;
- public $debug = false;
- public $lang = "de-DE";
- public $cache = false;
- public $cacheLifetime = 60 * 60 * 24;
- public $cachePrefix = "onlinecollection";
- public $defaultObjectTypesIndex = "global";
- public $debugCountRequests = [];
- public $objectTypes = [
- "global" => [
- "kunst",
- "biologie",
- "anthropologie",
- "geologie",
- "bibliothek",
- ],
- "kunst" => ["kunst"],
- "biologie" => ["anthropologie", "biologie", "geologie"],
- ];
- #public $filterDataAllowedTagTypeIDs = [1];
- public $filterPropertyPrefix = "filter";
- public $filterTypes = [
- # global
- [
- "objectTypesIndex" => "global",
- "id" => "farbeglobal",
- "label" => "Farbe",
- "name" => "Farbe",
- "icon" => "color",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "oc_farbe1.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["oc_farbe1"],
- "search" => [],
- "searchObjecttypes" => [
- "anthropologie",
- "bibliothek",
- "biologie",
- "geologie",
- "kunst",
- ],
- "searchfields" => [
- "anthropologie._nested:anthropologie__collections_online.oc_farbe._global_object_id",
- "bibliothek._nested:bibliothek__collections_online.farbe._global_object_id",
- "biologie._nested:biologie__collections_online.oc_farbe._global_object_id",
- "geologie._nested:geologie__collections_online.oc_farbe._global_object_id",
- "kunst._nested:kunst__collections_online.farbe._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "global",
- "id" => "ortglobal",
- "label" => "Orte",
- "name" => "Ort",
- "icon" => "place",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "wo_zu_finden.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["wo_zu_finden"],
- "search" => [],
- "searchObjecttypes" => [
- "anthropologie",
- "bibliothek",
- "biologie",
- "geologie",
- "kunst",
- ],
- "searchfields" => [
- "anthropologie._nested:anthropologie__collections_online.oc_ort._global_object_id",
- "bibliothek._nested:bibliothek__collections_online.ort._global_object_id",
- "biologie._nested:biologie__collections_online.oc_ort._global_object_id",
- "geologie._nested:geologie__collections_online.oc_ort._global_object_id",
- "kunst._nested:kunst__collections_online.ort._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "global",
- "id" => "zeitglobal",
- "label" => "Zeit",
- "name" => "Zeit",
- "icon" => "time",
- "type" => "multiselect",
- "values" => "remote",
- "valuesObjecttypes" => ["wozu_gehoerig"],
- "search" => [],
- "searchObjecttypes" => [
- "anthropologie",
- "bibliothek",
- "biologie",
- "geologie",
- "kunst",
- ],
- "searchfields" => [
- "anthropologie._nested:anthropologie__collections_online.oc_zeit_listenfeld._global_object_id",
- "biologie._nested:biologie__collections_online.oc_zeit_listefeld._global_object_id",
- "geologie._nested:geologie__collections_online.oc_zeit_listenfeld._global_object_id",
- "kunst._nested:kunst__collections_online.zeit._global_object_id",
- "bibliothek._nested:bibliothek__collections_online.zeit._global_object_id",
- ],
- ],
- # kunst
- [
- "objectTypesIndex" => "kunst",
- "id" => "kuenstlerkunst",
- "label" => "Künstler",
- "name" => "Künstler",
- "type" => "alphabeticallysearch",
- "values" => "remote",
- "valuesObjecttypes" => ["personen_kunst"],
- "search" => [],
- "searchObjecttypes" => ["kunst"],
- "searchfields" => [
- "kunst._nested:kunst__kuenstler.kuenstler._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "kunst",
- "id" => "gattungkunst",
- "label" => "Gattung",
- "name" => "Gattung",
- "type" => "multiselect",
- "values" => "remote",
- "valuesObjecttypes" => ["gattung"],
- "search" => [],
- "searchObjecttypes" => ["gattung"],
- "searchfields" => [
- "kunst._nested:kunst__gattung.gattung._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "kunst",
- "id" => "datierungkunst",
- "label" => "Datierung",
- "name" => "Datierung",
- "type" => "daterange",
- "values" => [],
- "valuesObjecttypes" => [],
- "search" => [
- "type" => "complex",
- "bool" => "must",
- "search" => [
- [
- "bool" => "should",
- "type" => "in",
- "fields" => ["kunst._pool.pool._id"],
- "in" => [1, 3, 4, 5, 8, 10],
- ],
- [
- "type" => "complex",
- "bool" => "should",
- "search" => [
- [
- "bool" => "must_not",
- "type" => "in",
- "fields" => ["_objecttype"],
- "in" => ["kunst"],
- ],
- ],
- ],
- ],
- ],
- "searchObjecttypes" => [
- "anthropologie",
- "bibliothek",
- "biologie",
- "geologie",
- "kunst",
- ],
- "searchfields" => "kunst.datierung_normiert",
- ],
- [
- "objectTypesIndex" => "kunst",
- "id" => "objekttypkunst",
- "label" => "Objekttyp",
- "name" => "Objekttyp",
- "type" => "multiselect",
- "values" => "remote",
- "valuesObjecttypes" => ["objekttyp_mwks"],
- "search" => [],
- "searchObjecttypes" => [
- "anthropologie",
- "bibliothek",
- "biologie",
- "geologie",
- "kunst",
- ],
- "searchfields" => [
- "kunst._nested:kunst__objekttyp_mwks.objekttyp_ks._path._global_object_id",
- ],
- ],
- # biologie
- [
- "objectTypesIndex" => "biologie",
- "id" => "sammlungstypbiologie",
- "label" => "Sammlungstyp",
- "name" => "Sammlungstyp",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "oc_es_sammlungstyp.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["oc_es_sammlungstyp"],
- "search" => [],
- "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
- "searchfields" => [
- "biologie._nested:biologie__collections_online.oc_es_sammlungstyp._global_object_id",
- "geologie._nested:geologie__collections_online.oc_es_sammlungstyp._global_object_id",
- "anthropologie._nested:anthropologie__collections_online.oc_es_sammlungstyp._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "biologie",
- "id" => "objekttyppbiologie",
- "label" => "Objekttyp",
- "name" => "Objekttyp",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "oc_es_objekttyp.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["oc_es_objekttyp"],
- "search" => [],
- "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
- "searchfields" => [
- "biologie._nested:biologie__collections_online.oc_es_objekttyp._global_object_id",
- "geologie._nested:geologie__collections_online.oc_es_objekttyp._global_object_id",
- "anthropologie._nested:anthropologie__collections_online.oc_es_objekttyp._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "biologie",
- "id" => "datierungbiologie",
- "label" => "Datierung",
- "name" => "Datierung",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "oc_es_datierung.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["oc_es_datierung"],
- "search" => [],
- "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
- "searchfields" => [
- "biologie._nested:biologie__collections_online.oc_es_datierung._global_object_id",
- "geologie._nested:geologie__collections_online.oc_es_datierung._global_object_id",
- "anthropologie._nested:anthropologie__collections_online.oc_es_datierung._global_object_id",
- ],
- ],
- [
- "objectTypesIndex" => "biologie",
- "id" => "standortbiologie",
- "label" => "Standort",
- "name" => "Standort",
- "type" => "multiselect",
- "values" => "remote",
- "valueSortby" => "oc_es_standort.bemerkung",
- "valueSortorder" => "ASC",
- "valuesObjecttypes" => ["oc_es_standort"],
- "search" => [],
- "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
- "searchfields" => [
- "biologie._nested:biologie__collections_online.oc_es_standort._global_object_id",
- "geologie._nested:geologie__collections_online.oc_es_standort._global_object_id",
- "anthropologie._nested:anthropologie__collections_online.oc_es_standort._global_object_id",
- ],
- ],
- ];
- # init
- public function init()
- {
- $this->cache = $this->modx->getCacheManager();
- $this->easydbUrl = $this->modx->getOption("easydbLoginUrl");
- $this->easydbUser = $this->modx->getOption("easydbLoginUser");
- $this->easydbPassword = $this->modx->getOption("easydbLoginPassword");
- $this->limit = $this->getProperty("limit", $this->limit);
- $this->offset = $this->getProperty("offset", $this->offset);
- $this->debug = $this->getProperty("debug", $this->debug);
- $this->lang = $this->getProperty("lang", $this->lang);
- $this->objectTypesIndex = $this->getProperty(
- "objecttypesindex",
- $this->defaultObjectTypesIndex
- );
- $this->action = $this->getProperty("action", $this->action);
- if ($this->debug && $this->modx->user->get("id") < 1) {
- die("Login to MODX to use debug mode.");
- }
- }
- # authenticate token
- public function auth()
- {
- $cacheKey = "token";
- $cacheData = $this->getCache($cacheKey);
- if ($cacheData) {
- $this->token = $cacheData;
- return true;
- }
- $data = $this->apiRequest("session", [], "get");
- $this->token = $data["token"];
- $this->apiRequest("session/authenticate", [
- "login" => $this->easydbUser,
- "password" => $this->easydbPassword,
- "token" => $this->token,
- ]);
- $this->setCache($cacheKey, $this->token, 60 * 5); // lifetime 5 minutes
- return true;
- }
- # request to easydb api
- public function apiRequest(
- $path,
- $payload = [],
- $method = "post",
- $setCount = true
- ) {
- $this->easydbPath = $path;
- $this->easydbRequest = $this->easydbUrl . $this->easydbPath;
- $easydb = $this->modx->getService("easydb", "rest.modRest");
- $easydb->setOption("header", true);
- $this->requestbody = array_merge($payload, [
- "token" => $this->token,
- ]);
- $payload = $this->requestbody;
- if (isset($payload["BODY"])) {
- $payload["BODY"] = json_encode($payload["BODY"]);
- }
- $response = $easydb->$method($this->easydbRequest, $payload);
- $this->easydbOutput = $response->process();
- if ($setCount) {
- $this->count = $this->easydbOutput["count"];
- }
- $this->debugCountRequests[] = [
- "path" => $path,
- "payload" =>
- $this->easydbPath != "session/authenticate"
- ? $payload
- : "[credentials]",
- "response" => $this->easydbOutput,
- ];
- return $this->easydbOutput;
- }
- public function prepareSearchFilter()
- {
- $searchJson = [];
- # search term
- if (!empty($this->getProperty("search"))) {
- $searchTerms = explode(",", $this->getProperty("search"));
- foreach ($searchTerms as $item) {
- $searchJson[] = [
- "phrase" => false,
- "bool" => "must",
- "mode" => "fulltext",
- "string" => $item,
- "type" => "match",
- ];
- }
- }
- # filter
- foreach ($this->filterTypes as $filterType) {
- if ($filterType["objectTypesIndex"] != $this->objectTypesIndex) {
- continue;
- }
- $property = $this->getProperty(
- $this->filterPropertyPrefix . $filterType["id"],
- false
- );
- if ($property) {
- $searchItems = [];
- switch ($filterType["type"]) {
- case "multiselect":
- foreach ($filterType["searchfields"] as $searchfield) {
- $searchItems[] = [
- "type" => "in",
- "fields" => [$searchfield],
- "in" => $property,
- "bool" => "should",
- ];
- }
- break;
- case "bool":
- foreach ($filterType["searchfields"] as $searchfield) {
- $searchItems[] = [
- "type" => "in",
- "fields" => [$searchfield],
- "in" => [true],
- "bool" => "must",
- ];
- }
- break;
- case "daterange":
- if (!empty($property[0]) || !empty($property[1])) {
- foreach (
- $filterType["searchfields"]
- as $searchfield
- ) {
- $searchItems[] = [
- "type" => "range",
- "fields" => [$searchfield],
- "from" => $property[0],
- "to" => $property[1],
- "bool" => "must",
- ];
- }
- }
- break;
- case "alphabeticallysearch":
- foreach ($filterType["searchfields"] as $searchfield) {
- $searchItems[] = [
- "type" => "in",
- "fields" => [$searchfield],
- "in" => $property,
- "bool" => "should",
- ];
- }
- break;
- }
- if (count($searchItems)) {
- $searchJson[] = [
- "type" => "complex",
- "search" => $searchItems,
- "bool" => "must",
- ];
- }
- if ($filterType["search"]) {
- $searchJson[] = $filterType["search"];
- }
- }
- }
- return $searchJson;
- }
- # search
- public function getSearch()
- {
- $searchJson = $this->prepareSearchFilter();
- $this->apiRequest("search", [
- "BODY" => [
- "search" => $searchJson,
- "sort" => [
- [
- "field" => "_changelog.date_last_updated",
- "order" => "DESC",
- ],
- [
- "field" => "_system_object_id",
- "order" => "DESC",
- ],
- ],
- "objecttypes" => $this->objectTypes[$this->objectTypesIndex],
- "limit" => (int) $this->limit,
- "offset" => (int) $this->offset,
- ],
- ]);
- $this->output = [];
- $objectIds = [];
- foreach ($this->easydbOutput["objects"] as $item) {
- $cacheKey = "objects/" . $item["_system_object_id"];
- $this->output[] =
- $this->getCache($cacheKey) ?: $this->migrateObjectData($item);
- }
- if (count($this->output) > 0) {
- foreach ($this->output as $item) {
- $cacheKey = "objects/" . $item["objektid"];
- $this->setCache($cacheKey, $item);
- }
- }
- }
- # set image details for object
- public function setObjectDataImages($objectImageData)
- {
- if (count($objectImageData) < 1) {
- return [
- [
- "id" => null,
- "url" => "",
- "width" => null,
- "height" => null,
- "preview" => [
- "id" => null,
- "url" => "",
- "width" => null,
- "height" => null,
- ],
- "full" => [
- "id" => null,
- "url" => "",
- "width" => null,
- "height" => null,
- ],
- ],
- ];
- }
- $images = [];
- foreach ($objectImageData as $objectImageDataItem) {
- if ($objectImageDataItem["class"] != "image") {
- continue;
- }
- $image = [
- "id" => $objectImageDataItem["_id"],
- "url" => $objectImageDataItem["versions"]["small"]["url"] ?: "",
- "width" => $objectImageDataItem["versions"]["small"]["width"],
- "height" => $objectImageDataItem["versions"]["small"]["height"],
- ];
- $easData = $this->getMediaUrls([$image["id"]]);
- if (count($easData) > 0) {
- $imgData = $easData[$image["id"]]["versions"];
- foreach (["preview", "full"] as $imgSize) {
- $image[$imgSize] = [
- "url" => $imgData[$imgSize]["url"] ?: "",
- "width" => $imgData[$imgSize]["width"],
- "height" => $imgData[$imgSize]["height"],
- ];
- }
- }
- $images[] = $image;
- }
- return $images;
- }
- # set video details for object
- public function setObjectDataVideos($objectVideoData)
- {
- if (count($objectVideoData) < 1) {
- return null;
- }
- $videos = [];
- foreach ($objectVideoData as $objectVideoDataItem) {
- if ($objectVideoDataItem["class"] != "video") {
- continue;
- }
- $videoId = $objectVideoDataItem["_id"];
- $easData = $this->getMediaUrls([$videoId]);
- if (count($easData) > 0) {
- $videoData = $easData[$videoId]["versions"]["1920p"];
- $video = [
- "url" => $videoData["url"] ?: "",
- "width" => $videoData["width"],
- "height" => $videoData["height"],
- ];
- }
- $videos[] = $video;
- }
- return $videos;
- }
- # set audio details for object
- public function setObjectDataAudios($objectAudioData)
- {
- if (count($objectAudioData) < 1) {
- return null;
- }
- $audios = [];
- foreach ($objectAudioData as $objectAudioDataItem) {
- if ($objectAudioDataItem["class"] != "audio") {
- continue;
- }
- $audioId = $objectAudioDataItem["_id"];
- $easData = $this->getMediaUrls([$audioId]);
- if (count($easData) > 0) {
- $audioData = $easData[$audioId]["versions"]["original"];
- $audio = [
- "url" => $audioData["url"] ?: "",
- ];
- }
- $audios[] = $audio;
- }
- return $audios;
- }
- # set object data dimensions
- public function setObjectDataDimensions($items)
- {
- if (count($items) < 1) {
- return [];
- }
- $dimenions = [];
- foreach ($items as $item) {
- $dimenions[] = [
- "breite" => $this->convertDimenions($item["breite"]),
- "hoehe" => $this->convertDimenions($item["hoehe"]),
- "tiefe" => $this->convertDimenions($item["tiefe"]),
- "art" =>
- $item["art_masse"]["_standard"][1]["text"][$this->lang],
- "bemerkung" => $item["bemerkung"],
- ];
- }
- return $dimenions;
- }
- # get image urls
- public function getMediaUrls($objectIds)
- {
- if (count($objectIds) < 1) {
- return;
- }
- return $this->apiRequest(
- "eas?ids=" . urlencode("[" . implode(",", $objectIds) . "]"),
- [],
- "get",
- false
- );
- }
- # migrate easydb structure
- public function migrateObjectData($item)
- {
- $objectTypeIndex = $item["_objecttype"];
- $material = [];
- foreach (
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__material_technik"
- ]
- as $materialItem
- ) {
- $material[] = $materialItem["material_u_technik"][$this->lang];
- }
- $data = [
- "objecttype" => $objectTypeIndex,
- "autor" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__kuenstler"
- ][0]["kuenstler"]["_standard"][1]["text"][$this->lang],
- "titel" => $item["_standard"][1]["text"][$this->lang],
- // $item[$objectTypeIndex]["werktitel"][$this->lang] ?:
- // $item[$objectTypeIndex]["objektname"][$this->lang],
- "zusatztitel" =>
- $item[$objectTypeIndex]["zusatztitel"] ?:
- $item[$objectTypeIndex]["objektname_wiss"],
- "beschriftung" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["text"][$this->lang],
- "images" => $this->setObjectDataImages(
- $item["_standard"]["eas"][1]
- ),
- "videos" => $this->setObjectDataVideos(
- $item["_standard"]["eas"][1]
- ),
- "audios" => $this->setObjectDataAudios(
- $item["_standard"]["eas"][1]
- ),
- "objektid" => $item["_system_object_id"],
- "material" => $material,
- "datierung" =>
- $item[$objectTypeIndex]["datierung_frei"] ?:
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["oc_es_datierung"]["_standard"][1]["text"][$this->lang],
- "thema" => $item[$objectTypeIndex]["thema"],
- "objekttyp" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__objekttyp_mwks"
- ][0]["objekttyp_ks"]["_standard"][1]["text"][$this->lang] ?:
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["oc_es_objekttyp"]["_standard"][1]["text"][$this->lang],
- "entstehungsort" =>
- $item[$objectTypeIndex]["entstehungsort"]["_standard"][1][
- "text"
- ][$this->lang],
- "masse" => $this->setObjectDataDimensions(
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__masse"
- ]
- ),
- "creditline" => $item[$objectTypeIndex]["creditline"],
- "inventarnummer" => $item[$objectTypeIndex]["inventarnummer"],
- "ausgestellt" => $item[$objectTypeIndex]["ausgestellt"]
- ? true
- : false,
- "sammlungstyp" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["oc_es_sammlungstyp"]["_standard"][1]["text"][$this->lang],
- "standort" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["oc_es_standort"]["_standard"][1]["text"][$this->lang],
- "ueberschrift" =>
- $item[$objectTypeIndex][
- "_nested:" . $objectTypeIndex . "__collections_online"
- ][0]["ueberschrift"][$this->lang],
- "cachecreated" => date("Y-m-d H:i:s"),
- ];
- if ($this->debug) {
- $data["__easydb"] = $item;
- }
- return $data;
- }
- # set object data dimensions
- public function setObjectDataDimensions($items)
- {
- if (count($items) < 1) {
- return [];
- }
- $dimenions = [];
- foreach ($items as $item) {
- $dimenions[] = [
- "breite" => $this->convertDimenions($item["breite"]),
- "hoehe" => $this->convertDimenions($item["hoehe"]),
- "tiefe" => $this->convertDimenions($item["tiefe"]),
- "art" =>
- $item["art_masse"]["_standard"][1]["text"][$this->lang],
- "bemerkung" => $item["bemerkung"],
- ];
- }
- return $dimenions;
- }
- # suggest
- public function getSuggest()
- {
- $this->apiRequest("suggest", [
- "BODY" => [
- "query" => $this->getProperty("suggest"),
- "objecttypes" => $this->objectTypes[$this->objectTypesIndex],
- "linked_objecttypes" => [
- "erwerbsart",
- "fotografen",
- "gattung",
- "masse_art",
- "objekttyp",
- "objekttyp_mwks",
- "orte",
- "personen_kunst",
- "schlagworte",
- ],
- "limit" => (int) $this->limit,
- ],
- ]);
- $this->output = [];
- foreach (
- $this->easydbOutput["suggestions"]["linked_objects"]
- as $item
- ) {
- $this->output[] = $item["_standard"][1]["text"][$this->lang];
- }
- }
- # filter data
- public function getFilterdata()
- {
- $cacheKey = "filterdata";
- $cacheData = $this->getCache($cacheKey);
- if ($cacheData) {
- $this->output = $cacheData;
- return;
- }
- $output = [];
- foreach ($this->filterTypes as $filterType) {
- $items = [];
- if ($filterType["values"] == "remote") {
- $items = $this->getFilterDataRemoteItems($filterType);
- } else {
- foreach ($filterType["values"] as $index => $value) {
- $items[] = [
- "id" => $index,
- "name" => $value,
- ];
- }
- }
- $output[] = array_merge($filterType, [
- "values" => $items,
- ]);
- }
- $this->setCache($cacheKey, $output);
- $this->output = $output;
- }
- # grab all remote values and bypass easydb global limit
- public function getFilterDataRemoteItems(
- $filterType,
- $items = [],
- $limit = 1000,
- $offset = 0
- ) {
- $this->apiRequest("search", [
- "BODY" => [
- "search" => $filterType["search"],
- "sort" => [
- [
- "field" =>
- $filterType["valueSortby"] ?: "_standard.1.text",
- "order" => $filterType["valueSortorder"] ?: "ASC",
- ],
- ],
- "limit" => $limit,
- "offset" => $offset,
- "objecttypes" => $filterType["valuesObjecttypes"],
- ],
- ]);
- foreach ($this->easydbOutput["objects"] as $item) {
- $items[] = [
- "id" => $item["_global_object_id"],
- "name" => $item["_standard"][1]["text"][$this->lang],
- ];
- }
- if ($this->easydbOutput["count"] - $limit - $offset > 0) {
- $offset = $offset + $limit;
- $items = $this->getFilterDataRemoteItems(
- $filterType,
- $items,
- $limit,
- $offset
- );
- }
- return $items;
- }
- # collection data
- public function getCollectionList()
- {
- # get all collections
- $this->apiRequest("collection/list", [], "get");
- $collections = [];
- foreach ($this->easydbOutput as $item) {
- if ($item["collection"]["is_system_collection"]) {
- continue;
- }
- $collectionId = $item["collection"]["_id"];
- $collectionDetails = $this->getCollectionDetails($collectionId);
- $collectionImgs = [];
- foreach ($collectionDetails as $collectionDetailsItem) {
- $imgPath = $collectionDetailsItem["bild"]["url"];
- if (!$imgPath) {
- continue;
- }
- $collectionImgs[] = $imgPath;
- }
- $collections[] = [
- "id" => $collectionId,
- "titel" => $item["collection"]["displayname"][$this->lang],
- "imgs" => array_slice($collectionImgs, 0, 3),
- ];
- }
- return $collections;
- }
- public function getCollectionDetails($collectionId)
- {
- # get objects
- $this->apiRequest("collection/objects/" . $collectionId, [], "get");
- $collectionObjectIds = [];
- foreach ($this->easydbOutput["objects"] as $item) {
- $collectionObjectIds[] = $item["_global_object_id"];
- }
- # get object details
- $this->apiRequest(
- "search",
- [
- "BODY" => [
- "search" => [
- [
- "type" => "in",
- "fields" => ["_global_object_id"],
- "in" => $collectionObjectIds,
- ],
- ],
- ],
- ],
- "get"
- );
- $collectionObjectsData = [];
- foreach ($this->easydbOutput["objects"] as $item) {
- $collectionObjectsData[] = $this->migrateObjectData($item);
- }
- return $collectionObjectsData;
- }
- public function getCollectiondata()
- {
- $collectionId = $this->getProperty("collectionId");
- if ($collectionId) {
- $this->output = $this->getCollectionDetails($collectionId);
- } else {
- $this->output = $this->getCollectionList();
- }
- }
- # get a random object
- public function getRandomobject()
- {
- $this->apiRequest("search", [
- "BODY" => [
- "sort" => [
- [
- "field" => "_system_object_id",
- "order" => "DESC",
- ],
- ],
- "objecttypes" => $this->objectTypes["global"],
- "limit" => 1,
- "offset" => random_int(0, 650),
- ],
- ]);
- $this->output = $this->migrateObjectData(
- $this->easydbOutput["objects"][0]
- );
- }
- # get home objects
- public function getHomeObjects()
- {
- $limit = $this->limit;
- $this->limit = 300;
- $this->getSearch();
- $this->limit = $limit;
- $output = $this->output;
- shuffle($output);
- $this->count = (int) $this->limit;
- $this->output = array_slice($output, $this->offset, $this->limit);
- }
- # get hero image
- public function getHeroimg()
- {
- $searchObjectTypes = [];
- foreach ($this->objectTypes["global"] as $objectType) {
- $searchObjectTypes[] = [
- "bool" => "should",
- "type" => "in",
- "fields" => [
- $objectType .
- "." .
- ($objectType == "kunst"
- ? "checkbox_highlight"
- : "highlight"),
- ],
- "in" => [true],
- ];
- }
- $this->apiRequest("search", [
- "BODY" => [
- "search" => $searchObjectTypes,
- "sort" => [
- [
- "field" => "_system_object_id",
- "order" => "DESC",
- ],
- ],
- "objecttypes" => $this->objectTypes["global"],
- "limit" => 100,
- "offset" => 0,
- ],
- ]);
- $randomObject =
- $this->easydbOutput["objects"][
- random_int(0, $this->easydbOutput["count"])
- ];
- $this->output = $this->migrateObjectData($randomObject);
- }
- # get export
- public function getDownload()
- {
- $objectsZip = [];
- $tempDir =
- MODX_CORE_PATH . "cache/" . $this->cachePrefix . "/download/";
- $filename = "muwi-online-collection-" . uniqid() . ".zip";
- $objectIds = explode(",", $this->getProperty("objectids"));
- $easData = $this->getMediaUrls($objectIds);
- mkdir($tempDir, 0777, true);
- foreach ($easData as $item) {
- $tmpFile = $tempDir . $item["original_filename"];
- $remoteUrl = $item["versions"]["full"]["url"];
- $fileContent = file_get_contents($remoteUrl);
- file_put_contents($tmpFile, $fileContent);
- $objectsZip[] = [
- "file" => $tmpFile,
- "url" => $remoteUrl,
- ];
- }
- $zip = new ZipArchive();
- $zip->open($tempDir . $filename, ZipArchive::CREATE);
- foreach ($objectsZip as $item) {
- $zip->addFile($item["file"], basename($item["file"]));
- }
- $zip->close();
- header("Content-type: application/zip");
- header("Content-Length: " . filesize($tempDir . $filename));
- header('Content-Disposition: attachment; filename="' . $filename . '"');
- readfile($tempDir . $filename);
- unlink($tempDir . $filename);
- die();
- }
- # build output
- public function getOutput()
- {
- unset($this->requestbody["password"], $this->requestbody["login"]);
- return array_merge(
- [
- "data" => $this->output,
- ],
- $this->debug
- ? [
- "debug" => [
- "query" => $_GET,
- "requests" => $this->debugCountRequests,
- ],
- ]
- : []
- );
- }
- # pagination
- public function getPagination()
- {
- return [
- "limit" => (int) $this->limit,
- "offset" => (int) $this->offset,
- "count" => $this->count,
- "pages" => ceil($this->count / $this->limit),
- "page" => ceil($this->offset / $this->limit) + 1,
- "nav" => [
- "offsetPrev" =>
- $this->offset - $this->limit < 0
- ? null
- : $this->offset - $this->limit,
- "offsetNext" =>
- $this->offset + $this->limit >= $this->count
- ? null
- : $this->offset + $this->limit,
- ],
- ];
- }
- # build list
- public function getList()
- {
- $this->init();
- $this->auth();
- switch ($this->action) {
- case "suggest":
- $this->getSuggest();
- break;
- case "filterdata":
- $this->getFilterdata();
- break;
- case "collection":
- $this->getCollectiondata();
- break;
- case "randomobject":
- $this->getRandomobject();
- break;
- case "heroimg":
- $this->getHeroimg();
- break;
- case "homeobjects":
- $this->getHomeObjects();
- break;
- case "download":
- $this->getDownload();
- break;
- case "refresh":
- $this->refreshCache();
- break;
- default:
- $this->getSearch();
- break;
- }
- return $this->collection($this->getOutput(), $this->getPagination());
- }
- # set cache
- public function setCache($cacheName, $data, $lifetime = null)
- {
- $cacheData = $this->cache->set(
- $cacheName,
- $data,
- $lifetime ?: $this->cacheLifetime,
- [
- xPDO::OPT_CACHE_KEY => $this->cachePrefix,
- ]
- );
- if (!$cacheData) {
- return;
- }
- return $cacheData;
- }
- # get cache
- public function getCache($cacheName)
- {
- $cacheData = $this->cache->get($cacheName, [
- xPDO::OPT_CACHE_KEY => $this->cachePrefix,
- ]);
- if (!$cacheData || $this->debug) {
- return;
- }
- return $cacheData;
- }
- # refresh cache
- public function refreshCache()
- {
- $this->cache->deleteTree(
- MODX_CORE_PATH . "cache/" . $this->cachePrefix
- );
- $this->getFilterdata();
- $this->getSearch();
- $this->output = ["msg" => "cache refreshed"];
- }
- # convert dimmensions
- public function convertDimenions($val)
- {
- if ($val > 0) {
- return number_format($val / 100, 2, ",", "");
- } else {
- return null;
- }
- }
- }
File Description
- dfasdf
- PHP Code
- 14 Sep-2021
- 39.8 Kb
You can Share it:
Latest PHP Pastes