<?php
namespace App\Controller;
use App\Model\Scan\Report;
use App\Entity\Scan;
use App\Model\Tag;
use DateTime;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Alerts Controller.
*/
class SearchController extends AbstractController
{
const DATETIME_FORMAT = 'Y-m-d';
const REQUEST_PARAM_NAME_AFFILIATE = 'affiliate';
const REQUEST_PARAM_NAME_CAMPAIGN_ID = 'campaignId';
const REQUEST_PARAM_NAME_DOMAINS = 'domain';
const REQUEST_PARAM_NAME_LANDING_DOMAIN = 'landingDomain';
const REQUEST_PARAM_NAME_TRAFFIC_SOURCE = 'trafficSource';
const REQUEST_PARAM_NAME_STATUS = 'status';
const REQUEST_PARAM_NAME_TAGS = 'tags';
const REQUEST_PARAM_NAME_FIRST_TRACKING_LINK = 'firstTrackingLink';
const ADVANCED_SEARCH_LABELS = [
self::REQUEST_PARAM_NAME_AFFILIATE => 'Affiliate',
self::REQUEST_PARAM_NAME_CAMPAIGN_ID => 'Campaign ID',
self::REQUEST_PARAM_NAME_DOMAINS => 'Domain',
self::REQUEST_PARAM_NAME_LANDING_DOMAIN => 'Landing Domain',
self::REQUEST_PARAM_NAME_TRAFFIC_SOURCE => 'Source',
self::REQUEST_PARAM_NAME_STATUS => 'Status',
self::REQUEST_PARAM_NAME_TAGS => 'Tags',
self::REQUEST_PARAM_NAME_FIRST_TRACKING_LINK => 'First Tracking Link',
];
const REQUEST_ADVANCED_SEARCH_PARAMS = array(
self::REQUEST_PARAM_NAME_AFFILIATE => self::REQUEST_PARAM_NAME_AFFILIATE,
self::REQUEST_PARAM_NAME_CAMPAIGN_ID => self::REQUEST_PARAM_NAME_CAMPAIGN_ID,
self::REQUEST_PARAM_NAME_DOMAINS => self::REQUEST_PARAM_NAME_DOMAINS,
self::REQUEST_PARAM_NAME_LANDING_DOMAIN => self::REQUEST_PARAM_NAME_LANDING_DOMAIN,
self::REQUEST_PARAM_NAME_TRAFFIC_SOURCE => self::REQUEST_PARAM_NAME_TRAFFIC_SOURCE,
self::REQUEST_PARAM_NAME_STATUS => self::REQUEST_PARAM_NAME_STATUS,
self::REQUEST_PARAM_NAME_TAGS => self::REQUEST_PARAM_NAME_TAGS,
self::REQUEST_PARAM_NAME_FIRST_TRACKING_LINK => self::REQUEST_PARAM_NAME_FIRST_TRACKING_LINK,
);
/**
* Search Index.
*
* @Route(path="/search", name="search_index", methods={"GET"})
*
* @return Response
*/
public function indexAction(): Response
{
return $this->render('search/list.html.twig', [
'statusLabels' => Scan::SEARCH_LABELS,
'trafficType' => Scan::FLAG_TRAFFIC_LABELS,
'advancedSearchLabels' => self::ADVANCED_SEARCH_LABELS,
'requestParams' => self::REQUEST_ADVANCED_SEARCH_PARAMS,
'tagTypes' => Tag::TYPE_LABELS,
]);
}
/**
* Gets Search list
*
* @Route(path="/search/list", name="search_list", methods={"GET"})
*
* @param Request $request
* @param Report $report
* @return JsonResponse
* @throws EntityNotFoundException
*/
public function listAction(Request $request, Report $report): JsonResponse
{
$from = $request->query->get('from', date("Y-m-d"));
$to = $request->query->get('to', date("Y-m-d"));
$searchKeyWord = $request->query->get('search', '');
$scanStatus = $request->query->get(self::REQUEST_PARAM_NAME_STATUS, '');
$trafficType = $request->query->get(self::REQUEST_PARAM_NAME_TRAFFIC_SOURCE, '');
$affiliates = $request->query->get(self::REQUEST_PARAM_NAME_AFFILIATE, '');
$campaignId = $request->query->get(self::REQUEST_PARAM_NAME_CAMPAIGN_ID, '');
$domain = $request->query->get(self::REQUEST_PARAM_NAME_DOMAINS, '');
$landingDomain = $request->query->get(self::REQUEST_PARAM_NAME_LANDING_DOMAIN, '');
$tags = $request->query->get(self::REQUEST_PARAM_NAME_TAGS, '');
$firstTrackingLink = $request->query->get(self::REQUEST_PARAM_NAME_FIRST_TRACKING_LINK, '');
// case filter scan status None
$statusFilter = [];
if (($scanStatus || $scanStatus === (string) Scan::STATUS_LANDING_ISSUE) && $scanStatus !== 'None') {
$statusFilter = explode(',', $scanStatus);
}
$trafficFilter = [];
if ($trafficType && $trafficType !== 'None') {
$trafficFilter = explode(',', $trafficType);
}
$affiliateFilter = [];
if ($affiliates) {
$affiliateFilter = explode('(||)', $affiliates);
}
$campaignIdFilter = [];
if ($campaignId) {
$campaignIdFilter = explode('(||)', $campaignId);
}
$domainFilter = [];
if ($domain) {
$domainFilter = explode('(||)', $domain);
}
$landingDomainFilter = [];
if ($landingDomain) {
$landingDomainFilter = explode('(||)', $landingDomain);
}
$tagsFilter = [];
if ($tags && $tags !== 'None') {
$tagsFilter = explode(',', $tags);
}
if ($firstTrackingLink && $firstTrackingLink !== 'True') {
$firstTrackingLink = '';
}
// if there are no filters provided return empty json
if (!($statusFilter || $trafficFilter || $affiliateFilter || $campaignIdFilter || $domainFilter
|| $landingDomainFilter || $searchKeyWord || $tags)) {
return $this->json([]);
}
$from = DateTime::createFromFormat(self::DATETIME_FORMAT, $from);
$to = DateTime::createFromFormat(self::DATETIME_FORMAT, $to);
$scans = $report->getScansForSearch($from, $to, $statusFilter, $trafficFilter, $affiliateFilter, $campaignIdFilter, $domainFilter, $landingDomainFilter, $tagsFilter, $searchKeyWord, !empty($firstTrackingLink));
return $this->json($scans);
}
}