-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from heimrichhannot/feature/custom_routes
Add custom route feature
- Loading branch information
Showing
19 changed files
with
368 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: '' | ||
labels: '' | ||
assignees: '' | ||
|
||
--- | ||
|
||
**Context** | ||
Contao version: | ||
Bundle version: | ||
PHP version: | ||
|
||
**Description** | ||
<!-- Please describe the issue and provide reproduction steps (in a clean contao installation). If you report an error, please provide the full error message (ideally a screenshot of the error page in dev mode) --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\ListWidgetBundle\Controller; | ||
|
||
use Ausi\SlugGenerator\SlugGenerator; | ||
use Ausi\SlugGenerator\SlugOptions; | ||
use Contao\Controller; | ||
use Doctrine\Common\Collections\Criteria; | ||
use HeimrichHannot\ListWidgetBundle\Widget\ListWidget; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class ListWidgetContext | ||
{ | ||
public function __construct( | ||
public readonly string $table, | ||
public readonly string $field, | ||
public readonly string $id, | ||
public readonly array $fieldConfig, | ||
public readonly Request $request, | ||
) { | ||
} | ||
|
||
public static function createFromRequest(Request $request): ListWidgetContext | ||
{ | ||
$table = $request->query->get('table'); | ||
$field = $request->query->get('scope'); | ||
|
||
if (!$table || !$field) { | ||
throw new \Exception('Missing required parameters.'); | ||
} | ||
|
||
Controller::loadDataContainer($table); | ||
|
||
$fieldConfig = $GLOBALS['TL_DCA'][$table]['fields'][$field] ?? false; | ||
|
||
if (!$fieldConfig) { | ||
throw new \Exception('Invalid field type.'); | ||
} | ||
|
||
if (ListWidget::TYPE !== $fieldConfig['inputType']) { | ||
throw new \Exception('Invalid field type.'); | ||
} | ||
|
||
$id = $request->query->get('id'); | ||
|
||
return new ListWidgetContext($table, $field, $id, $fieldConfig, $request); | ||
} | ||
|
||
public function applySearchToCriteria(Criteria $criteria, array $fields): void | ||
{ | ||
$slugger = new SlugGenerator((new SlugOptions())->setValidChars('A-Za-z0-9')); | ||
if (!empty($this->request->query->get('search')['value'])) { | ||
foreach ($fields as $field) { | ||
$criteria->orWhere(Criteria::expr()->contains($field, $slugger->generate($this->request->query->get('search')['value']))); | ||
} | ||
} | ||
} | ||
|
||
public function applyListConfigToCriteria(Criteria $criteria, array $fields): void | ||
{ | ||
$criteria->setMaxResults($this->request->query->get('length')); | ||
$criteria->setFirstResult($this->request->query->get('start', 0)); | ||
|
||
if (!empty($this->request->query->get('order'))) { | ||
foreach ($this->request->query->get('order') as $order) { | ||
$criteria->orderBy([ | ||
$fields[$order['column']] => $order['dir'], | ||
]); | ||
} | ||
} | ||
} | ||
|
||
public function adjustDataResultStructure(array &$data): void | ||
{ | ||
array_walk($data, function (&$date) { | ||
$date = array_map(fn($value) => [ | ||
'value' => $value, | ||
], array_values($date)); | ||
}); | ||
} | ||
|
||
public function createResponse(int $countTotal, int $countFiltered, array $data): ListWidgetResponse | ||
{ | ||
$this->adjustDataResultStructure($data); | ||
|
||
return new ListWidgetResponse($this->request->query->get('draw'), $countTotal, $countFiltered, $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\ListWidgetBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
class ListWidgetResponse extends JsonResponse | ||
{ | ||
public function __construct(int $draw, int $recordsTotal, int $recordsFiltered, array $data = null, ?string $error = null, int $status = 200, array $headers = [], bool $json = false) | ||
{ | ||
$data = [ | ||
'result' => [ | ||
'data' => [ | ||
'draw' => $draw, | ||
'recordsTotal' => $recordsTotal, | ||
'recordsFiltered' => $recordsFiltered, | ||
'data' => $data, | ||
], | ||
], | ||
]; | ||
|
||
if ($error) { | ||
$data['result']['error'] = $error; | ||
} | ||
|
||
parent::__construct($data, $status, $headers, $json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.