aboutsummaryrefslogtreecommitdiff
path: root/lib/models/EventData.class.php
blob: 75e6f0c36ea03adb2084d5da25c041458ab2fe9c (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
<?php
/**
 * EventData.class.php - Model class for calendar events.
 *
 * 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.
 *
 * @author      Peter Thienel <thienel@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.2
 *
 * @property string $id alias column for event_id
 * @property string $event_id database column
 * @property string $author_id database column
 * @property string|null $editor_id database column
 * @property string $uid database column
 * @property int $start database column
 * @property int $end database column
 * @property string $summary database column
 * @property string|null $description database column
 * @property string $class database column
 * @property string|null $categories database column
 * @property int $category_intern database column
 * @property int $priority database column
 * @property string|null $location database column
 * @property int $ts database column
 * @property int|null $linterval database column
 * @property int|null $sinterval database column
 * @property string|null $wdays database column
 * @property int|null $month database column
 * @property int|null $day database column
 * @property string $rtype database column
 * @property int $duration database column
 * @property int|null $count database column
 * @property int $expire database column
 * @property string|null $exceptions database column
 * @property int $mkdate database column
 * @property int $chdate database column
 * @property int $importdate database column
 * @property SimpleORMapCollection|CalendarEvent[] $calendars has_many CalendarEvent
 * @property User $author belongs_to User
 * @property User|null $editor belongs_to User
 */

class EventData extends SimpleORMap implements PrivacyObject
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'event_data';

        $config['belongs_to']['author'] = [
            'class_name'  => User::class,
            'foreign_key' => 'author_id',
        ];
        $config['belongs_to']['editor'] = [
            'class_name'  => User::class,
            'foreign_key' => 'editor_id',
        ];
        $config['has_many']['calendars'] = [
            'class_name'  => CalendarEvent::class,
            'foreign_key' => 'event_id'
        ];

        $config['default_values']['linterval'] = 0;
        $config['default_values']['sinterval'] = 0;

        $config['registered_callbacks']['before_create'][] = 'cbDefaultValues';

        parent::configure($config);

    }

    public function delete()
    {
        // do not delete until one calendar is left
        if (sizeof($this->calendars) > 1) {
            return false;
        }
        $calendars = $this->calendars;
        $ret = parent::delete();
        // only one calendar is left
        if ($ret) {
            $calendars->each(function($c) { $c->delete(); });
        }
        return $ret;
    }

    public static function garbageCollect()
    {
        DBManager::get()->query('DELETE event_data '
                . 'FROM calendar_event LEFT JOIN event_data USING(event_id)'
                . 'WHERE range_id IS NULL');
    }

    public function getDefaultValue($field)
    {
        if ($field == 'start') {
            return time();
        }
        if ($field == 'end' && $this->content['start']) {
            return $this->content['start'] + 3600;
        }
        if ($field == 'ts' && $this->content['start']) {
            return mktime(12, 0, 0, date('n', $this->content['start']),
                date('j', $this->content['start']), date('Y', $this->content['start']));
        }
        return parent::getDefaultValue($field);
    }

    protected function cbDefaultValues()
    {
        if (empty($this->content['uid'])) {
            $this->content['uid'] = 'Stud.IP-' . $this->event_id . '@' . $_SERVER['SERVER_NAME'];
        }
    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $sorm = EventData::findThru($storage->user_id, [
            'thru_table'        => 'calendar_event',
            'thru_key'          => 'range_id',
            'thru_assoc_key'    => 'event_id',
            'assoc_foreign_key' => 'event_id',
        ]);
        if ($sorm) {
            $field_data = [];
            foreach ($sorm as $row) {
                $field_data[] = $row->toRawArray();
            }
            if ($field_data) {
                $storage->addTabularData(_('Kalender Einträge'), 'event_data', $field_data);
            }
        }
    }
}