Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkp authored Feb 7, 2025
1 parent 44d0906 commit b30de07
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 21 deletions.
36 changes: 36 additions & 0 deletions module/Database/migrations/Version20250126144021.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Database\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250126144021 extends AbstractMigration
{
public function getDescription(): string
{
return 'Introduce query categories';
}

public function up(Schema $schema): void
{
// phpcs:disable SlevomatCodingStandard.Functions.RequireMultiLineCall.RequiredMultiLineCall
$this->addSql('ALTER TABLE savedquery ADD category VARCHAR(255)');
$this->addSql('UPDATE savedquery SET category = trim(split_part(name, \':\', 1)), name = trim(split_part(name, \':\', -1))');

Check warning on line 24 in module/Database/migrations/Version20250126144021.php

View workflow job for this annotation

GitHub Actions / php-codesniffer / PHP_CodeSniffer (8.3)

Line exceeds 120 characters; contains 133 characters
$this->addSql('ALTER TABLE savedquery ALTER COLUMN category SET NOT NULL');
// phpcs:enable SlevomatCodingStandard.Functions.RequireMultiLineCall.RequiredMultiLineCall
}

public function down(Schema $schema): void
{
// phpcs:disable SlevomatCodingStandard.Functions.RequireMultiLineCall.RequiredMultiLineCall
$this->addSql('UPDATE savedquery SET name = concat(category, \': \', name)');
$this->addSql('ALTER TABLE SavedQuery DROP category');
// phpcs:enable SlevomatCodingStandard.Functions.RequireMultiLineCall.RequiredMultiLineCall
}
}
6 changes: 6 additions & 0 deletions module/Database/src/Form/QueryExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Database\Form;

use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\Select;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Mvc\I18n\Translator;
Expand All @@ -14,6 +15,11 @@ public function __construct(private readonly Translator $translator)
{
parent::__construct($this->translator);

$this->add([
'name' => 'name',
'type' => Hidden::class,
]);

$this->add([
'name' => 'type',
'type' => Select::class,
Expand Down
10 changes: 9 additions & 1 deletion module/Database/src/Form/QuerySave.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public function __construct(private readonly Translator $translator)
{
parent::__construct($this->translator);

$this->add([
'name' => 'category',
'type' => Text::class,
'options' => [
'label' => $this->translator->translate('Category'),
],
]);

$this->add([
'name' => 'name',
'type' => Text::class,
Expand All @@ -29,7 +37,7 @@ public function __construct(private readonly Translator $translator)
'type' => Submit::class,
'attributes' => [
'label' => $this->translator->translate('Save'),
'value' => $this->translator->translate('Save'),
'value' => 'save',
],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion module/Database/src/Mapper/SavedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function findByName(string $name): ?SavedQueryModel
public function findAll(): array
{
$qb = $this->getRepository()->createQueryBuilder('q');
$qb->add('orderBy', 'lower(q.name) ASC');
$qb->add('orderBy', 'lower(q.category), lower(q.name) ASC');

return $qb->getQuery()->getResult();
}
Expand Down
22 changes: 22 additions & 0 deletions module/Database/src/Model/SavedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class SavedQuery
#[GeneratedValue(strategy: 'AUTO')]
protected ?int $id = null;

/**
* Category.
*/
#[Column(type: 'string')]
protected string $category;

/**
* Name.
*/
Expand Down Expand Up @@ -69,6 +75,22 @@ public function setName(string $name): void
$this->name = $name;
}

/**
* Get the category.
*/
public function getCategory(): string
{
return $this->category;
}

/**
* Set the category.
*/
public function setCategory(string $category): void
{
$this->category = $category;
}

/**
* Set the query.
*/
Expand Down
2 changes: 2 additions & 0 deletions module/Database/src/Service/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function save(array $data): ?SavedQueryModel
{
$form = $this->getQuerySaveForm();

// This is an intentional choice to find the query by name
// (we require unique names even across categories)
$query = $this->getSavedQueryMapper()->findByName($data['name']);
if (null !== $query) {
$form->bind($query);
Expand Down
42 changes: 23 additions & 19 deletions module/Database/view/database/query/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ $this->headScript()->appendFile($this->basePath() . '/js/codemirror-compressed.j
<?php
/* These are part of the QuerySave form */ ?>
<div class="form-group">
<input type="text" class="form-control input-small" name="category" placeholder="<?= $this->translate('Category') ?>">
<input type="text" class="form-control input-small" name="name" placeholder="<?= $this->translate('Name') ?>">
</div>
<button class="btn btn-success" name="submit_save" type="submit">
Expand All @@ -68,38 +69,41 @@ $this->headScript()->appendFile($this->basePath() . '/js/codemirror-compressed.j
<?php
$prefix = null;
foreach ($saved as $query):
if ($prefix !== strtoupper(explode(":", $query->getName(), 2)[0])):
$prefix = strtoupper(explode(":", $query->getName(), 2)[0]);
if ($prefix !== $query->getCategory()):
$prefix = $query->getCategory();
?>
</ul>
</li>
<li><b><?= $prefix ?></b>
<ul>
<?php endif; ?>
<li>
<a href="<?= $this->url('query/show', ['query' => $query->getId()]) ?>">
<?=
explode(
":",
$query->getName(),
2,
)[1] ?>
</a>
</li>
</ul>
</li>
<li><b><?= $prefix ? $prefix : $this->translate('No category') ?></b>
<ul>
<?php endif; ?>
<li>
<a href="<?= $this->url('query/show', ['query' => $query->getId()]) ?>">
<?= $query->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
</ul>

<?=
<p>
<?= $this->translate('Saving a query with a previously used name will overwrite the previously saved query and update its category.') ?>
</p>

<p>
<?=
sprintf(
$this->translate('For an overview of how you can use the Doctrine Query Language (DQL) please refer to %sits documentation%s.'),
'<a href="https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/dql-doctrine-query-language.html">',
'</a>',
)
?>
?>
</p>

<p>
<?= $this->translate('The entities that you can use:') ?>
</p>
<ul>
<?php foreach ($entities as $entity): ?>
<li><?= $entity; ?></li>
Expand Down

0 comments on commit b30de07

Please sign in to comment.