Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set rrule from string #37

Open
cmfcmf opened this issue May 13, 2015 · 9 comments
Open

Add option to set rrule from string #37

cmfcmf opened this issue May 13, 2015 · 9 comments
Labels

Comments

@cmfcmf
Copy link
Contributor

cmfcmf commented May 13, 2015

I have all my rrules stored as strings (like FREQ=WEEKLY;INTERVAL=1;COUNT=30). I would like to directly pass that string to the Eluceo/iCal/Component/Event.
I tried to use $vEvent->setRecurrenceRule($myRRule);, but it only accepts a RecurrenceRule object. The RecurrenceRule object, however, has no method to set the rrule from string. So I basically have to parse my stored rrules and then call the setBy* methods on the RecurrenceRule object.

I suggest to either

  • make setRecurrenceRule accept a string as well.
  • add a setFromString method to RecurrenceRule.
  • add a setRecurrenceRuleString method to the event class.

Thank you for this cool library 😄 💯

@markuspoerschke
Copy link
Owner

I think we need something that can parse content from an ICS file into the corresponding PHP objects. The idea behind this library is to work with PHP objects rather than plain text. I would prefer a method like $myRecurrenceRule = RecurrenceRule::createFromString('FREQ=WEEKLY;INTERVAL=1;COUNT=30').

@cmfcmf
Copy link
Contributor Author

cmfcmf commented Nov 15, 2015

I think we need something that can parse content from an ICS file into the corresponding PHP objects. The idea behind this library is to work with PHP objects rather than plain text. I would prefer a method like $myRecurrenceRule = RecurrenceRule::createFromString('FREQ=WEEKLY;INTERVAL=1;COUNT=30').

Yes, something along these lines would be great.

@LiviuChirca
Copy link

LiviuChirca commented May 3, 2016

I've been modified a little bit to allow me to use a string RRULE;

In property.php:

$line .= ':' . $this->value->getEscapedValue();

change to

        if($this->name != "RRULE")
            $line .= ':' . $this->value->getEscapedValue();
        else
            $line .= ':' . stripslashes($this->value->getEscapedValue());

and in Event.php

    public function setRecurrenceRule(RecurrenceRule $recurrenceRule)
    {
        $this->recurrenceRule = $recurrenceRule;

        return $this;
    }

change to

    public function setRecurrenceRule($recurrenceRule)
    {
        $this->recurrenceRule = $recurrenceRule;
        return $this;
    }

and when you set the RRULE to the event object, just do it:

$vEvent->setRecurrenceRule("FREQ=DAILY;INTERVAL=1;COUNT=5");

just make sure your RRULE is already validated.

@Ydalb
Copy link

Ydalb commented Apr 8, 2017

Maybe we could use the excellent work of https://github.com/simshaun/recurr ?

@markuspoerschke
Copy link
Owner

@Ydalb having a library to integrate into this project would be nice. I will check how I can integrate that into this library!

@drmmr763
Copy link

drmmr763 commented Sep 7, 2018

Just a +1 on this feature request.

I actually already have a code base which implements https://github.com/simshaun/recurr's library and I have stored RRULE strings from that. Now I'm trying to add iCal to that and it would be great if I could just take my existing strings and parse them into iCal's ReccuranceRule object.

@ankurk91
Copy link

I would be nice to have this feature baked in into v2.0

@markuspoerschke markuspoerschke removed this from the Version 1.0.0 milestone Feb 13, 2020
@williamdes
Copy link

Maybe we could use the excellent work of https://github.com/simshaun/recurr ?

@markuspoerschke I need to have rrule (https://icalendar.org/rrule-tool.html) on my Events. Are contributions welcome to bake this cool lib (simshaun/recurr) into this one ?

@maskedjellybean
Copy link

maskedjellybean commented Mar 26, 2024

I'd like to see this method too. In the meanwhile I have this absurd bit of code in case it's helpful for others. $date['rrule'] is an RRULE string, and $iCalendarEvent is a \Eluceo\iCal\Component\Event object.

// Even though we already have an RRULE string,
// the library doesn't allow us to simply use it.
// We have to deconstruct it, create a RecurrenceRule object,
// then have the object reconstruct the string. Phew.
// See: https://github.com/markuspoerschke/iCal/issues/37
$rruleStr = $date['rrule'];
$recurrenceRule = new RecurrenceRule();
$rruleSplit = explode(';', $rruleStr);
foreach ($rruleSplit as $rruleProp) {
  if (str_contains($rruleProp, 'FREQ')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setFreq($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'INTERVAL')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setInterval($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYSECOND')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setBySecond($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYMINUTE')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByMinute($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYHOUR')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByHour($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYDAY')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByDay($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYMONTHDAY')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByMonthDay($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYYEARDAY')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByYearDay($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYWEEKNO')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByWeekNo($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYMONTH')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setByMonth($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'BYSETPOS')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setBySetPos($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'WKST')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setWkst($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'UNTIL')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setUntil($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'COUNT')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setCount($rrulePropVal);
  }
  else if (str_contains($rruleProp, 'INTERVAL')) {
    $rrulePropVal = explode('=', $rruleProp)[1];
    $recurrenceRule->setInterval($rrulePropVal);
  }
}

$iCalendarEvent->addRecurrenceRule($recurrenceRule);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants