summaryrefslogtreecommitdiff
path: root/includes/mailer/shifts_mailer.php
blob: 37b26838538d315cab2d0e9d6fa3130cf859643c (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
<?php

use Engelsystem\Models\User\User;

/**
 * @param array $old_shift
 * @param array $new_shift
 */
function mail_shift_change($old_shift, $new_shift)
{
    $users = ShiftEntries_by_shift($old_shift['SID']);
    $old_room = Room($old_shift['RID']);
    $new_room = Room($new_shift['RID']);

    $noticeable_changes = false;

    $message = __('A Shift you are registered on has changed:');
    $message .= "\n";

    if ($old_shift['name'] != $new_shift['name']) {
        $message .= sprintf(__('* Shift type changed from %s to %s'), $old_shift['name'], $new_shift['name']) . "\n";
        $noticeable_changes = true;
    }

    if ($old_shift['title'] != $new_shift['title']) {
        $message .= sprintf(__('* Shift title changed from %s to %s'), $old_shift['title'], $new_shift['title']) . "\n";
        $noticeable_changes = true;
    }

    if ($old_shift['start'] != $new_shift['start']) {
        $message .= sprintf(
                __('* Shift Start changed from %s to %s'),
                date('Y-m-d H:i', $old_shift['start']),
                date('Y-m-d H:i', $new_shift['start'])
            ) . "\n";
        $noticeable_changes = true;
    }

    if ($old_shift['end'] != $new_shift['end']) {
        $message .= sprintf(
                __('* Shift End changed from %s to %s'),
                date('Y-m-d H:i', $old_shift['end']),
                date('Y-m-d H:i', $new_shift['end'])
            ) . "\n";
        $noticeable_changes = true;
    }

    if ($old_shift['RID'] != $new_shift['RID']) {
        $message .= sprintf(__('* Shift Location changed from %s to %s'), $old_room['Name'], $new_room['Name']) . "\n";
        $noticeable_changes = true;
    }

    if (!$noticeable_changes) {
        // There are no changes worth sending an E-Mail
        return;
    }

    $message .= "\n";
    $message .= __('The updated Shift:') . "\n";

    $message .= $new_shift['name'] . "\n";
    $message .= $new_shift['title'] . "\n";
    $message .= date('Y-m-d H:i', $new_shift['start']) . ' - ' . date('H:i', $new_shift['end']) . "\n";
    $message .= $new_room['Name'] . "\n";

    foreach ($users as $user) {
        $user = (new User())->forceFill($user);
        if ($user->settings->email_shiftinfo) {
            engelsystem_email_to_user(
                $user,
                __('Your Shift has changed'),
                $message,
                true
            );
        }
    }
}

/**
 * @param array $shift
 */
function mail_shift_delete($shift)
{
    $users = ShiftEntries_by_shift($shift['SID']);
    $room = Room($shift['RID']);

    $message = __('A Shift you are registered on was deleted:') . "\n";

    $message .= $shift['name'] . "\n";
    $message .= $shift['title'] . "\n";
    $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
    $message .= $room['Name'] . "\n";

    foreach ($users as $user) {
        $user = (new User())->forceFill($user);
        if ($user->settings->email_shiftinfo) {
            engelsystem_email_to_user($user, __('Your Shift was deleted'), $message, true);
        }
    }
}

/**
 * @param User  $user
 * @param array $shift
 */
function mail_shift_assign($user, $shift)
{
    if (!$user->settings->email_shiftinfo) {
        return;
    }

    $room = Room($shift['RID']);

    $message = __('You have been assigned to a Shift:') . "\n";
    $message .= $shift['name'] . "\n";
    $message .= $shift['title'] . "\n";
    $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
    $message .= $room['Name'] . "\n";

    engelsystem_email_to_user($user, __('Assigned to Shift'), $message, true);
}

/**
 * @param User  $user
 * @param array $shift
 */
function mail_shift_removed($user, $shift)
{
    if (!$user->settings->email_shiftinfo) {
        return;
    }

    $room = Room($shift['RID']);

    $message = __('You have been removed from a Shift:') . "\n";
    $message .= $shift['name'] . "\n";
    $message .= $shift['title'] . "\n";
    $message .= date('Y-m-d H:i', $shift['start']) . ' - ' . date('H:i', $shift['end']) . "\n";
    $message .= $room['Name'] . "\n";

    engelsystem_email_to_user($user, __('Removed from Shift'), $message, true);
}