Commit dba0f5df authored by Carsten  Rose's avatar Carsten Rose
Browse files

Refs #7634. Refactor. REST form/id's are now in STORE_CLIENT. Unit Tests will...

Refs #7634. Refactor. REST form/id's are now in STORE_CLIENT. Unit Tests will fail - not updated to new function.
parent 07c86019
......@@ -15,13 +15,20 @@ require_once(__DIR__ . '/../core/exceptions/UserFormException.php');
require_once(__DIR__ . '/../core/exceptions/CodeException.php');
require_once(__DIR__ . '/../core/exceptions/DbException.php');
$restIds=array();
$restId=array();
$restForm=array();
try {
try {
$bodytext = OnString::extractFormRecordId($_SERVER['PATH_INFO'], $restIds);
$form = OnString::extractFormRecordId($_SERVER['PATH_INFO'], $restId, $restForm);
$id=end($restId);
// Fake Bodytext setup
$bodytext = TYPO3_RECORD_ID . '=' . $id . PHP_EOL;
$bodytext .= TYPO3_FORM . '=' . $form . PHP_EOL;
$qfq = new QuickFormQuery(['bodytext' => $bodytext]);
$answer = $qfq->rest($restIds);
$answer = $qfq->rest($restId, $restForm);
} catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
......
......@@ -991,6 +991,8 @@ const F_SHOW_ID_IN_FORM_TITLE = SYSTEM_SHOW_ID_IN_FORM_TITLE;
const F_REST_SQL_LIST = 'restSqlList';
const F_REST_SQL_DATA = 'restSqlData';
const F_REST_PARAM = 'restParam';
const CLIENT_REST_ID = '_id';
const CLIENT_REST_FORM = '_form';
// FORM_ELEMENT_STATI
const FE_MODE_SHOW = 'show';
......
......@@ -325,7 +325,6 @@ class QuickFormQuery {
*
* @param string $formMode FORM_LOAD | FORM_UPDATE | FORM_SAVE | FORM_DELETE
*
* @param array $restIds
* @return array|string
* @throws CodeException
* @throws DbException
......@@ -336,7 +335,7 @@ class QuickFormQuery {
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
private function doForm($formMode, array $restIds=array()) {
private function doForm($formMode) {
$data = '';
$foundInStore = '';
$flagApiStructureReGroup = true;
......@@ -579,7 +578,7 @@ class QuickFormQuery {
break;
case FORM_REST:
$data = $this->doRestGet($restIds);
$data = $this->doRestGet();
$flagApiStructureReGroup = false;
break;
......@@ -603,33 +602,14 @@ class QuickFormQuery {
* @throws UserFormException
* @throws UserReportException
*/
private function doRestGet(array $restIds) {
$param=array();
$paramNames=explode(',', $this->formSpec[F_REST_PARAM]??'');
foreach($paramNames as $key){
switch($key){
case TYPO3_FORM:
case TYPO3_RECORD_ID:
throw new UserFormException("Name '$key' is forbidden in " . F_REST_PARAM, ERROR_INVALID_VALUE);
break;
default:
break;
}
$param[$key]=array_shift($restIds);
if(null===$param[$key]){
throw new UserFormException("Parameter '$key' not found in REST path (typically too few parameter)", ERROR_MISSING_REQUIRED_PARAMETER);
}
}
private function doRestGet() {
// Copy url parameter to STORE_TYPO3
$this->store::appendToStore($param, STORE_TYPO3);
$this->copyGenericRestParamToNamed();
$r = $this->store::getVar(TYPO3_RECORD_ID, STORE_TYPO3);
$key = empty($r) ? F_REST_SQL_LIST : F_REST_SQL_DATA;
if(!isset($this->formSpec[$key])){
if (!isset($this->formSpec[$key])) {
throw new UserFormException("Missing Parameter '$key'", ERROR_INVALID_VALUE);
}
......@@ -637,6 +617,33 @@ class QuickFormQuery {
}
/**
* STORE_CLIENT: copy parameter _id1,_id2,...,_idN to named variables, specified via $this->formSpec[F_REST_PARAM] (CSV list)
*
* @throws CodeException
* @throws UserFormException
* @throws UserReportException
*/
private function copyGenericRestParamToNamed() {
$paramNames = explode(',', $this->formSpec[F_REST_PARAM] ?? '');
$ii = 1;
foreach ($paramNames as $key) {
switch ($key) {
case CLIENT_FORM:
case CLIENT_RECORD_ID:
throw new UserFormException("Name '$key' is forbidden in " . F_REST_PARAM, ERROR_INVALID_VALUE);
break;
default:
break;
}
$val = $this->store::getVar(CLIENT_REST_ID . $ii, STORE_CLIENT);
$this->store::setVar($key, $val, STORE_CLIENT);
$ii++;
}
}
/**
* Copies state 'hidden' from a FE pill to all FE child elements of that pill.
*
......@@ -1819,7 +1826,8 @@ EOF;
}
/**
* @param array $restIds
* @param array $restId
* @param array $restForm
* @return array|string
* @throws CodeException
* @throws DbException
......@@ -1830,9 +1838,23 @@ EOF;
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function rest(array $restIds) {
public function rest(array $restId, array $restForm) {
// Copy 'id' from REST Url to STORE_CLIENT. Naming is generic with '_idX'
$ii = 1;
foreach ($restId as $val) {
$this->store::setVar(CLIENT_REST_ID . $ii, $val, STORE_CLIENT);
$ii++;
}
// Copy 'form' from REST Url to STORE_CLIENT. Naming is generic with '_formX'
$ii = 1;
foreach ($restForm as $val) {
$this->store::setVar(CLIENT_REST_FORM . $ii, $val, STORE_CLIENT);
$ii++;
}
return $this->doForm(FORM_REST, $restIds);
return $this->doForm(FORM_REST);
}
......
......@@ -207,17 +207,17 @@ class OnString {
}
/**
* Split a $_SERVER['PATH_INFO'] of the form '/form1/id1/form2/id2/form3/id3/...)' to
* r=id1
* form=form1
* restPath=form2/id2/form3/id3/...
* Split a $_SERVER['PATH_INFO'] of the form '/form1/id1/form2/id2/form3/id3/.../formN[/idN])' to
* $rcArrrIds=[ id1, id2, ..., idN]
* return: 'formN'
*
* @param $pathInfo
* @param array $rcArrIds
* @param array $rcArrId
* @param array $rcArrForm
* @return string
* @throws UserFormException
*/
public static function extractFormRecordId($pathInfo, array &$rcArrIds) {
public static function extractFormRecordId($pathInfo, array &$rcArrId, array &$rcArrForm) {
// Empty: do nothing
if ($pathInfo == '') {
......@@ -235,7 +235,7 @@ class OnString {
$pathInfo = substr($pathInfo, 0, $len - 1);
}
// Empty now: do nothing
// Empty? do nothing
if ($pathInfo == '') {
return '';
}
......@@ -248,27 +248,24 @@ class OnString {
array_push($param, 0);
}
$rcArrIds = array();
$rcArrId = array();
$rcArrForm = array();
while (count($param)>0) {
$form = array_shift($param);
$form= array_shift($param);
if (!ctype_alnum($form)) {
throw new UserFormException('Expect alphanumeric string', ERROR_BROKEN_PARAMETER);
}
$rcArrForm[]=$form;
$id = array_shift($param);
// if (!ctype_digit($id)) {
// throw new UserFormException('Expect numerical id', ERROR_BROKEN_PARAMETER);
// }
$rcArrIds[] = $id;
if (!ctype_digit((string) $id)) {
throw new UserFormException('Expect numerical id', ERROR_BROKEN_PARAMETER);
}
$rcArrId[] = $id;
}
// Fake Bodytext setup
$text = TYPO3_RECORD_ID . '=' . $id . PHP_EOL;
$text .= TYPO3_FORM . '=' . $form . PHP_EOL;
return $text;
return $form;
}
}
Markdown is supported
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