aboutsummaryrefslogtreecommitdiff
path: root/lib/models/ConsultationEvent.php
blob: 2a856c9406378687d43f243eb942d95b450f2021 (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
<?php
/**
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 5.1
 *
 * @property array $id alias for pk
 * @property int $slot_id database column
 * @property string $user_id database column
 * @property string $event_id database column
 * @property int $mkdate database column
 * @property ConsultationSlot $slot belongs_to ConsultationSlot
 * @property CalendarDate $event has_one CalendarDate
 */
class ConsultationEvent extends SimpleORMap
{
    protected static function configure($config = [])
    {
        $config['db_table'] = 'consultation_events';

        $config['belongs_to']['slot'] = [
            'class_name'  => ConsultationSlot::class,
            'foreign_key' => 'slot_id',
        ];
        $config['has_one']['event'] = [
            'class_name'        => CalendarDate::class,
            'foreign_key'       => 'event_id',
            'assoc_foreign_key' => 'id',
            'on_delete'         => 'delete',
        ];

        $config['registered_callbacks'] = [
            'before_delete' => [
                function (ConsultationEvent $event) {
                    if (!isset($event->event->calendars)) {
                        return;
                    }

                    // Suppress all mails from calendar for users that do not want to receive emails about
                    // consultation bookings or for calendar dates that lie in the past.
                    $event->event->calendars->each(function (CalendarDateAssignment $assignment) {
                        if (
                            empty($assignment->calendar_date->end)
                            || $assignment->calendar_date->end < time()
                            || (
                                $assignment->user
                                && !$assignment->user->getConfiguration()->CONSULTATION_SEND_MESSAGES
                            )

                        ) {
                            $assignment->suppress_mails = true;
                        }
                        $assignment->delete();
                    });
                },
            ],
        ];

        parent::configure($config);
    }
}