Add initial Plenario live polling app
CakePHP 5.4 app for live polls: create via email without login, manage via secret admin link, join via code. Includes slide types (choice, wordcloud, open, scale, content), presenter mode, CSV export, captcha, throttling, tests and Gitea CI/deploy workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
eb319f8c1e
120 files changed
+15572
No files matched your search
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use Cake\Http\Exception\NotFoundException;
|
||||
use Cake\Http\Response;
|
||||
|
||||
/**
|
||||
* Slide CRUD below /admin/{token}/slides.
|
||||
*/
|
||||
class SlidesController extends AdminAppController
|
||||
{
|
||||
protected SlidesTable $Slides;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
/** @var \App\Model\Table\SlidesTable $slides */
|
||||
$slides = $this->fetchTable('Slides');
|
||||
$this->Slides = $slides;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function add(): ?Response
|
||||
{
|
||||
$type = (string)($this->request->getData('type') ?? $this->request->getQuery('type', Slide::TYPE_CHOICE));
|
||||
if (!array_key_exists($type, Slide::typeLabels())) {
|
||||
$type = Slide::TYPE_CHOICE;
|
||||
}
|
||||
$slide = $this->Slides->newEntity(['type' => $type, 'settings' => Slide::DEFAULT_SETTINGS[$type]], [
|
||||
'validate' => false,
|
||||
]);
|
||||
$slide->setNew(true);
|
||||
$slide->poll_id = $this->poll->id;
|
||||
$optionsText = $type === Slide::TYPE_CHOICE ? "\n" : '';
|
||||
|
||||
if ($this->request->is('post')) {
|
||||
$optionsText = (string)$this->request->getData('options_text', '');
|
||||
if ($this->saveSlide($slide, $optionsText)) {
|
||||
$this->Flash->success(__('Folie angelegt.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
}
|
||||
|
||||
$this->set(compact('slide', 'optionsText'));
|
||||
$this->render('form');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id Slide id
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function edit(string $id): ?Response
|
||||
{
|
||||
$slide = $this->loadSlide($id);
|
||||
$optionsText = implode("\n", array_map(fn($o) => $o->label, $slide->slide_options ?? []));
|
||||
|
||||
if ($this->request->is(['post', 'put', 'patch'])) {
|
||||
$optionsText = (string)$this->request->getData('options_text', '');
|
||||
if ($this->saveSlide($slide, $optionsText)) {
|
||||
$this->Flash->success(__('Folie gespeichert.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
}
|
||||
|
||||
$hasResponses = $this->Slides->Responses->exists(['slide_id' => $slide->id]);
|
||||
$this->set(compact('slide', 'optionsText', 'hasResponses'));
|
||||
$this->render('form');
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id Slide id
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function delete(string $id): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$slide = $this->loadSlide($id);
|
||||
if ($this->poll->current_slide_id === $slide->id) {
|
||||
$this->poll->current_slide_id = null;
|
||||
$this->Polls->saveOrFail($this->poll);
|
||||
}
|
||||
$this->Slides->deleteOrFail($slide);
|
||||
$this->Slides->normalizePositions($this->poll->id);
|
||||
$this->Flash->success(__('Folie gelöscht.'));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id Slide id
|
||||
* @param string $direction up|down
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function move(string $id, string $direction): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$this->Slides->move($this->loadSlide($id), $direction);
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all responses of a slide.
|
||||
*
|
||||
* @param string $id Slide id
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function reset(string $id): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$slide = $this->loadSlide($id);
|
||||
$this->Slides->Responses->deleteAll(['slide_id' => $slide->id]);
|
||||
$this->Flash->success(__('Antworten der Folie „{0}“ wurden gelöscht.', $slide->question));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a slide that belongs to the current poll (404 otherwise).
|
||||
*
|
||||
* @param string $id Slide id
|
||||
* @return \App\Model\Entity\Slide
|
||||
*/
|
||||
protected function loadSlide(string $id): Slide
|
||||
{
|
||||
/** @var \App\Model\Entity\Slide|null $slide */
|
||||
$slide = $this->Slides->find()
|
||||
->where(['Slides.id' => (int)$id, 'Slides.poll_id' => $this->poll->id])
|
||||
->contain(['SlideOptions'])
|
||||
->first();
|
||||
if ($slide === null) {
|
||||
throw new NotFoundException(__('Folie nicht gefunden.'));
|
||||
}
|
||||
|
||||
return $slide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches and saves a slide incl. its options.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide (new or existing)
|
||||
* @param string $optionsText Options textarea (choice slides)
|
||||
* @return bool
|
||||
*/
|
||||
protected function saveSlide(Slide $slide, string $optionsText): bool
|
||||
{
|
||||
$data = (array)$this->request->getData();
|
||||
// The type of an existing slide cannot change (would invalidate responses).
|
||||
$data['type'] = $slide->type;
|
||||
$this->Slides->patchEntity($slide, $data, [
|
||||
'fields' => ['type', 'question', 'description', 'settings', 'is_voting_open'],
|
||||
'associated' => [],
|
||||
]);
|
||||
|
||||
$labels = SlidesTable::parseOptionLines($optionsText);
|
||||
if ($slide->type === Slide::TYPE_CHOICE && count($labels) < 2) {
|
||||
$slide->setError('options_text', __('Bitte gib mindestens zwei Antwortoptionen an (eine pro Zeile).'));
|
||||
}
|
||||
if ($slide->hasErrors()) {
|
||||
$this->Flash->error(__('Bitte prüfe deine Eingaben.'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels): bool {
|
||||
if (!$this->Slides->save($slide)) {
|
||||
return false;
|
||||
}
|
||||
if ($slide->type === Slide::TYPE_CHOICE) {
|
||||
$this->Slides->syncOptions($slide, $labels);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user