1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
<?php
declare(strict_types=1);
namespace App\ConfigModule\Forms;
use App\ConfigModule\Model\SchedulerManager;
use App\ConfigModule\Presenters\SchedulerPresenter;
use App\Forms\FormFactory;
use Nette;
use Nette\Application\UI\Form;
class ConfigSchedulerFormFactory {
use Nette\SmartObject;
private $manager;
private $factory;
public function __construct(FormFactory $factory, SchedulerManager $manager) {
$this->manager = $manager;
$this->factory = $factory;
}
public function create(SchedulerPresenter $presenter) {
$id = intval($presenter->getParameter('id'));
$form = $this->factory->create();
$defaults = $this->manager->load($id);
$types = [
'raw' => 'raw',
'raw-hdp' => 'raw-hdp',
'std-per-thermometer' => 'std-per-thermometer',
'std-per-ledg' => 'std-per-ledg',
'std-per-ledr' => 'std-per-ledr',
'std-per-frc' => 'std-per-frc',
'std-per-io' => 'std-per-io',
'std-sen' => 'std-sen',
];
foreach (array_keys($defaults) as $key) {
switch ($key) {
case 'hwpid':
$form->addText($key, $key)
->setRequired(false)
->addRule(Form::PATTERN, 'It has to contain hexadecimal number - ' . $key, '[0-9A-Fa-f]{4}')
->addRule(Form::MAX_LENGTH, 'It has to have maximal length of 4 chars.', 4);
break;
case 'pcmd':
case 'pnum':
case 'nadr':
$form->addText($key, $key)
->setRequired(false)
->addRule(Form::PATTERN, 'It has to contain hexadecimal number - ' . $key, '[0-9A-Fa-f]{1,2}')
->addRule(Form::MAX_LENGTH, 'It has to have maximal length of 2 chars.', 2);
break;
case 'sensors':
$form->addTextArea($key, $key);
break;
case 'timeout':
$form->addInteger($key, $key);
break;
case 'type':
$form->addSelect($key, $key, $types)->setRequired();
break;
default:
$form->addText($key, $key);
}
}
$form->addSubmit('save', 'Save');
$form->setDefaults($defaults);
$form->addProtection('Timeout expired, resubmit the form.');
$form->onSuccess[] = function (Form $form, $values) use ($presenter, $id) {
$this->manager->save($values, $id);
$presenter->redirect('Scheduler:default');
};
return $form;
}
}