2021-09-14 13:40:50 +01:00
< ? php
2021-10-10 09:26:18 +01:00
declare ( strict_types = 1 );
2021-09-14 13:40:50 +01:00
namespace Component\Tag\Controller ;
2021-09-20 16:17:13 +01:00
use App\Core\Cache ;
2021-09-14 13:40:50 +01:00
use App\Core\Controller ;
2021-09-21 16:39:36 +01:00
use App\Util\Common ;
2022-01-04 22:20:12 +00:00
use Component\Language\Entity\Language ;
2021-09-20 16:17:13 +01:00
use Component\Tag\Tag as CompTag ;
2021-09-14 13:40:50 +01:00
class Tag extends Controller
{
2022-01-04 22:20:12 +00:00
// TODO: Use Feed::query
// TODO: If ?canonical=something, respect
// TODO: Allow to set locale of tag being selected
private function process ( null | string | array $tag_single_or_multi , string $key , string $query , string $template , bool $include_locale = false )
2021-09-14 13:40:50 +01:00
{
2021-11-24 15:51:01 +00:00
$actor = Common :: actor ();
$page = $this -> int ( 'page' ) ? : 1 ;
2022-01-04 22:20:12 +00:00
$query_args = [ 'tag' => $tag_single_or_multi ];
if ( $include_locale ) {
if ( ! \is_null ( $locale = $this -> string ( 'locale' ))) {
$query_args [ 'language_id' ] = Language :: getByLocale ( $locale ) -> getId ();
} else {
$query_args [ 'language_id' ] = Common :: actor () -> getTopLanguage () -> getId ();
}
}
2021-12-07 19:53:56 +00:00
2021-11-28 13:09:04 +00:00
$results = Cache :: pagedStream (
2021-12-07 19:53:56 +00:00
key : $key ,
2021-11-26 11:48:35 +00:00
query : $query ,
2022-01-04 22:20:12 +00:00
query_args : $query_args ,
2021-11-24 15:51:01 +00:00
actor : $actor ,
2021-10-10 09:26:18 +01:00
page : $page ,
2021-09-20 16:17:13 +01:00
);
return [
2021-11-28 13:09:04 +00:00
'_template' => $template ,
2021-12-07 19:53:56 +00:00
'tag_name' => $tag_single_or_multi ,
2021-11-28 13:09:04 +00:00
'results' => $results ,
2021-09-20 16:17:13 +01:00
'page' => $page ,
];
2021-09-14 13:40:50 +01:00
}
2021-11-26 11:48:35 +00:00
2022-01-04 22:20:12 +00:00
public function single_note_tag ( string $tag )
2021-11-26 11:48:35 +00:00
{
return $this -> process (
2022-01-04 22:20:12 +00:00
tag_single_or_multi : $tag ,
key : CompTag :: cacheKeys ( $tag )[ 'note_single' ],
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' ,
2021-11-28 13:09:04 +00:00
template : 'note_tag_feed.html.twig' ,
2022-01-04 22:20:12 +00:00
include_locale : true ,
2021-11-26 11:48:35 +00:00
);
}
2022-01-04 22:20:12 +00:00
public function multi_note_tags ( string $tags )
2021-11-26 11:48:35 +00:00
{
return $this -> process (
2022-01-04 22:20:12 +00:00
tag_single_or_multi : explode ( ',' , $tags ),
key : CompTag :: cacheKeys ( str_replace ( ',' , '-' , $tags ))[ 'note_multi' ],
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' ,
2021-11-28 13:09:04 +00:00
template : 'note_tag_feed.html.twig' ,
2022-01-04 22:20:12 +00:00
include_locale : true ,
2021-11-28 13:09:04 +00:00
);
}
2021-09-14 13:40:50 +01:00
}