Commit 88093a16 authored by Carsten  Rose's avatar Carsten Rose
Browse files

delete.php: Implemented simple delete by giving table and record ID (by SIP).

save.php: moved constants to Constants.php to reuse them in delete.php. Fixed bug by building error messages.
Sip: extended queryStringToSip() by optional Parameter $scriptName.
AbstractBuilForm: renamed formEditLink() to createFormEditUrl(). Changed createFormEditUrl() to only return the final URL, without HTML tags. Implemented createDeleteUrl().
BuildFormBootstrap, BuildFormTable: rewrote code for using createFormEditLink(). Use of createDeleteUrl().
Constants: defined SIP_TABLE, API_*
QuickFormQuery: reformat Code
parent a5506bbd
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 12/23/15
* Time: 6:16 PM
*/
namespace qfq;
use qfq;
require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
//require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
/**
* Return JSON encoded answer
*
* status: success|error
* message: <message>
* redirect: client|url|no
* redirect-url: <url>
* field-name:<field name>
* field-message: <message>
*
* Description:
*
* Delete successfull.
* status = 'success'
* message = <message>
* redirect = 'client'
*
* Delete successfull.
* status = 'success'
* message = <message>
* redirect = 'url'
* redirect-url = <URL>
*
* Delete failed: Show message.
* status = 'error'
* message = <message>
* redirect = 'no'
*/
$answer = array();
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_NO;
$answer[API_MESSAGE] = '';
$answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
try {
$qfq = new \qfq\QuickFormQuery(['bodytext' => "debugShowStack=1"]);
$qfq->delete();
$answer[API_MESSAGE] = 'delete: success';
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_CLIENT;
$answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
} catch (qfq\UserException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\DbException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
} catch (\Exception $e) {
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}
header("Content-Type: application/json");
echo json_encode($answer);
......@@ -12,6 +12,7 @@ use qfq;
require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
/**
......@@ -46,51 +47,38 @@ require_once(__DIR__ . '/../qfq/store/Store.php');
* field-message = <message appearing as tooltip (or similar) near the form element>
*/
const SAVE_STATUS = 'status';
const SAVE_MESSAGE = 'message';
const SAVE_REDIRECT = 'redirect';
const SAVE_REDIRECT_URL = 'redirect-url';
const SAVE_FIELD_NAME = 'field-name';
const SAVE_FIELD_MESSAGE = 'field-message';
const ANSWER_STATUS_SUCCESS = 'success';
const ANSWER_STATUS_ERROR = 'error';
const ANSWER_REDIRECT_CLIENT = 'client';
const ANSWER_REDIRECT_NO = 'no';
const ANSWER_REDIRECT_URL = 'url';
$answer = array();
$answer[SAVE_STATUS] = 'error';
$answer[SAVE_MESSAGE] = '';
$answer[SAVE_REDIRECT] = 'client';
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_CLIENT;
$answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
$answer[API_MESSAGE] = '';
try {
$qfq = new \qfq\QuickFormQuery(['bodytext' => "debugShowStack=1"]);
$qfq->saveForm();
$answer[SAVE_REDIRECT] = $qfq->getForwardMode($answer[SAVE_REDIRECT_URL]);
$answer[SAVE_MESSAGE] = 'save: success';
$answer[SAVE_STATUS] = ANSWER_STATUS_SUCCESS;
$answer[API_REDIRECT] = $qfq->getForwardMode($answer[API_REDIRECT_URL]);
$answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
$answer[API_MESSAGE] = 'save: success';
} catch (qfq\UserException $e) {
$answer[SAVE_MESSAGE] = $e->formatMessage();
$answer[API_MESSAGE] = $e->formatMessage();
$val = Store::getVar(SYSTEM_FORM_ELEMENT, STORE_SYSTEM);
if ($val !== false)
$answer[SAVE_FIELD_NAME] =
$answer[API_FIELD_NAME] = $val;
$val = Store::getVar(SYSTEM_FORM_ELEMENT_MESSAGE, STORE_SYSTEM);
$val = Store::getVar(SYSTEM_FORM_ELEMENT_MESSAGE, STORE_SYSTEM);
if ($val !== false)
$answer[SAVE_FIELD_MESSAGE] = $val;
$answer[API_FIELD_MESSAGE] = $val;
} catch (qfq\CodeException $e) {
$answer[SAVE_MESSAGE] = $e->formatMessage();
$answer[API_MESSAGE] = $e->formatMessage();
} catch (qfq\DbException $e) {
$answer[SAVE_MESSAGE] = $e->formatMessage();
$answer[API_MESSAGE] = $e->formatMessage();
} catch (\Exception $e) {
$answer[SAVE_MESSAGE] = "Generic Exception: " . $e->getMessage();
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}
header("Content-Type: application/json");
......
......@@ -96,7 +96,6 @@ abstract class AbstractBuildForm {
];
$this->inputCheckPattern = OnArray::inputCheckPatternArray();
}
abstract public function fillWrap();
......@@ -139,9 +138,9 @@ abstract class AbstractBuildForm {
*
* @return string String: <a href="?pageId&sip=....">Edit</a> <small>[sip:..., r:..., urlparam:..., ...]</small>
*/
public function formEditLink() {
public function createFormEditUrl() {
if($this->store->getVar('SHOW_DEBUG_INFO', STORE_SYSTEM) !== 'yes') {
if ($this->store->getVar('SHOW_DEBUG_INFO', STORE_SYSTEM) !== 'yes') {
return '';
}
......@@ -156,9 +155,26 @@ abstract class AbstractBuildForm {
$sip = $this->store->getSipInstance();
$url = $sip->queryStringToSip($queryString);
$sipParamString = OnArray::toString($this->store->getStore(STORE_SIP),':', ', ',"'");
return $url;
}
/**
* Create a link (incl. SIP) to delete the current record.
*
* @return string String: "API_DIR/delete.php?sip=...."
*/
public function createDeleteUrl($table, $recordId) {
$queryStringArray = [
SIP_TABLE => $table,
SIP_RECORD_ID => $recordId
];
$queryString = Support::arrayToQueryString($queryStringArray);
$sip = $this->store->getSipInstance();
return "<p></p><a href='$url'>Edit</a> <small>[$sipParamString]</small></p>";
return $sip->queryStringToSip($queryString, RETURN_URL, API_DIR . '/delete.php');
}
/**
......@@ -172,8 +188,11 @@ abstract class AbstractBuildForm {
$html .= '<div ' . $this->getAttribute('class', $this->formSpec['class'], TRUE) . '>'; // main <div class=...> around everything
// Logged in BE User will see a FormEdit Link
$html .= $this->formEditLink();
$sipParamString = OnArray::toString($this->store->getStore(STORE_SIP), ':', ', ', "'");
$formEditUrl = $this->createFormEditUrl();
$html .= "<p><a href='$formEditUrl'>Edit</a> <small>[$sipParamString]</small></p>";
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= $this->getFormTag();
......@@ -247,7 +266,7 @@ abstract class AbstractBuildForm {
*/
public function getActionUrl() {
return 'typo3conf/ext/qfq/qfq/api/save.php';
return API_DIR . '/save.php';
}
/**
......@@ -283,8 +302,8 @@ abstract class AbstractBuildForm {
// get current data record
if ($recordId > 0 && $this->store->getVar('id', STORE_RECORD) === false) {
$row = $this->db->sql("SELECT * FROM " . $this->formSpec['tableName'] . " WHERE id = ?", ROW_REGULAR, array($recordId));
$this->store->setVarArray($row[0], STORE_RECORD);
$row = $this->db->sql("SELECT * FROM " . $this->formSpec['tableName'] . " WHERE id = ?", ROW_EXPECT_1, array($recordId));
$this->store->setVarArray($row, STORE_RECORD);
}
// Iterate over all FormElements
......@@ -316,8 +335,8 @@ abstract class AbstractBuildForm {
$elementHtml = $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value, $debugStack);
// debugStack as Tooltip
if( $this->store->getVar('SHOW_DEBUG_INFO', STORE_SYSTEM) === 'yes' && count($debugStack)>0) {
$elementHtml = Support::appendTooltip($elementHtml, implode("\n", OnArray::htmlentitiesOnArray($debugStack)));
if ($this->store->getVar('SHOW_DEBUG_INFO', STORE_SYSTEM) === 'yes' && count($debugStack) > 0) {
$elementHtml = Support::appendTooltip($elementHtml, implode("\n", OnArray::htmlentitiesOnArray($debugStack)));
}
// Construct Marshaller Name
......
......@@ -90,7 +90,13 @@ class BuildFormBootstrap extends AbstractBuildForm {
* @return string
*/
public function head() {
$html = $this->formEditLink();
// Logged in BE User will see a FormEdit Link
$sipParamString = OnArray::toString($this->store->getStore(STORE_SIP), ':', ', ', "'");
$formEditUrl = $this->createFormEditUrl();
$html = "<p><a href='$formEditUrl'>Edit</a> <small>[$sipParamString]</small></p>";
$deleteUrl = $this->createDeleteUrl($this->formSpec['tableName'], $this->store->getVar(SIP_RECORD_ID, STORE_SIP));
$html .= "<p><a href='$deleteUrl'>Delete</a>";
$html .= '<div ' . $this->getAttribute('class', $this->formSpec['class'], TRUE) . '>'; // main <div class=...> around everything, Whole FORM; class="container" or class="container-fluid"
......
......@@ -66,7 +66,15 @@ class BuildFormTable extends AbstractBuildForm {
$html = '';
$html .= '<div ' . $this->getAttribute('class', $this->formSpec['class'], TRUE) . '>'; // main <div class=...> around everything
$html .= $this->formEditLink();
// Logged in BE User will see a FormEdit Link
$sipParamString = OnArray::toString($this->store->getStore(STORE_SIP), ':', ', ', "'");
$formEditUrl = $this->createFormEditUrl();
$html .= "<p><a href='$formEditUrl'>Edit</a> <small>[$sipParamString]</small></p>";
$deleteUrl = $this->createDeleteUrl($this->formSpec['tableName'], $this->store->getVar(SIP_RECORD_ID, STORE_SIP));
$html .= "<p><a href='$deleteUrl'>Delete</a>";
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= $this->getFormTag();
$html .= '<table>';
......
......@@ -8,6 +8,9 @@
const CONFIG_INI = "config.ini"; // QFQ configuration file: db access
const GFX_INFO = 'typo3conf/ext/qfq/Resources/Public/icons/note.gif';
const API_DIR = 'typo3conf/ext/qfq/qfq/api/';
const QFQ_LOG = 'qfq.log';
const FORM_LOAD = 'form_load';
......@@ -109,6 +112,7 @@ const ERROR_UNNOWN_STORE = 1046;
const ERROR_GET_STORE_ZERO = 1047;
const ERROR_SET_STORE_ZERO = 1048;
const ERROR_MISSING_FORMELEMENT = 1049;
const ERROR_INVALID_OR_MISSING_PARAMETER = 1050;
// Store
const ERROR_STORE_VALUE_ALREADY_CODPIED = 1100;
......@@ -220,6 +224,7 @@ const SYSTEM_FORM_ELEMENT_MESSAGE = 'formElementMessage'; // '<columnname of cur
const SIP_SIP = CLIENT_SIP; // s
const SIP_RECORD_ID = CLIENT_RECORD_ID; // r
const SIP_FORM = CLIENT_FORM;
const SIP_TABLE = 'table'; // delete a record from 'table'
const SIP_URLPARAM = 'urlparam';
// FURTHER: all extracted params from 'urlparam
......@@ -229,4 +234,16 @@ const SIP_URLPARAM = 'urlparam';
// FORMELEMENT - copy of all formElements of processed form
//const DEF_FORM_ELEMENT_ID = 'id';
const GFX_INFO = 'typo3conf/ext/qfq/Resources/Public/icons/note.gif';
\ No newline at end of file
// api/save.php, api/delete.php
const API_STATUS = 'status';
const API_MESSAGE = 'message';
const API_REDIRECT = 'redirect';
const API_REDIRECT_URL = 'redirect-url';
const API_FIELD_NAME = 'field-name';
const API_FIELD_MESSAGE = 'field-message';
const API_ANSWER_STATUS_SUCCESS = 'success';
const API_ANSWER_STATUS_ERROR = 'error';
const API_ANSWER_REDIRECT_CLIENT = 'client';
const API_ANSWER_REDIRECT_NO = 'no';
const API_ANSWER_REDIRECT_URL = 'url';
......@@ -107,7 +107,6 @@ class QuickFormQuery {
if (!isset($t3data['uid']))
$t3data['uid'] = 0;
$this->t3data = $t3data;
$bodytext = $this->t3data['bodytext'];
......@@ -119,6 +118,8 @@ class QuickFormQuery {
}
/**
* Returns the defined forwardMode and set, if necessary, $forwardPage
*
* @param $forwardPage
* @return mixed
*/
......@@ -128,6 +129,8 @@ class QuickFormQuery {
}
/**
* Main entrypoint for display content: form or report
*
* @return string
*/
public function process() {
......@@ -140,6 +143,7 @@ class QuickFormQuery {
/**
* Process form (load or save) if a formname is found.
*
* @return string
* @throws CodeException
* @throws UserException
......@@ -189,8 +193,7 @@ class QuickFormQuery {
}
/**
* Loads specification of recent form.
* Evaluates Form and all FormElements.
* Load form. Evaluates form. Load FormElements.
*
* Loaded Form is in $this->formSpec
* Loaded 'action' FormElements are in $this->feSpecAction
......@@ -203,12 +206,15 @@ class QuickFormQuery {
*/
private function loadFormSpecification($mode) {
// formName
if (false === $formName = $this->getFormName()) {
return false;
}
$this->store->setVar(SYSTEM_FORM, $formName, STORE_SYSTEM);
$form = $this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXPECT_1, [$formName], 'Form not found or multiple forms with the same name.');
// Load form
$form = $this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXPECT_1,
[$formName], 'Form not found or multiple forms with the same name.');
$this->formSpec = $this->eval->parseArray($form);
HelperFormElement::explodeParameter($this->formSpec);
......@@ -217,18 +223,21 @@ class QuickFormQuery {
$this->store->setVar(SYSTEM_FORM_ELEMENT, '', STORE_SYSTEM);
// FE: Action
$this->feSpecAction = $this->eval->parseArray($this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'action']));
$this->feSpecAction = $this->eval->parseArray($this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR,
['no', $this->formSpec["id"], 'action']));
HelperFormElement::explodeParameterInArrayElements($this->feSpecAction);
// FE: Native & Container
// "SELECT *, ? AS 'nestedInFieldSet' FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted = 'no' AND FIND_IN_SET(fe.class, ? ) AND fe.feIdContainer = ? AND fe.enabled='yes' ORDER BY fe.ord, fe.id";
switch ($mode) {
case FORM_LOAD:
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'native,container', 0]);
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER, ROW_REGULAR,
['no', $this->formSpec["id"], 'native,container', 0]);
break;
case FORM_SAVE:
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR, ['no', $this->formSpec["id"], 'native']);
$this->feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_ALL_CONTAINER, ROW_REGULAR,
['no', $this->formSpec["id"], 'native']);
break;
default:
......@@ -240,6 +249,8 @@ class QuickFormQuery {
}
/**
* Get the formName from STORE_TYPO3 (bodytext), STORE_SIP or by STORE_CLIENT (URL).
*
* @return string|bool Formname (Form.name) or FALSE if no formname found.
* @throws UserException
* @throws CodeException
......@@ -252,6 +263,8 @@ class QuickFormQuery {
}
/**
* Check if loading of the given form is permit. If not, throw an exception.
*
* @return bool - 'true' if SIP exists, else 'false'
* @throws CodeException
* @throws UserException
......@@ -262,13 +275,13 @@ class QuickFormQuery {
$r = $this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_CLIENT);
// If there is a record_id>0: EDIT else NEW
$mode = ($r > 0) ? $this->formSpec['permitEdit'] : $this->formSpec['permitNew'];
$permitMode = ($r > 0) ? $this->formSpec['permitEdit'] : $this->formSpec['permitNew'];
$feUserLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) && $GLOBALS["TSFE"]->fe_user->user["uid"] > 0;
$sipFound = $this->store->getVar(SIP_SIP, STORE_SIP) !== false;
switch ($mode) {
switch ($permitMode) {
case FORM_PERMISSION_SIP:
if (!$sipFound) {
throw new UserException("SIP Parameter needed for this form.", ERROR_SIP_NEEDED_FOR_THIS_FORM);
......@@ -289,7 +302,7 @@ class QuickFormQuery {
case FORM_PERMISSION_NEVER:
throw new UserException("Loading form forbidden.", ERROR_FORM_FORBIDDEN);
default:
throw new CodeException("Unknown permission mode: '" . $mode . "'", ERROR_FORM_UNKNOWN_PERMISSION_MODE);
throw new CodeException("Unknown permission mode: '" . $permitMode . "'", ERROR_FORM_UNKNOWN_PERMISSION_MODE);
}
// Form Definition valid?
......@@ -301,6 +314,8 @@ class QuickFormQuery {
}
/**
* Process the SQL Queries from bodytext. Return the output.
*
* @return string
*/
private function doReport() {
......@@ -313,6 +328,8 @@ class QuickFormQuery {
}
/**
* Save the current form.
*
* @return string
*/
public function saveForm() {
......@@ -322,4 +339,26 @@ class QuickFormQuery {
return $html;
}
/**
* Delete a record (tablename and recordid are given) or process a 'delete form'
*
* @throws CodeException
* @throws DbException
* @throws UserException
*/
public function delete() {
#TODO: implement 'delete form'
// simple delete: table and recordId are given
$recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);
$table = $this->store->getVar(SIP_TABLE, STORE_SIP);
if( $recordId === false || $recordId<1 || $table === false || $table === '') {
throw new UserException("Invalid or missing parameter: recordId=$recordId, table=$table", ERROR_INVALID_OR_MISSING_PARAMETER);
}
$this->db->sql("DELETE FROM $table WHERE id = ? LIMIT 1", ROW_REGULAR, [ $recordId ]);
}
}
\ No newline at end of file
......@@ -68,7 +68,7 @@ class Sip {
* @throws CodeException
* @throws UserException
*/
public function queryStringToSip($queryString, $mode = RETURN_URL) {
public function queryStringToSip($queryString, $mode = RETURN_URL, $scriptName = 'index.php') {
// Validate: Check if still the same fe_user is logged in.
if (isset($GLOBALS["TSFE"]->fe_user->user["uid"])) {
......@@ -106,7 +106,7 @@ class Sip {
$clientArray[CLIENT_SIP] = $s;
if ($script[0] === '?')
$script = 'index.php' . $script;
$script = $scriptName . $script;
return $script . OnArray::toString($clientArray);
} else {
......
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