dfasdf - PHP Online

Form of PHP Sandbox

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.



Your result can be seen below.

Result of php executing





Full code of dfasdf.php

  1. <?php
  2.  
  3. class RestOnlineCollection extends modRestController
  4. {
  5.     /*
  6.     
  7.     api properties
  8.     
  9.     general
  10.         &action={search|suggest|object|filterdata|collection}
  11.         &limit=10
  12.         &objecttypesindex=kunst
  13.         &debug=true
  14.         &lang=en-US
  15.     
  16.     search
  17.         &search=Jawlensky
  18.         &filter{ID}[]=1
  19.     
  20.     suggest
  21.         &suggest=Jaw
  22.     
  23.     object
  24.         &object=medien/medien__online_collections/system_object_id/95353
  25.         
  26.     filter data
  27.         &filterdata=all
  28.     
  29.     **/
  30.  
  31.     # vars
  32.     public $allowedMethods = ["GET"];
  33.     public $action = "search";
  34.     public $easydbRequest = null;
  35.     public $easydbUrl = null;
  36.     public $easydbUser = null;
  37.     public $easydbPassword = null;
  38.     public $easydbPath = "v1/";
  39.     public $easydbPost = 1;
  40.     public $easydbOutput = null;
  41.     public $output = [];
  42.     public $token = null;
  43.     public $requestbody = null;
  44.     public $limit = 25;
  45.     public $offset = 0;
  46.     public $count = 0;
  47.     public $debug = false;
  48.     public $lang = "de-DE";
  49.     public $cache = false;
  50.     public $cacheLifetime = 60 * 60 * 24;
  51.     public $cachePrefix = "onlinecollection";
  52.     public $defaultObjectTypesIndex = "global";
  53.  
  54.     public $debugCountRequests = [];
  55.  
  56.     public $objectTypes = [
  57.         "global" => [
  58.             "kunst",
  59.             "biologie",
  60.             "anthropologie",
  61.             "geologie",
  62.             "bibliothek",
  63.         ],
  64.         "kunst" => ["kunst"],
  65.         "biologie" => ["anthropologie", "biologie", "geologie"],
  66.     ];
  67.     #public $filterDataAllowedTagTypeIDs = [1];
  68.     public $filterPropertyPrefix = "filter";
  69.     public $filterTypes = [
  70.         # global
  71.         [
  72.             "objectTypesIndex" => "global",
  73.             "id" => "farbeglobal",
  74.             "label" => "Farbe",
  75.             "name" => "Farbe",
  76.             "icon" => "color",
  77.             "type" => "multiselect",
  78.             "values" => "remote",
  79.             "valueSortby" => "oc_farbe1.bemerkung",
  80.             "valueSortorder" => "ASC",
  81.             "valuesObjecttypes" => ["oc_farbe1"],
  82.             "search" => [],
  83.             "searchObjecttypes" => [
  84.                 "anthropologie",
  85.                 "bibliothek",
  86.                 "biologie",
  87.                 "geologie",
  88.                 "kunst",
  89.             ],
  90.             "searchfields" => [
  91.                 "anthropologie._nested:anthropologie__collections_online.oc_farbe._global_object_id",
  92.                 "bibliothek._nested:bibliothek__collections_online.farbe._global_object_id",
  93.                 "biologie._nested:biologie__collections_online.oc_farbe._global_object_id",
  94.                 "geologie._nested:geologie__collections_online.oc_farbe._global_object_id",
  95.                 "kunst._nested:kunst__collections_online.farbe._global_object_id",
  96.             ],
  97.         ],
  98.         [
  99.             "objectTypesIndex" => "global",
  100.             "id" => "ortglobal",
  101.             "label" => "Orte",
  102.             "name" => "Ort",
  103.             "icon" => "place",
  104.             "type" => "multiselect",
  105.             "values" => "remote",
  106.             "valueSortby" => "wo_zu_finden.bemerkung",
  107.             "valueSortorder" => "ASC",
  108.             "valuesObjecttypes" => ["wo_zu_finden"],
  109.             "search" => [],
  110.             "searchObjecttypes" => [
  111.                 "anthropologie",
  112.                 "bibliothek",
  113.                 "biologie",
  114.                 "geologie",
  115.                 "kunst",
  116.             ],
  117.             "searchfields" => [
  118.                 "anthropologie._nested:anthropologie__collections_online.oc_ort._global_object_id",
  119.                 "bibliothek._nested:bibliothek__collections_online.ort._global_object_id",
  120.                 "biologie._nested:biologie__collections_online.oc_ort._global_object_id",
  121.                 "geologie._nested:geologie__collections_online.oc_ort._global_object_id",
  122.                 "kunst._nested:kunst__collections_online.ort._global_object_id",
  123.             ],
  124.         ],
  125.         [
  126.             "objectTypesIndex" => "global",
  127.             "id" => "zeitglobal",
  128.             "label" => "Zeit",
  129.             "name" => "Zeit",
  130.             "icon" => "time",
  131.             "type" => "multiselect",
  132.             "values" => "remote",
  133.             "valuesObjecttypes" => ["wozu_gehoerig"],
  134.             "search" => [],
  135.             "searchObjecttypes" => [
  136.                 "anthropologie",
  137.                 "bibliothek",
  138.                 "biologie",
  139.                 "geologie",
  140.                 "kunst",
  141.             ],
  142.             "searchfields" => [
  143.                 "anthropologie._nested:anthropologie__collections_online.oc_zeit_listenfeld._global_object_id",
  144.                 "biologie._nested:biologie__collections_online.oc_zeit_listefeld._global_object_id",
  145.                 "geologie._nested:geologie__collections_online.oc_zeit_listenfeld._global_object_id",
  146.                 "kunst._nested:kunst__collections_online.zeit._global_object_id",
  147.                 "bibliothek._nested:bibliothek__collections_online.zeit._global_object_id",
  148.             ],
  149.         ],
  150.  
  151.         # kunst
  152.         [
  153.             "objectTypesIndex" => "kunst",
  154.             "id" => "kuenstlerkunst",
  155.             "label" => "Künstler",
  156.             "name" => "Künstler",
  157.             "type" => "alphabeticallysearch",
  158.             "values" => "remote",
  159.             "valuesObjecttypes" => ["personen_kunst"],
  160.             "search" => [],
  161.             "searchObjecttypes" => ["kunst"],
  162.             "searchfields" => [
  163.                 "kunst._nested:kunst__kuenstler.kuenstler._global_object_id",
  164.             ],
  165.         ],
  166.         [
  167.             "objectTypesIndex" => "kunst",
  168.             "id" => "gattungkunst",
  169.             "label" => "Gattung",
  170.             "name" => "Gattung",
  171.             "type" => "multiselect",
  172.             "values" => "remote",
  173.             "valuesObjecttypes" => ["gattung"],
  174.             "search" => [],
  175.             "searchObjecttypes" => ["gattung"],
  176.             "searchfields" => [
  177.                 "kunst._nested:kunst__gattung.gattung._global_object_id",
  178.             ],
  179.         ],
  180.         [
  181.             "objectTypesIndex" => "kunst",
  182.             "id" => "datierungkunst",
  183.             "label" => "Datierung",
  184.             "name" => "Datierung",
  185.             "type" => "daterange",
  186.             "values" => [],
  187.             "valuesObjecttypes" => [],
  188.             "search" => [
  189.                 "type" => "complex",
  190.                 "bool" => "must",
  191.                 "search" => [
  192.                     [
  193.                         "bool" => "should",
  194.                         "type" => "in",
  195.                         "fields" => ["kunst._pool.pool._id"],
  196.                         "in" => [1, 3, 4, 5, 8, 10],
  197.                     ],
  198.                     [
  199.                         "type" => "complex",
  200.                         "bool" => "should",
  201.                         "search" => [
  202.                             [
  203.                                 "bool" => "must_not",
  204.                                 "type" => "in",
  205.                                 "fields" => ["_objecttype"],
  206.                                 "in" => ["kunst"],
  207.                             ],
  208.                         ],
  209.                     ],
  210.                 ],
  211.             ],
  212.             "searchObjecttypes" => [
  213.                 "anthropologie",
  214.                 "bibliothek",
  215.                 "biologie",
  216.                 "geologie",
  217.                 "kunst",
  218.             ],
  219.             "searchfields" => "kunst.datierung_normiert",
  220.         ],
  221.         [
  222.             "objectTypesIndex" => "kunst",
  223.             "id" => "objekttypkunst",
  224.             "label" => "Objekttyp",
  225.             "name" => "Objekttyp",
  226.             "type" => "multiselect",
  227.             "values" => "remote",
  228.             "valuesObjecttypes" => ["objekttyp_mwks"],
  229.             "search" => [],
  230.             "searchObjecttypes" => [
  231.                 "anthropologie",
  232.                 "bibliothek",
  233.                 "biologie",
  234.                 "geologie",
  235.                 "kunst",
  236.             ],
  237.             "searchfields" => [
  238.                 "kunst._nested:kunst__objekttyp_mwks.objekttyp_ks._path._global_object_id",
  239.             ],
  240.         ],
  241.  
  242.         # biologie
  243.         [
  244.             "objectTypesIndex" => "biologie",
  245.             "id" => "sammlungstypbiologie",
  246.             "label" => "Sammlungstyp",
  247.             "name" => "Sammlungstyp",
  248.             "type" => "multiselect",
  249.             "values" => "remote",
  250.             "valueSortby" => "oc_es_sammlungstyp.bemerkung",
  251.             "valueSortorder" => "ASC",
  252.             "valuesObjecttypes" => ["oc_es_sammlungstyp"],
  253.             "search" => [],
  254.             "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
  255.             "searchfields" => [
  256.                 "biologie._nested:biologie__collections_online.oc_es_sammlungstyp._global_object_id",
  257.                 "geologie._nested:geologie__collections_online.oc_es_sammlungstyp._global_object_id",
  258.                 "anthropologie._nested:anthropologie__collections_online.oc_es_sammlungstyp._global_object_id",
  259.             ],
  260.         ],
  261.         [
  262.             "objectTypesIndex" => "biologie",
  263.             "id" => "objekttyppbiologie",
  264.             "label" => "Objekttyp",
  265.             "name" => "Objekttyp",
  266.             "type" => "multiselect",
  267.             "values" => "remote",
  268.             "valueSortby" => "oc_es_objekttyp.bemerkung",
  269.             "valueSortorder" => "ASC",
  270.             "valuesObjecttypes" => ["oc_es_objekttyp"],
  271.             "search" => [],
  272.             "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
  273.             "searchfields" => [
  274.                 "biologie._nested:biologie__collections_online.oc_es_objekttyp._global_object_id",
  275.                 "geologie._nested:geologie__collections_online.oc_es_objekttyp._global_object_id",
  276.                 "anthropologie._nested:anthropologie__collections_online.oc_es_objekttyp._global_object_id",
  277.             ],
  278.         ],
  279.         [
  280.             "objectTypesIndex" => "biologie",
  281.             "id" => "datierungbiologie",
  282.             "label" => "Datierung",
  283.             "name" => "Datierung",
  284.             "type" => "multiselect",
  285.             "values" => "remote",
  286.             "valueSortby" => "oc_es_datierung.bemerkung",
  287.             "valueSortorder" => "ASC",
  288.             "valuesObjecttypes" => ["oc_es_datierung"],
  289.             "search" => [],
  290.             "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
  291.             "searchfields" => [
  292.                 "biologie._nested:biologie__collections_online.oc_es_datierung._global_object_id",
  293.                 "geologie._nested:geologie__collections_online.oc_es_datierung._global_object_id",
  294.                 "anthropologie._nested:anthropologie__collections_online.oc_es_datierung._global_object_id",
  295.             ],
  296.         ],
  297.         [
  298.             "objectTypesIndex" => "biologie",
  299.             "id" => "standortbiologie",
  300.             "label" => "Standort",
  301.             "name" => "Standort",
  302.             "type" => "multiselect",
  303.             "values" => "remote",
  304.             "valueSortby" => "oc_es_standort.bemerkung",
  305.             "valueSortorder" => "ASC",
  306.             "valuesObjecttypes" => ["oc_es_standort"],
  307.             "search" => [],
  308.             "searchObjecttypes" => ["anthropologie", "biologie", "geologie"],
  309.             "searchfields" => [
  310.                 "biologie._nested:biologie__collections_online.oc_es_standort._global_object_id",
  311.                 "geologie._nested:geologie__collections_online.oc_es_standort._global_object_id",
  312.                 "anthropologie._nested:anthropologie__collections_online.oc_es_standort._global_object_id",
  313.             ],
  314.         ],
  315.     ];
  316.  
  317.     # init
  318.     public function init()
  319.     {
  320.         $this->cache = $this->modx->getCacheManager();
  321.         $this->easydbUrl = $this->modx->getOption("easydbLoginUrl");
  322.         $this->easydbUser = $this->modx->getOption("easydbLoginUser");
  323.         $this->easydbPassword = $this->modx->getOption("easydbLoginPassword");
  324.         $this->limit = $this->getProperty("limit", $this->limit);
  325.         $this->offset = $this->getProperty("offset", $this->offset);
  326.         $this->debug = $this->getProperty("debug", $this->debug);
  327.         $this->lang = $this->getProperty("lang", $this->lang);
  328.         $this->objectTypesIndex = $this->getProperty(
  329.             "objecttypesindex",
  330.             $this->defaultObjectTypesIndex
  331.         );
  332.         $this->action = $this->getProperty("action", $this->action);
  333.  
  334.         if ($this->debug && $this->modx->user->get("id") < 1) {
  335.             die("Login to MODX to use debug mode.");
  336.         }
  337.     }
  338.  
  339.     # authenticate token
  340.     public function auth()
  341.     {
  342.         $cacheKey = "token";
  343.         $cacheData = $this->getCache($cacheKey);
  344.         if ($cacheData) {
  345.             $this->token = $cacheData;
  346.             return true;
  347.         }
  348.  
  349.         $data = $this->apiRequest("session", [], "get");
  350.         $this->token = $data["token"];
  351.         $this->apiRequest("session/authenticate", [
  352.             "login" => $this->easydbUser,
  353.             "password" => $this->easydbPassword,
  354.             "token" => $this->token,
  355.         ]);
  356.  
  357.         $this->setCache($cacheKey, $this->token, 60 * 5); // lifetime 5 minutes
  358.         return true;
  359.     }
  360.  
  361.     # request to easydb api
  362.     public function apiRequest(
  363.         $path,
  364.         $payload = [],
  365.         $method = "post",
  366.         $setCount = true
  367.     ) {
  368.         $this->easydbPath = $path;
  369.         $this->easydbRequest = $this->easydbUrl . $this->easydbPath;
  370.         $easydb = $this->modx->getService("easydb", "rest.modRest");
  371.         $easydb->setOption("header", true);
  372.         $this->requestbody = array_merge($payload, [
  373.             "token" => $this->token,
  374.         ]);
  375.         $payload = $this->requestbody;
  376.         if (isset($payload["BODY"])) {
  377.             $payload["BODY"] = json_encode($payload["BODY"]);
  378.         }
  379.         $response = $easydb->$method($this->easydbRequest, $payload);
  380.         $this->easydbOutput = $response->process();
  381.  
  382.         if ($setCount) {
  383.             $this->count = $this->easydbOutput["count"];
  384.         }
  385.  
  386.         $this->debugCountRequests[] = [
  387.             "path" => $path,
  388.             "payload" =>
  389.                 $this->easydbPath != "session/authenticate"
  390.                     ? $payload
  391.                     : "[credentials]",
  392.             "response" => $this->easydbOutput,
  393.         ];
  394.  
  395.         return $this->easydbOutput;
  396.     }
  397.  
  398.     public function prepareSearchFilter()
  399.     {
  400.         $searchJson = [];
  401.  
  402.         # search term
  403.         if (!empty($this->getProperty("search"))) {
  404.             $searchTerms = explode(",", $this->getProperty("search"));
  405.             foreach ($searchTerms as $item) {
  406.                 $searchJson[] = [
  407.                     "phrase" => false,
  408.                     "bool" => "must",
  409.                     "mode" => "fulltext",
  410.                     "string" => $item,
  411.                     "type" => "match",
  412.                 ];
  413.             }
  414.         }
  415.  
  416.         # filter
  417.         foreach ($this->filterTypes as $filterType) {
  418.             if ($filterType["objectTypesIndex"] != $this->objectTypesIndex) {
  419.                 continue;
  420.             }
  421.             $property = $this->getProperty(
  422.                 $this->filterPropertyPrefix . $filterType["id"],
  423.                 false
  424.             );
  425.             if ($property) {
  426.                 $searchItems = [];
  427.                 switch ($filterType["type"]) {
  428.                     case "multiselect":
  429.                         foreach ($filterType["searchfields"] as $searchfield) {
  430.                             $searchItems[] = [
  431.                                 "type" => "in",
  432.                                 "fields" => [$searchfield],
  433.                                 "in" => $property,
  434.                                 "bool" => "should",
  435.                             ];
  436.                         }
  437.                         break;
  438.                     case "bool":
  439.                         foreach ($filterType["searchfields"] as $searchfield) {
  440.                             $searchItems[] = [
  441.                                 "type" => "in",
  442.                                 "fields" => [$searchfield],
  443.                                 "in" => [true],
  444.                                 "bool" => "must",
  445.                             ];
  446.                         }
  447.                         break;
  448.                     case "daterange":
  449.                         if (!empty($property[0]) || !empty($property[1])) {
  450.                             foreach (
  451.                                 $filterType["searchfields"]
  452.                                 as $searchfield
  453.                             ) {
  454.                                 $searchItems[] = [
  455.                                     "type" => "range",
  456.                                     "fields" => [$searchfield],
  457.                                     "from" => $property[0],
  458.                                     "to" => $property[1],
  459.                                     "bool" => "must",
  460.                                 ];
  461.                             }
  462.                         }
  463.                         break;
  464.                     case "alphabeticallysearch":
  465.                         foreach ($filterType["searchfields"] as $searchfield) {
  466.                             $searchItems[] = [
  467.                                 "type" => "in",
  468.                                 "fields" => [$searchfield],
  469.                                 "in" => $property,
  470.                                 "bool" => "should",
  471.                             ];
  472.                         }
  473.                         break;
  474.                 }
  475.                 if (count($searchItems)) {
  476.                     $searchJson[] = [
  477.                         "type" => "complex",
  478.                         "search" => $searchItems,
  479.                         "bool" => "must",
  480.                     ];
  481.                 }
  482.                 if ($filterType["search"]) {
  483.                     $searchJson[] = $filterType["search"];
  484.                 }
  485.             }
  486.         }
  487.  
  488.         return $searchJson;
  489.     }
  490.  
  491.     # search
  492.     public function getSearch()
  493.     {
  494.         $searchJson = $this->prepareSearchFilter();
  495.         $this->apiRequest("search", [
  496.             "BODY" => [
  497.                 "search" => $searchJson,
  498.                 "sort" => [
  499.                     [
  500.                         "field" => "_changelog.date_last_updated",
  501.                         "order" => "DESC",
  502.                     ],
  503.                     [
  504.                         "field" => "_system_object_id",
  505.                         "order" => "DESC",
  506.                     ],
  507.                 ],
  508.                 "objecttypes" => $this->objectTypes[$this->objectTypesIndex],
  509.                 "limit" => (int) $this->limit,
  510.                 "offset" => (int) $this->offset,
  511.             ],
  512.         ]);
  513.         $this->output = [];
  514.         $objectIds = [];
  515.         foreach ($this->easydbOutput["objects"] as $item) {
  516.             $cacheKey = "objects/" . $item["_system_object_id"];
  517.             $this->output[] =
  518.                 $this->getCache($cacheKey) ?: $this->migrateObjectData($item);
  519.         }
  520.         if (count($this->output) > 0) {
  521.             foreach ($this->output as $item) {
  522.                 $cacheKey = "objects/" . $item["objektid"];
  523.                 $this->setCache($cacheKey, $item);
  524.             }
  525.         }
  526.     }
  527.  
  528.     # set image details for object
  529.     public function setObjectDataImages($objectImageData)
  530.     {
  531.         if (count($objectImageData) < 1) {
  532.             return [
  533.                 [
  534.                     "id" => null,
  535.                     "url" => "",
  536.                     "width" => null,
  537.                     "height" => null,
  538.                     "preview" => [
  539.                         "id" => null,
  540.                         "url" => "",
  541.                         "width" => null,
  542.                         "height" => null,
  543.                     ],
  544.                     "full" => [
  545.                         "id" => null,
  546.                         "url" => "",
  547.                         "width" => null,
  548.                         "height" => null,
  549.                     ],
  550.                 ],
  551.             ];
  552.         }
  553.  
  554.         $images = [];
  555.         foreach ($objectImageData as $objectImageDataItem) {
  556.             if ($objectImageDataItem["class"] != "image") {
  557.                 continue;
  558.             }
  559.             $image = [
  560.                 "id" => $objectImageDataItem["_id"],
  561.                 "url" => $objectImageDataItem["versions"]["small"]["url"] ?: "",
  562.                 "width" => $objectImageDataItem["versions"]["small"]["width"],
  563.                 "height" => $objectImageDataItem["versions"]["small"]["height"],
  564.             ];
  565.  
  566.             $easData = $this->getMediaUrls([$image["id"]]);
  567.  
  568.             if (count($easData) > 0) {
  569.                 $imgData = $easData[$image["id"]]["versions"];
  570.                 foreach (["preview", "full"] as $imgSize) {
  571.                     $image[$imgSize] = [
  572.                         "url" => $imgData[$imgSize]["url"] ?: "",
  573.                         "width" => $imgData[$imgSize]["width"],
  574.                         "height" => $imgData[$imgSize]["height"],
  575.                     ];
  576.                 }
  577.             }
  578.  
  579.             $images[] = $image;
  580.         }
  581.  
  582.         return $images;
  583.     }
  584.  
  585.     # set video details for object
  586.     public function setObjectDataVideos($objectVideoData)
  587.     {
  588.         if (count($objectVideoData) < 1) {
  589.             return null;
  590.         }
  591.  
  592.         $videos = [];
  593.         foreach ($objectVideoData as $objectVideoDataItem) {
  594.             if ($objectVideoDataItem["class"] != "video") {
  595.                 continue;
  596.             }
  597.  
  598.             $videoId = $objectVideoDataItem["_id"];
  599.             $easData = $this->getMediaUrls([$videoId]);
  600.  
  601.             if (count($easData) > 0) {
  602.                 $videoData = $easData[$videoId]["versions"]["1920p"];
  603.                 $video = [
  604.                     "url" => $videoData["url"] ?: "",
  605.                     "width" => $videoData["width"],
  606.                     "height" => $videoData["height"],
  607.                 ];
  608.             }
  609.  
  610.             $videos[] = $video;
  611.         }
  612.  
  613.         return $videos;
  614.     }
  615.  
  616.     # set audio details for object
  617.     public function setObjectDataAudios($objectAudioData)
  618.     {
  619.         if (count($objectAudioData) < 1) {
  620.             return null;
  621.         }
  622.  
  623.         $audios = [];
  624.         foreach ($objectAudioData as $objectAudioDataItem) {
  625.             if ($objectAudioDataItem["class"] != "audio") {
  626.                 continue;
  627.             }
  628.  
  629.             $audioId = $objectAudioDataItem["_id"];
  630.             $easData = $this->getMediaUrls([$audioId]);
  631.  
  632.             if (count($easData) > 0) {
  633.                 $audioData = $easData[$audioId]["versions"]["original"];
  634.                 $audio = [
  635.                     "url" => $audioData["url"] ?: "",
  636.                 ];
  637.             }
  638.  
  639.             $audios[] = $audio;
  640.         }
  641.  
  642.         return $audios;
  643.     }
  644.  
  645.     # set object data dimensions
  646.     public function setObjectDataDimensions($items)
  647.     {
  648.         if (count($items) < 1) {
  649.             return [];
  650.         }
  651.  
  652.         $dimenions = [];
  653.         foreach ($items as $item) {
  654.             $dimenions[] = [
  655.                 "breite" => $this->convertDimenions($item["breite"]),
  656.                 "hoehe" => $this->convertDimenions($item["hoehe"]),
  657.                 "tiefe" => $this->convertDimenions($item["tiefe"]),
  658.                 "art" =>
  659.                     $item["art_masse"]["_standard"][1]["text"][$this->lang],
  660.                 "bemerkung" => $item["bemerkung"],
  661.             ];
  662.         }
  663.  
  664.         return $dimenions;
  665.     }
  666.  
  667.     # get image urls
  668.     public function getMediaUrls($objectIds)
  669.     {
  670.         if (count($objectIds) < 1) {
  671.             return;
  672.         }
  673.  
  674.         return $this->apiRequest(
  675.             "eas?ids=" . urlencode("[" . implode(",", $objectIds) . "]"),
  676.             [],
  677.             "get",
  678.             false
  679.         );
  680.     }
  681.  
  682.     # migrate easydb structure
  683.     public function migrateObjectData($item)
  684.     {
  685.         $objectTypeIndex = $item["_objecttype"];
  686.         $material = [];
  687.         foreach (
  688.             $item[$objectTypeIndex][
  689.                 "_nested:" . $objectTypeIndex . "__material_technik"
  690.             ]
  691.             as $materialItem
  692.         ) {
  693.             $material[] = $materialItem["material_u_technik"][$this->lang];
  694.         }
  695.         $data = [
  696.             "objecttype" => $objectTypeIndex,
  697.             "autor" =>
  698.                 $item[$objectTypeIndex][
  699.                     "_nested:" . $objectTypeIndex . "__kuenstler"
  700.                 ][0]["kuenstler"]["_standard"][1]["text"][$this->lang],
  701.             "titel" => $item["_standard"][1]["text"][$this->lang],
  702.             //     $item[$objectTypeIndex]["werktitel"][$this->lang] ?:
  703.             //     $item[$objectTypeIndex]["objektname"][$this->lang],
  704.             "zusatztitel" =>
  705.                 $item[$objectTypeIndex]["zusatztitel"] ?:
  706.                 $item[$objectTypeIndex]["objektname_wiss"],
  707.             "beschriftung" =>
  708.                 $item[$objectTypeIndex][
  709.                     "_nested:" . $objectTypeIndex . "__collections_online"
  710.                 ][0]["text"][$this->lang],
  711.             "images" => $this->setObjectDataImages(
  712.                 $item["_standard"]["eas"][1]
  713.             ),
  714.             "videos" => $this->setObjectDataVideos(
  715.                 $item["_standard"]["eas"][1]
  716.             ),
  717.             "audios" => $this->setObjectDataAudios(
  718.                 $item["_standard"]["eas"][1]
  719.             ),
  720.             "objektid" => $item["_system_object_id"],
  721.             "material" => $material,
  722.             "datierung" =>
  723.                 $item[$objectTypeIndex]["datierung_frei"] ?:
  724.                 $item[$objectTypeIndex][
  725.                     "_nested:" . $objectTypeIndex . "__collections_online"
  726.                 ][0]["oc_es_datierung"]["_standard"][1]["text"][$this->lang],
  727.             "thema" => $item[$objectTypeIndex]["thema"],
  728.             "objekttyp" =>
  729.                 $item[$objectTypeIndex][
  730.                     "_nested:" . $objectTypeIndex . "__objekttyp_mwks"
  731.                 ][0]["objekttyp_ks"]["_standard"][1]["text"][$this->lang] ?:
  732.                 $item[$objectTypeIndex][
  733.                     "_nested:" . $objectTypeIndex . "__collections_online"
  734.                 ][0]["oc_es_objekttyp"]["_standard"][1]["text"][$this->lang],
  735.             "entstehungsort" =>
  736.                 $item[$objectTypeIndex]["entstehungsort"]["_standard"][1][
  737.                     "text"
  738.                 ][$this->lang],
  739.             "masse" => $this->setObjectDataDimensions(
  740.                 $item[$objectTypeIndex][
  741.                     "_nested:" . $objectTypeIndex . "__masse"
  742.                 ]
  743.             ),
  744.             "creditline" => $item[$objectTypeIndex]["creditline"],
  745.             "inventarnummer" => $item[$objectTypeIndex]["inventarnummer"],
  746.             "ausgestellt" => $item[$objectTypeIndex]["ausgestellt"]
  747.                 ? true
  748.                 : false,
  749.             "sammlungstyp" =>
  750.                 $item[$objectTypeIndex][
  751.                     "_nested:" . $objectTypeIndex . "__collections_online"
  752.                 ][0]["oc_es_sammlungstyp"]["_standard"][1]["text"][$this->lang],
  753.             "standort" =>
  754.                 $item[$objectTypeIndex][
  755.                     "_nested:" . $objectTypeIndex . "__collections_online"
  756.                 ][0]["oc_es_standort"]["_standard"][1]["text"][$this->lang],
  757.             "ueberschrift" =>
  758.                 $item[$objectTypeIndex][
  759.                     "_nested:" . $objectTypeIndex . "__collections_online"
  760.                 ][0]["ueberschrift"][$this->lang],
  761.             "cachecreated" => date("Y-m-d H:i:s"),
  762.         ];
  763.         if ($this->debug) {
  764.             $data["__easydb"] = $item;
  765.         }
  766.         return $data;
  767.     }
  768.  
  769.     # set object data dimensions
  770.     public function setObjectDataDimensions($items)
  771.     {
  772.         if (count($items) < 1) {
  773.             return [];
  774.         }
  775.  
  776.         $dimenions = [];
  777.         foreach ($items as $item) {
  778.             $dimenions[] = [
  779.                 "breite" => $this->convertDimenions($item["breite"]),
  780.                 "hoehe" => $this->convertDimenions($item["hoehe"]),
  781.                 "tiefe" => $this->convertDimenions($item["tiefe"]),
  782.                 "art" =>
  783.                     $item["art_masse"]["_standard"][1]["text"][$this->lang],
  784.                 "bemerkung" => $item["bemerkung"],
  785.             ];
  786.         }
  787.  
  788.         return $dimenions;
  789.     }
  790.  
  791.     # suggest
  792.     public function getSuggest()
  793.     {
  794.         $this->apiRequest("suggest", [
  795.             "BODY" => [
  796.                 "query" => $this->getProperty("suggest"),
  797.                 "objecttypes" => $this->objectTypes[$this->objectTypesIndex],
  798.                 "linked_objecttypes" => [
  799.                     "erwerbsart",
  800.                     "fotografen",
  801.                     "gattung",
  802.                     "masse_art",
  803.                     "objekttyp",
  804.                     "objekttyp_mwks",
  805.                     "orte",
  806.                     "personen_kunst",
  807.                     "schlagworte",
  808.                 ],
  809.                 "limit" => (int) $this->limit,
  810.             ],
  811.         ]);
  812.         $this->output = [];
  813.         foreach (
  814.             $this->easydbOutput["suggestions"]["linked_objects"]
  815.             as $item
  816.         ) {
  817.             $this->output[] = $item["_standard"][1]["text"][$this->lang];
  818.         }
  819.     }
  820.  
  821.     # filter data
  822.     public function getFilterdata()
  823.     {
  824.         $cacheKey = "filterdata";
  825.         $cacheData = $this->getCache($cacheKey);
  826.         if ($cacheData) {
  827.             $this->output = $cacheData;
  828.             return;
  829.         }
  830.  
  831.         $output = [];
  832.         foreach ($this->filterTypes as $filterType) {
  833.             $items = [];
  834.             if ($filterType["values"] == "remote") {
  835.                 $items = $this->getFilterDataRemoteItems($filterType);
  836.             } else {
  837.                 foreach ($filterType["values"] as $index => $value) {
  838.                     $items[] = [
  839.                         "id" => $index,
  840.                         "name" => $value,
  841.                     ];
  842.                 }
  843.             }
  844.             $output[] = array_merge($filterType, [
  845.                 "values" => $items,
  846.             ]);
  847.         }
  848.  
  849.         $this->setCache($cacheKey, $output);
  850.         $this->output = $output;
  851.     }
  852.  
  853.     # grab all remote values and bypass easydb global limit
  854.     public function getFilterDataRemoteItems(
  855.         $filterType,
  856.         $items = [],
  857.         $limit = 1000,
  858.         $offset = 0
  859.     ) {
  860.         $this->apiRequest("search", [
  861.             "BODY" => [
  862.                 "search" => $filterType["search"],
  863.                 "sort" => [
  864.                     [
  865.                         "field" =>
  866.                             $filterType["valueSortby"] ?: "_standard.1.text",
  867.                         "order" => $filterType["valueSortorder"] ?: "ASC",
  868.                     ],
  869.                 ],
  870.                 "limit" => $limit,
  871.                 "offset" => $offset,
  872.                 "objecttypes" => $filterType["valuesObjecttypes"],
  873.             ],
  874.         ]);
  875.         foreach ($this->easydbOutput["objects"] as $item) {
  876.             $items[] = [
  877.                 "id" => $item["_global_object_id"],
  878.                 "name" => $item["_standard"][1]["text"][$this->lang],
  879.             ];
  880.         }
  881.         if ($this->easydbOutput["count"] - $limit - $offset > 0) {
  882.             $offset = $offset + $limit;
  883.             $items = $this->getFilterDataRemoteItems(
  884.                 $filterType,
  885.                 $items,
  886.                 $limit,
  887.                 $offset
  888.             );
  889.         }
  890.         return $items;
  891.     }
  892.  
  893.     # collection data
  894.     public function getCollectionList()
  895.     {
  896.         # get all collections
  897.         $this->apiRequest("collection/list", [], "get");
  898.         $collections = [];
  899.         foreach ($this->easydbOutput as $item) {
  900.             if ($item["collection"]["is_system_collection"]) {
  901.                 continue;
  902.             }
  903.             $collectionId = $item["collection"]["_id"];
  904.             $collectionDetails = $this->getCollectionDetails($collectionId);
  905.             $collectionImgs = [];
  906.             foreach ($collectionDetails as $collectionDetailsItem) {
  907.                 $imgPath = $collectionDetailsItem["bild"]["url"];
  908.                 if (!$imgPath) {
  909.                     continue;
  910.                 }
  911.                 $collectionImgs[] = $imgPath;
  912.             }
  913.             $collections[] = [
  914.                 "id" => $collectionId,
  915.                 "titel" => $item["collection"]["displayname"][$this->lang],
  916.                 "imgs" => array_slice($collectionImgs, 0, 3),
  917.             ];
  918.         }
  919.  
  920.         return $collections;
  921.     }
  922.  
  923.     public function getCollectionDetails($collectionId)
  924.     {
  925.         # get objects
  926.         $this->apiRequest("collection/objects/" . $collectionId, [], "get");
  927.         $collectionObjectIds = [];
  928.         foreach ($this->easydbOutput["objects"] as $item) {
  929.             $collectionObjectIds[] = $item["_global_object_id"];
  930.         }
  931.  
  932.         # get object details
  933.         $this->apiRequest(
  934.             "search",
  935.             [
  936.                 "BODY" => [
  937.                     "search" => [
  938.                         [
  939.                             "type" => "in",
  940.                             "fields" => ["_global_object_id"],
  941.                             "in" => $collectionObjectIds,
  942.                         ],
  943.                     ],
  944.                 ],
  945.             ],
  946.             "get"
  947.         );
  948.         $collectionObjectsData = [];
  949.         foreach ($this->easydbOutput["objects"] as $item) {
  950.             $collectionObjectsData[] = $this->migrateObjectData($item);
  951.         }
  952.  
  953.         return $collectionObjectsData;
  954.     }
  955.  
  956.     public function getCollectiondata()
  957.     {
  958.         $collectionId = $this->getProperty("collectionId");
  959.         if ($collectionId) {
  960.             $this->output = $this->getCollectionDetails($collectionId);
  961.         } else {
  962.             $this->output = $this->getCollectionList();
  963.         }
  964.     }
  965.  
  966.     # get a random object
  967.     public function getRandomobject()
  968.     {
  969.         $this->apiRequest("search", [
  970.             "BODY" => [
  971.                 "sort" => [
  972.                     [
  973.                         "field" => "_system_object_id",
  974.                         "order" => "DESC",
  975.                     ],
  976.                 ],
  977.                 "objecttypes" => $this->objectTypes["global"],
  978.                 "limit" => 1,
  979.                 "offset" => random_int(0, 650),
  980.             ],
  981.         ]);
  982.         $this->output = $this->migrateObjectData(
  983.             $this->easydbOutput["objects"][0]
  984.         );
  985.     }
  986.  
  987.     # get home objects
  988.     public function getHomeObjects()
  989.     {
  990.         $limit = $this->limit;
  991.         $this->limit = 300;
  992.         $this->getSearch();
  993.         $this->limit = $limit;
  994.         $output = $this->output;
  995.         shuffle($output);
  996.         $this->count = (int) $this->limit;
  997.         $this->output = array_slice($output, $this->offset, $this->limit);
  998.     }
  999.  
  1000.     # get hero image
  1001.     public function getHeroimg()
  1002.     {
  1003.         $searchObjectTypes = [];
  1004.         foreach ($this->objectTypes["global"] as $objectType) {
  1005.             $searchObjectTypes[] = [
  1006.                 "bool" => "should",
  1007.                 "type" => "in",
  1008.                 "fields" => [
  1009.                     $objectType .
  1010.                     "." .
  1011.                     ($objectType == "kunst"
  1012.                         ? "checkbox_highlight"
  1013.                         : "highlight"),
  1014.                 ],
  1015.                 "in" => [true],
  1016.             ];
  1017.         }
  1018.         $this->apiRequest("search", [
  1019.             "BODY" => [
  1020.                 "search" => $searchObjectTypes,
  1021.                 "sort" => [
  1022.                     [
  1023.                         "field" => "_system_object_id",
  1024.                         "order" => "DESC",
  1025.                     ],
  1026.                 ],
  1027.                 "objecttypes" => $this->objectTypes["global"],
  1028.                 "limit" => 100,
  1029.                 "offset" => 0,
  1030.             ],
  1031.         ]);
  1032.         $randomObject =
  1033.             $this->easydbOutput["objects"][
  1034.                 random_int(0, $this->easydbOutput["count"])
  1035.             ];
  1036.         $this->output = $this->migrateObjectData($randomObject);
  1037.     }
  1038.  
  1039.     # get export
  1040.     public function getDownload()
  1041.     {
  1042.         $objectsZip = [];
  1043.         $tempDir =
  1044.             MODX_CORE_PATH . "cache/" . $this->cachePrefix . "/download/";
  1045.         $filename = "muwi-online-collection-" . uniqid() . ".zip";
  1046.         $objectIds = explode(",", $this->getProperty("objectids"));
  1047.         $easData = $this->getMediaUrls($objectIds);
  1048.         mkdir($tempDir, 0777, true);
  1049.         foreach ($easData as $item) {
  1050.             $tmpFile = $tempDir . $item["original_filename"];
  1051.             $remoteUrl = $item["versions"]["full"]["url"];
  1052.             $fileContent = file_get_contents($remoteUrl);
  1053.             file_put_contents($tmpFile, $fileContent);
  1054.             $objectsZip[] = [
  1055.                 "file" => $tmpFile,
  1056.                 "url" => $remoteUrl,
  1057.             ];
  1058.         }
  1059.         $zip = new ZipArchive();
  1060.         $zip->open($tempDir . $filename, ZipArchive::CREATE);
  1061.         foreach ($objectsZip as $item) {
  1062.             $zip->addFile($item["file"], basename($item["file"]));
  1063.         }
  1064.         $zip->close();
  1065.         header("Content-type: application/zip");
  1066.         header("Content-Length: " . filesize($tempDir . $filename));
  1067.         header('Content-Disposition: attachment; filename="' . $filename . '"');
  1068.         readfile($tempDir . $filename);
  1069.         unlink($tempDir . $filename);
  1070.         die();
  1071.     }
  1072.  
  1073.     # build output
  1074.     public function getOutput()
  1075.     {
  1076.         unset($this->requestbody["password"], $this->requestbody["login"]);
  1077.         return array_merge(
  1078.             [
  1079.                 "data" => $this->output,
  1080.             ],
  1081.             $this->debug
  1082.                 ? [
  1083.                     "debug" => [
  1084.                         "query" => $_GET,
  1085.                         "requests" => $this->debugCountRequests,
  1086.                     ],
  1087.                 ]
  1088.                 : []
  1089.         );
  1090.     }
  1091.  
  1092.     # pagination
  1093.     public function getPagination()
  1094.     {
  1095.         return [
  1096.             "limit" => (int) $this->limit,
  1097.             "offset" => (int) $this->offset,
  1098.             "count" => $this->count,
  1099.             "pages" => ceil($this->count / $this->limit),
  1100.             "page" => ceil($this->offset / $this->limit) + 1,
  1101.             "nav" => [
  1102.                 "offsetPrev" =>
  1103.                     $this->offset - $this->limit < 0
  1104.                         ? null
  1105.                         : $this->offset - $this->limit,
  1106.                 "offsetNext" =>
  1107.                     $this->offset + $this->limit >= $this->count
  1108.                         ? null
  1109.                         : $this->offset + $this->limit,
  1110.             ],
  1111.         ];
  1112.     }
  1113.  
  1114.     # build list
  1115.     public function getList()
  1116.     {
  1117.         $this->init();
  1118.         $this->auth();
  1119.  
  1120.         switch ($this->action) {
  1121.             case "suggest":
  1122.                 $this->getSuggest();
  1123.                 break;
  1124.             case "filterdata":
  1125.                 $this->getFilterdata();
  1126.                 break;
  1127.             case "collection":
  1128.                 $this->getCollectiondata();
  1129.                 break;
  1130.             case "randomobject":
  1131.                 $this->getRandomobject();
  1132.                 break;
  1133.             case "heroimg":
  1134.                 $this->getHeroimg();
  1135.                 break;
  1136.             case "homeobjects":
  1137.                 $this->getHomeObjects();
  1138.                 break;
  1139.             case "download":
  1140.                 $this->getDownload();
  1141.                 break;
  1142.             case "refresh":
  1143.                 $this->refreshCache();
  1144.                 break;
  1145.             default:
  1146.                 $this->getSearch();
  1147.                 break;
  1148.         }
  1149.  
  1150.         return $this->collection($this->getOutput(), $this->getPagination());
  1151.     }
  1152.  
  1153.     # set cache
  1154.     public function setCache($cacheName, $data, $lifetime = null)
  1155.     {
  1156.         $cacheData = $this->cache->set(
  1157.             $cacheName,
  1158.             $data,
  1159.             $lifetime ?: $this->cacheLifetime,
  1160.             [
  1161.                 xPDO::OPT_CACHE_KEY => $this->cachePrefix,
  1162.             ]
  1163.         );
  1164.  
  1165.         if (!$cacheData) {
  1166.             return;
  1167.         }
  1168.  
  1169.         return $cacheData;
  1170.     }
  1171.  
  1172.     # get cache
  1173.     public function getCache($cacheName)
  1174.     {
  1175.         $cacheData = $this->cache->get($cacheName, [
  1176.             xPDO::OPT_CACHE_KEY => $this->cachePrefix,
  1177.         ]);
  1178.  
  1179.         if (!$cacheData || $this->debug) {
  1180.             return;
  1181.         }
  1182.  
  1183.         return $cacheData;
  1184.     }
  1185.  
  1186.     # refresh cache
  1187.     public function refreshCache()
  1188.     {
  1189.         $this->cache->deleteTree(
  1190.             MODX_CORE_PATH . "cache/" . $this->cachePrefix
  1191.         );
  1192.         $this->getFilterdata();
  1193.         $this->getSearch();
  1194.         $this->output = ["msg" => "cache refreshed"];
  1195.     }
  1196.  
  1197.     # convert dimmensions
  1198.     public function convertDimenions($val)
  1199.     {
  1200.         if ($val > 0) {
  1201.             return number_format($val / 100, 2, ",", "");
  1202.         } else {
  1203.             return null;
  1204.         }
  1205.     }
  1206. }
  1207.  
File Description
  • dfasdf
  • PHP Code
  • 14 Sep-2021
  • 39.8 Kb
You can Share it: