Skip to content

Commit

Permalink
fix(Calendar/js): create individual event from new event
Browse files Browse the repository at this point in the history
  • Loading branch information
ccheng-dev committed Dec 19, 2024
1 parent 52badb3 commit 1735464
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 24 deletions.
73 changes: 73 additions & 0 deletions tests/tine20/Calendar/JsonTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2586,4 +2586,77 @@ public function testCopyEvent()
$this->assertTrue($eventData['alarms'][0]['id'] !== $copiedEventData['alarms'][0]['id']);
$this->assertTrue($eventData['alarms'][0]['record_id'] !== $copiedEventData['alarms'][0]['record_id']);
}

public function testCreateIndividualEventWithExceptions()
{
$this->_testNeedsTransaction();
$event = new Calendar_Model_Event(array(
'summary' => 'Individual event',
'dtstart' => '2009-03-25 06:00:00',
'dtend' => '2009-03-25 06:15:00',
'description' => 'test create individual event',
'attendee' => $this->_getAttendee(),
'container_id' => $this->_getTestCalendar()->getId(),
'organizer' => $this->_getTestUserContact()->getId(),
'uid' => Calendar_Model_Event::generateUID(),

Tinebase_Model_Grants::GRANT_READ => true,
Tinebase_Model_Grants::GRANT_EDIT => true,
Tinebase_Model_Grants::GRANT_DELETE => true,
));

$savedEvent = $this->_uit->saveEvent($event->toArray());

$persistentException = $savedEvent;
$persistentException['id'] = '';
$persistentException['summary'] = 'go sleeping';
$persistentException['dtstart'] = '2009-04-25 08:00:00';
$persistentException['dtend'] = '2009-04-25 08:15:00';
$persistentException['base_event_id'] = $savedEvent['id'];
$persistentException['rrule'] = [
'freq' => 'INDIVIDUAL',
'interval' => 1,
'count' => 2
];

// create persistent exception1
$persistentException = $this->_uit->createRecurException($persistentException, FALSE, FALSE);
$persistentException = $this->_uit->getEvent($persistentException['id']);
$this->assertEquals($savedEvent['uid'] . '-2009-03-26 05:00:00', $persistentException['recurid']);
$updatedBaseEvent = $this->_uit->getEvent($savedEvent['id']);
$this->assertEquals(2, $updatedBaseEvent['rrule']['count']);

// create persistent exception2
$updatedBaseEvent['dtstart'] = '2009-05-25 18:00:00';
$updatedBaseEvent['dtend'] = '2009-05-25 18:15:00';
$updatedBaseEvent['base_event_id'] = $updatedBaseEvent['id'];
$updatedBaseEvent['id'] = '';
$updatedBaseEvent['rrule'] = [
'freq' => 'INDIVIDUAL',
'interval' => 1,
'count' => 3
];
$persistentException2 = $this->_uit->createRecurException($updatedBaseEvent, FALSE, FALSE);
$persistentException2 = $this->_uit->getEvent($persistentException2['id']);
$this->assertEquals($savedEvent['uid'] . '-2009-03-27 05:00:00', $persistentException2['recurid']);
$updatedBaseEvent = $this->_uit->getEvent($savedEvent['id']);
$this->assertEquals(3, $updatedBaseEvent['rrule']['count']);

//create self exception
$savedEventException = $this->_uit->createRecurException($updatedBaseEvent, FALSE, FALSE);
$savedEventException = $this->_uit->getEvent($savedEventException['id']);
$this->assertEquals(3, $updatedBaseEvent['rrule']['count']);

$exceptions = $this->_uit->searchEvents([
['field' => 'container_id', 'operator' => 'equals', 'value' => $savedEvent['container_id']],
['field' => 'period', 'operator' => 'within', 'value' => [
'from' => '2009-02-10 06:00:00',
'until' => '2009-06-14 18:15:00',
]]
], []);
$this->assertEquals(3, count($exceptions['results']));

$events = $this->_uit->getEventExceptions($savedEvent['id']);
$this->assertEquals(3, count($events['results']));
}
}
1 change: 0 additions & 1 deletion tine20/Calendar/Controller/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,6 @@ public function createRecurException($_event, $_deleteInstance = FALSE, $_allFol
}
$baseEvent = Calendar_Controller_Event::getInstance()->update($baseEvent);


