| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/modules/forms/code/ |
Upload File : |
<?php
namespace modules\forms\code;
/**
* Description of AbstractForm
*
* @author maros
*/
abstract class AbstractForm {
protected ?\app\classes\Translator $t;
private $htmlInputKeyCounter = 0;
private $token;
private $name = 'form';
private $checkRequired = true;
private \PFBC\View $view;
public function __construct() {
$this->t = \app\classes\Translator::getInstance();
$this->token = getUniqueId();
$this->view = new \PFBC\View\ThreeColumns();
}
/**
* @return \PFBC\Form
*/
protected function getNewForm($name, $ajaxMethod, $view): \PFBC\Form {
$form = new \PFBC\Form("form-" . $name);
$form->configure(array(
"prevent" => array("bootstrap", "jQuery", "focus"),
"ajax" => 1,
"ajaxCallback" => "",
"action" => "",
"view" => $view,
"ajaxMethod" => $ajaxMethod,
"autocomplete" => "off",
"class" => "needs-validation form-" . $name,
));
return $form;
}
protected function getNextHtmlInputKey() {
$this->htmlInputKeyCounter++;
return "html-" . $this->htmlInputKeyCounter;
}
public function getToken() {
return $this->token;
}
public function render() {
$form = $this->getNewForm($this->name, 'submitForm($(this));', $this->getView());
$form->addElement(new \PFBC\Element\HTML('<div class="row">'));
$elements = $this->getAllElements();
foreach ($elements as $element) {
if (isset($element['pfbc'])) {
$form->addElement($element['pfbc']);
}
}
$form->addElement(new \PFBC\Element\HTML('</div>'));
$form->render();
}
/**
* @return array
*/
abstract function getAllElements();
public function getFrontendData($itemData, $prefix = '') {
$data = [];
$allElements = $this->getAllElements();
foreach ($allElements as $key => $element) {
if (isset($element['controller']) && $element['db'] && $element['pfbc']) {
$controllerClass = $element['controller'];
$controller = new $controllerClass(isset($element['pfbc']) ? $element['pfbc'] : null, $itemData);
$key = str_replace("[]", "", $element['pfbc']->getAttribute('name'));
if (array_key_exists($key, $itemData)) {
$data[$key] = $controller->getFrontendValue($itemData[$key]);
}
}
}
if ($prefix) {
foreach ($data as $key => $val) {
$data[$prefix . '_' . $key] = $val;
unset($data[$key]);
}
}
return $data;
}
public function prepareDbData(array $post) {
$data = [];
$allElements = $this->getAllElements();
$allElements = $this->updateIsRequired($allElements, $post);
foreach ($allElements as $key => $element) {
if (isset($element['controller'])) {
$controllerClass = $element['controller'];
/** @var \modules\forms\code\AbstractController $controller */
$controller = new $controllerClass(isset($element['pfbc']) ? $element['pfbc'] : null, $post);
$value = isset($post[str_replace('[]', '', $element['pfbc']?->getAttribute('name'))]) ? $post[str_replace('[]', '', $element['pfbc']?->getAttribute('name'))] : false;
$validation = $controller->validate($value);
if ($validation !== true) {
throw new \Exception($validation . " " . $element['pfbc']?->getLabel() . " <small><i>(" . $element['pfbc']?->getAttribute('name') . ")</i></small>", \app\constants\HttpReponseCodes::HTTP_BAD_REQUEST);
}
if ($element['pfbc']?->isRequired()) {
if (!is_array($value)) {
if ((strlen($value) === 0 || $value === false) && $this->checkRequired) {
throw new \Exception($this->t->_get("Chýbajúci povinný paramater") . " " . $element['pfbc']?->getLabel() . " <small><i>(" . $element['pfbc']?->getAttribute('name') . ")</i></small>", \app\constants\HttpReponseCodes::HTTP_BAD_REQUEST);
}
} else {
$isEmpty = true;
foreach ($value as $val) {
if (strlen($val) !== 0 && $val !== false) {
$isEmpty = false;
break;
}
}
if ($isEmpty && $this->checkRequired) {
throw new \Exception($this->t->_get("Chýbajúci povinný paramater") . " " . $element['pfbc']?->getLabel() . " <small><i>(" . $element['pfbc']?->getAttribute('name') . ")</i></small>", \app\constants\HttpReponseCodes::HTTP_BAD_REQUEST);
}
}
}
if ($element['db']) {
$dbValue = $controller->getDbValue($value);
if ($dbValue !== null) {
$data[str_replace("[]", "", $element['pfbc']?->getAttribute('name'))] = toNonHtml($dbValue);
}
}
}
}
return $data;
}
protected function updateIsRequired(array $elements, $post) {
return $elements;
}
/**
* @return array
*/
protected function getTitleElement($title, $h = 5, $hrClass = "", $hClass = "mt-3") {
return ["db" => false, "pfbc" => new \PFBC\Element\HTML('<div class="col-md-12"><h' . $h . ' class="fw-500 ' . $hClass . '">' . $this->t->_get($title) . '</h' . $h . '><hr class="' . $hrClass . '"></div>')];
}
/**
* @return array
*/
protected function getNewRowElement() {
return ["db" => false, "pfbc" => new \PFBC\Element\HTML('<div class="row"></div>')];
}
public function getName() {
return 'form-' . $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
public function getView(): \PFBC\View {
return $this->view;
}
public function setView(\PFBC\View $view): void {
$this->view = $view;
}
public function setCheckRequired(bool $check) {
$this->checkRequired = $check;
}
}