save.php 2.26 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 * Created by PhpStorm.
 * User: ep
 * Date: 12/23/15
 * Time: 6:16 PM
Carsten  Rose's avatar
Carsten Rose committed
7
8
 */

9
10
11
12
13
14
namespace qfq;

use qfq;

require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
require_once(__DIR__ . '/../qfq/store/Store.php');
15
require_once(__DIR__ . '/../qfq/Constants.php');
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


/**
 * Return JSON encoded answer
 *
 * status: success|error
 * message: <message>
 * redirect: client|url|no
 * redirect-url: <url>
 * field-name:<field name>
 * field-message: <message>
 *
 * Description:
 *
 * Save successfull. Button 'close', 'new'. Form.forward: 'auto'. Client logic decide to redirect or not. Show message if no redirect.
 *  status = 'success'
 *  message = <message>
 *  redirect = 'client'
 *
 * Save successfull. Button 'close': Form.forward: 'page'. Client redirect to url.
 *  status = 'success'
 *  message = <message>
 *  redirect = 'url'
 *  redirect-url = <URL>
 *
 * Save failed: Button: any. Show message and set 'alert' on _optional_ specified form element. Bring 'pill' of specified form element to front.
 *  status = 'error'
 *  message = <message>
 *  redirect = 'no'
 *  Optional:
 *   field-name = <field name>
 *   field-message = <message appearing as tooltip (or similar) near the form element>
 */

$answer = array();

52
53
54
$answer[API_REDIRECT] = API_ANSWER_REDIRECT_CLIENT;
$answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
$answer[API_MESSAGE] = '';
55
56
57
58
59
60

try {
    $qfq = new \qfq\QuickFormQuery(['bodytext' => "debugShowStack=1"]);

    $qfq->saveForm();

61
62
63
    $answer[API_REDIRECT] = $qfq->getForwardMode($answer[API_REDIRECT_URL]);
    $answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
    $answer[API_MESSAGE] = 'save: success';
64
65

} catch (qfq\UserException $e) {
66
    $answer[API_MESSAGE] = $e->formatMessage();
67
68
69

    $val = Store::getVar(SYSTEM_FORM_ELEMENT, STORE_SYSTEM);
    if ($val !== false)
70
        $answer[API_FIELD_NAME] = $val;
71

72
    $val = Store::getVar(SYSTEM_FORM_ELEMENT_MESSAGE, STORE_SYSTEM);
73
    if ($val !== false)
74
        $answer[API_FIELD_MESSAGE] = $val;
75
76

} catch (qfq\CodeException $e) {
77
    $answer[API_MESSAGE] = $e->formatMessage();
78
} catch (qfq\DbException $e) {
79
    $answer[API_MESSAGE] = $e->formatMessage();
80
} catch (\Exception $e) {
81
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
82
83
}

Carsten  Rose's avatar
Carsten Rose committed
84
header("Content-Type: application/json");
85
86
echo json_encode($answer);