save.php 2.89 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
require_once(__DIR__ . '/../qfq/exceptions/UserFormException.php');
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
19
20
21
22
23
24
25
26
27


/**
 * Return JSON encoded answer
 *
 * status: success|error
 * message: <message>
 * redirect: client|url|no
 * redirect-url: <url>
28
 * field-name: <field name>
29
 * field-message: <message>
30
 * form-data: [ fieldname1 => value1, fieldname2 => value2, ... ]
Carsten  Rose's avatar
Carsten Rose committed
31
32
 * form-control: [ fieldname1 => status1, fieldname2 => status2, ... ]  status: show|hide, enabled|disabled,
 * readonly|readwrite
33
34
35
 *
 * Description:
 *
36
 * Save successful. Button 'close', 'new'. Form.forward: 'auto'. Client logic decide to redirect or not. Show message
Carsten  Rose's avatar
Carsten Rose committed
37
 * if no redirect. status = 'success' message = <message> redirect = 'client'
38
 *
39
 * Save successful. Button 'close': Form.forward: 'page'. Client redirect to url.
40
41
42
43
44
 *  status = 'success'
 *  message = <message>
 *  redirect = 'url'
 *  redirect-url = <URL>
 *
Carsten  Rose's avatar
Carsten Rose committed
45
46
47
 * 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>
48
49
50
51
 */

$answer = array();

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

try {
Carsten  Rose's avatar
Carsten Rose committed
57
    $qfq = new \qfq\QuickFormQuery(['bodytext' => ""]);
58

59
    $data = $qfq->saveForm();
60

61
62
63
    $arr = $qfq->getForwardMode();
    $answer[API_REDIRECT] = $arr[API_REDIRECT];
    $answer[API_REDIRECT_URL] = $arr[API_REDIRECT_URL];
64

65
    $answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
66
    $answer[API_MESSAGE] = 'Saved';
67
68
69
70
71
72
73
    if (isset($data[API_ELEMENT_UPDATE])) {
        $answer[API_ELEMENT_UPDATE] = $data[API_ELEMENT_UPDATE];
    }

    if (isset($data[API_FORM_UPDATE])) {
        $answer[API_FORM_UPDATE] = $data[API_FORM_UPDATE];
    }
74

75
} catch (qfq\UserFormException $e) {
76
    $answer[API_MESSAGE] = $e->formatMessage();
77
78

    $val = Store::getVar(SYSTEM_FORM_ELEMENT, STORE_SYSTEM);
Carsten  Rose's avatar
Carsten Rose committed
79
    if ($val !== false) {
80
        $answer[API_FIELD_NAME] = $val;
Carsten  Rose's avatar
Carsten Rose committed
81
    }
82

83
    $val = Store::getVar(SYSTEM_FORM_ELEMENT_MESSAGE, STORE_SYSTEM);
Carsten  Rose's avatar
Carsten Rose committed
84
    if ($val !== false) {
85
        $answer[API_FIELD_MESSAGE] = $val;
Carsten  Rose's avatar
Carsten Rose committed
86
    }
87
88

} catch (qfq\CodeException $e) {
89
    $answer[API_MESSAGE] = $e->formatMessage();
90
} catch (qfq\DbException $e) {
91
    $answer[API_MESSAGE] = $e->formatMessage();
92
} catch (\Exception $e) {
93
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
94
95
}

Carsten  Rose's avatar
Carsten Rose committed
96
header("Content-Type: application/json");
97
98
echo json_encode($answer);