Files
survey-bundle/src/Repository/SurveyConditionRepository.php
T
Jürgen Mummert 5f9a77d5bd Set tstamp on record inserts/updates
Questions, editor assignments and conditions were inserted without tstamp,
so Contao rendered them with tstamp=0 -> the backend "draft" styling
(opacity .5), making question rows look greyed out despite being published.

- SurveyQuestionRepository::insert()/update() (covers create, edit, duplicate)
- SurveyEditorRepository::insert()
- SurveyConditionRepository::insert()
SurveyRepository already set tstamp. Also widened the assigned-members
checkbox-wizard columns at smaller breakpoints.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 22:31:25 +02:00

83 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Mummert\SurveyBundle\Repository;
use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\DBAL\Connection;
use Mummert\SurveyBundle\Model\SurveyConditionModel;
final class SurveyConditionRepository
{
public function __construct(
private readonly Connection $connection,
private readonly ContaoFramework $framework,
) {
}
public function findById(int $id): ?SurveyConditionModel
{
$adapter = $this->framework->getAdapter(SurveyConditionModel::class);
$model = $adapter->findByPk($id);
return $model instanceof SurveyConditionModel ? $model : null;
}
public function findMatchingCondition(int $surveyId, int $sourceQuestionId, string $answerValue): ?SurveyConditionModel
{
$id = $this->connection->fetchOne(
'SELECT id FROM tl_survey_condition WHERE pid = ? AND sourceQuestion = ? AND LOWER(TRIM(answerValue)) = LOWER(TRIM(?)) ORDER BY id ASC LIMIT 1',
[$surveyId, $sourceQuestionId, $answerValue],
);
return false === $id ? null : $this->findById((int) $id);
}
/**
* @return list<array<string, mixed>>
*/
public function findOverviewBySurvey(int $surveyId): array
{
return $this->connection->fetchAllAssociative(
<<<'SQL'
SELECT
c.id,
c.answerValue,
c.sourceQuestion,
c.targetQuestion,
source.question AS sourceQuestionLabel,
target.question AS targetQuestionLabel
FROM tl_survey_condition c
LEFT JOIN tl_survey_content source ON source.id = c.sourceQuestion
LEFT JOIN tl_survey_content target ON target.id = c.targetQuestion
WHERE c.pid = :survey
ORDER BY source.sorting ASC, c.id ASC
SQL,
['survey' => $surveyId],
);
}
public function create(int $surveyId, array $data): SurveyConditionModel
{
$this->connection->insert('tl_survey_condition', [
'pid' => $surveyId,
'tstamp' => time(),
'sourceQuestion' => (int) ($data['sourceQuestion'] ?? 0),
'answerValue' => mb_strtolower(trim((string) ($data['answerValue'] ?? ''))),
'targetQuestion' => (int) ($data['targetQuestion'] ?? 0),
]);
$id = (int) $this->connection->lastInsertId();
return $this->findById($id) ?? throw new \RuntimeException('Bedingung konnte nicht angelegt werden.');
}
public function delete(int $surveyId, int $conditionId): void
{
$this->connection->delete('tl_survey_condition', [
'id' => $conditionId,
'pid' => $surveyId,
]);
}
}