Tag.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\Tag\Controller;
  4. use App\Core\Cache;
  5. use App\Core\Controller;
  6. use App\Util\Common;
  7. use Component\Language\Entity\Language;
  8. use Component\Tag\Tag as CompTag;
  9. class Tag extends Controller
  10. {
  11. // TODO: Use Feed::query
  12. // TODO: If ?canonical=something, respect
  13. // TODO: Allow to set locale of tag being selected
  14. private function process(null|string|array $tag_single_or_multi, string $key, string $query, string $template, bool $include_locale = false)
  15. {
  16. $actor = Common::actor();
  17. $page = $this->int('page') ?: 1;
  18. $query_args = ['tag' => $tag_single_or_multi];
  19. if ($include_locale) {
  20. if (!\is_null($locale = $this->string('locale'))) {
  21. $query_args['language_id'] = Language::getByLocale($locale)->getId();
  22. } else {
  23. $query_args['language_id'] = Common::actor()->getTopLanguage()->getId();
  24. }
  25. }
  26. $results = Cache::pagedStream(
  27. key: $key,
  28. query: $query,
  29. query_args: $query_args,
  30. actor: $actor,
  31. page: $page,
  32. );
  33. return [
  34. '_template' => $template,
  35. 'tag_name' => $tag_single_or_multi,
  36. 'results' => $results,
  37. 'page' => $page,
  38. ];
  39. }
  40. public function single_note_tag(string $tag)
  41. {
  42. return $this->process(
  43. tag_single_or_multi: $tag,
  44. key: CompTag::cacheKeys($tag)['note_single'],
  45. query: 'SELECT n FROM note AS n JOIN note_tag AS nt WITH n.id = nt.note_id WHERE nt.tag = :tag AND nt.language_id = :language_id ORDER BY nt.created DESC, nt.note_id DESC',
  46. template: 'note_tag_feed.html.twig',
  47. include_locale: true,
  48. );
  49. }
  50. public function multi_note_tags(string $tags)
  51. {
  52. return $this->process(
  53. tag_single_or_multi: explode(',', $tags),
  54. key: CompTag::cacheKeys(str_replace(',', '-', $tags))['note_multi'],
  55. query: 'select n from note n join note_tag nt with n.id = nt.note_id where nt.tag in (:tag) AND nt.language_id = :language_id order by nt.created DESC, nt.note_id DESC',
  56. template: 'note_tag_feed.html.twig',
  57. include_locale: true,
  58. );
  59. }
  60. }