Commit 2541d843 authored by Marc Egger's avatar Marc Egger
Browse files

Refs #10120 update all forms when report operates on Form/FormElement. (unfinished)

parent 9d966f75
Pipeline #3613 failed with stages
in 41 seconds
......@@ -5,9 +5,10 @@ namespace IMATHUZH\Qfq\Core\Form;
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\OnString;
use IMATHUZH\Qfq\Core\Helper\SqlQuery;
// TODO: test creating form and formElement
// TODO: test form copying, does it create a form file?
// TODO: ensure that form file is imported if Form is loaded via Rest API
// TODO: gibt es UPDATE/INSERT/DELETE statements die an Form/FormElement arbeiten koennen, die nicht nur durch doForm aufgerufen werden?
// TODO: Testen: Dies sollte schon erfuellt sein, testen: bevor ein form/formelement im form editor gespeichert wird, soll es vom file geladen werden (Save.php). Falls das file importiert wurde, gibt es eine fehlermeldung.
......@@ -347,6 +348,71 @@ class FormAsFile
}
}
/**
* Returns true if the sql query selects Form or FormElement table
*
* @param string $sql
* @return bool
*/
public static function isFormQuery(string $sql): bool
{
// $sql = <<<END
// SELECT CONCAT('p:{{pageId:T}}&form=form&r=', f.id) as _pagee, f.id, f.name, f.title, f.tableName, CONCAT('form=form&r=', f.id) as _Paged FROM where
// FormFormElement,Form AS f ORDER BY f.name
//
// SELECT CONCAT('p:{{pageId:T}}&form=form&r=', f.id) as _pagee, f.id, f.name, f.title, f.tableName, CONCAT('form=form&r=', f.id) as _Paged From Order by Form AS f ORDER BY f.name
//
// SELECT CONCAT('p:{{pageId:T}}&form=form&r=', f.id) as _pagee, f.id, f.name, f.title, f.tableName, CONCAT('form=form&r=', f.id) as _Paged FRom having Form AS f ORDER BY f.name
//
// SELECT CONCAT('p:{{pageId:T}}&form=form&r=', f.id) as _pagee, f.id, f.name, f.title, f.tableName, CONCAT('form=form&r=', f.id) as _Paged from Form AS f ORDER BY f.name
//END;
// find substrings whihch start with FROM and are followed by Form or FormElement
preg_match_all('/(?i)FROM(?-i)(.*?)\b(' . TABLE_NAME_FORM . '|' . TABLE_NAME_FORM_ELEMENT . ')\b/s', $sql, $matches);
// Check if other keywords are in between FROM and the table name
$keywordsAfterFrom = ['WHERE', 'GROUP BY', 'HAVING', 'WINDOW', 'ORDER BY', 'LIMIT', 'FOR', 'INTO'];
foreach($matches[0] as $match)
{
if(!OnString::containsOneOfWords($keywordsAfterFrom, $match)) {
return true;
}
}
return false;
}
/**
* Import all form files and delete forms in the DB for which there are not form files.
*
* @throws \UserFormException
*/
public static function updateAllForms(): void
{
// if form dir does not exist, create it
// get content of form dir (flse => error)
// for each file check if it is json with pathinfo()
// does pathinfo always succeed?
// if a file is json, cut json away and use importForm()
// get all forms from DB
// delete all forms which are in DB but not in files
$files = scandir(self::formPath());
$files = array_diff($files, array('.', '..'));
foreach ($files as $file) {
}
/////// DEBUG /////////
throw new \UserFormException(json_encode([
ERROR_MESSAGE_TO_USER => json_encode($files, JSON_PRETTY_PRINT),
ERROR_MESSAGE_TO_DEVELOPER => json_encode([], JSON_PRETTY_PRINT)
]));
///////////////////////
}
/**
* Insert formElement to the given form.
* Keys removed before insert: id, formId, feIdContainer
......@@ -453,16 +519,26 @@ class FormAsFile
}
// create form folder path
if (!is_dir(SYSTEM_FORM_FILE_PATH)) {
$success = mkdir(SYSTEM_FORM_FILE_PATH, 0777, true);
if (!is_dir(self::formPath())) {
$success = mkdir(self::formPath(), 0777, true);
if ($success === false) {
throw new \UserFormException(json_encode([
ERROR_MESSAGE_TO_USER => "Can't create form file path.",
ERROR_MESSAGE_TO_DEVELOPER => "Can't create path: " . SYSTEM_FORM_FILE_PATH]),
ERROR_MESSAGE_TO_DEVELOPER => "Can't create path: " . self::formPath()]),
ERROR_IO_WRITE_FILE);
}
}
return HelperFile::correctRelativePathFileName(SYSTEM_FORM_FILE_PATH . '/' . $formName . ".json");
return self::formPath() . '/' . $formName . ".json";
}
/**
* Return the path of the form directory relative to CWD
*
* @return string
*/
private static function formPath(): string
{
return HelperFile::correctRelativePathFileName(SYSTEM_FORM_FILE_PATH);
}
}
\ No newline at end of file
......@@ -269,4 +269,23 @@ class OnString {
return $form;
}
/**
* Returns true if the subject string contains any of the given words in targets.
* Case insensitive!
*
* @param array $words
* @param string $subject
* @return bool
*/
public static function containsOneOfWords(array $words, string $subject): bool
{
foreach($words as $word)
{
if (preg_match("/\b" . $word . "\b/i", $subject)) {
return true;
}
}
return false;
}
}
......@@ -25,6 +25,7 @@ namespace IMATHUZH\Qfq\Core\Report;
use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Evaluate;
use IMATHUZH\Qfq\Core\Form\FormAsFile;
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\OnString;
......@@ -491,6 +492,11 @@ class Report {
$this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
// import form files if changed
if (FormAsFile::isFormQuery($sql)) {
FormAsFile::updateAllForms();
}
//Execute SQL. All errors have been already catched.
unset($result);
$result = $this->db->sql($sql, ROW_KEYS, array(), '', $keys, $stat);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment