[COMPONENT][Search][LeftPanel] Add way of adding a search result as a left panel feed

This commit is contained in:
Hugo Sales 2021-12-11 10:48:08 +00:00
parent 9afe6ecfac
commit dbc8bf2ae1
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 80 additions and 11 deletions

View File

@ -21,9 +21,16 @@ declare(strict_types = 1);
namespace Component\LeftPanel;
use App\Core\Cache;
use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\Component;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
use App\Entity\Actor;
use App\Entity\Feed;
use App\Util\Exception\ClientException;
use Component\LeftPanel\Controller as C;
class LeftPanel extends Component
@ -34,6 +41,27 @@ class LeftPanel extends Component
return Event::next;
}
public function onAppendFeed(Actor $actor, string $title, string $route, array $route_params)
{
$cache_key = Feed::cacheKey($actor);
$feeds = Feed::getFeeds($actor);
$ordering = end($feeds)->getOrdering();
$url = Router::url($route, $route_params);
if (DB::count('feed', ['actor_id' => $actor->getId(), 'url' => $url]) === 0) {
DB::persist(Feed::create([
'actor_id' => $actor->getId(),
'url' => $url,
'route' => $route,
'title' => $title,
'ordering' => $ordering + 1,
]));
DB::flush();
Cache::delete($cache_key);
return Event::stop;
}
throw new ClientException(_m('Cannot add feed with url "{url}" because it already exists', ['{url}' => $url]));
}
/**
* Output our dedicated stylesheet
*

View File

@ -92,7 +92,7 @@ class Search extends FeedController
$query[] = "{$key}:{$langs}";
}
} else {
throw new BugFoundException('Search form seems to have new fields the code did not expect');
throw new BugFoundException('Search builder form seems to have new fields the code did not expect');
}
}
}
@ -103,7 +103,7 @@ class Search extends FeedController
return [
'_template' => 'search/show.html.twig',
'search_form' => Comp\Search::searchForm($request, $q),
'search_form' => Comp\Search::searchForm($request, query: $q, add_subscribe: true),
'search_builder_form' => $search_builder_form->createView(),
'notes' => $notes,
'actors' => $actors,

View File

@ -27,10 +27,12 @@ use App\Core\Event;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Modules\Component;
use App\Util\Common;
use App\Util\Exception\RedirectException;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request;
class Search extends Component
@ -40,28 +42,67 @@ class Search extends Component
$r->connect('search', '/search', Controller\Search::class);
}
public static function searchForm(Request $request, ?string $query = null): FormView
public static function searchForm(Request $request, ?string $query = null, bool $add_subscribe = false): FormView
{
$form = Form::create([
$actor = Common::actor();
if (\is_null($actor)) {
$add_subscribe = false;
}
$form_definition = [
['search_query', TextType::class, [
'attr' => ['placeholder' => _m('Input desired query...'), 'value' => $query],
]],
[$form_name = 'submit_search', SubmitType::class,
];
if ($add_subscribe) {
$form_definition[] = [
'title', TextType::class, ['label' => _m('Title'), 'attr' => ['title' => _m('Title for this new feed in your left panel')]],
];
$form_definition[] = [
'subscribe_to_search',
SubmitType::class,
[
'label' => _m('Search'),
'label' => _m('Subscribe to this search'),
'attr' => [
//'class' => 'button-container search-button-container',
'title' => _m('Query notes for specific tags.'),
'title' => _m('Add this search as a feed in your feeds section of the left panel'),
],
],
];
}
$form_definition[] = [
$form_name = 'submit_search',
SubmitType::class,
[
'label' => _m('Search'),
'attr' => [
//'class' => 'button-container search-button-container',
'title' => _m('Query notes for specific tags.'),
],
],
]);
];
$form = Form::create($form_definition);
if ('POST' === $request->getMethod() && $request->request->has($form_name)) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
throw new RedirectException('search', ['q' => $data['search_query']]);
$data = $form->getData();
$redirect = false;
if ($add_subscribe) {
/** @var SubmitButton $subscribe */
$subscribe = $form->get('subscribe_to_search');
if ($subscribe->isClicked()) {
Event::handle('AppendFeed', [$actor, $data['title'], 'search', ['q' => $data['search_query']]]);
$redirect = true;
}
}
/** @var SubmitButton $submit */
$submit = $form->get($form_name);
if ($submit->isClicked() || $redirect) {
throw new RedirectException('search', ['q' => $data['search_query']]);
}
}
}
return $form->createView();