Brad Rush
Brad Rush

Reputation: 11

YIi2 FullCalendar and working with event instances from rrule post processing

First StackOverflow question so my apologies for being a newbie in several areas. I have integrated a full calendar into my yii2 application and I can generate events successfully with rules. I am not databasing recurrence instances but rather feeding events in with JSON via DB calls and an RRULE string. I handle exceptions via the same string.

The event rrule I am playing with that works fine is: DTSTART:20210430T133040\nRRULE:INTERVAL=1;COUNT=6;BYDAY=FR;FREQ=WEEKLY\nEXDATE:20210507T133040\nEXDATE:20210521T133040

My question is how do I get a parsable list or something that has all my event recurrences listed that I can work with post save once the full calendar works out the instances from the rule? My end goal is that I will have a cron that reads through my event instances and if a date matches my specific call then I fire off an email notification to the user they have an event that day or coming up or whatever.

In other words, I can get events in and displaying via rule but I can't get them out. I am coding in PHP and essentially have no javascript experience. I'm reading the documentation that says I could use .all(), but I don't know how to do that or am missing something obviously easy.

Below is the script I use to display the calendar view and is working well. The last few lines of code are just me trying to display the events in a list below the calendar so I can at least see if I can see the instances the calendar has in memory.

<?php
use myapp\models\TaskStatus;

$this->title = Yii::t ( 'app','Event / Task Management | My Calendar | Active Events / Tasks');
?>
 <script src="../../vendor/bower/jquery/dist/jquery.js"></script>
<!--<link href='css/plugins/fullcalendar/main.css' rel='stylesheet' />-->

<link href='https://cdn.jsdelivr.net/npm/[email protected]/main.css' rel='stylesheet' />


<!-- rrule lib -->
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/es5/rrule.min.js'></script>

<!--<script src='js/plugins/fullcalendar/main.js'></script>-->
<script src='https://cdn.jsdelivr.net/npm/[email protected]/main.min.js'></script>

<!-- the rrule-to-fullcalendar connector. must go AFTER the rrule lib -->
<script src='https://cdn.jsdelivr.net/npm/@fullcalendar/[email protected]/main.global.min.js'></script>

