2021-09-14 21:40:50 +09:00
< ? php
2021-10-10 17:26:18 +09:00
declare ( strict_types = 1 );
2021-09-14 21:40:50 +09:00
namespace Component\Tag\Controller ;
2021-09-21 00:17:13 +09:00
use App\Core\Cache ;
2021-09-14 21:40:50 +09:00
use App\Core\Controller ;
2021-09-22 00:39:36 +09:00
use App\Util\Common ;
2022-01-05 07:20:12 +09:00
use Component\Language\Entity\Language ;
2021-09-21 00:17:13 +09:00
use Component\Tag\Tag as CompTag ;
2021-09-14 21:40:50 +09:00
class Tag extends Controller
{
2022-01-05 07:20:12 +09: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 21:40:50 +09:00
{
2021-11-25 00:51:01 +09:00
$actor = Common :: actor ();
$page = $this -> int ( 'page' ) ? : 1 ;
2022-01-05 07:20:12 +09: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-08 04:53:56 +09:00
2021-11-28 22:09:04 +09:00
$results = Cache :: pagedStream (
2021-12-08 04:53:56 +09:00
key : $key ,
2021-11-26 20:48:35 +09:00
query : $query ,
2022-01-05 07:20:12 +09:00
query_args : $query_args ,
2021-11-25 00:51:01 +09:00
actor : $actor ,
2021-10-10 17:26:18 +09:00
page : $page ,
2021-09-21 00:17:13 +09:00
);
return [
2021-11-28 22:09:04 +09:00
'_template' => $template ,
2021-12-08 04:53:56 +09:00
'tag_name' => $tag_single_or_multi ,
2021-11-28 22:09:04 +09:00
'results' => $results ,
2021-09-21 00:17:13 +09:00
'page' => $page ,
];
2021-09-14 21:40:50 +09:00
}
2021-11-26 20:48:35 +09:00
2022-01-05 07:20:12 +09:00
public function single_note_tag ( string $tag )
2021-11-26 20:48:35 +09:00
{
return $this -> process (
2022-01-05 07:20:12 +09: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 22:09:04 +09:00
template : 'note_tag_feed.html.twig' ,
2022-01-05 07:20:12 +09:00
include_locale : true ,
2021-11-26 20:48:35 +09:00
);
}
2022-01-05 07:20:12 +09:00
public function multi_note_tags ( string $tags )
2021-11-26 20:48:35 +09:00
{
return $this -> process (
2022-01-05 07:20:12 +09: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 22:09:04 +09:00
template : 'note_tag_feed.html.twig' ,
2022-01-05 07:20:12 +09:00
include_locale : true ,
2021-11-28 22:09:04 +09:00
);
}
2021-09-14 21:40:50 +09:00
}