Mongoer.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Meramo\mrm_be;
  3. class Mongoer
  4. {
  5. function __construct(){}
  6. public static function sendRequest($dataType, $action, $data){
  7. $mongodburl = $_ENV['FE_DATABASE_DSN'];
  8. $mongodbdb = $_ENV['FE_DATABASE'];
  9. $client = new \MongoDB\Client($mongodburl);
  10. $collection = null;
  11. if($dataType == "pages")
  12. $collection = $client->$mongodbdb->pages;
  13. if($dataType == "search")
  14. $collection = $client->$mongodbdb->searches;
  15. if($dataType == "topthemen")
  16. $collection = $client->$mongodbdb->topthemens;
  17. if($dataType == "themaderwoche")
  18. $collection = $client->$mongodbdb->themaderwoches;
  19. if($dataType == "security")
  20. $collection = $client->$mongodbdb->pagesecurities;
  21. if($dataType == "abialtcache")
  22. $collection = $client->$mongodbdb->abialtcaches;
  23. if($dataType == "menues")
  24. $collection = $client->$mongodbdb->menues;
  25. if($dataType == "maintenance")
  26. $collection = $client->$mongodbdb->maintenance;
  27. if($dataType == "redirects")
  28. $collection = $client->$mongodbdb->redirects;
  29. if($dataType == "forwardings")
  30. $collection = $client->$mongodbdb->forwardings;
  31. if($dataType == "pdfgenjobs")
  32. $collection = $client->$mongodbdb->pdfgenjobs;
  33. if($dataType == "chatbot")
  34. $collection = $client->$mongodbdb->chatbot;
  35. if(!$collection) return self::log("no collection for senddata: ".$dataType);
  36. self::log("sendRequest ".$action." ".$dataType);
  37. if($action == "save"){
  38. $updateResult = $collection->updateOne(
  39. ['pageuid' => $data['pageuid']],
  40. ['$set' => $data],
  41. ['upsert' => true],
  42. );
  43. }
  44. if($action == "delete"){
  45. $collection->deleteOne(['pageuid' => $data['pageuid']]);
  46. }
  47. if($action == "deleteAll") {
  48. $collection->deleteMany([]);
  49. }
  50. }
  51. /* PRIVATES */
  52. private static function log($msg, $isString = true){
  53. if(!$isString){
  54. $msg = json_encode($msg, JSON_PRETTY_PRINT);
  55. }
  56. file_put_contents('/tmp/t3mongoerdebug.log', $msg.PHP_EOL, FILE_APPEND);
  57. }
  58. /* Private function to drop a collection */
  59. public static function deleteAll($collection) {
  60. return $collection->deleteMany();
  61. }
  62. }