<style>
    .fc .fc-button-primary {
        color: #fff;
        background-color: var(--fc-button-bg-color, #ff0000);
        border-color: var(--fc-button-border-color, #ff0000);
    }

    .fc .fc-button-primary:hover {
        color: #fff;
        background-color: var(--fc-button-bg-color, #000740);
        border-color: var(--fc-button-border-color, #000740);
    }

    fc-button-active
    {
        color: #fff;
        background-color: var(--fc-button-bg-color, #000740);
        border-color: var(--fc-button-border-color, #000740);
    }

    #calendar > div.fc-header-toolbar.fc-toolbar.fc-toolbar-ltr > div:nth-child(3) > div > button.fc-today-button.fc-button.fc-button-primary {
        color: #fff;
        background-color: var(--fc-button-bg-color, #ff0000);
        border-color: var(--fc-button-border-color, #ff0000);
    }

    #calendar > div.fc-header-toolbar.fc-toolbar.fc-toolbar-ltr > div:nth-child(3) > div > button.fc-dayGridMonth-button.fc-button.fc-button-primary.fc-button-active{
        color: #fff;
        background-color: var(--fc-button-bg-color, #ff0000);
        border-color: var(--fc-button-border-color, #ff0000);
    }

</style>

<?php
$jSon="[";
$coma='';

foreach($dataProvider as $row){
    //print_r($row);
    if(($row['expected_end_datetime']) < time() and $row['task_status_id'] !=TaskStatus::_COMPLETED){
        $color='#008000';
    }else if($row['task_status_id'] ==TaskStatus::_COMPLETED){
        $color='#ffffff';
    }else{
        $color='#ffffff';
    }

    if ($row['rrule']!='') {

        $jSon.=$coma."{
    'id':'".$row['id']."',
    'eventColor':'".$color."',
    'title':'".addslashes($row['task_name'])."',
    'end':'".$row['expected_end_datetime']."',
    'rrule':'".$row['rrule']."',
    'allDay':".$row['all_day'].",
    'wkst':'SU',
    'groupId':'".$row['parent_id']."',
    
    }";

    } if ($row['rrule']=='' && $row['parent_id']=='0') {
        date_default_timezone_set(Yii::$app->params['TIME_ZONE']);
        $row['expected_end_datetime'] = date('Y-m-d H:i:s', $row['expected_end_datetime']);
        $row['expected_start_datetime'] = date('Y-m-d H:i:s', $row['expected_start_datetime']);

        $jSon.=$coma."{
    
    'id':'".$row['id']."',
    'eventColor':'".$color."',
    'title':'".addslashes($row['task_name'])."',
    'start':'".$row['expected_start_datetime']."',
    'end':'".$row['expected_end_datetime']."',
    'allDay':".$row['all_day'].",
    'groupId':'".$row['parent_id']."',
    'url':'?r=pmt/task/subtask-view&id=".$row['id']."'
    
    }";

    }

      if ($row['rrule']=='' && $row['parent_id']!='0') {
        date_default_timezone_set(Yii::$app->params['TIME_ZONE']);
        $row['expected_end_datetime'] = date('Y-m-d H:i:s', $row['expected_end_datetime']);
        $row['expected_start_datetime'] = date('Y-m-d H:i:s', $row['expected_start_datetime']);

        $jSon.=$coma."{
    
    'id':'".$row['parent_id']."',
    'eventColor':'".$color."',
    'title':'".addslashes($row['task_name'])."',
    'start':'".$row['expected_start_datetime']."',
    'end':'".$row['expected_end_datetime']."',
    'allDay':".$row['all_day'].",
    'groupId':'".$row['parent_id']."',
    
    
    }";

    }


    $coma=",";
 }
$jSon.="]";
//echo $jSon;
 ?>


<script>

    document.addEventListener('DOMContentLoaded', function() {
        var calendarEl = document.getElementById('calendar');
        var calendar = new FullCalendar.Calendar(calendarEl, {
            schedulerLicenseKey: 'CC-Attribution-NonCommercial-NoDerivatives',
            height: 750,
            timeZone: 'America/Chicago',
            customButtons: {
                printButton: {
                    text: 'Print',
                    click: function() {
                        window.print();
                    }
                },
                addEventButton: {
                    text: '+ Task',
                    click:function(event, jsEvent, view){
                        window.location.href = '?r=pmt/task/create';
                    }
                },
                viewArchivedButton: {
                    text: 'View Archived',
                    click:function(event, jsEvent, view){
                        window.location.href = '?r=pmt/task/my-calendararchived';
                    }
                }
            },
            eventClick: function(info) {
                var eventStart = info.event.startStr;
                var eventEnd = info.event.endStr;
                var eventID = info.event.id;
                var groupID = info.event.groupId;
                window.location.assign('?r=pmt/task/subtask-view&id='+ eventID+'&start='+ eventStart+'&end='+ eventEnd+'&group='+ groupID);
                },

            initialView: 'dayGridMonth',
            eventColor: '#FF0000',
            headerToolbar: {
                left: 'addEventButton',
                center: 'title',
                right: 'viewArchivedButton,printButton,prev,next,today,dayGridMonth,timeGridWeek,timeGridDay,listMonth'
            }
        });

        calendar.addEventSource( <?=$jSon?> )
        calendar.render();

        calendar.getEvents() -> [Array];
        var list = calendar.all();
        console.log(list);


    });



</script>








<div class="panel panel-info">



    <div class="panel-body">

        <div id='calendar' >
            <script>var list = calendar.all();
                console.log(list);</script>
        </div>


    </div>

</div>

    

Upvotes: 0

Views: 390

Answers (1)

Brad Rush
Brad Rush

Reputation: 11

Your exactly right and thats what I figured just didnt know if I was missing something. I found a yii2 plugin from rlanvin that does exactly that with rrule string parsing that skips full calendar for this entirely. Will still use fullcalendar for the calendar display and functionality. I will just build a secondary process via php. Thanks!

Upvotes: 1

Related Questions