brouillon de Form avec variables de session

This commit is contained in:
Deltacms 2024-01-08 08:18:17 +01:00
parent 2b82f5c4a5
commit 54f120fd27
2 changed files with 22 additions and 38 deletions

View File

@ -422,57 +422,49 @@ class form extends common {
}
// Création du brouillon s'il n'existe pas
if( $this->getData(['module', $this->getUrl(0), 'draft']) === "" ){
$this->setData([
'module',
$this->getUrl(0),
'draft',
[
'mail' => "",
'textarea' => "",
'datetime' => null,
'checkbox' => false,
'select' => "",
'text' => "",
'file' =>""
]
]);
if( $_SESSION['draft']===[] || $_SESSION['draft']===null){
$_SESSION['draft']['mail'] = "";
$_SESSION['draft']['textarea'] = "";
$_SESSION['draft']['datetime'] = null;
$_SESSION['draft']['checkbox'] = [];
$_SESSION['draft']['select'] = [];
$_SESSION['draft']['text'] = [];
$_SESSION['draft']['file'] = "";
}
// Mise à jour du brouillon
$textIndex = 0; $selectIndex=0; $checkboxIndex=0;
for( $index = 0; $index <= count($this->getData(['module', $this->getUrl(0), 'input'])); $index++){
switch ($this->getData(['module', $this->getUrl(0), 'input', $index, 'type'])){
case self::TYPE_MAIL:
$this->setData(['module', $this->getUrl(0), 'draft', 'mail', $this->getInput('formInput[' . $index . ']',helper::FILTER_MAIL)]);
$_SESSION['draft']['mail'] = $this->getInput('formInput[' . $index . ']',helper::FILTER_MAIL);
break;
case self::TYPE_TEXTAREA:
$this->setData(['module', $this->getUrl(0), 'draft', 'textarea', $this->getInput('formInput[' . $index . ']',helper::FILTER_STRING_LONG_NOSTRIP)]);
$_SESSION['draft']['textarea'] = $this->getInput('formInput[' . $index . ']',helper::FILTER_STRING_LONG_NOSTRIP);
break;
case self::TYPE_DATETIME:
$dateTime = time();
if( $this->getInput('formInput[' . $index . ']') !== "" && $this->getInput('formInput[' . $index . ']') !== null) $dateTime = $this->getInput('formInput[' . $index . ']',helper::FILTER_DATETIME, true);
$this->setData(['module', $this->getUrl(0), 'draft', 'datetime', $dateTime]);
$_SESSION['draft']['datetime'] = $dateTime;
break;
case self::TYPE_CHECKBOX:
$this->setData(['module', $this->getUrl(0), 'draft', 'checkbox', $checkboxIndex, $this->getInput('formInput[' . $index . ']',helper::FILTER_BOOLEAN)]);
$_SESSION['draft']['checkbox'][$checkboxIndex] = $this->getInput('formInput[' . $index . ']',helper::FILTER_BOOLEAN);
$checkboxIndex++;
break;
case self::TYPE_SELECT:
$this->setData(['module', $this->getUrl(0), 'draft', 'select', $selectIndex, $this->getInput('formInput[' . $index . ']')]);
$_SESSION['draft']['select'][$selectIndex] = $this->getInput('formInput[' . $index . ']');
$selectIndex++;
break;
case self::TYPE_TEXT:
$this->setData(['module', $this->getUrl(0), 'draft', 'text', $textIndex, $this->getInput('formInput[' . $index . ']')]);
$_SESSION['draft']['text'][$textIndex] = $this->getInput('formInput[' . $index . ']');
$textIndex++;
break;
case self::TYPE_FILE:
$this->setData(['module', $this->getUrl(0), 'draft', 'file', basename($_FILES["fileToUpload"]["name"]) ]);
$_SESSION['draft']['file'] = basename($_FILES["fileToUpload"]["name"]);
break;
default:
$filter = helper::FILTER_STRING_SHORT;
}
}
$this->setData(['module', $this->getUrl(0), 'draft', 'draftTime', time() ]);
// Préparation du contenu du mail
$data = [];
@ -637,7 +629,7 @@ class form extends common {
$redirect = helper::baseUrl() . $this->getUrl(0);
if ( $this->getData(['module', $this->getUrl(0), 'config', 'pageId']) !== '') $redirect = helper::baseUrl() . $this->getData(['module', $this->getUrl(0), 'config', 'pageId']);
// Effacement des données provisoires
$this->setData(['module', $this->getUrl(0), 'draft', '']);
if( self::$inputNotices === [] )$_SESSION['draft']=[];
} else {
$sent = false;
}
@ -658,13 +650,6 @@ class form extends common {
]);
}
// Effacement des données provisoires trop anciennes
if( null !== $this->getData(['module', $this->getUrl(0), 'draft', 'draftTime']) && ($this->getData(['module', $this->getUrl(0), 'draft', 'draftTime']) + 300) < time() ){
$this->setData(['module', $this->getUrl(0), 'draft', '']);
$this->addOutput([
'redirect' => helper::baseUrl() . $this->getUrl(0)
]);
}
// Valeurs en sortie
$this->addOutput([
'showBarEditButton' => true,

View File

@ -48,7 +48,7 @@ if($this->getData(['module', $this->getUrl(0), 'input'])): ?>
<?php echo template::mail('formInput[' . $index . ']', [
'id' => 'formInput_' . $index,
'label' => $input['name'],
'value' => $this->getData([ 'module', $this->getUrl(0), 'draft', 'mail'])
'value' => $_SESSION['draft']['mail']
]); ?>
<?php elseif($input['type'] === $module::TYPE_SELECT): ?>
<?php
@ -60,21 +60,21 @@ if($this->getData(['module', $this->getUrl(0), 'input'])): ?>
<?php echo template::select('formInput[' . $index . ']', $values, [
'id' => 'formInput_' . $index,
'label' => $input['name'],
'selected' => $values[$this->getData([ 'module', $this->getUrl(0), 'draft', 'select', $selectIndex])]
'selected' => $values[$_SESSION['draft']['select'][$selectIndex]]
]);
$selectIndex++; ?>
<?php elseif($input['type'] === $module::TYPE_TEXT): ?>
<?php echo template::text('formInput[' . $index . ']', [
'id' => 'formInput_' . $index,
'label' => $input['name'],
'value' => $this->getData([ 'module', $this->getUrl(0), 'draft', 'text', $textIndex])
'value' => $_SESSION['draft']['text'][$textIndex]
]);
$textIndex++; ?>
<?php elseif($input['type'] === $module::TYPE_TEXTAREA): ?>
<?php echo template::textarea('formInput[' . $index . ']', [
'id' => 'formInput_' . $index,
'label' => $input['name'],
'value' => $this->getData([ 'module', $this->getUrl(0), 'draft', 'textarea']),
'value' => $_SESSION['draft']['textarea'],
'class' => 'editorWysiwygComment',
'noDirty' => true
]); ?>
@ -82,12 +82,11 @@ if($this->getData(['module', $this->getUrl(0), 'input'])): ?>
<?php echo template::date('formInput[' . $index . ']', [
'id' => 'formInput_' . $index,
'label' => $input['name'],
'value' => $this->getData([ 'module', $this->getUrl(0), 'draft', 'datetime'])
'value' => $_SESSION['draft']['datetime']
]); ?>
<?php elseif($input['type'] === $module::TYPE_CHECKBOX):
echo template::checkbox('formInput[' . $index . ']', true, $input['name'], [
'before' => false,
'checked' => false // Problème non solutionné avec $this->getData([ 'module', $this->getUrl(0), 'draft', 'checkbox', $checkboxIndex])
'checked' => $_SESSION['draft']['checkbox'][$checkboxIndex]
]);
$checkboxIndex++; ?>
<?php elseif($input['type'] === $module::TYPE_FILE): ?>