Initial survey bundle import
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?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,
|
||||
'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 update(SurveyConditionModel $condition, array $data): void
|
||||
{
|
||||
$this->connection->update('tl_survey_condition', [
|
||||
'sourceQuestion' => (int) ($data['sourceQuestion'] ?? $condition->sourceQuestion),
|
||||
'answerValue' => mb_strtolower(trim((string) ($data['answerValue'] ?? $condition->answerValue))),
|
||||
'targetQuestion' => (int) ($data['targetQuestion'] ?? $condition->targetQuestion),
|
||||
], [
|
||||
'id' => (int) $condition->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(int $surveyId, int $conditionId): void
|
||||
{
|
||||
$this->connection->delete('tl_survey_condition', [
|
||||
'id' => $conditionId,
|
||||
'pid' => $surveyId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user