Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Feature Gestion Error 404 #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<loop name="rewrite_url" class="RewriteUrl\Loop\RewriteUrlLoop" />
<loop name="list-notrewritenurls" class="RewriteUrl\Loop\NotRewritenUrlCategoryLoop"/>
<loop name="rewrite_url_rule" class="RewriteUrl\Loop\RewriteUrlRuleLoop"/>
<loop name="rewrite_url_error_url" class="RewriteUrl\Loop\RewriteUrlErrorUrlLoop"/>
</loops>

<forms>
Expand Down
46 changes: 46 additions & 0 deletions Controller/Admin/ManageErrorUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Exception;
use Propel\Runtime\Exception\PropelException;
use RewriteUrl\Form\ResearchForm;
use RewriteUrl\Form\UpdateRewriteUrlForm;
use RewriteUrl\Model\RewriteurlErrorUrlQuery;
use RewriteUrl\Model\RewriteurlRule;
use RewriteUrl\Model\RewriteurlRuleQuery;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\HttpFoundation\JsonResponse;
Expand All @@ -24,6 +26,50 @@ public function manageErrorUrl(): Response|RedirectResponse
{
return $this->render('manage-errors-url');
}

#[Route('/get-rule', name: 'get_value', methods: ['GET'])]
public function getRewriteUrlRuleValue(RequestStack $requestStack): JsonResponse
{
$ruleId = $requestStack->getCurrentRequest()->query->get('rule_id');
return new JsonResponse(RewriteurlRuleQuery::create()->findOneById($ruleId)?->getRedirectUrl());
}

#[Route('/search', name: 'search', methods: ['POST'])]
public function search(ParserContext $parserContext): RedirectResponse|Response
{
$url = "/admin/module/RewriteUrl/manageErrorUrl?";
$form = $this->createForm(ResearchForm::getName());

try {
$data = $this->validateForm($form)->getData();

if (null !== $userAgent = $data['user_agent']) {
$url .= "user_agent=" . $userAgent;
}

if (null !== $data['user_agent'] && null !== $data['url_source']) {
$url .= "&";
}

if (null !== $urlSource = $data['url_source']) {
$url .= "url_source=" . $urlSource;
}

return $this->generateRedirect($url);
} catch (FormValidationException $e) {
$error_message = $this->createStandardFormValidationErrorMessage($e);
} catch (Exception $e) {
$error_message = $e->getMessage();
}

$form->setErrorMessage($error_message);

$parserContext
->addForm($form)
->setGeneralError($error_message);

return $this->generateErrorRedirect($form);
}

#[Route('/update/{id}', name: 'update', methods: ['POST'])]
public function updateRewriteUrl(ParserContext $parserContext, $id): Response|RedirectResponse
Expand Down
24 changes: 24 additions & 0 deletions Form/ResearchForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RewriteUrl\Form;

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Thelia\Core\Translation\Translator;
use Thelia\Form\BaseForm;

class ResearchForm extends BaseForm
{
protected function buildForm(): void
{
$this->formBuilder
->add('user_agent', TextType::class, [
'required' => false,
'label' => Translator::getInstance()?->trans('User Agent')
])
->add('url_source', TextType::class, [
'required' => false,
'label' => Translator::getInstance()?->trans('URL Source')
])
;
}
}
82 changes: 82 additions & 0 deletions Loop/RewriteUrlErrorUrlLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace RewriteUrl\Loop;

use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\Exception\PropelException;
use RewriteUrl\Model\RewriteurlErrorUrl;
use RewriteUrl\Model\RewriteurlErrorUrlQuery;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;

/**
* @method getId()
* @method getUserAgent()
* @method getUrlSource()
* @method getOrders()
*/
class RewriteUrlErrorUrlLoop extends BaseLoop implements PropelSearchLoopInterface
{
/**
* @throws PropelException
*/
public function parseResults(LoopResult $loopResult): LoopResult
{
/** @var RewriteurlErrorUrl $rewriteUrlErrorUrl */
foreach ($loopResult->getResultDataCollection() as $rewriteUrlErrorUrl) {
$loopResultRow = new LoopResultRow($rewriteUrlErrorUrl);

$loopResultRow->set('ID', $rewriteUrlErrorUrl->getId())
->set('URL_SOURCE', $rewriteUrlErrorUrl->getUrlSource())
->set('COUNT', $rewriteUrlErrorUrl->getCount())
->set('USER_AGENT', $rewriteUrlErrorUrl->getUserAgent())
->set('REWRITEURL_RULE_ID', $rewriteUrlErrorUrl->getRewriteurlRuleId())
->set('REDIRECT_URL', $rewriteUrlErrorUrl->getRewriteurlRule()?->getRedirectUrl())
->set('CREATED_AT', $rewriteUrlErrorUrl->getCreatedAt())
->set('UPDATED_AT', $rewriteUrlErrorUrl->getUpdatedAt());
$this->addOutputFields($loopResultRow, $rewriteUrlErrorUrl);

$loopResult->addRow($loopResultRow);
}

return $loopResult;
}

public function buildModelCriteria(): ModelCriteria
{
$search = RewriteUrlErrorUrlQuery::create();

if (null !== $id = $this->getId()) {
$search->filterById($id);
}

if (null !== $userAgent = $this->getUserAgent()) {
$search->filterByUserAgent($userAgent);
}

if (null !== $userSource = $this->getUrlSource()) {
$search->filterByUrlSource($userSource);
}

if (null !== $orders = $this->getOrders()) {
[$order, $criteria] = explode(":", $orders);
$search->orderBy($order, $criteria);
}

return $search;
}

protected function getArgDefinitions(): ArgumentCollection
{
return new ArgumentCollection(
Argument::createIntTypeArgument('id'),
Argument::createAnyTypeArgument('user_agent'),
Argument::createAnyTypeArgument('url_source'),
Argument::createAnyTypeArgument('orders'),
);
}
}
Loading