aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/UpdateInformation.php
blob: 959ef72353d4881bbe34716165fb7a783b59ea69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
 * Copyright (c) 2011  Rasmus Fuhse
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

/**
 * Class to set information that should be given to javascript.
 *
 * For a plugin to hand the information "test" to the javascript-function
 * STUDIP.myplugin.myfunction just put the line:
 *  if (UpdateInformation::isCollecting()) {
 *      UpdateInformation::setInformation("myplugin.myfunction", "test");
 *  }
 *
 * @author Rasmus Fuhse
 */
class UpdateInformation
{
    protected static $infos = [];
    protected static $collecting = null;
    protected static $request = null;

    /**
     * Returns the timestamp of the beginning of the run before.
     * Use this to only partially update new stuff.
     *
     * @return int Timestamp of the last run
     */
    public static function getTimestamp()
    {
        return Request::get('server_timestamp');
    }

    /**
     * Extracts updater data from request
     *
     * @return Array Request data (may be empty if no data is present)
     */
    protected static function getRequest()
    {
        if (self::$request === null) {
            self::$request = Request::getArray('page_info');
        }
        return self::$request ?: [];
    }

    /**
     * Checks whether the request has data for the given index.
     *
     * @return bool indicating whether there is data present for the given index
     */
    public static function hasData($index)
    {
        $request = self::getRequest();
        return isset($request[$index]);
    }

    /**
     * Returns request data for the given index.
     *
     * @param String $index Index to get the request data for
     * @return mixed Array with request data or null if index is invalid
     */
    public static function getData($index)
    {
        $request = self::getRequest();
        return $request[$index] ?: null;
    }

    /**
     * Gives information to the buffer for the javascript. The first parameter is
     * the name of the corresponding javascript-function minus the "STUDIP"
     * and the second parameter is the value handed to that function.
     * @param string $js_function : "test.testfunction" to get the JS-function "STUDIP.test.testfunction(information);"
     * @param mixed $information : anything that could be translated into a json-object
     */
    public static function setInformation($js_function, $information)
    {
        self::$infos[$js_function] = $information;
    }

    /**
     * returns the information to give it to javascript
     * @return array
     */
    public static function getInformation()
    {
        return self::$infos;
    }

    /**
     * returns if this request is a request, that wants to collect information
     * to hand it to javascript. Ask for this in your SystemPlugin-constructor.
     * @return: boolean
     */
    public static function isCollecting()
    {
        if (self::$collecting === null) {
            $page = $_SERVER['REQUEST_URI'];
            if (mb_strpos($page, "?") !== false) {
                $page = mb_substr($page, 0, mb_strpos($page, "?"));
            }
            self::$collecting = (mb_stripos($page, "dispatch.php/jsupdater/get") !== false);

            // If we are collecting, store the current timestamp
            if (self::$collecting) {
                self::$infos['server_timestamp'] = time();
            }
        }
        return self::$collecting;
    }
}