Hallo in die Runde,
ich habe da mal wieder ein paar Fragen
Ich bin ja gerade dabei mein kleines Projekt für meine Abschlussarbeit zu bewerkstelligen und komme nicht weiter.
Zum Projekt:
Ich muss ein Backend Modul erstellen, indem ich Filialen anlegen kann und diesen Filialen Werktage zuordnen und Öffnungszeiten. Das funktioniert soweit.
So sieht das aus:
backendansicht.png
backend.png
Ich kann hier nun Filialen und zu den Filialen Werktage anlegen:
filialen anlegen.png
tage anlegen.png
Die Werktagen kann ich Öffnungszeiten zuweisen:
werktag anlegen.png
Der Code für tl_CC_filialen:
PHP-Code:
<?php declare(strict_types=1);
$table = 'tl_CC_filialen';
// contao/dca/tl_CC_filialen.php
use Contao\DC_Table;
$GLOBALS['TL_DCA'][$table] = [
//Config
'config' => [
'dataContainer' => DC_Table::class,
'ctable' => ['tl_CC_wochentag_uhrzeit'],
'enableVersioning' => true,
'switchToEdit' => true,
'sql' => [
'keys' => [
'id' => 'primary',
],
],
],
//Listen
'list' => [
'sorting' => [
'mode' => 1,
'fields' => ['name'],
'flag' => 1,
'panelLayout' => 'search,limit'
],
'label' => [
'fields' => ['name'],
'format' => '%s',
],
'operations' => [
'edit' => [
'href' => 'table=tl_CC_wochentag_uhrzeit',
'icon' => 'edit.svg',
],
'editheader' => [
'href' => 'act=edit',
'icon' => 'header.svg',
],
'delete' => [
'href' => 'act=delete',
'icon' => 'delete.svg',
],
'show' => [
'href' => 'act=show',
'icon' => 'show.svg'
],
],
],
//Felder
'fields' => [
'id' => [
'sql' => ['type' => 'integer', 'unsigned' => true, 'autoincrement' => true],
],
'tstamp' => [
'sql' => ['type' => 'integer', 'unsigned' => true, 'default' => 0]
],
'name' => [
'search' => true,
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'street' => [
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'postal' => [
'inputType' => 'text',
'eval' => ['tl_class' => 'clr w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'city' => [
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'country' => [
'inputType' => 'select',
'options_callback' => static function(): array {
return \Contao\System::getContainer()->get('contao.intl.countries')->getCountries();
},
'eval' => ['tl_class' => 'w50', 'mandatory' => true, 'includeBlankOption' => true],
'sql' => ['type' => 'string', 'length' => 2, 'default' => '']
],
'phone' => [
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
],
//Palettes
'palettes' => [
'default' => '{vendor_legend},name;{address_legend},street,postal,city,country,phone'
],
];
Und der Code für die Wochentage:
PHP-Code:
<?php declare(strict_types=1);
// contao/dca/tl_parts.php
use Contao\Database;
use Contao\DC_Table;
use Contao\Input;
$GLOBALS['TL_DCA']['tl_CC_wochentag_uhrzeit'] = [
'config' => [
'dataContainer' => DC_Table::class,
'enableVersioning' => true,
'ptable' => 'tl_CC_filialen',
'sql' => [
'keys' => [
'id' => 'primary'
],
],
'onload_callback' => [
function () {
$db = Database::getInstance();
$pid = Input::get('pid');
if (empty($pid)) {
return;
}
$result = $db->prepare('SELECT `name` FROM `tl_CC_filialen` WHERE `id` = ?')
->execute([$pid]);
$prefix = strtoupper(substr($result->name, 0, 2));
$GLOBALS['TL_DCA']['tl_CC_wochentag_uhrzeit']['fields']['number']['default'] = $prefix;
},
]
],
'list' => [
'sorting' => [
'mode' => 4,
'fields' => ['name'],
'headerFields' => ['name'],
'panelLayout' => 'search,limit',
'child_record_callback' => function (array $row) {
return '<div class="tl_content_left">'.$row['name'].' ['.$row['number'].']</div>';
},
],
'operations' => [
'edit' => [
'href' => 'act=edit',
'icon' => 'edit.svg',
],
'delete' => [
'href' => 'act=delete',
'icon' => 'delete.svg',
],
'show' => [
'href' => 'act=show',
'icon' => 'show.svg'
],
],
],
'fields' => [
'id' => [
'sql' => ['type' => 'integer', 'unsigned' => true, 'autoincrement' => true],
],
'pid' => [
'foreignKey' => 'tl_CC_filialen.name',
'sql' => ['type' => 'integer', 'unsigned' => true, 'default' => 0],
'relation' => ['type'=>'belongsTo', 'load'=>'lazy']
],
'tstamp' => [
'sql' => ['type' => 'integer', 'unsigned' => true, 'default' => 0]
],
'name' => [
'search' => true,
'flag' => 1,
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'number' => [
'search' => true,
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
'description' => [
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'sql' => ['type' => 'string', 'length' => 255, 'default' => '']
],
//Palettes
],
'palettes' => [
'default' => '{parts_legend},name,number,description,singleSRC'
],
];
Die Tabellen sehen so aus:
tabelle filialen.png
tabelle wochentag.png
Anschließend benötige ich ein Frontent Modul, damit im Frontend der Nutzer über Auswahlfelder sich die jeweilige Filiale auswählen kann und dann EIGENTLICH in einem weiteren Auswahlfeld zwischen den Wochentagen (mit vorgegebener Uhrzeit) wählen kann.
Das Frontend Modul funktioniert soweit:
frontendmodul.png
Ich bekomme im Frontend nun auch mit folgenden Template eine Selectfeld und kann die Filialen wählen:
frontend filiale.png
Der Code fürs Frontend Modul:
PHP-Code:
<?php declare(strict_types = 1);
namespace wcs\ClickAndCollect\Module;
use Contao\BackendTemplate;
use Contao\Database;
use Contao\Module;
class Testmodule extends Module
{
protected $strTemplate = 'mod_testmodule';
public function generate()
{
if (TL_MODE == 'BE') {
$template = new BackendTemplate('be_wildcard');
$template->wildcard = '### '.utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['testmodule'][0]).' ###';
$template->title = $this->headline;
$template->id = $this->id;
$template->link = $this->name;
$template->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id='.$this->id;
return $template->parse();
}
return parent::generate();
}
protected function compile()
{
// Daten aus tl_CC_filialen abrufen
$filialenData = Database::getInstance()
->prepare("SELECT * FROM tl_CC_filialen")
->execute()
->fetchAllAssoc();
// Daten aus tl_CC_wochentag_uhrzeit abrufen
if (isset($_POST['filialeSelect'])) {
$filialeId = (int)$_POST['filialeSelect'];
$wochentagData = Database::getInstance()
->prepare("SELECT * FROM tl_CC_wochentag_uhrzeit WHERE pid = ?")
->execute($filialeId)
->fetchAllAssoc();
}
// Daten an das Template übergeben
$this->Template->foo = 'bar';
$this->Template->template = 'mod_testmodule';
$this->Template->filialenData = $filialenData;
$this->Template->wochentagData = $wochentagData;
}
}
Und das Template:
PHP-Code:
<div class="cc-frontend-module">
<form method="post">
<label for="filialSelect">Filiale auswählen:</label>
<select id="filialSelect" name="filialeSelect">
<?php foreach ($filialenData as $filiale): ?>
<option value="<?= $filiale['id'] ?>"><?= $filiale['name'] ?> - <?= $filiale['street'] ?>, <?= $filiale['city'] ?></option>
<?php endforeach; ?>
</select>
<button type="submit">Filiale auswählen</button>
</form>
<?php if ($selectedFilialeId): ?>
<form method="post">
<input type="hidden" name="selectedFilialeId" value="<?= $selectedFilialeId ?>">
<label for="wochentagSelect">Wochentag auswählen:</label>
<select id="wochentagSelect" name="wochentagSelect">
<?php foreach ($wochentagData as $wochentag): ?>
<?php if ($wochentag['pid'] == $selectedFilialeId): ?>
<option value="<?= $wochentag['id'] ?>"><?= $wochentag['name'] ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<button type="submit">Wochentag auswählen</button>
</form>
<?php if ($selectedWochentagId): ?>
<h1>Filiale</h1>
<p><?= $selectedFiliale['name'] ?></p>
<p><?= $selectedFiliale['street'] ?>, <?= $selectedFiliale['city'] ?></p>
<h1>Wochentag</h1>
<p><?= $selectedWochentag['name'] ?> - <?= $selectedWochentag['number'] ?>: <?= $selectedWochentag['description'] ?></p>
<?php endif; ?>
<?php endif; ?>
<h1>Wochentage und Öffnungszeiten</h1>
<ul>
<?php foreach ($wochentagData as $wochentag): ?>
<li><?= $wochentag['name'] ?> - <?= $wochentag['number'] ?>: <?= $wochentag['description'] ?></li>
<?php endforeach; ?>
</ul>
</div>
Allerdings komm ich nicht an die Daten von der Tabelle tl_CC_wochentag_uhrzeit ran. Beim betätigen des Submits bekomm ich einen Anfrage Token Fehler. Ich habe auch bereits das über JS irgendwie zu lösen. Komme da aber nicht weiter.
Liegt an der Verbindung der Tabellen? Liegts am Code, liegts am Template?
Ich möchte eigentlich erreich, dass wenn eine Filiale gewählt wird ein weiteres Selectfeld sich öffnet und dann ein angelegte Werktag gewählt werden kann an den dann wiederum die Uhrzeit gekoppelt ist.
Vielleicht mag sich jemand von euch das mal anschauen und mir unter die Arme greifen.
Lesezeichen