Support.php 5.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/28/16
 * Time: 8:05 AM
 */

namespace qfq;

Carsten  Rose's avatar
Carsten Rose committed
11
require_once(__DIR__ . '/Sanitize.php');
12
13
14

class Support {

15
16
17
18
    /**
     * @param array $queryArray Empty or prefilled assoc array with url parameter
     */
    public static function appendTypo3ParameterToArray(array &$queryArray) {
19
20
//        if (isset($_GET['id']))
//            $queryArray['id'] = self::getCurrentPage();
21
22
        if (isset($GLOBALS["TSFE"]->id))
            $queryArray['id'] = $GLOBALS["TSFE"]->id;
23
24
25
26
27
28
29
30
31

        if (isset($_GET['L']))
            $queryArray['L'] = $_GET['L'];

        if (isset($_GET['type']))
            $queryArray['type'] = $_GET['type'];

    }

32
    /**
33
34
     * Builds a urlencoded query string of an assoc array.
     *
35
     * @param array $queryArray
36
     * @return string Querystring (e.g.: id=23&type=99
37
38
39
40
41
42
43
44
45
46
47
48
49
50
     */
    public static function arrayToQueryString(array $queryArray) {
        $items = array();

        foreach ($queryArray as $key => $value) {
            if (is_int($key)) {
                $items[] = urlencode($value);
            } else {
                $items[] = $key . '=' . urlencode($value);
            }
        }

        return implode('&', $items);
    }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    /**
     * Extract Tag from $tag (might contain further attributes) and wrap it around $value. If $flagOmitEmpty==true && $value=='': return ''.
     *
     * @param $tag
     * @param $value
     * @param bool|false $omitIfValueEmpty
     * @return string
     */
    public static function wrapTag($tag, $value, $omitIfValueEmpty = false) {
        if ($omitIfValueEmpty && $value === "")
            return '';

        // a) <div class="container-fluid">, b) <label>
        $arr = explode(' ', $tag);

        $tagPlain = (count($arr) === 1) ? substr($arr[0], 1, strlen($arr[0]) - 2) : substr($arr[0], 1);
        $closing = '</' . $tagPlain . '>';

        return $tag . $value . $closing;
    }

73
74
75
76
77
    /**
     * Format's an attribute: $type=$value. If $flagOmitEmpty==true && $value=='': return ''.
     *
     * @param $type
     * @param $value
Carsten  Rose's avatar
Carsten Rose committed
78
     * @param bool $flagOmitEmpty
79
80
81
82
83
84
85
86
87
     * @return string
     */
    public static function doAttribute($type, $value, $flagOmitEmpty = true) {
        if ($flagOmitEmpty && $value === "")
            return '';

        return $type . '="' . trim($value) . '" ';
    }

88
89
    /**
     * Wraps some $inner fragment with a CSS styled $tooltipText . CSS is configured in 'Resources/Public/qfq-jqw.css'.
90
     *
91
     * Based on: http://www.w3schools.com/howto/howto_css_tooltip.asp
92
     *
93
     * @param $before
94
95
96
     * @param $tooltipText
     * @return string
     */
97
    public static function appendTooltip($before, $tooltipText) {
98

99
100
101
102
        // addslashes, htmlentities & htmlspecialchars
        // str_replace('"', '&quot;', $tooltipText)

        return $before . "<img src='" . GFX_INFO . "' title=\"" . htmlentities($tooltipText) . "\">";
103
104
    }

105
    /**
106
107
108
109
110
111
112
     * Search for the parameter $needle in $haystack. The argumenst has to be seperated by ','.
     *
     * Returns the false if not found or index of found place. Be carefull: use unary operator to compare for 'false'
     *
     * @param $needle
     * @param $haystack
     * @return boolean     true if found, else false
113
     */
114
115
116
117
    public static function findInSet($needle, $haystack) {
        $arr = explode(',', $haystack);

        return array_search($needle, $arr) !== false;
118

119
    }
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    /**
     * Converts a dateTime String to the english format:
     * 1.2.79 > 1979-02-01 00:00:00
     * 01.02.13 3:24 >  1979-02-01 03:24:00
     * 1.2.1979 14:21:5 > 1979-02-01 14:21:05
     *
     * @param $dateTimeString
     * @return string
     * @throws UserException
     */
    public static function dateTime2mysql($dateTimeString) {
        $dateRaw = '';
        $timeRaw = '';

        $tmpArr = explode(' ', $dateTimeString);
        switch (count($tmpArr)) {
            case 0:
                return '';

            case 1:
                if (strpos($tmpArr[0], '.') === false) {
                    $timeRaw = $tmpArr[0];
                } else {
                    $dateRaw = $tmpArr[0];
                }
                break;

            case 2:
                $dateRaw = $tmpArr[0];
                $timeRaw = $tmpArr[1];
                break;

            default:
                throw new UserException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
                break;
        }

        if ($dateRaw === '') {
            $date = '0000-00-00';
        } else {
            $tmpArr = explode('.', $dateRaw);
            if ($tmpArr[2] < 70) {
                $tmpArr[2] = 2000 + $tmpArr[2];
            } elseif ($tmpArr[2] < 100) {
                $tmpArr[2] = 1900 + $tmpArr[2];
            }
            $date = sprintf("%04d-%02d-%02d", $tmpArr[2], $tmpArr[1], $tmpArr[0]);
        }

        if ($timeRaw === '') {
            $time = '00:00:00';
        } else {
            $tmpArr = explode(':', $timeRaw);
            switch (count($tmpArr)) {
                case 2:
                    $time = sprintf("%02d:%02d:00", $tmpArr[0], $tmpArr[1]);
                    break;
                case 3:
                    $time = sprintf("%02d:%02d:%02d", $tmpArr[0], $tmpArr[1], $tmpArr[2]);
                    break;
                default:
                    throw new UserException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
                    break;
            }
        }

        return $date . ' ' . $time;
    }
189
}