src/Controller/Api/NewsApiController.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Model\DataObject\News;
  4. use Carbon\Carbon;
  5. use Pimcore\Controller\FrontendController;
  6. use Pimcore\Model\DataObject;
  7. use Pimcore\Model\Element\Tag;
  8. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  9. use Pimcore\Tool;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use App\Model\GeoLocation;
  15. use Symfony\Component\String\Slugger\AsciiSlugger;
  16. class NewsApiController extends FrontendController
  17. {
  18.     /**
  19.      * @var PimcoreUrl
  20.      */
  21.     protected PimcoreUrl $pimcoreUrl;
  22.     protected $condition;
  23.     protected $conditionParams;
  24.     protected $conditionText;
  25.     protected $ortNamen;
  26.     /**
  27.      * @param PimcoreUrl $pimcoreUrl
  28.      */
  29.     public function __construct(PimcoreUrl $pimcoreUrl)
  30.     {
  31.         $this->pimcoreUrl $pimcoreUrl;
  32.         $this->condition = [];
  33.         $this->conditionParams = [];
  34.         $this->conditionText = [];
  35.         $this->ortNamen = [];
  36.     }
  37.     private function conditionSQL(): string
  38.     {
  39.         return join(' AND 'array_map(fn($c) => '(' $c ')'$this->condition));
  40.     }
  41.     private function formatArticle(DataObject\News $article)
  42.     {
  43.         $link $this->generateUrl('news_detail', ['path' => $article->getKey()]);
  44.         $getTags \Pimcore\Model\Element\Tag::getTagsForElement("object"$article->getId());
  45.         $tags = [];
  46.         foreach ($getTags as $tag) {
  47.             $tags[] = $tag->getName();
  48.         }
  49.         $imageObject null;
  50.         if ($article->getImage()) {
  51.             $imageObject $article->getImage();
  52.         } elseif ($article->getImageGallery() && count($article->getImageGallery()->getItems())) {
  53.             $imageObject $article->getImageGallery()->getItems()[0]->getImage();
  54.         }
  55.         $image $imageObject?->getThumbnail('newsArticleImage')->getPath();
  56.         if (!$image) {
  57.             $image "";
  58.         }
  59.         $imageCopyright $imageObject?->getMetadata("copyright");
  60.         $tags = [];
  61.         $tagList self::getTagsForElement('object'$article->getId());
  62.         foreach ($tagList as $tag) {
  63.             $tags[] = ['name' => $tag->getName(), 'path' => $tag->getNamePath()];
  64.         }
  65.         $ad = [];
  66.         if ($article->getAdvertisement()) {
  67.             $ad = [
  68.                 'customer' => $article->getAdvertisementCustomer(),
  69.                 'address' => $article->getAdvertisementAddress(),
  70.                 'link' => $article->getAdvertisementLink()?->getPath(),
  71.                 'linkTitle' => $article->getAdvertisementLink()?->getTitle(),
  72.                 'logo' => $article->getAdvertisementLogo()?->getThumbnail('advertisementLogo')->getPath()
  73.             ];
  74.         }
  75.         return [
  76.             "id" => $article->getId(),
  77.             "category" => $article->getCategory(),
  78.             "topic" => $article->getTopic(),
  79.             "locality" => $article->getLocations(),
  80.             "title" => $article->getTitle(),
  81.             "text" => $article->getText() ?? "",
  82.             "excerpt" => $article->getExerpt(),
  83.             "image" => $image,
  84.             "imageCopyright" => $imageCopyright,
  85.             "articleDate" => date("c"strtotime($article->getArticleDate())),
  86.             "published" => $article->getPublished(),
  87.             "highlight" => $article->getStaticPosition() != null,
  88.             "advertisement" => $article->getAdvertisement(),
  89.             "advertisementData" => $ad,
  90.             "createdAt" => date("c"$article->getCreationDate()),
  91.             "updatedAt" => date("c"$article->getModificationDate()),
  92.             "link" => $link,
  93.             "tags" => $tags
  94.         ];
  95.     }
  96.     public function getAdvertisements(Request $request) {
  97.         $news = new DataObject\News\Listing();
  98.         $conditions $this->setConditions($requesttrue);
  99.         $news->setCondition($this->conditionSQL(), $this->conditionParams);
  100.     }
  101.     /**
  102.      * @Route("/api/get-news", name="news_article_list")
  103.      * @param Request $request
  104.      * @return JsonResponse
  105.      */
  106.     public function defaultAction(Request $request)
  107.     {
  108.         $news = new DataObject\News\Listing();
  109.         $landkreise $request->get("lk"null);
  110.         $offset $request->get("offset"0);
  111.         $limit $request->get("limit"50);
  112.         $appSection $request->get("app_section"null);
  113.         // boa did not always send app_section parameter.
  114.         // We can recognize old versions (only having home news) by their user agent.
  115.         if (!$appSection && str_contains(strtolower($request->headers->get('User-Agent')), 'dart/')) {
  116.             $appSection "home";
  117.         }
  118.         if ($limit 50) { $limit 50; }
  119.         $this->setConditions($requestfalse$appSection);
  120.         $news->setLimit($limit);
  121.         $news->setOffset($offset);
  122.         $news->setOrderKey(["staticPosition""articleDate"]);
  123.         $news->setOrder("desc");
  124.         $news->setCondition($this->conditionSQL(), $this->conditionParams);
  125.         // Inject items with staticPosition != null ("highlights") at that position
  126.         // This only works reliably if $offset === 0
  127.         $newsItems $news->load();
  128.         $staticPositionNews array_filter($newsItems, fn($news) => $news->getStaticPosition() !== null);
  129.         $regularNews array_filter($newsItems, fn($news) => $news->getStaticPosition() === null);
  130.         $sortedNews = [];
  131.         $idx 0;
  132.         foreach ($regularNews as $item) {
  133.             foreach ($staticPositionNews as $s) {
  134.                 if ($s->getStaticPosition() === $idx) {
  135.                     $sortedNews[] = $s;
  136.                 }
  137.             }
  138.             $sortedNews[] = $item;
  139.             $idx++;
  140.         }
  141.         $data = [];
  142.         foreach ($sortedNews as $article) {
  143.             $d $this->formatArticle($article);
  144.             if (count($this->ortNamen)) {
  145.                 $found false;
  146.                 // Landkreise ersetzen mit den alten Tags
  147.                 $dataTopics $article->getTopic();
  148.                 if (!is_array($landkreise)) {
  149.                     $landkreise explode(','$landkreise);
  150.                 }
  151.                 if ($dataTopics && count($dataTopics) && $landkreise && count($landkreise)) {
  152.                     foreach ($landkreise as $landkreis) {
  153.                         if (isset($landkreisTags[$landkreis])) {
  154.                             foreach ($dataTopics as $topic) {
  155.                                 if (in_array($topic$landkreisTags[$landkreis])) {
  156.                                     $found true;
  157.                                 }
  158.                             }
  159.                         }
  160.                     }
  161.                 }
  162.                 if (!$found) {
  163.                     // Ortnamen direkt finden
  164.                     if ($d['locality'] && count($d['locality'])) {
  165.                         foreach ($d['locality'] as $locationName) {
  166.                             if ($locationName && in_array(trim($locationName), $this->ortNamen)) {
  167.                                 $found true;
  168.                             }
  169.                         }
  170.                     }
  171.                 }
  172.                 if (!$found) {
  173.                     $d null;
  174.                 }
  175.             }
  176.             if ($d) {
  177.                 $data[] = $d;
  178.             }
  179.         }
  180.         $ad null;
  181.         if ($appSection === "home" || $request->get('ad'false)) {
  182.             // get one advertisment
  183.             $ad $this->getAdvertisment();
  184.             if ($data && count($data) > && $ad) {
  185.                 array_splice($data30, [$ad]);
  186.             }
  187.         }
  188.         $response $this->json([
  189.             "success" => true,
  190. //            "condition" => $this->condition,
  191. //            "conditionParams" => $this->conditionParams,
  192.             "orte" => $this->ortNamen,
  193. //            "landkreise" => $landkreise,
  194.             "data" => $data,
  195.             "ad" => $ad,
  196.         ]);
  197.         $response->setExpires(new \DateTime('+15 seconds'));
  198.         return $response;
  199.     }
  200.     /**
  201.      * @Route("/api/get-news/{id}", name="news_article_by_id")
  202.      */
  203.     public function articleAction(int $idRequest $request)
  204.     {
  205.         $article DataObject\News::getById($id);
  206.         if (!$article) {
  207.             throw $this->createNotFoundException('The news does not exist');
  208.         }
  209.         $response $this->json([
  210.             "success" => true,
  211.             "data" => $this->formatArticle($article)
  212.         ]);
  213.         $response->setExpires(new \DateTime('+15 seconds'));
  214.         return $response;
  215.     }
  216.     /**
  217.      * @Route("/api/create-news-preflight", name="news_article_create_preflight")
  218.      * @param Request $request
  219.      * @return JsonResponse
  220.      */
  221.     public function preflightAction(Request $request)
  222.     {
  223.         $r = [
  224.             'error' => false
  225.         ];
  226.         $user Tool\Admin::getCurrentUser();
  227.         if (!$user) {
  228.             $user Tool\Session::getReadonly()->get("user");
  229.         }
  230.         if ($user) {
  231.             $r['user'] = $user->getName();
  232.         } else {
  233.             $r['error'] = true;
  234.         }
  235.         return $this->json($r);
  236.     }
  237.     /**
  238.      * @Route("/api/create-news", name="news_article_create")
  239.      * @param Request $request
  240.      * @return JsonResponse
  241.      */
  242.     public function createAction(Request $request)
  243.     {
  244.         $r = [
  245.             'error' => false,
  246.             'newsId' => -1
  247.         ];
  248.         $user Tool\Admin::getCurrentUser();
  249.         if (!$user) {
  250.             $user Tool\Session::getReadonly()->get("user");
  251.         }
  252.         if ($user) {
  253.             $data $request->getContent();
  254.             if ($data) {
  255.                 $news = new News();
  256.                 $data json_decode($datatrue);
  257.                 $data['title'] = trim($data['title']);
  258.                 $text nl2br(trim($data['text']));
  259.                 if (strpos($text'<br />') > 0) {
  260.                     $text explode('<br />'$text);
  261.                     $newText '';
  262.                     foreach ($text as $line) {
  263.                         $newText .= '<p>' trim($line) . '</p>';
  264.                     }
  265.                     $text $newText;
  266.                 }
  267.                 $data['text'] = $text;
  268.                 $slugger = new AsciiSlugger();
  269.                 $now = new Carbon();
  270.                 $news->setTitle($data['title']);
  271.                 $news->setText($data['text']);
  272.                 $news->setArticleDate($now);
  273.                 $news->setKey($now->format('Y-m-d') . '-' $slugger->slug(strtolower($data['title'])));
  274.                 $news->setLocations($data['locations']);
  275.                 $news->setCategory('Nachrichten');
  276.                 $news->setParentId(24);
  277.                 $news->setPublished(true);
  278.                 $news->save();
  279.                 if ($news->getId()) {
  280.                     $r['slug'] = $news->getKey();
  281.                     $r['newsId'] = $news->getId();
  282.                 }
  283.             }
  284.         }
  285.         return $this->json($r);
  286.     }
  287.     /**
  288.      * @param Request $request
  289.      * @param boolean $advertisement
  290.      * @return array $conditions
  291.      */
  292.     private function setConditions(Request $request$advertisement false, ?string $appSection null): array
  293.     {
  294.         $now time();
  295.         $category $request->get("category"null);
  296.         $search $request->get("search"null);
  297.         $tags $request->get("topics"null);
  298.         $landkreise $request->get("lk"null);
  299.         $highlight $request->get("highlight"false);
  300.         $ids $request->get('id'null);
  301.         $not $request->get("notId"'');
  302.         $limit $request->get("limit"50);
  303.         if ($limit 50) { $limit 50; }
  304.         $conditions = [
  305.             'condition' => [],
  306.             'conditionParams' => []
  307.         ];
  308.         if ($advertisement) {
  309.             $conditions['condition'][] = "advertisement is true";
  310.             $conditions['condition'][] = "advertisementStart >= :now";
  311.             $conditions['conditionParams']['now'] = $now;
  312.         } else {
  313.             $this->condition[] = "advertisement is not true";
  314.             $this->condition[] = "articleDate <= :now";
  315.             $this->conditionParams["now"] = $now;
  316.         }
  317.         /*
  318.           name: 'Garmisch-Partenkirchen', search: 'DE21D',
  319.           name: 'Weilheim-Schongau', search: 'DE21N',
  320.           name: 'Starnberg', search: 'DE21L',
  321.           name: 'Bad Tölz-Wolfratshausen', search: 'DE216',
  322.          */
  323.         $landkreisTags = [
  324.             'DE21N' => ['Landkreis WeilheimSchongau''WeilheimSchongau'],
  325.             'DE21D' => ['GarmischPartenkirchen''Landkreis GAP''Landkreis GarmischPartenkirchen'],
  326.             'DE21L' => ['Landkreis Starnberg'],
  327.             'DE216' => ['Bad TölzWolfratshausen''Landkreis Bad TölzWolfratshausen']
  328.         ];
  329.         if ($landkreise) {
  330.             $landkreise explode(','$landkreise);
  331.             if (count($landkreise)) {
  332.                 $landkreisCondition = [];
  333.                 foreach ($landkreise as $landkreis) {
  334.                     if (!$advertisement) {
  335.                         if (isset($landkreisTags[$landkreis])) {
  336.                             $this->conditionText = [];
  337.                             foreach ($landkreisTags[$landkreis] as $i => $tag) {
  338.                                 $this->conditionText[] = 'topic LIKE :lkTag_' $landkreis '_' $i;
  339.                                 $this->conditionParams['lkTag_' $landkreis '_' $i] = '%' $tag '%';
  340.                             }
  341.                             $landkreisCondition[] = '(' implode(' OR '$this->conditionText) . ')';
  342.                         }
  343.                     }
  344.                 }
  345.                 $locationList = new GeoLocation\Listing();
  346.                 $locationList->setCondition('kreisNutscode in (?)', [$landkreise]);
  347.                 /** @var GeoLocation $locations */
  348.                 foreach ($locationList as $i => $locations) {
  349.                     if (!$advertisement) {
  350.                         $this->ortNamen[] = $locations->gemeinde;
  351.                         $landkreisCondition[] = 'locations LIKE :loc_' $i;
  352.                         $this->conditionParams['loc_' $i] = '%' $locations->gemeinde ',%';
  353.                     }
  354.                 }
  355.                 if (!$advertisement) {
  356.                     $this->condition[] = '(' implode(' OR '$landkreisCondition) . ')';
  357.                 }
  358.             }
  359.         }
  360.         if (!$advertisement) {
  361.             if ($search) {
  362.                 $this->condition[] = "(topic LIKE :search OR title LIKE :search OR text LIKE :search OR locations LIKE :search)";
  363.                 $this->conditionParams["search"] = "%$search%";
  364.             }
  365.             if ($category) {
  366.                 $this->condition[] = "category LIKE :category";
  367.                 $this->conditionParams["category"] = "%$category%";
  368.             }
  369.             if ($tags) {
  370.                 $this->condition[] = "topic LIKE :topic";
  371.                 $this->conditionParams["topic"] = "%$tags%";
  372.             }
  373.             if ($highlight) {
  374.                 $this->condition[] = "highlight = :highlight";
  375.                 $this->conditionParams["highlight"] = $highlight $highlight 'null';
  376.             }
  377.             if ($not) {
  378.                 $this->condition[] = "oo_id NOT IN (:notId)";
  379.                 $this->conditionParams["notId"] = explode(','$not);
  380.             }
  381.             if ($ids) {
  382.                 $this->condition[] = "oo_id IN(:ids)";
  383.                 $this->conditionParams["ids"] = explode(','$ids);
  384.             }
  385.             if ($appSection) {
  386.                 $sportTag \Pimcore\Model\Element\Tag::getByPath("/Sport");
  387.                 if ($sportTag) {
  388.                     $sportCond = fn($include) => "oo_id " . ($include "" "NOT") . " IN (
  389.                         SELECT cid FROM tags_assignment INNER JOIN tags ON tags.id = tags_assignment.tagid
  390.                         WHERE
  391.                             ctype = 'object' AND
  392.                             (id = :tagId OR idPath LIKE :tagIdPath)
  393.                     )";
  394.                     if ($appSection === "sport") {
  395.                         // sport contains all articles with tags under /Sport, whether app-exclusive or not
  396.                         $this->condition[] = $sportCond(true);
  397.                     } else {
  398.                         // home (fallback) is mixed:
  399.                         // - all non-app-exclusive articles (including sports)
  400.                         // - all non-sports articles
  401.                         // - NO app-exclusive sports articles
  402.                         $this->condition[] = "(" $sportCond(false) . ") OR appExclusive IS NULL OR appExclusive = false";
  403.                     }
  404.                     $this->conditionParams["tagId"] = $sportTag->getId();
  405.                     $this->conditionParams["tagIdPath"] = \Pimcore\Db\Helper::escapeLike($sportTag->getFullIdPath()) . "%";
  406.                 }
  407.             } else {
  408.                 // website has all articles except app-exclusive ones
  409.                 $this->condition[] = "appExclusive IS NULL OR appExclusive = false";
  410.             }
  411.         }
  412.         return $conditions;
  413.     }
  414.     /**
  415.      * @Route("/api/feed", name="news_article_feed")
  416.      * @param Request $request
  417.      * @return Response
  418.      */
  419.     public function rssAction(Request $request)
  420.     {
  421.         $news = new DataObject\News\Listing();
  422.         $landkreise $request->get("lk"null);
  423.         $offset $request->get("offset"0);
  424.         $limit $request->get("limit"50);
  425.         if ($limit 50) { $limit 50; }
  426.         $this->setConditions($requestfalse);
  427.         $news->setLimit($limit);
  428.         $news->setOffset($offset);
  429.         $news->setOrderKey("articleDate");
  430.         $news->setOrder("desc");
  431.         $news->setCondition($this->conditionSQL(), $this->conditionParams);
  432.         $items '';
  433.         foreach ($news as $key => $article) {
  434.             $link $this->generateUrl('news_detail', ['path' => $article->getKey()]);
  435.             $getTags \Pimcore\Model\Element\Tag::getTagsForElement("object"$article->getId());
  436.             $tags = [];
  437.             foreach ($getTags as $tag) {
  438.                 $tags[] = $tag->getName();
  439.             }
  440.             $image "";
  441.             if ($article->getImage()) {
  442.                 $image $article->getImage()->getThumbnail('newsArticleImage')->getPath();
  443.             } elseif ($article->getImageGallery() && count($article->getImageGallery()->getItems())) {
  444.                 $image $article->getImageGallery()->getItems()[0]->getImage()->getThumbnail('newsArticleImage')->getPath();
  445.             }
  446.             /*
  447.              <item>
  448.                 <title>2023: Okowa may emerge Atiku’s running mate - Punch Newspapers</title>
  449.                 <link>https://news.google.com/__i/rss/rd/articles/CBMiPmh0dHBzOi8vcHVuY2huZy5jb20vMjAyMy1va293YS1tYXktZW1lcmdlLWF0aWt1cy1ydW5uaW5nLW1hdGUv0gEA?oc=5</link>
  450.                 <guid isPermaLink="false">1450966479</guid>
  451.                 <pubDate>Tue, 14 Jun 2022 11:29:19 GMT</pubDate>
  452.                 <description><ol><li><a href="https://news.google.com/__i/rss/rd/articles/CBMiPmh0dHBzOi8vcHVuY2huZy5jb20vMjAyMy1va293YS1tYXktZW1lcmdlLWF0aWt1cy1ydW5uaW5nLW1hdGUv0gEA?oc=5" target="_blank">2023: Okowa may emerge Atiku’s running mate</a>&nbsp;&nbsp;<font color="#6f6f6f">Punch Newspapers</font></li><li><a href="https://news.google.com/__i/rss/rd/articles/CBMidWh0dHBzOi8vZGFpbHlwb3N0Lm5nLzIwMjIvMDYvMTQvMjAyMy1oZS1oYXRlcy1tdXNsaW1zLWlzbGFtaWMtZ3JvdXAtd2FybnMtYXRpa3UtYWdhaW5zdC1waWNraW5nLXdpa2UtYXMtcnVubmluZy1tYXRlL9IBe2h0dHBzOi8vZGFpbHlwb3N0Lm5nLzIwMjIvMDYvMTQvMjAyMy1oZS1oYXRlcy1tdXNsaW1zLWlzbGFtaWMtZ3JvdXAtd2FybnMtYXRpa3UtYWdhaW5zdC1waWNraW5nLXdpa2UtYXMtcnVubmluZy1tYXRlLz9hbXA9MQ?oc=5" target="_blank">2023: He hates Muslims – Islamic group warns Atiku against picking Wike as running mate</a>&nbsp;&nbsp;<font color="#6f6f6f">Daily Post Nigeria</font></li><li><a href="https://news.google.com/__i/rss/rd/articles/CBMiWWh0dHBzOi8vd3d3LnZhbmd1YXJkbmdyLmNvbS8yMDIyLzA2L3ZwLWFwYy1leWVzLXNoZXR0aW1hLWxhbG9uZy13aWtlLW9rb3dhLWJhdHRsZS1pbi1wZHAv0gEA?oc=5" target="_blank">VP: APC eyes Shettima, Lalong; Wike, Okowa battle in PDP</a>&nbsp;&nbsp;<font color="#6f6f6f">Vanguard</font></li><li><a href="https://news.google.com/__i/rss/rd/articles/CBMiVmh0dHBzOi8vcHVuY2huZy5jb20vc291dGgtZWFzdC1wZHAtZGVtYW5kcy1hdGlrdXMtdnAtc2xvdC1wYXJ0eS1kaXNtaXNzZXMtb2Jpcy10aHJlYXQv0gEA?oc=5" target="_blank">South-East PDP demands Atiku’s VP slot, party dismisses Obi’s threat</a>&nbsp;&nbsp;<font color="#6f6f6f">Punch Newspapers</font></li><li><a href="https://news.google.com/__i/rss/rd/articles/CBMiT2h0dHBzOi8vdGhlbmF0aW9ub25saW5lbmcubmV0L3BkcC1yZXBzLXRpcC13aWtlLWFueWltLW5uYW1hbmktZm9yLXJ1bm5pbmctbWF0ZS_SAQA?oc=5" target="_blank">PDP Reps tip Wike, Anyim, Nnamani for running mate - The Nation Newspaper</a>&nbsp;&nbsp;<font color="#6f6f6f">The Nation Newspaper </font></li><li><strong><a href="https://news.google.com/stories/CAAqNggKIjBDQklTSGpvSmMzUnZjbmt0TXpZd1NoRUtEd2pQLS0tekJSSDQxLUc5WEo3N3VpZ0FQAQ?oc=5" target="_blank">View Full coverage on Google News</a></strong></li></ol></description>
  453.                 <source url="https://punchng.com">Punch Newspapers</source>
  454.             </item>
  455.              */
  456.             $previewText strip_tags($article->getText());
  457.             if (strlen($previewText) > 50) {
  458.                 $rest substr($previewText50);
  459.                 $nextSpace strpos($rest' ');
  460.                 $previewText trim(substr($previewText050 $nextSpace)) . '...';
  461.                 $radioText '';
  462.                 $radioTextRand rand(14);
  463.                 switch ($radioTextRand) {
  464.                     case 1:
  465.                         $radioText 'Radio Oberland';
  466.                         break;
  467.                     case 2:
  468.                         $radioText 'radio-oberland.de';
  469.                         break;
  470.                     case 3:
  471.                         $radioText 'radio oberland';
  472.                         break;
  473.                     case 4:
  474.                         $radioText 'radiooberland';
  475.                         break;
  476.                 }
  477.                 $watermark '';
  478.                 $watermarkRand rand(14);
  479.                 switch ($watermarkRand) {
  480.                     case 1:
  481.                         $watermark '('.$radioText.')';
  482.                         break;
  483.                     case 2:
  484.                         $watermark '{'.$radioText.'}';
  485.                         break;
  486.                     case 3:
  487.                         $watermark '- (c) '.$radioText.' -';
  488.                         break;
  489.                     case 4:
  490.                         $watermark '--'.$radioText.'--';
  491.                 }
  492.             }
  493.             $date = new \DateTime(date("D, d M Y G:i:s"strtotime($article->getArticleDate())));
  494.             $keywords '';
  495.             if ($article->getTopic() && count($article->getTopic())) {
  496.                 $keywords '<media:keywords xmlns:media="http://search.yahoo.com/mrss/">' str_replace('<'''join(', '$article->getTopic())) . '</media:keywords>';
  497.             }
  498.             $text trim($article->getText());
  499.             $rand rand(12);
  500.             if ($rand === 2) {
  501.                 $text '<p>' $watermark '</p> ' $text;
  502.             } else {
  503.                 $text .= ' <p>' $watermark '</p>';
  504.             }
  505.             $item '
  506. <item>
  507.     <guid isPermaLink="false">' $article->getKey() . '</guid>
  508.     <pubDate>' date(DATE_RFC822strtotime($article->getArticleDate())) . '</pubDate>
  509.     <title>' $article->getTitle() . '</title>
  510.     <link>https://radio-oberland.de' $link '</link>
  511.     <description><![CDATA[' $previewText ']]></description>
  512.     ' $keywords '
  513.     <content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[' $text ']]></content:encoded>
  514.     ' . ($image '<media:content xmlns:media="http://search.yahoo.com/mrss/" url="https://radio-oberland.de' $image '" medium="image" />' '') . '
  515. </item>';
  516. //
  517. //            $d = [
  518. //                "category" => $article->getCategory(),
  519. //                "topic" => $article->getTopic(),
  520. //                "locality" => $article->getLocations(),
  521. //                "title" => $article->getTitle(),
  522. //                "text" => $article->getText(),
  523. //                "excerpt" => $article->getExerpt(),
  524. //                "image" => $image,
  525. //                "articleDate" => date("c", strtotime($article->getArticleDate())),
  526. //                "published" => $article->getPublished(),
  527. //                "highlight" => $article->getHighlight(),
  528. //                "createdAt" => date("c", $article->getCreationDate()),
  529. //                "updatedAt" => date("c", $article->getModificationDate()),
  530. //                "link" => $link
  531. //            ];
  532.             if (count($this->ortNamen)) {
  533.                 $found false;
  534.                 // Landkreise ersetzen mit den alten Tags
  535.                 $dataTopics $article->getTopic();
  536.                 if ($dataTopics && count($dataTopics) && $landkreise && count($landkreise)) {
  537.                     foreach ($landkreise as $landkreis) {
  538.                         if (isset($landkreisTags[$landkreis])) {
  539.                             foreach ($dataTopics as $topic) {
  540.                                 if (in_array($topic$landkreisTags[$landkreis])) {
  541.                                     $found true;
  542.                                 }
  543.                             }
  544.                         }
  545.                     }
  546.                 }
  547.                 if (!$found) {
  548.                     // Ortnamen direkt finden
  549.                     $dataLocations $article->getLocations();
  550.                     if ($dataLocations && count($dataLocations)) {
  551.                         foreach ($dataLocations as $locationName) {
  552.                             if ($locationName && in_array($locationName$this->ortNamen)) {
  553.                                 $found true;
  554.                             }
  555.                         }
  556.                     }
  557.                 }
  558.                 if (!$found) {
  559.                     $item null;
  560.                 }
  561.             }
  562.             if ($item) {
  563.                 $items .= $item;
  564.             }
  565.         }
  566.         $content '<?xml version="1.0" encoding="UTF-8"?>
  567. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  568. <channel>
  569. <title>Radio Oberland</title>
  570. <link>https://radio-oberland.de</link>
  571. <description>So klingt meine Heimat. Nachrichten aus Oberbayern, Garmisch-Partenkirchen, Weilheim, Bad Tölz, Wolfratshausen und Starnberg.</description>
  572. <language>de-de</language>
  573. <atom:link href="https://radio-oberland.de/api/feed" rel="self" type="application/rss+xml" />
  574. $items '
  575. </channel>
  576. </rss>';
  577.         $response = new Response(
  578.             'rss',
  579.             Response::HTTP_OK,
  580.             [
  581.                 'Content-Type' => 'application/xml; charset=utf-8'
  582.             ]
  583.         );
  584.         $response->setContent($content);
  585.         $response->setExpires(new \DateTime('+15 seconds'));
  586.         return $response;
  587.     }
  588.     /**
  589.      * returns all assigned tags for element
  590.      *
  591.      * @param string $cType
  592.      * @param int $cId
  593.      * @return Tag[]
  594.      */
  595.     public static function getTagsForElement($cType$cId)
  596.     {
  597.         $tag = new Tag();
  598.         return $tag->getDao()->getTagsForElement($cType$cId);
  599.     }
  600.     private function getAdvertisment() {
  601.         $listing = new DataObject\News\Listing();
  602.         $listing->setOrderKey("RAND()"false);
  603.         $listing->setLimit(1);
  604.         $listing->setCondition('advertisement is true');
  605.         $advertisements $listing->load();
  606.         if (count($advertisements)) {
  607.             return $this->formatArticle($advertisements[0]);
  608.         }
  609.         return null;
  610.     }
  611. }