1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2008 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24include_once './Services/Mail/classes/class.ilMailNotification.php';
25
26/**
27* Distributes calendar mail notifications
28*
29* @author Stefan Meyer <smeyer.ilias@gmx.de>
30* @version $Id$
31*
32* @ingroup ServicesCalendar
33*/
34class ilCalendarMailNotification extends ilMailNotification
35{
36    const TYPE_GRP_NOTIFICATION = 1;
37    const TYPE_GRP_NEW_NOTIFICATION = 2;
38    const TYPE_CRS_NOTIFICATION = 3;
39    const TYPE_CRS_NEW_NOTIFICATION = 4;
40    const TYPE_BOOKING_CONFIRMATION = 5;
41    const TYPE_BOOKING_CANCELLATION = 6;
42    const TYPE_USER = 7;
43    const TYPE_USER_ANONYMOUS = 8;
44    const TYPE_BOOKING_REMINDER = 9;
45
46    private $appointment_id = null;
47
48    /**
49     * Constructor
50     */
51    public function __construct()
52    {
53        $this->setSender(ANONYMOUS_USER_ID);
54    }
55
56    /**
57     * Set calendar appointment id
58     * @param object $a_id
59     * @return
60     */
61    public function setAppointmentId($a_id)
62    {
63        $this->appointment_id = $a_id;
64
65        include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
66        $this->appointment = new ilCalendarEntry($this->getAppointmentId());
67    }
68
69    /**
70     * Get appointment
71     * @return ilCalendarEntry
72     */
73    public function getAppointment()
74    {
75        return $this->appointment;
76    }
77
78    /**
79     * get appointment id
80     * @return
81     */
82    public function getAppointmentId()
83    {
84        return $this->appointment_id;
85    }
86
87    public function appendAppointmentDetails()
88    {
89        include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
90        $app = new ilCalendarEntry($this->getAppointmentId());
91        $this->appendBody($app->appointmentToMailString($this->getLanguage()));
92    }
93
94
95    /**
96     *
97     * @return
98     */
99    public function send()
100    {
101        global $DIC;
102
103        $rbacreview = $DIC['rbacreview'];
104        $lng = $DIC['lng'];
105
106        switch ($this->getType()) {
107            case self::TYPE_USER:
108                $rcp = array_pop($this->getRecipients());
109                $this->initLanguage($rcp);
110                $this->getLanguage()->loadLanguageModule('dateplaner');
111                $this->initMail();
112                $this->setSubject(
113                    sprintf(
114                        $this->getLanguageText('cal_mail_notification_subject'),
115                        $this->getAppointment()->getTitle()
116                    )
117                );
118                $this->setBody(ilMail::getSalutation($rcp, $this->getLanguage()));
119                $this->appendBody("\n\n");
120                $this->appendBody(
121                    $this->getLanguageText('cal_mail_notification_body')
122                );
123                $this->appendBody("\n\n");
124                $this->appendAppointmentDetails();
125                $this->appendBody("\n\n");
126                $this->getMail()->appendInstallationSignature(true);
127                $this->addAttachment();
128
129                $this->sendMail(
130                    $this->getRecipients(),
131                    array('system'),
132                    true
133                );
134                break;
135
136            case self::TYPE_USER_ANONYMOUS:
137
138                $rcp = array_pop($this->getRecipients());
139
140                $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
141                $this->getLanguage()->loadLanguageModule('dateplaner');
142                $this->getLanguage()->loadLanguageModule('mail');
143                $this->initMail();
144                $this->setSubject(
145                    sprintf(
146                        $this->getLanguageText('cal_mail_notification_subject'),
147                        $this->getAppointment()->getTitle()
148                    )
149                );
150                $this->setBody(ilMail::getSalutation(0, $this->getLanguage()));
151                $this->appendBody("\n\n");
152                $this->appendBody(
153                    $this->getLanguageText('cal_mail_notification_body')
154                );
155                $this->appendBody("\n\n");
156                $this->appendAppointmentDetails();
157                $this->appendBody("\n\n");
158                $this->getMail()->appendInstallationSignature(true);
159                $this->addAttachment();
160
161                $this->sendMail(
162                    $this->getRecipients(),
163                    array('email'),
164                    false
165                );
166                break;
167
168            case self::TYPE_GRP_NEW_NOTIFICATION:
169
170                $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
171                $this->getLanguage()->loadLanguageModule('grp');
172                $this->getLanguage()->loadLanguageModule('dateplaner');
173                $this->initMail();
174                $this->setSubject(
175                    sprintf($this->getLanguageText('cal_grp_new_notification_sub'), $this->getObjectTitle(true))
176                );
177                $this->setBody($this->getLanguageText('grp_notification_salutation'));
178                $this->appendBody("\n\n");
179                $this->appendBody(
180                    sprintf($this->getLanguageText('cal_grp_new_notification_body'), $this->getObjectTitle(true))
181                );
182                $this->appendBody("\n\n");
183                $this->appendBody($this->getLanguageText('grp_mail_permanent_link'));
184                $this->appendBody("\n\n");
185
186                $this->appendAppointmentDetails();
187
188                $this->appendBody("\n\n");
189                $this->appendBody($this->createPermanentLink());
190                $this->getMail()->appendInstallationSignature(true);
191
192                $this->addAttachment();
193
194                $this->sendMail(array('#il_grp_admin_' . $this->getRefId(),'#il_grp_member_' . $this->getRefId()), array('system'), false);
195                break;
196
197            case self::TYPE_GRP_NOTIFICATION:
198
199                $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
200                $this->getLanguage()->loadLanguageModule('grp');
201                $this->getLanguage()->loadLanguageModule('dateplaner');
202                $this->initMail();
203                $this->setSubject(
204                    sprintf($this->getLanguageText('cal_grp_notification_sub'), $this->getObjectTitle(true))
205                );
206                $this->setBody($this->getLanguageText('grp_notification_salutation'));
207                $this->appendBody("\n\n");
208                $this->appendBody(
209                    sprintf($this->getLanguageText('cal_grp_notification_body'), $this->getObjectTitle(true))
210                );
211                $this->appendBody("\n\n");
212
213                $this->appendAppointmentDetails();
214
215                $this->appendBody("\n\n");
216                $this->appendBody($this->getLanguageText('grp_mail_permanent_link'));
217                $this->appendBody("\n\n");
218                $this->appendBody($this->createPermanentLink());
219                $this->getMail()->appendInstallationSignature(true);
220
221                $this->addAttachment();
222
223                $this->sendMail(array('#il_grp_admin_' . $this->getRefId(),'#il_grp_member_' . $this->getRefId()), array('system'), false);
224                break;
225
226            case self::TYPE_CRS_NEW_NOTIFICATION:
227
228                $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
229                $this->getLanguage()->loadLanguageModule('crs');
230                $this->getLanguage()->loadLanguageModule('dateplaner');
231                $this->initMail();
232                $this->setSubject(
233                    sprintf($this->getLanguageText('cal_crs_new_notification_sub'), $this->getObjectTitle(true))
234                );
235                $this->setBody($this->getLanguageText('crs_notification_salutation'));
236                $this->appendBody("\n\n");
237                $this->appendBody(
238                    sprintf($this->getLanguageText('cal_crs_new_notification_body'), $this->getObjectTitle(true))
239                );
240                $this->appendBody("\n\n");
241                $this->appendBody($this->getLanguageText('crs_mail_permanent_link'));
242                $this->appendBody("\n\n");
243                $this->appendBody($this->createPermanentLink());
244                $this->appendBody("\n\n");
245                $this->appendAppointmentDetails();
246
247                $this->getMail()->appendInstallationSignature(true);
248
249                $this->addAttachment();
250
251                $this->sendMail(array('#il_crs_admin_' . $this->getRefId(),'#il_crs_tutor_' . $this->getRefId(),'#il_crs_member_' . $this->getRefId()), array('system'), false);
252                break;
253
254            case self::TYPE_CRS_NOTIFICATION:
255
256                $this->setLanguage(ilLanguageFactory::_getLanguage($lng->getDefaultLanguage()));
257                $this->getLanguage()->loadLanguageModule('crs');
258                $this->getLanguage()->loadLanguageModule('dateplaner');
259                $this->initMail();
260                $this->setSubject(
261                    sprintf($this->getLanguageText('cal_crs_notification_sub'), $this->getObjectTitle(true))
262                );
263                $this->setBody($this->getLanguageText('crs_notification_salutation'));
264                $this->appendBody("\n\n");
265                $this->appendBody(
266                    sprintf($this->getLanguageText('cal_crs_notification_body'), $this->getObjectTitle(true))
267                );
268                $this->appendBody("\n\n");
269
270                $this->appendAppointmentDetails();
271
272                $this->appendBody("\n\n");
273                $this->appendBody($this->getLanguageText('crs_mail_permanent_link'));
274                $this->appendBody("\n\n");
275                $this->appendBody($this->createPermanentLink());
276                $this->getMail()->appendInstallationSignature(true);
277
278                $this->addAttachment();
279
280                $this->sendMail(array('#il_crs_admin_' . $this->getRefId(),'#il_crs_tutor_' . $this->getRefId(),'#il_crs_member_' . $this->getRefId()), array('system'), false);
281                break;
282
283            case self::TYPE_BOOKING_CONFIRMATION:
284
285                $user_id = array_pop($this->getRecipients());
286                include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
287                include_once 'Services/Booking/classes/class.ilBookingEntry.php';
288                $entry = new ilCalendarEntry($this->getAppointmentId());
289                $booking = new ilBookingEntry($entry->getContextId());
290
291                $this->initLanguage($user_id);
292                $this->getLanguage()->loadLanguageModule('dateplaner');
293                $this->initMail();
294                $this->setSubject(
295                    sprintf($this->getLanguageText('cal_booking_confirmation_subject'), $entry->getTitle())
296                );
297                $this->setBody(ilMail::getSalutation($user_id, $this->getLanguage()));
298                $this->appendBody("\n\n");
299                $this->appendBody(
300                    sprintf($this->getLanguageText('cal_booking_confirmation_body'), ilObjUser::_lookupFullname($booking->getObjId()))
301                );
302                $this->appendBody("\n\n");
303
304                $this->appendAppointmentDetails($booking);
305
306                /*
307                $this->appendBody("\n\n");
308                $this->appendBody($this->getLanguageText('cal_booking_confirmation_link'));
309                $this->appendBody("\n\n");
310                $this->appendBody($this->createPermanentLink());
311                 */
312                $this->getMail()->appendInstallationSignature(true);
313
314                $this->sendMail(array($user_id), array('system'), true);
315
316                $this->appendBody("\n\n");
317                $this->appendBody($this->getLanguageText('cal_booking_confirmation_user') . "\n");
318                $this->appendBody(ilObjUser::_lookupFullname($user_id));
319
320                $this->sendMail(array($booking->getObjId()), array('system'), true);
321                break;
322
323            case self::TYPE_BOOKING_CANCELLATION:
324
325                $user_id = array_pop($this->getRecipients());
326                include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
327                include_once 'Services/Booking/classes/class.ilBookingEntry.php';
328                $entry = new ilCalendarEntry($this->getAppointmentId());
329                $booking = new ilBookingEntry($entry->getContextId());
330
331                $user_id = array_pop($this->getRecipients());
332                $this->initLanguage($user_id);
333                $this->getLanguage()->loadLanguageModule('dateplaner');
334                $this->initMail();
335                $this->setSubject(
336                    sprintf($this->getLanguageText('cal_booking_cancellation_subject'), $entry->getTitle())
337                );
338                $this->setBody(ilMail::getSalutation($user_id, $this->getLanguage()));
339                $this->appendBody("\n\n");
340                $this->appendBody(
341                    sprintf($this->getLanguageText('cal_booking_cancellation_body'), ilObjUser::_lookupFullname($booking->getObjId()))
342                );
343                $this->appendBody("\n\n");
344
345                $this->appendAppointmentDetails($booking);
346
347                $this->getMail()->appendInstallationSignature(true);
348
349                $this->sendMail(array($user_id), array('system'), true);
350
351                $this->appendBody("\n\n");
352                $this->appendBody($this->getLanguageText('cal_booking_cancellation_user') . "\n");
353                $this->appendBody(ilObjUser::_lookupFullname($user_id));
354
355                $this->sendMail(array($booking->getObjId()), array('system'), true);
356                break;
357
358            case ilCalendarMailNotification::TYPE_BOOKING_REMINDER:
359
360                $user_id = array_pop($this->getRecipients());
361
362                include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
363                include_once 'Services/Booking/classes/class.ilBookingEntry.php';
364                $entry = new ilCalendarEntry($this->getAppointmentId());
365                $booking = new ilBookingEntry($entry->getContextId());
366
367                $this->initLanguage($user_id);
368                $this->getLanguage()->loadLanguageModule('dateplaner');
369                $this->initMail();
370                $this->setSubject(
371                    sprintf($this->getLanguageText('cal_ch_booking_reminder_subject'), $entry->getTitle())
372                );
373                $this->setBody(ilMail::getSalutation($user_id, $this->getLanguage()));
374                $this->appendBody("\n\n");
375                $this->appendBody(
376                    sprintf($this->getLanguageText('cal_ch_booking_reminder_body'), ilObjUser::_lookupFullname($booking->getObjId()))
377                );
378                $this->appendBody("\n\n");
379
380                $this->appendAppointmentDetails($booking);
381
382                $this->getMail()->appendInstallationSignature(true);
383                $this->sendMail(array($user_id), array('system'), true);
384                break;
385        }
386
387        $this->deleteAttachments();
388    }
389
390    protected function addAttachment()
391    {
392        global $DIC;
393
394        $ilUser = $DIC['ilUser'];
395
396        include_once './Services/Calendar/classes/Export/class.ilCalendarExport.php';
397        $export = new ilCalendarExport();
398        $export->setExportType(ilCalendarExport::EXPORT_APPOINTMENTS);
399        $export->setAppointments(array($this->getAppointmentId()));
400        $export->export();
401
402        include_once './Services/Mail/classes/class.ilFileDataMail.php';
403        $attachment = new ilFileDataMail($this->getSender());
404        $attachment->storeAsAttachment(
405            'appointment.ics',
406            $export->getExportString()
407        );
408
409        $this->setAttachments(
410            array(
411                'appointment.ics'
412            )
413        );
414    }
415
416    /**
417     * Delete attachments
418     */
419    protected function deleteAttachments()
420    {
421        include_once './Services/Mail/classes/class.ilFileDataMail.php';
422        $attachment = new ilFileDataMail($this->getSender());
423        $attachment->unlinkFiles($this->getAttachments());
424    }
425}
426