Completed
Push — master ( f80d51...8f85be )
by Markus
05:26 queued 03:30
created

Event::setDescriptionHTML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eluceo\iCal\Component;
13
14
use Eluceo\iCal\Component;
15
use Eluceo\iCal\Property;
16
use Eluceo\iCal\Property\DateTimeProperty;
17
use Eluceo\iCal\Property\Event\Attendees;
18
use Eluceo\iCal\Property\Event\Organizer;
19
use Eluceo\iCal\Property\Event\RecurrenceRule;
20
use Eluceo\iCal\Property\Event\Description;
21
use Eluceo\iCal\PropertyBag;
22
use Eluceo\iCal\Property\Event\RecurrenceId;
23
use Eluceo\iCal\Property\DateTimesProperty;
24
25
/**
26
 * Implementation of the EVENT component.
27
 */
28
class Event extends Component
29
{
30
    const TIME_TRANSPARENCY_OPAQUE      = 'OPAQUE';
31
    const TIME_TRANSPARENCY_TRANSPARENT = 'TRANSPARENT';
32
33
    const STATUS_TENTATIVE = 'TENTATIVE';
34
    const STATUS_CONFIRMED = 'CONFIRMED';
35
    const STATUS_CANCELLED = 'CANCELLED';
36
37
    /**
38
     * @var string
39
     */
40
    protected $uniqueId;
41
42
    /**
43
     * The property indicates the date/time that the instance of
44
     * the iCalendar object was created.
45
     *
46
     * The value MUST be specified in the UTC time format.
47
     *
48
     * @var \DateTime
49
     */
50
    protected $dtStamp;
51
52
    /**
53
     * @var \DateTime
54
     */
55
    protected $dtStart;
56
57
    /**
58
     * Preferentially chosen over the duration if both are set.
59
     *
60
     * @var \DateTime
61
     */
62
    protected $dtEnd;
63
64
    /**
65
     * @var \DateInterval
66
     */
67
    protected $duration;
68
69
    /**
70
     * @var boolean
71
     */
72
    protected $noTime = false;
73
74
    /**
75
     * @var string
76
     */
77
    protected $url;
78
79
    /**
80
     * @var string
81
     */
82
    protected $location;
83
84
    /**
85
     * @var string
86
     */
87
    protected $locationTitle;
88
89
    /**
90
     * @var string
91
     */
92
    protected $locationGeo;
93
94
    /**
95
     * @var string
96
     */
97
    protected $summary;
98
99
    /**
100
     * @var Organizer
101
     */
102
    protected $organizer;
103
104
    /**
105
     * @see http://www.ietf.org/rfc/rfc2445.txt 4.8.2.7 Time Transparency
106
     *
107
     * @var string
108
     */
109
    protected $transparency = self::TIME_TRANSPARENCY_OPAQUE;
110
111
    /**
112
     * If set to true the timezone will be added to the event.
113
     *
114
     * @var bool
115
     */
116
    protected $useTimezone = false;
117
118
    /**
119
     * @var int
120
     */
121
    protected $sequence = 0;
122
123
    /**
124
     * @var Attendees
125
     */
126
    protected $attendees;
127
128
    /**
129
     * @var string
130
     */
131
    protected $description;
132
133
    /**
134
     * @var string
135
     */
136
    protected $descriptionHTML;
137
138
    /**
139
     * @var string
140
     */
141
    protected $status;
142
143
    /**
144
     * @var RecurrenceRule
145
     */
146
    protected $recurrenceRule;
147
148
    /**
149
     * This property specifies the date and time that the calendar
150
     * information was created.
151
     *
152
     * The value MUST be specified in the UTC time format.
153
     *
154
     * @var \DateTime
155
     */
156
    protected $created;
157
158
    /**
159
     * The property specifies the date and time that the information
160
     * associated with the calendar component was last revised.
161
     *
162
     * The value MUST be specified in the UTC time format.
163
     *
164
     * @var \DateTime
165
     */
166
    protected $modified;
167
168
    /**
169
     * Indicates if the UTC time should be used or not.
170
     *
171
     * @var bool
172
     */
173
    protected $useUtc = true;
174
175
    /**
176
     * @var bool
177
     */
178
    protected $cancelled;
179
180
    /**
181
     * This property is used to specify categories or subtypes
182
     * of the calendar component.  The categories are useful in searching
183
     * for a calendar component of a particular type and category.
184
     *
185
     * @see https://tools.ietf.org/html/rfc5545#section-3.8.1.2
186
     *
187
     * @var array
188
     */
189
    protected $categories;
190
191
    /**
192
     * https://tools.ietf.org/html/rfc5545#section-3.8.1.3
193
     *
194
     * @var bool
195
     */
196
    protected $isPrivate = false;
197
    
198
    /**
199
     * Dates to be excluded from a series of events
200
     * 
201
     * @var \DateTime[]
202
     */
203
    protected $exDates = array();
204
    
205
    /**
206
     * @var RecurrenceId
207
     */
208
    protected $recurrenceId;
209
210 4
    public function __construct($uniqueId = null)
211
    {
212 4
        if (null == $uniqueId) {
213 2
            $uniqueId = uniqid();
214
        }
215
216 4
        $this->uniqueId = $uniqueId;
217 4
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 4
    public function getType()
223
    {
224 4
        return 'VEVENT';
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230 4
    public function buildPropertyBag()
231
    {
232 4
        $propertyBag = new PropertyBag();
233
234
        // mandatory information
235 4
        $propertyBag->set('UID', $this->uniqueId);
236
237 4
        $propertyBag->add(new DateTimeProperty('DTSTART', $this->dtStart, $this->noTime, $this->useTimezone, $this->useUtc));
238 4
        $propertyBag->set('SEQUENCE', $this->sequence);
239 4
        $propertyBag->set('TRANSP', $this->transparency);
240
241 4
        if ($this->status) {
242
            $propertyBag->set('STATUS', $this->status);
243
        }
244
245
        // An event can have a 'dtend' or 'duration', but not both.
246 4
        if (null != $this->dtEnd) {
247 2
            $propertyBag->add(new DateTimeProperty('DTEND', $this->dtEnd, $this->noTime, $this->useTimezone, $this->useUtc));
248 2
        } elseif (null != $this->duration) {
249
            $propertyBag->set('DURATION', $this->duration->format('P%dDT%hH%iM%sS'));
250
        }
251
252
        // optional information
253 4
        if (null != $this->url) {
254
            $propertyBag->set('URL', $this->url);
255
        }
256
257 4
        if (null != $this->location) {
258
            $propertyBag->set('LOCATION', $this->location);
259
260
            if (null != $this->locationGeo) {
261
                $propertyBag->add(
262
                    new Property(
263
                        'X-APPLE-STRUCTURED-LOCATION',
264
                        'geo:' . $this->locationGeo,
265
                        array(
266
                            'VALUE'          => 'URI',
267
                            'X-ADDRESS'      => $this->location,
268
                            'X-APPLE-RADIUS' => 49,
269
                            'X-TITLE'        => $this->locationTitle,
270
                        )
271
                    )
272
                );
273
            }
274
        }
275
276 4
        if (null != $this->summary) {
277
            $propertyBag->set('SUMMARY', $this->summary);
278
        }
279
280 4
        if (null != $this->attendees) {
281
            $propertyBag->add($this->attendees);
282
        }
283
284 4
        $propertyBag->set('CLASS', $this->isPrivate ? 'PRIVATE' : 'PUBLIC');
285
286 4
        if (null != $this->description) {
287 2
            $propertyBag->set('DESCRIPTION', new Description($this->description));
288
        }
289
290 4
        if (null != $this->descriptionHTML) {
291
            $propertyBag->add(
292
                new Property(
293
                    'X-ALT-DESC',
294
                    $this->descriptionHTML,
295
                    array(
296
                        'FMTTYPE'          => 'text/html',
297
                    )
298
                )
299
            );
300
        }
301
302 4
        if (null != $this->recurrenceRule) {
303
            $propertyBag->set('RRULE', $this->recurrenceRule);
304
        }
305
        
306 4
        if (null != $this->recurrenceId) {
307
            $this->recurrenceId->applyTimeSettings($this->noTime, $this->useTimezone, $this->useUtc);
308
            $propertyBag->add($this->recurrenceId);
309
        }
310
        
311 4
        if (!empty($this->exDates)) {
312
            $propertyBag->add(new DateTimesProperty('EXDATE', $this->exDates, $this->noTime, $this->useTimezone, $this->useUtc));
313
        }
314
315 4
        if ($this->cancelled) {
316
            $propertyBag->set('STATUS', 'CANCELLED');
317
        }
318
319 4
        if (null != $this->organizer) {
320 2
            $propertyBag->add($this->organizer);
321
        }
322
323 4
        if ($this->noTime) {
324
            $propertyBag->set('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
325
        }
326
327 4
        if (null != $this->categories) {
328
            $propertyBag->set('CATEGORIES', $this->categories);
329
        }
330
331 4
        $propertyBag->add(
332 4
            new DateTimeProperty('DTSTAMP', $this->dtStamp ?: new \DateTime(), false, false, true)
333
        );
334
335 4
        if ($this->created) {
336
            $propertyBag->add(new DateTimeProperty('CREATED', $this->created, false, false, true));
337
        }
338
339 4
        if ($this->modified) {
340
            $propertyBag->add(new DateTimeProperty('LAST-MODIFIED', $this->modified, false, false, true));
341
        }
342
343 4
        return $propertyBag;
344
    }
345
346
    /**
347
     * @param $dtEnd
348
     *
349
     * @return $this
350
     */
351 2
    public function setDtEnd($dtEnd)
352
    {
353 2
        $this->dtEnd = $dtEnd;
354
355 2
        return $this;
356
    }
357
358
    public function getDtEnd()
359
    {
360
        return $this->dtEnd;
361
    }
362
363 2
    public function setDtStart($dtStart)
364
    {
365 2
        $this->dtStart = $dtStart;
366
367 2
        return $this;
368
    }
369
370
    /**
371
     * @param $dtStamp
372
     *
373
     * @return $this
374
     */
375
    public function setDtStamp($dtStamp)
376
    {
377
        $this->dtStamp = $dtStamp;
378
379
        return $this;
380
    }
381
382
    /**
383
     * @param $duration
384
     *
385
     * @return $this
386
     */
387
    public function setDuration($duration)
388
    {
389
        $this->duration = $duration;
390
391
        return $this;
392
    }
393
394
    /**
395
     * @param        $location
396
     * @param string $title
397
     * @param null   $geo
398
     *
399
     * @return $this
400
     */
401
    public function setLocation($location, $title = '', $geo = null)
402
    {
403
        $this->location      = $location;
404
        $this->locationTitle = $title;
405
        $this->locationGeo   = $geo;
406
407
        return $this;
408
    }
409
410
    /**
411
     * @param $noTime
412
     *
413
     * @return $this
414
     */
415
    public function setNoTime($noTime)
416
    {
417
        $this->noTime = $noTime;
418
419
        return $this;
420
    }
421
422
    /**
423
     * @param int $sequence
424
     *
425
     * @return $this
426
     */
427
    public function setSequence($sequence)
428
    {
429
        $this->sequence = $sequence;
430
431
        return $this;
432
    }
433
434
    /**
435
     * @return int
436
     */
437
    public function getSequence()
438
    {
439
        return $this->sequence;
440
    }
441
442
    /**
443
     * @param Organizer $organizer
444
     * @return $this
445
     */
446 2
    public function setOrganizer(Organizer $organizer)
447
    {
448 2
        $this->organizer = $organizer;
449
450 2
        return $this;
451
    }
452
453
    /**
454
     * @param $summary
455
     *
456
     * @return $this
457
     */
458
    public function setSummary($summary)
459
    {
460
        $this->summary = $summary;
461
462
        return $this;
463
    }
464
465
    /**
466
     * @param $uniqueId
467
     *
468
     * @return $this
469
     */
470
    public function setUniqueId($uniqueId)
471
    {
472
        $this->uniqueId = $uniqueId;
473
474
        return $this;
475
    }
476
477
    /**
478
     * @return string
479
     */
480
    public function getUniqueId()
481
    {
482
        return $this->uniqueId;
483
    }
484
485
    /**
486
     * @param $url
487
     *
488
     * @return $this
489
     */
490
    public function setUrl($url)
491
    {
492
        $this->url = $url;
493
494
        return $this;
495
    }
496
497
    /**
498
     * @param $useTimezone
499
     *
500
     * @return $this
501
     */
502
    public function setUseTimezone($useTimezone)
503
    {
504
        $this->useTimezone = $useTimezone;
505
506
        return $this;
507
    }
508
509
    /**
510
     * @return bool
511
     */
512
    public function getUseTimezone()
513
    {
514
        return $this->useTimezone;
515
    }
516
517
    /**
518
     * @param Attendees $attendees
519
     *
520
     * @return $this
521
     */
522
    public function setAttendees(Attendees $attendees)
523
    {
524
        $this->attendees = $attendees;
525
526
        return $this;
527
    }
528
529
    /**
530
     * @param string $attendee
531
     * @param array  $params
532
     *
533
     * @return $this
534
     */
535
    public function addAttendee($attendee, $params = array())
536
    {
537
        if (!isset($this->attendees)) {
538
            $this->attendees = new Attendees();
539
        }
540
        $this->attendees->add($attendee, $params);
541
542
        return $this;
543
    }
544
545
    /**
546
     * @return Attendees
547
     */
548
    public function getAttendees()
549
    {
550
        return $this->attendees;
551
    }
552
553
    /**
554
     * @param $description
555
     *
556
     * @return $this
557
     */
558 2
    public function setDescription($description)
559
    {
560 2
        $this->description = $description;
561
562 2
        return $this;
563
    }
564
565
    /**
566
     * @param $descriptionHTML
567
     *
568
     * @return $this
569
     */
570
    public function setDescriptionHTML($descriptionHTML)
571
    {
572
        $this->descriptionHTML = $descriptionHTML;
573
574
        return $this;
575
    }
576
577
    /**
578
     * @param bool $useUtc
579
     *
580
     * @return $this
581
     */
582
    public function setUseUtc($useUtc = true)
583
    {
584
        $this->useUtc = $useUtc;
585
586
        return $this;
587
    }
588
589
    /**
590
     * @return string
591
     */
592
    public function getDescription()
593
    {
594
        return $this->description;
595
    }
596
597
    /**
598
     * @return string
599
     */
600
    public function getDescriptionHTML()
601
    {
602
        return $this->descriptionHTML;
603
    }
604
605
    /**
606
     * @param $status
607
     *
608
     * @return $this
609
     */
610
    public function setCancelled($status)
611
    {
612
        $this->cancelled = (bool) $status;
613
614
        return $this;
615
    }
616
617
    /**
618
     * @param $transparency
619
     *
620
     * @return $this
621
     *
622
     * @throws \InvalidArgumentException
623
     */
624 View Code Duplication
    public function setTimeTransparency($transparency)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
625
    {
626
        $transparency = strtoupper($transparency);
627
        if ($transparency === self::TIME_TRANSPARENCY_OPAQUE
628
            || $transparency === self::TIME_TRANSPARENCY_TRANSPARENT
629
        ) {
630
            $this->transparency = $transparency;
631
        } else {
632
            throw new \InvalidArgumentException('Invalid value for transparancy');
633
        }
634
635
        return $this;
636
    }
637
638
    /**
639
     * @param $status
640
     *
641
     * @return $this
642
     *
643
     * @throws \InvalidArgumentException
644
     */
645
    public function setStatus($status)
646
    {
647
        $status = strtoupper($status);
648
        if ($status == self::STATUS_CANCELLED
649
            || $status == self::STATUS_CONFIRMED
650
            || $status == self::STATUS_TENTATIVE
651
        ) {
652
            $this->status = $status;
653
        } else {
654
            throw new \InvalidArgumentException('Invalid value for status');
655
        }
656
657
        return $this;
658
    }
659
660
    /**
661
     * @param RecurrenceRule $recurrenceRule
662
     *
663
     * @return $this
664
     */
665
    public function setRecurrenceRule(RecurrenceRule $recurrenceRule)
666
    {
667
        $this->recurrenceRule = $recurrenceRule;
668
669
        return $this;
670
    }
671
672
    /**
673
     * @return RecurrenceRule
674
     */
675
    public function getRecurrenceRule()
676
    {
677
        return $this->recurrenceRule;
678
    }
679
680
    /**
681
     * @param $dtStamp
682
     *
683
     * @return $this
684
     */
685
    public function setCreated($dtStamp)
686
    {
687
        $this->created = $dtStamp;
688
689
        return $this;
690
    }
691
692
    /**
693
     * @param $dtStamp
694
     *
695
     * @return $this
696
     */
697
    public function setModified($dtStamp)
698
    {
699
        $this->modified = $dtStamp;
700
701
        return $this;
702
    }
703
704
    /**
705
     * @param $categories
706
     *
707
     * @return $this
708
     */
709
    public function setCategories($categories)
710
    {
711
        $this->categories = $categories;
712
713
        return $this;
714
    }
715
716
    /**
717
     * Sets the event privacy
718
     *
719
     * @param bool $flag
720
     * @return $this
721
     */
722
    public function setIsPrivate($flag)
723
    {
724
        $this->isPrivate = (bool) $flag;
725
726
        return $this;
727
    }
728
729
    /**
730
     * @param \DateTime $dateTime
731
     * @return \Eluceo\iCal\Component\Event
732
     */
733
    public function addExDate(\DateTime $dateTime)
734
    {
735
        $this->exDates[] = $dateTime;
736
        return $this;
737
    }
738
    
739
    /**
740
     * @return DateTime[]
741
     */
742
    public function getExDates()
743
    {
744
        return $this->exDates;
745
    }
746
747
    /**
748
     * @param \DateTime[]
749
     * @return \Eluceo\iCal\Component\Event
750
     */
751
    public function setExDates(Array $exDates)
752
    {
753
        $this->exDates = $exDates;
754
        return $this;
755
    }
756
757
    /**
758
     * @return \Eluceo\iCal\Property\Event\RecurrenceId
759
     */
760
    public function getRecurrenceId()
761
    {
762
        return $this->recurrenceId;
763
    }
764
765
    /**
766
     * @param RecurrenceId $recurrenceId
767
     * @return \Eluceo\iCal\Component\Event
768
     */
769
    public function setRecurrenceId(RecurrenceId $recurrenceId)
770
    {
771
        $this->recurrenceId = $recurrenceId;
772
        return $this;
773
    }
774
 
775
}
776