$from = clone $baseEvent->dtstart;
$until = clone $baseEvent->dtstart;
$until->addDay($count);
Expand Down
56 changes: 33 additions & 23 deletions tine20/Calendar/js/RrulePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,19 @@ Tine.Calendar.RrulePanel = Ext.extend(Ext.Panel, {
this.NONEcard.html = this.app.i18n._("Exceptions of reccuring events can't have recurrences themselves.");
}

if (!this.record.get('id')) {
this.INDIVIDUALcard.setDisabled(true);
} else {
if (this.activeRuleCard.freq === 'INDIVIDUAL') {
await Tine.Calendar.getEventExceptions(this.record.get('id'))
.then((response) => {
this.INDIVIDUALcard.exceptionGrid.store.loadData(response.results);
});
}
if (this.activeRuleCard.freq === 'INDIVIDUAL') {
await Tine.Calendar.getEventExceptions(this.record.get('id'))
.then((response) => {
this.INDIVIDUALcard.exceptionGrid.store.loadData(response.results);
});
}
},

onRecordUpdate: function(record) {
const rendered = _.get(this, 'activeRuleCard.rendered', false);
const rrule = rendered ? this.activeRuleCard.getRule() : this.rrule;

if (rrule && (! this.rrule || !this.record.data.creation_time)) {
if (rrule && (!this.rrule || !this.record.data.creation_time || (rrule.freq === 'INDIVIDUAL' && this.rrule.count !== rrule.count))) {
// mark as new rule to avoid series confirm dlg
rrule.newrule = true;
}
Expand Down Expand Up @@ -1069,6 +1065,7 @@ Tine.Calendar.RrulePanel.INDIVIDUALcard = Ext.extend(Tine.Calendar.RrulePanel.Ab
minWidth: 100,
hideable: false,
sortable: true,
editor: new Ext.form.TextField({allowBlank: true}),
}, {
id: 'info',
header: this.app.i18n._('Info'),
Expand All @@ -1078,28 +1075,25 @@ Tine.Calendar.RrulePanel.INDIVIDUALcard = Ext.extend(Tine.Calendar.RrulePanel.Ab
}]),
});

this.exceptionGrid.on('beforeaddrecord', async (event) => {
this.exceptionGrid.on('beforeaddrecord', async (individualEvent) => {
if (this.rrulePanel.activeRuleCard.freq !== 'INDIVIDUAL') return false;

const exception = Tine.Tinebase.data.Record.setFromJson(this.rrulePanel.record.data, Tine.Calendar.Model.Event);
const duration = (exception.get('dtend').getTime() - exception.get('dtstart').getTime()) / 60000;

exception.set('base_event_id', exception.get('base_event_id') || exception.get('id'));
exception.set('id', '');
exception.set('recurid', '');
exception.set('dtstart', event.get('dtstart'));
exception.set('dtend', event.get('dtstart').clone().add(Date.MINUTE, duration));
exception.set('summary', exception.get('summary'));
exception.set('rrule', this.rrulePanel.activeRuleCard.getRule() || {
if (!this.rrulePanel.record?.data?.id && !this.rrulePanel.activeRuleCard.getRule()) {
const proxy = this.rrulePanel.eventEditDialog.recordClass.getProxy();
this.rrulePanel.record = await proxy.promiseSaveRecord(this.rrulePanel.record);
this.rrulePanel.eventEditDialog.record = this.rrulePanel.record;
}
const rrule = this.rrulePanel.activeRuleCard.getRule() || {
freq: 'INDIVIDUAL',
interval: 1,
count: 2
});
};
const exception = this.createIndividualEvent(individualEvent, rrule);

await Tine.Calendar.createRecurException(exception, false, false)
.then((result) => {
this.rrulePanel.eventEditDialog.loadRecord('remote');
})
.catch((e) => {})
});

this.items = [
Expand Down Expand Up @@ -1127,4 +1121,20 @@ Tine.Calendar.RrulePanel.INDIVIDUALcard = Ext.extend(Tine.Calendar.RrulePanel.Ab
'</span>';
}).join('');
},

createIndividualEvent(individualEvent, rrule) {
const baseEvent = this.rrulePanel.record;
const exception = Tine.Tinebase.data.Record.setFromJson(baseEvent.data, Tine.Calendar.Model.Event);
const duration = (baseEvent.get('dtend').getTime() - baseEvent.get('dtstart').getTime()) / 60000;

exception.set('base_event_id', individualEvent.get('base_event_id') || baseEvent.get('id'));
exception.set('id', '');
exception.set('recurid', '');
exception.set('dtstart', individualEvent.get('dtstart'));
exception.set('dtend', individualEvent.get('dtstart').clone().add(Date.MINUTE, duration));
exception.set('summary', individualEvent.get('summary') || baseEvent.get('summary'));
exception.set('rrule', rrule);

return exception;
},
});

0 comments on commit 1735464

Please sign in to comment.