/
delete_occurrence.php
121 lines (103 loc) · 4.04 KB
/
delete_occurrence.php
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
<?php
namespace Concrete\Controller\Dialog\Event;
use Concrete\Controller\Backend\UserInterface as BackendInterfaceController;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\User\User;
use Concrete\Core\Workflow\Progress\Response;
use Concrete\Core\Workflow\Request\ApproveCalendarEventRequest;
use Core;
use Concrete\Core\Calendar\Calendar;
use Concrete\Core\Calendar\CalendarServiceProvider;
use Concrete\Core\Calendar\Event\EditResponse;
use Concrete\Core\Calendar\Event\EventOccurrenceService;
use Concrete\Core\Calendar\Event\EventService;
use Concrete\Core\Calendar\Utility\Preferences;
class DeleteOccurrence extends BackendInterfaceController
{
protected $viewPath = '/dialogs/event/delete_occurrence';
/**
* @var Preferences
*/
protected $preferences;
/**
* @var EventService
*/
protected $eventService;
public function __construct()
{
parent::__construct();
$app = Facade::getFacadeApplication();
$this->preferences = $app->make(Preferences::class);
$this->eventService = $app->make(EventService::class);
$this->eventOccurrenceService = $app->make(EventOccurrenceService::class);
$this->dateFormatter = $app->make(CalendarServiceProvider::class)->getDateFormatter();
}
public function submit()
{
$occurrence = $this->eventOccurrenceService->getByID($this->request->request->get('versionOccurrenceID'));
$e = \Core::make('error');
if (!$occurrence) {
$e->add(t('Invalid occurrence.'));
}
if (!$this->canAccess()) {
$e->add(t('Access Denied.'));
}
$r = new EditResponse($e);
$year = date('Y', $occurrence->getStart());
$month = date('m', $occurrence->getStart());
$r->setRedirectURL(
\URL::to(
$this->preferences->getPreferredViewPath(),
'view',
$occurrence->getEvent()->getCalendar()->getID(),
$year,
$month
)
);
if (!$e->has()) {
$u = $this->app->make(User::class);
$eventVersion = $this->eventService->getVersionToModify($occurrence->getEvent(), $u);
$this->eventService->addEventVersion($eventVersion->getEvent(), $eventVersion->getEvent()->getCalendar(), $eventVersion);
$this->eventOccurrenceService->delete($eventVersion, $occurrence->getOccurrence());
if ($eventVersion->isApproved()) {
$pkr = new ApproveCalendarEventRequest();
$pkr->setCalendarEventVersionID($eventVersion->getID());
$pkr->setRequesterUserID($u->getUserID());
$response = $pkr->trigger();
if ($response instanceof Response) {
$this->flash('success', t('Event occurrence removed.'));
} else {
$this->flash('success', t('Event occurrence cancellation requested. This must be approved before it is fully removed.'));
}
} else {
$this->flash('success', t('Event occurrence removed.'));
}
}
$r->outputJSON();
}
public function view()
{
if ($this->canAccess()) {
$occurrence = $this->eventOccurrenceService->getByID($this->request->query->get('versionOccurrenceID'));
if (!$occurrence) {
throw new \Exception(t('Invalid occurrence.'));
}
$this->set('occurrence', $occurrence);
$this->set('dateFormatter', $this->dateFormatter);
} else {
die('Access Denied.');
}
}
protected function canAccess()
{
$occurrence = $this->eventOccurrenceService->getByID($_REQUEST['versionOccurrenceID']);
if (is_object($occurrence)) {
$calendar = $occurrence->getEvent()->getCalendar();
if (is_object($calendar)) {
$cp = new \Permissions($calendar);
return $cp->canEditCalendarEvents();
}
}
return false;
}
}