aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StoredUserData.php
blob: e18783a20bc2c361c476d2da724e7df22a83b87f (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
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
<?php
/**
 * This class is used to transport all data a plugin might have stored for a
 * specific user.
 * Stored data may be a number of files, tabular data or urls.
 *
 * @license GPL2 or any later version
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 */
class StoredUserData
{
    public $user_id;

    protected $data = [
        'file'    => [],
        'tabular' => [],
    ];

    /**
     * Construct a storage object for a specific user.
     *
     * @param User $user User object
     */
    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

    /**
     * Returns the associated user.
     *
     * @return User object
     */
    public function getUserId()
    {
        return $this->user_id;
    }

    /**
     * Returns whether this object actually contains any data.
     *
     * @return bool
     */
    public function hasData()
    {
        return array_sum(array_map('count', $this->data)) > 0;
    }

    /**
     * Adds tabular data under the specified key. By passing an optional
     * context, the data may be associated with this context.
     *
     * @param string      $name Display label
     * @param string      $key Table name (e.g. database table)
     * @param array       $value Array containing the rows
     * @param SimpleORMap $context Optional context
     */
    public function addTabularData($name, $key, array $value, SimpleORMap $context = null)
    {
        if ($value) {
            $this->addData('tabular', compact('name', 'key', 'value'), $context);
        }
    }

    /**
     * Adds a file reference. By passing an optional context, the data may be
     * associated with this context.
     *
     * @param FileRef     $fileref
     * @param SimpleORMap $context Optional context
     */
    public function addFileRef(FileRef $fileref, SimpleORMap $context = null)
    {
        $filetype = $fileref->getFileType();

        if ($filetype instanceof URLFile) {
            $this->addFileWithContents(
                $filetype->getFilename() . '.url',
                "[InternetShortcut]\nURL={$filetype->getDownloadURL()}\n",
                $context
            );
        } else if ($filetype->getPath()) {
            $this->addFileAtPath($filetype->getFilename(), $filetype->getPath(), $context);
        }
    }

    /**
     * Adds a local file on disk. By passing an optional context, the data may be
     * associated with this context.
     *
     * @param string      $name File name
     * @param string      $path File path
     * @param SimpleORMap $context Optional context
     */
    public function addFileAtPath($name, $path, SimpleORMap $context = null)
    {
        $this->addData('file', compact('name', 'path'), $context);
    }

    /**
     * Adds content as a file. By passing an optional context, the data may be
     * associated with this context.
     *
     * @param string      $name File name
     * @param string      $contents File contents (text or binary)
     * @param SimpleORMap $context Optional context
     */
    public function addFileWithContents($name, $contents, SimpleORMap $context = null)
    {
        $this->addData('file', compact('name', 'contents'), $context);
    }

    /**
     * Returns the stored file data for all contexts (if $context is null)
     * or a specific context.
     *
     * @param SimpleORMap $context Optional context
     * @return array
     */
    public function getFileData(SimpleORMap $context = null)
    {
        return $this->getData('file', $context);
    }

    /**
     * Returns the stored tabular data for all contexts (if $context is null)
     * or a specific context.
     *
     * @param SimpleORMap $context Optional context
     * @return array
     */
    public function getTabularData(SimpleORMap $context = null)
    {
        return $this->getData('tabular', $context);
    }

    /**
     * Adds stored data. By passing an optional context, the data may be
     * associated with this context.
     *
     * @param string      $type    Type of data
     * @param mixed       $data
     * @param SimpleORMap $context Optional context
     */
    protected function addData($type, $data, SimpleORMap $context = null)
    {
        if (!isset($this->data[$type])) {
            throw new InvalidArgumentException('Invalid data type');
        }

        $this->data[$type][] = $data + compact('context');
    }

    /**
     * Returns the stored data of the given type for all contexts
     * (if $context is null) or a specific context.
     *
     * @param string      $type    Type of data
     * @param SimpleORMap $context Optional context
     * @return array
     */
    protected function getData($type, SimpleORMap $context = null)
    {
        if (!isset($this->data[$type])) {
            throw new InvalidArgumentException('Invalid data type');
        }

        $data = $this->data[$type];

        if ($context) {
            $data = array_filter($data, function ($item) use ($context) {
                return $item['context'] instanceof $context && $item['context']->id == $context->id;
            });
        }

        return $data;
    }
}