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

delete.php, QuickFormQuery.php: Error handling improved.

parent 794aa0c0
......@@ -13,6 +13,7 @@ use qfq;
require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
/**
......@@ -47,14 +48,20 @@ require_once(__DIR__ . '/../qfq/Constants.php');
$answer = array();
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO;
$answer[API_MESSAGE] = 'Something failed';
$answer[API_MESSAGE] = '';
$answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
$result[MSG_HEADER] = '';
$result[MSG_CONTENT] = '';
$modeAnswer = false;
$flagSuccess = false;
try {
$qfq = new \qfq\QuickFormQuery(['bodytext' => '']);
$qfq->delete();
$flagSuccess = $qfq->delete();
$targetUrl = Store::getVar(SIP_TARGET_URL, STORE_SIP);
$modeAnswer = Store::getVar(SIP_MODE_ANSWER, STORE_SIP);
......@@ -65,21 +72,14 @@ try {
$answer[API_MESSAGE] = 'delete: success';
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_CLIENT;
$answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
$result[MSG_HEADER] = "Content-Type: application/json";
$result[MSG_CONTENT] = json_encode($answer);
break;
case MODE_HTML:
if ($targetUrl === false || $targetUrl === '') {
$result[MSG_CONTENT] = 'Missing target URL. ' . ERROR_MISSING_VALUE;
} else {
$result[MSG_CONTENT] = '';
throw new CodeException('Missing target URL', ERROR_MISSING_VALUE);
}
$result[MSG_HEADER] = "Location: $targetUrl";
break;
default:
......@@ -100,8 +100,6 @@ try {
$answer[API_FIELD_MESSAGE] = $val;
}
$result[MSG_CONTENT] = json_encode($answer);
} catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\DbException $e) {
......@@ -110,13 +108,27 @@ try {
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}
// Fallbak. in case an exception has been thrown and $result is not filled.
if (!isset($result[MSG_HEADER]) && !isset($result[MSG_CONTENT])) {
// In case $modeAnswer is still missing: try to get it again - maybe the SIP store has been initialized before the exception has been thrown.
if ($modeAnswer === false) {
$modeAnswer = Store::getVar(SIP_MODE_ANSWER, STORE_SIP);
}
if ($modeAnswer === MODE_JSON) {
// JSON
$result[MSG_HEADER] = "Content-Type: application/json";
$result[MSG_CONTENT] = json_encode($answer);
} else {
// HTML
if (!$flagSuccess) {
$result[MSG_CONTENT] = "<p>" . $answer[API_MESSAGE] . "</p>";
if (isset($answer[API_FIELD_NAME])) {
$result[MSG_CONTENT] .= "<p>" . $answer[API_FIELD_NAME] . " : " . $answer[API_FIELD_MESSAGE] . "</p>";
}
}
}
if (isset($result[MSG_HEADER]) && $result[MSG_HEADER] !== '') {
// Sent header, if given.
if ($result[MSG_HEADER] !== '') {
header($result[MSG_HEADER]);
}
......
......@@ -236,7 +236,7 @@ class QuickFormQuery {
// FORM_DELETE without a form definition: Fake the form wiht only a tableName.
$table = $this->store->getVar(SIP_TABLE, STORE_SIP);
if ($table === false) {
return '';
throw new UserFormException("No 'form' and no 'table' definition found.", ERROR_MISSING_VALUE);
}
$sipFound = true;
$this->formSpec[F_NAME] = '';
......@@ -655,11 +655,14 @@ class QuickFormQuery {
/**
* Delete a record (tablename and recordid are given) or process a 'delete form'
*
* @return bool
* @throws CodeException
*/
public function delete() {
$this->doForm(FORM_DELETE);
return true;
}
/**
......
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