src/Controller/WikiController.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\RedirectService;
  4. use Exception;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Model\DataObject\Press\Listing;
  8. use Pimcore\Model\DataObject\Wiki;
  9. use Pimcore\Model\Document\Page;
  10. use Pimcore\Model\Redirect\Listing as RedirectListing;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Webmozart\Assert\Assert;
  18. class WikiController extends FrontendController
  19. {
  20. /**
  21. * @Route ("/wiki/{searchTerm}", name="wiki_search")
  22. * @Template("wiki/index.html.twig")
  23. * @param Request $request
  24. * @return array
  25. */
  26. public function indexAction(Request $request, PaginatorInterface $paginator, $searchTerm = null)
  27. {
  28. $wikiEntries = $this->getWikiArticlesByParameters($searchTerm);
  29. return [
  30. 'index' => $wikiEntries,
  31. 'searchTerm' => $searchTerm,
  32. ];
  33. }
  34. /**
  35. * @Route ("{underworld}/wissen/wiki/{searchTerm}", name="wiki_underworld_search")
  36. * @Template("wiki/index.html.twig")
  37. * @param Request $request
  38. * @return array
  39. */
  40. public function wikiUnderworldIndexAction(Request $request, PaginatorInterface $paginator, $searchTerm = null): array
  41. {
  42. $subworld = $this->document->getProperty("subworld");
  43. $wikiEntries = $this->getWikiArticlesByParameters($searchTerm, $subworld);
  44. return [
  45. 'index' => $wikiEntries,
  46. 'searchTerm' => $searchTerm,
  47. ];
  48. }
  49. private function getWikiArticlesByParameters($searchTerm = null, $subworld = null)
  50. {
  51. $wikiEntries = Wiki::getList([
  52. 'orderKey' => ["title"],
  53. 'order' => 'asc',
  54. ]);
  55. if ($searchTerm == "0-9") {
  56. $wikiEntries->setCondition('title regexp ?', ["^[0-9]+"]);
  57. } elseif ($searchTerm && strlen($searchTerm) == 1) {
  58. $wikiEntries->setCondition('title LIKE ?', ["$searchTerm%"]);
  59. } elseif ($searchTerm && strlen($searchTerm) > 1) {
  60. $wikiEntries->setCondition('title LIKE ?', ["%$searchTerm%"]);
  61. } elseif ($searchTerm) {
  62. $wikiEntries->setCondition('title LIKE ?', ["a%"]);
  63. }
  64. if ($subworld && $subworld != "main") {
  65. $wikiEntries->filterByUnderworld($subworld);
  66. }
  67. if (!$subworld) {
  68. $wikiEntries->addConditionParam("(underworld IS NULL OR underworld = '')");
  69. }
  70. return $wikiEntries;
  71. }
  72. /**
  73. * @Route ("/wissen/wiki/{slug}", name="wiki_detail")
  74. * @Template("wiki/detail.html.twig")
  75. * @param Request $request
  76. */
  77. public function detailAction(Request $request, RedirectService $redirectService, $slug): array|Response
  78. {
  79. $redirect410Response = $redirectService->check410Redirect($request);
  80. if ($redirect410Response) {
  81. return $redirect410Response;
  82. }
  83. $entry = $this->getWikiArticleBySlug($slug);
  84. if ($entry instanceof Response) {
  85. return $entry;
  86. }
  87. if (!$entry) {
  88. throw $this->createNotFoundException('Dieser Wikiartikel existiert nicht');
  89. }
  90. if ($entry->hasProperty("underworld") && $entry->getUnderworld()) {
  91. if ($this->mapUnderworldURL($entry->getUnderworld())) {
  92. $redirectUrl = $this->mapUnderworldURL($entry->getUnderworld()) . "/wissen/" . $slug;
  93. return new RedirectResponse($redirectUrl, 301);
  94. }
  95. }
  96. return [
  97. 'wiki' => $entry
  98. ];
  99. }
  100. /**
  101. * @Route ("/wissen/wiki/preview-latest/{slug}", name="wiki_detail_preview")
  102. * @Template("wiki/detail.html.twig")
  103. * @param Request $request
  104. */
  105. public function detailPreviewAction(Request $request, $slug): array|Response
  106. {
  107. $entry = $this->getWikiArticleBySlug($slug);
  108. if ($entry instanceof Response) {
  109. return $entry;
  110. }
  111. if (!$entry) {
  112. throw $this->createNotFoundException('Dieser Wikiartikel existiert nicht');
  113. }
  114. if ($entry->getUnderworld()) {
  115. if ($this->mapUnderworldURL($entry->getUnderworld())) {
  116. $redirectUrl = $this->mapUnderworldURL($entry->getUnderworld()) . "/wissen/preview-latest/" . $slug;
  117. return new RedirectResponse($redirectUrl, 301);
  118. }
  119. }
  120. $versions = $entry->getVersions();
  121. if (empty($versions)) {
  122. return [
  123. 'wiki' => $entry
  124. ];
  125. }
  126. $latestVersion = $versions[count($versions)-1];
  127. $latestVersion = $latestVersion->getData();
  128. return [
  129. 'wiki' => $latestVersion
  130. ];
  131. }
  132. /**
  133. * @Route ("{underworld}/wissen/preview-latest/{slug}", name="wiki_underworld_detail_preview")
  134. * @Template("wiki/detail.html.twig")
  135. * @param Request $request
  136. */
  137. public function wikiUnderworldDetailPreviewAction(Request $request, $slug): array|Response|Page
  138. {
  139. $entry = $this->getWikiArticleBySlug($slug);
  140. if ($entry instanceof Response) {
  141. return $entry;
  142. }
  143. if (!$entry) {
  144. $document = \Pimcore\Model\Document::getByPath($request->getRequestUri());
  145. if ($document && $document->getTemplate()) {
  146. return $this->render($document->getTemplate(), $document->getObjectVars());
  147. }
  148. }
  149. if (!$entry) {
  150. throw $this->createNotFoundException('Dieser Wikiartikel existiert nicht');
  151. }
  152. $versions = $entry->getVersions();
  153. if (empty($versions)) {
  154. return [
  155. 'wiki' => $entry
  156. ];
  157. }
  158. $latestVersion = $versions[count($versions)-1];
  159. $latestVersion = $latestVersion->getData();
  160. return [
  161. 'wiki' => $latestVersion
  162. ];
  163. }
  164. /**
  165. * @Route ("{underworld}/wissen/{slug}", name="wiki_underworld_detail")
  166. * @Template("wiki/detail.html.twig")
  167. * @param Request $request
  168. */
  169. public function wikiUnderworldDetailAction(Request $request, $slug): array|Response|Page
  170. {
  171. $entry = $this->getWikiArticleBySlug($slug);
  172. if ($entry instanceof Response) {
  173. return $entry;
  174. }
  175. if (!$entry) {
  176. $document = \Pimcore\Model\Document::getByPath($request->getRequestUri());
  177. if ($document && $document->getTemplate()) {
  178. return $this->render($document->getTemplate(), $document->getObjectVars());
  179. }
  180. }
  181. if (!$entry) {
  182. throw $this->createNotFoundException('Dieser Wikiartikel existiert nicht');
  183. }
  184. return [
  185. 'wiki' => $entry
  186. ];
  187. }
  188. private function getWikiArticleBySlug($slug)
  189. {
  190. $entry = Wiki::getBySlugDe($slug, 1);
  191. if (!$entry) {
  192. $entry = Wiki::getById($slug);
  193. }
  194. if (!$entry) {
  195. $entry = Wiki::getBySlugDe($slug, ['limit' => 1, 'unpublished' => true]);
  196. if ($entry && $entry->getRemoved()) {
  197. return new Response("", 410);
  198. }
  199. }
  200. return $entry;
  201. }
  202. private function mapUnderworldURL($shortname)
  203. {
  204. $underworldMappingArray = [
  205. "chm" => "/change-management",
  206. 'nw' => '/new-work',
  207. 'agile' => '/agilitaet',
  208. 'fup' => '/fuehrung-und-performance',
  209. 'nac' => '/nachhaltigkeit',
  210. 'ges' => '/prozessoptimierung',
  211. 'sui' => '/strategie-und-innovation',
  212. 'prm' => '/projektmanagement',
  213. 'tur' => '/turnaround-und-restrukturierung',
  214. 'vem' => '/vertriebsmanagement'
  215. ];
  216. return $underworldMappingArray[$shortname] ?? false;
  217. }
  218. /**
  219. * Funktion zum Unveröffentlichen/Inaktiv-setzen von Wiki-Artikeln aus einer in Unpublish-CSV/file.csv abgelegten CSV.
  220. * @Route ("admin/wiki/unpublish-articles-from-csv", name="wiki_unpublish-articles_from_csv")
  221. * @return JsonResponse
  222. * @throws Exception
  223. */
  224. public function unpublishArticlesFromCSV()
  225. {
  226. $rowNum = 0;
  227. $failedArticles = [];
  228. $successArticles = [];
  229. if (($fp = fopen("var/assets/Unpublish-CSV/offline.csv", "r")) !== FALSE) {
  230. while (($row = fgetcsv($fp, 1000, ";")) !== FALSE) {
  231. $slugFromUrl = $row[0];
  232. $rowNum++;
  233. $entry = Wiki::getBySlugDe($slugFromUrl, 1);
  234. if (!$entry) {
  235. $entry = Wiki::getById($slugFromUrl);
  236. }
  237. if (!$entry) {
  238. $failedArticles[] = $slugFromUrl;
  239. }
  240. if ($entry) {
  241. $entry?->setPublished(false);
  242. $entry?->setActive(false);
  243. $entry?->setRemoved(true);
  244. $entry?->save();
  245. $successArticles[] = $slugFromUrl;
  246. }
  247. }
  248. fclose($fp);
  249. }
  250. return new JsonResponse(["erfolgreiche Artikel" => $successArticles, "fehlgeschlagene/nicht gefundene Artikel" => $failedArticles]);
  251. }
  252. /**
  253. * Funktion zum Auslösen des Slug-Generator Event-Listeners.
  254. * @Route ("admin/wiki/create-slugs", name="wiki_create_slugs")
  255. * @return JsonResponse
  256. * @throws Exception
  257. */
  258. public function initializeWikiAndPressSlugCreation()
  259. {
  260. $pressArticles = new Listing();
  261. $wikiArticles = new Wiki\Listing();
  262. foreach ($pressArticles as $pressArticle) {
  263. $pressArticle->save();
  264. }
  265. foreach ($wikiArticles as $wikiArticle) {
  266. $wikiArticle->save();
  267. }
  268. return new JsonResponse(["success" => true]);
  269. }
  270. /**
  271. * Funktion zum Ersetzen von h3s zu h2s
  272. * @Route ("admin/wiki/replace-h3s", name="wiki_replace_h3s")
  273. * @return JsonResponse
  274. * @throws Exception
  275. */
  276. public function replaceWikiH3s()
  277. {
  278. $wikiArticles = new Wiki\Listing();
  279. foreach ($wikiArticles as $wikiArticle) {
  280. $wikiArticle->setText(str_replace(array('<h3>', '</h3>'), array('<h2>', '</h2>'), $wikiArticle->getText('de')), 'de');
  281. $wikiArticle->setText(str_replace(array('<h3>', '</h3>'), array('<h2>', '</h2>'), $wikiArticle->getText('en')), 'en');
  282. $wikiArticle->setText(str_replace(array('<a'), array('<a class="text-blue-600 font-semibold"'), $wikiArticle->getText('de')), 'de');
  283. $wikiArticle->setText(str_replace(array('<a'), array('<a class="text-blue-600 font-semibold"'), $wikiArticle->getText('en')), 'en');
  284. $wikiArticle->save();
  285. }
  286. return new JsonResponse(["success" => true]);
  287. }
  288. }