delete.php 4.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 12/23/15
 * Time: 6:16 PM
 */

namespace qfq;

use qfq;

require_once(__DIR__ . '/../qfq/QuickFormQuery.php');
14
require_once(__DIR__ . '/../qfq/store/Store.php');
15
require_once(__DIR__ . '/../qfq/Constants.php');
16
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
17
18
19


/**
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * delete: success
 *   SIP_MODE_ANSWER: MODE_HTML
 *     Send header 'location' to targetUrl
 *
 *   SUP_MODE_ANSWER: MODE_JSON
 *     Send AJAX answer with Status 'success'
 *
 * delete: failed
 *   SIP_MODE_ANSWER: MODE_HTML
 *     No forward, print error message.
 *
 *   SUP_MODE_ANSWER: MODE_JSON
 *     Send AJAX answer with Status 'error' and 'error message' as JSON encoded
 *
 *
 * JSON Format:
36
37
38
39
40
41
42
43
44
45
 *
 * status: success|error
 * message: <message>
 * redirect: client|url|no
 * redirect-url: <url>
 * field-name:<field name>
 * field-message: <message>
 *
 * Description:
 *
46
 * Delete successful.
47
48
49
50
 *  status = 'success'
 *  message = <message>
 *  redirect = 'client'
 *
51
 * Delete successful.
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 *  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;
66
$answer[API_MESSAGE] = '';
67
68
$answer[API_STATUS] = API_ANSWER_STATUS_ERROR;

69
70
71
72
73
74
$result[MSG_HEADER] = '';
$result[MSG_CONTENT] = '';

$modeAnswer = false;
$flagSuccess = false;

75
try {
76
    try {
77

78
        $qfq = new \qfq\QuickFormQuery(['bodytext' => '']);
79

80
        $answer = $qfq->delete();
81

82
83
        // in case everything is fine, an empty string is returned. Else an Array.
        $flagSuccess = ($answer === '');
84

85
86
        $targetUrl = Store::getVar(SIP_TARGET_URL, STORE_SIP);
        $modeAnswer = Store::getVar(SIP_MODE_ANSWER, STORE_SIP);
87

88
89
90
91
92
93
94
95
96
97
        switch ($modeAnswer) {
            case MODE_JSON:
                $answer = array();
                if ($flagSuccess) {
                    $answer[API_MESSAGE] = 'Deleted';
                    $answer[API_REDIRECT] = API_ANSWER_REDIRECT_AUTO;
                    $answer[API_STATUS] = API_ANSWER_STATUS_SUCCESS;
                } else {
                    $answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
                }
98

99
                break;
100

101
102
103
104
            case MODE_HTML:
                if ($targetUrl === false || $targetUrl === '') {
                    throw new CodeException('Missing target URL', ERROR_MISSING_VALUE);
                }
105

106
107
108
109
                if ($flagSuccess) {
                    $result[MSG_HEADER] = "Location: $targetUrl";
                }
                break;
110

111
112
113
114
            default:
                throw new CodeException('Unknown mode: ' . $modeAnswer, ERROR_UNKNOWN_MODE);
                break;
        }
115

116
117
    } catch (qfq\UserFormException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
        $val = Store::getVar(SYSTEM_FORM_ELEMENT, STORE_SYSTEM);
        if ($val !== false) {
            $answer[API_FIELD_NAME] = $val;
        }

        $val = Store::getVar(SYSTEM_FORM_ELEMENT_MESSAGE, STORE_SYSTEM);
        if ($val !== false) {
            $answer[API_FIELD_MESSAGE] = $val;
        }

    } catch (qfq\CodeException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
    } catch (qfq\DbException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
Carsten  Rose's avatar
Carsten Rose committed
133
    }
134

135
136
137
// 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);
Carsten  Rose's avatar
Carsten Rose committed
138
    }
139

140
141
142
143
} catch (\Exception $e) {
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}

144
145
146

if ($modeAnswer === MODE_JSON) {
    // JSON
147
148
    $result[MSG_HEADER] = "Content-Type: application/json";
    $result[MSG_CONTENT] = json_encode($answer);
149
150
151
152
153
154
155
156
} 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>";
        }
    }
157
158
}

159
// Send header, if given.
160
if ($result[MSG_HEADER] !== '') {
161
    header($result[MSG_HEADER]);
162
}
163

Carsten  Rose's avatar
Carsten Rose committed
164
echo $result[MSG_CONTENT];