/**
  * creates formatted output for calendar component property trigger
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.4.16 - 2008-10-21
  * @uses calendarComponent::$trigger
  * @uses calendarComponent::getConfig()
  * @uses calendarComponent::_createElement()
  * @uses iCalUtilityFunctions::_date2strdate()
  * @uses calendarComponent::$intAttrDelimiter
  * @uses iCalUtilityFunctions::_duration2str()
  * @uses calendarComponent::_createParams()
  * @return string
  */
 function createTrigger()
 {
     if (empty($this->trigger)) {
         return FALSE;
     }
     if (empty($this->trigger['value'])) {
         return $this->getConfig('allowEmpty') ? $this->_createElement('TRIGGER') : FALSE;
     }
     $content = $attributes = null;
     if (isset($this->trigger['value']['year']) && isset($this->trigger['value']['month']) && isset($this->trigger['value']['day'])) {
         $content .= iCalUtilityFunctions::_date2strdate($this->trigger['value']);
     } else {
         if (TRUE !== $this->trigger['value']['relatedStart']) {
             $attributes .= $this->intAttrDelimiter . 'RELATED=END';
         }
         if ($this->trigger['value']['before']) {
             $content .= '-';
         }
         $content .= iCalUtilityFunctions::_duration2str($this->trigger['value']);
     }
     $attributes .= $this->_createParams($this->trigger['params']);
     return $this->_createElement('TRIGGER', $attributes, $content);
 }
 /**
  * select components from calendar on date or selectOption basis
  *
  * Ensure DTSTART is set for every component.
  * No date controls occurs.
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.21.11 - 2015-03-31
  * @param mixed $startY optional,      (int) start Year,  default current Year
  *                                ALT. (obj) start date (datetime)
  *                                ALT. array selecOptions ( *[ <propName> => <uniqueValue> ] )
  * @param mixed $startM optional,      (int) start Month, default current Month
  *                                ALT. (obj) end date (datetime)
  * @param int   $startD optional, start Day,   default current Day
  * @param int   $endY   optional, end   Year,  default $startY
  * @param int   $endM   optional, end   Month, default $startM
  * @param int   $endD   optional, end   Day,   default $startD
  * @param mixed $cType  optional, calendar component type(-s), default FALSE=all else string/array type(-s)
  * @param bool  $flat   optional, FALSE (default) => output : array[Year][Month][Day][]
  *                                TRUE            => output : array[] (ignores split)
  * @param bool  $any    optional, TRUE (default) - select component(-s) that occurs within period
  *                                FALSE          - only component(-s) that starts within period
  * @param bool  $split  optional, TRUE (default) - one component copy every DAY it occurs during the
  *                                                 period (implies flat=FALSE)
  *                                FALSE          - one occurance of component only in output array
  * @uses vcalendar::$components
  * @uses vcalendar::selectComponents2()
  * @uses iCalUtilityFunctions::$vComps
  * @uses calendarComponent::$objName
  * @uses calendarComponent::getProperty()
  * @uses iCaldateTime::factory()
  * @uses iCaldateTime::getTimezoneName()
  * @uses iCaldateTime::getTime()
  * @uses iCalUtilityFunctions::$fmt
  * @uses iCaldateTime::$SCbools
  * @uses iCaldateTime::format()
  * @uses iCalUtilityFunctions::_strDate2arr()
  * @uses iCalUtilityFunctions::_recur2date()
  * @uses iCalUtilityFunctions::_inScope()
  * @uses calendarComponent::copy()
  * @uses calendarComponent::setProperty()
  * @uses iCalUtilityFunctions::$fmt
  * @uses calendarComponent::deleteProperty()
  * @uses iCalUtilityFunctions::_setSortArgs()
  * @uses iCalUtilityFunctions::_cmpfcn()
  * @return array or FALSE
  */
 function selectComponents($startY = FALSE, $startM = FALSE, $startD = FALSE, $endY = FALSE, $endM = FALSE, $endD = FALSE, $cType = FALSE, $flat = FALSE, $any = TRUE, $split = TRUE)
 {
     /* check  if empty calendar */
     if (0 >= count($this->components)) {
         return FALSE;
     }
     if (is_array($startY)) {
         return $this->selectComponents2($startY);
     }
     /* check default dates */
     if (is_a($startY, 'DateTime') && is_a($startM, 'DateTime')) {
         $endY = $startM->format('Y');
         $endM = $startM->format('m');
         $endD = $startM->format('d');
         $startD = $startY->format('d');
         $startM = $startY->format('m');
         $startY = $startY->format('Y');
     } else {
         if (!$startY) {
             $startY = date('Y');
         }
         if (!$startM) {
             $startM = date('m');
         }
         if (!$startD) {
             $startD = date('d');
         }
         if (!$endY) {
             $endY = $startY;
         }
         if (!$endM) {
             $endM = $startM;
         }
         if (!$endD) {
             $endD = $startD;
         }
     }
     // echo "selectComp args={$startY}-{$startM}-{$startD} - {$endY}-{$endM}-{$endD}<br>\n"; $tcnt = 0;// test ###
     /* check component types */
     if (empty($cType)) {
         $cType = iCalUtilityFunctions::$vComps;
     } else {
         if (!is_array($cType)) {
             $cType = array($cType);
         }
         $cType = array_map('strtolower', $cType);
         foreach ($cType as $cix => $theType) {
             if (!in_array($theType, iCalUtilityFunctions::$vComps)) {
                 $cType[$cix] = 'vevent';
             }
         }
         $cType = array_unique($cType);
     }
     if (FALSE === $flat && FALSE === $any) {
         // invalid combination
         $split = FALSE;
     }
     if (TRUE === $flat && TRUE === $split) {
         // invalid combination
         $split = FALSE;
     }
     /* iterate components */
     $result = array();
     $this->sort('UID');
     $compUIDcmp = null;
     $exdatelist = $recurridList = array();
     $intervalP1D = new DateInterval('P1D');
     foreach ($this->components as $cix => $component) {
         if (empty($component)) {
             continue;
         }
         /* deselect unvalid type components */
         if (!in_array($component->objName, $cType)) {
             continue;
         }
         unset($compStart, $compEnd);
         /* select start from dtstart or due if dtstart is missing */
         $prop = $component->getProperty('dtstart', FALSE, TRUE);
         if (empty($prop) && $component->objName == 'vtodo' && FALSE === ($prop = $component->getProperty('due', FALSE, TRUE))) {
             continue;
         }
         if (empty($prop)) {
             continue;
         }
         /* get UID */
         $compUID = $component->getProperty('UID');
         if ($compUIDcmp != $compUID) {
             $compUIDcmp = $compUID;
             $exdatelist = $recurridList = array();
         }
         $recurrid = FALSE;
         // file_put_contents( '/opt/work/iCal/iCalcreator/iCalcreator-2.20.x/log/log.txt', "#$cix".PHP_EOL.var_export( $component, TRUE ).PHP_EOL.PHP_EOL, FILE_APPEND ); // test ###
         $compStart = iCaldateTime::factory($prop['value'], $prop['params'], $prop['value']);
         $dtstartTz = $compStart->getTimezoneName();
         if (isset($prop['params']['VALUE']) && 'DATE' == $prop['params']['VALUE']) {
             $compStartHis = '';
         } else {
             $his = $compStart->getTime();
             $compStartHis = sprintf(iCalUtilityFunctions::$fmt['His'], (int) $his[0], (int) $his[1], (int) $his[2]);
         }
         /* get end date from dtend/due/duration properties */
         if (FALSE !== ($prop = $component->getProperty('dtend', FALSE, TRUE))) {
             $compEnd = iCaldateTime::factory($prop['value'], $prop['params'], $prop['value'], $dtstartTz);
             $compEnd->SCbools['dtendExist'] = TRUE;
         }
         if (empty($prop) && $component->objName == 'vtodo' && FALSE !== ($prop = $component->getProperty('due', FALSE, TRUE))) {
             $compEnd = iCaldateTime::factory($prop['value'], $prop['params'], $prop['value'], $dtstartTz);
             $compEnd->SCbools['dueExist'] = TRUE;
         }
         if (empty($prop) && FALSE !== ($prop = $component->getProperty('duration', FALSE, TRUE, TRUE))) {
             // in dtend (array) format
             $compEnd = iCaldateTime::factory($prop['value'], $prop['params'], $prop['value'], $dtstartTz);
             $compEnd->SCbools['durationExist'] = TRUE;
         }
         if (!empty($prop) && !isset($prop['value']['hour'])) {
             /* a DTEND without time part denotes an end of an event that actually ends the day before,
                for an all-day event DTSTART=20071201 DTEND=20071202, taking place 20071201!!! */
             $compEnd->SCbools['endAllDayEvent'] = TRUE;
             $compEnd->modify('-1 day');
             $compEnd->setTime(23, 59, 59);
         }
         unset($prop);
         if (empty($compEnd)) {
             $compDuration = FALSE;
             $compEnd = clone $compStart;
             $compEnd->setTime(23, 59, 59);
             //  23:59:59 the same day as start
         } else {
             if ($compEnd->format('Ymd') < $compStart->format('Ymd')) {
                 // MUST be after start date!!
                 $compEnd = clone $compStart;
                 $compEnd->setTime(23, 59, 59);
                 //  23:59:59 the same day as start or ???
             }
             $compDuration = $compStart->diff($compEnd);
             // DateInterval
         }
         /* check recurrence-id (note, a missing sequence is the same as sequence=0 so don't test for sequence), to alter when hit dtstart/recurlist */
         if (FALSE !== ($prop = $component->getProperty('recurrence-id', FALSE, TRUE))) {
             $recurrid = iCaldateTime::factory($prop['value'], $prop['params'], $prop['value'], $dtstartTz);
             $rangeSet = isset($prop['params']['RANGE']) && 'THISANDFUTURE' == $prop['params']['RANGE'] ? TRUE : FALSE;
             $recurridList[$recurrid->key] = array(clone $compStart, clone $compEnd, $compDuration, $rangeSet);
             // change recur this day to new YmdHis/duration/range
             // echo "adding comp no:$cix with date=".$compStart->format(iCalUtilityFunctions::$fmt['YmdHis2e'])." to recurridList id={$recurrid->key}, newDate={$compStart->key}<br>\n"; // test ###
             unset($prop);
             continue;
             // ignore any other props in the component
         }
         // end recurrence-id/sequence test
         // else echo "comp no:$cix with date=".$compStart->format().", NO recurrence-id<br>\n"; // test ###
         ksort($recurridList, SORT_STRING);
         // echo 'recurridList='.implode(', ', array_keys( $recurridList ))."<br>\n"; // test ###
         $fcnStart = clone $compStart;
         $fcnStart->setDate((int) $startY, (int) $startM, (int) $startD);
         $fcnStart->setTime(0, 0, 0);
         $fcnEnd = clone $compEnd;
         $fcnEnd->setDate((int) $endY, (int) $endM, (int) $endD);
         $fcnEnd->setTime(23, 59, 59);
         // echo 'compStart='.$compStart->format().', compEnd'.$compEnd->format(); if($compDuration)echo ', interval='.$compDuration->format( iCalUtilityFunctions::$fmt['durDHis'] ); echo "<br>\n"; $tcnt = 0;// test ###
         /* *************************************************************
            make a list of optional exclude dates for component occurence from exrule and exdate
            *********************************************************** */
         $workStart = clone $compStart;
         $workStart->sub($compDuration ? $compDuration : $intervalP1D);
         $workEnd = clone $fcnEnd;
         $workEnd->add($compDuration ? $compDuration : $intervalP1D);
         while (FALSE !== ($prop = $component->getProperty('EXRULE'))) {
             $exdatelist2 = array();
             if (isset($prop['UNTIL']['hour'])) {
                 // convert until date to dtstart timezone
                 $until = iCaldateTime::factory($prop['UNTIL'], array('TZID' => 'UTC'), null, $dtstartTz);
                 $until = $until->format();
                 iCalUtilityFunctions::_strDate2arr($until);
                 $prop['UNTIL'] = $until;
             }
             iCalUtilityFunctions::_recur2date($exdatelist2, $prop, $compStart, $workStart, $workEnd);
             foreach ($exdatelist2 as $k => $v) {
                 $exdatelist[$k . $compStartHis] = $v;
             }
             // point out exact every excluded ocurrence (incl. opt. His)
             unset($until, $exdatelist2);
         }
         while (FALSE !== ($prop = $component->getProperty('EXDATE', FALSE, TRUE))) {
             // - start check exdate
             foreach ($prop['value'] as $exdate) {
                 $exdate = iCaldateTime::factory($exdate, $prop['params'], $exdate, $dtstartTz);
                 $exdatelist[$exdate->key] = TRUE;
             }
             // end - foreach( $exdate as $exdate )
         }
         // end - check exdate
         unset($prop, $exdate);
         // echo 'exdatelist='  .implode(', ', array_keys( $exdatelist ))  ."<br>\n"; // test ###
         /* *************************************************************
            select only components within.. .
            *********************************************************** */
         $xRecurrence = 1;
         if (!$any && iCalUtilityFunctions::_inScope($compStart, $fcnStart, $compStart, $fcnEnd, $compStart->dateFormat) || $any && iCalUtilityFunctions::_inScope($fcnEnd, $compStart, $fcnStart, $compEnd, $compStart->dateFormat)) {
             /* add the selected component (WITHIN valid dates) to output array */
             if ($flat) {
                 // any=true/false, ignores split
                 if (!$recurrid) {
                     $result[$compUID] = $component->copy();
                 }
                 // copy original to output (but not anyone with recurrence-id)
             } elseif ($split) {
                 // split the original component
                 // echo 'split org.:'.$compStart->format().' < '.$fcnStart->format( 'Ymd His e' )."<br>\n"; // test ###
                 if ($compStart->format(iCalUtilityFunctions::$fmt['YmdHis2']) < $fcnStart->format(iCalUtilityFunctions::$fmt['YmdHis2'])) {
                     $rstart = clone $fcnStart;
                 } else {
                     $rstart = clone $compStart;
                 }
                 if ($compEnd->format(iCalUtilityFunctions::$fmt['YmdHis2']) > $fcnEnd->format(iCalUtilityFunctions::$fmt['YmdHis2'])) {
                     $rend = clone $fcnEnd;
                 } else {
                     $rend = clone $compEnd;
                 }
                 // echo "going to test comp no:$cix, rstart=".$rstart->format( iCalUtilityFunctions::$fmt['YmdHis2e'] )." (key={$rstart->key}), end=".$rend->format( iCalUtilityFunctions::$fmt['YmdHis2e'] )."<br>\n"; // test ###
                 if (!isset($exdatelist[$rstart->key])) {
                     // not excluded in exrule/exdate
                     if (isset($recurridList[$rstart->key])) {
                         // change start day to new YmdHis/duration
                         $k = $rstart->key;
                         // echo "recurridList HIT, key={$k}, recur Date=".$recurridList[$k][0]->key."<br>\n"; // test ###
                         $rstart = clone $recurridList[$k][0];
                         $startHis = $rstart->getTime();
                         $rend = clone $rstart;
                         if (FALSE !== $recurridList[$k][2]) {
                             $rend->add($recurridList[$k][2]);
                         } elseif (FALSE !== $compDuration) {
                             $rend->add($compDuration);
                         }
                         $endHis = $rend->getTime();
                         unset($recurridList[$k]);
                     } else {
                         $startHis = $compStart->getTime();
                         $endHis = $compEnd->getTime();
                     }
                     // echo "_____testing comp no:$cix, rstart=".$rstart->format( iCalUtilityFunctions::$fmt['YmdHis2e'] )." (key={$rstart->key}), end=".$rend->format( iCalUtilityFunctions::$fmt['YmdHis2e'] )."<br>\n"; // test ###
                     $cnt = 0;
                     // exclude any recurrence START date, found in exdatelist or recurridList but accept the reccurence-id comp itself
                     $occurenceDays = 1 + (int) $rstart->diff($rend)->format('%a');
                     // count the days (incl start day)
                     while ($rstart->format(iCalUtilityFunctions::$fmt['Ymd2']) <= $rend->format(iCalUtilityFunctions::$fmt['Ymd2'])) {
                         $cnt += 1;
                         if (1 < $occurenceDays) {
                             $component->setProperty('X-OCCURENCE', sprintf(iCalUtilityFunctions::$fmt['dayOfDays'], $cnt, $occurenceDays));
                         }
                         if (1 < $cnt) {
                             $rstart->setTime(0, 0, 0);
                         } else {
                             $rstart->setTime($startHis[0], $startHis[1], $startHis[2]);
                             $exdatelist[$rstart->key] = $compDuration;
                             // make sure to exclude start day from the recurrence pattern
                         }
                         $component->setProperty('X-CURRENT-DTSTART', $rstart->format($compStart->dateFormat));
                         if (FALSE !== $compDuration) {
                             $propName = isset($compEnd->SCbools['dueExist']) ? 'X-CURRENT-DUE' : 'X-CURRENT-DTEND';
                             if ($cnt < $occurenceDays) {
                                 $rstart->setTime(23, 59, 59);
                             } else {
                                 $rstart->setTime($endHis[0], $endHis[1], $endHis[2]);
                             }
                             $component->setProperty($propName, $rstart->format($compEnd->dateFormat));
                         }
                         $result[(int) $rstart->format('Y')][(int) $rstart->format('m')][(int) $rstart->format('d')][$compUID] = $component->copy();
                         // copy to output
                         $rstart->add($intervalP1D);
                     }
                     // end while(( $rstart->format( 'Ymd' ) < $rend->format( 'Ymd' ))
                     unset($cnt, $occurenceDays);
                 }
                 // end if( ! isset( $exdatelist[$rstart->key] ))
                 // else echo "skip no:$cix with date=".$compStart->format()."<br>\n"; // test ###
                 unset($rstart, $rend);
             } else {
                 // !$flat && !$split, i.e. no flat array and DTSTART within period
                 $tstart = isset($recurridList[$compStart->key]) ? clone $recurridList[$k][0] : clone $compStart;
                 // echo "going to test comp no:$cix with checkDate={$compStart->key} with recurridList=".implode(',',array_keys($recurridList)); // test ###
                 if (!$any || !isset($exdatelist[$tstart->key])) {
                     // exclude any recurrence date, found in exdatelist
                     // echo " and copied to output<br>\n"; // test ###
                     $result[(int) $tstart->format('Y')][(int) $tstart->format('m')][(int) $tstart->format('d')][$compUID] = $component->copy();
                     // copy to output
                 }
                 unset($tstart);
             }
         }
         // end (dt)start within the period OR occurs within the period
         /* *************************************************************
            if 'any' components, check components with reccurrence rules, removing all excluding dates
            *********************************************************** */
         if (TRUE === $any) {
             $recurlist = array();
             /* make a list of optional repeating dates for component occurence, rrule, rdate */
             while (FALSE !== ($prop = $component->getProperty('RRULE'))) {
                 // get all rrule dates (multiple values allowed)
                 $recurlist2 = array();
                 if (isset($prop['UNTIL']['hour'])) {
                     // convert $rrule['UNTIL'] to the same timezone as DTSTART !!
                     $until = iCaldateTime::factory($prop['UNTIL'], array('TZID' => 'UTC'), null, $dtstartTz);
                     $until = $until->format();
                     iCalUtilityFunctions::_strDate2arr($until);
                     $prop['UNTIL'] = $until;
                 }
                 iCalUtilityFunctions::_recur2date($recurlist2, $prop, $compStart, $workStart, $workEnd);
                 foreach ($recurlist2 as $recurkey => $recurvalue) {
                     // recurkey=Ymd
                     $recurkey .= $compStartHis;
                     // add opt His
                     if (!isset($exdatelist[$recurkey])) {
                         $recurlist[$recurkey] = $compDuration;
                     }
                     // DateInterval or FALSE
                 }
                 unset($prop, $until, $recurlist2);
             }
             $workStart = clone $fcnStart;
             $workStart->sub($compDuration ? $compDuration : $intervalP1D);
             $format = $compStart->dateFormat;
             while (FALSE !== ($prop = $component->getProperty('RDATE', FALSE, TRUE))) {
                 $rdateFmt = isset($prop['params']['VALUE']) ? $prop['params']['VALUE'] : 'DATETIME';
                 $params = $prop['params'];
                 $prop = $prop['value'];
                 foreach ($prop as $theRdate) {
                     if ('PERIOD' == $rdateFmt) {
                         // all days within PERIOD
                         $rdate = iCaldateTime::factory($theRdate[0], $params, $theRdate[0], $dtstartTz);
                         if (!iCalUtilityFunctions::_inScope($rdate, $workStart, $rdate, $fcnEnd, $format) || isset($exdatelist[$rdate->key])) {
                             continue;
                         }
                         if (isset($theRdate[1]['year'])) {
                             // date-date period end
                             $recurlist[$rdate->key] = $rdate->diff(iCaldateTime::factory($theRdate[1], $params, $theRdate[1], $dtstartTz));
                         } else {
                             // period duration
                             $recurlist[$rdate->key] = new DateInterval(iCalUtilityFunctions::_duration2str($theRdate[1]));
                         }
                     } elseif ('DATE' == $rdateFmt) {
                         // single recurrence, date
                         $rdate = iCaldateTime::factory($theRdate, array_merge($params, array('TZID' => $dtstartTz)), null, $dtstartTz);
                         if (iCalUtilityFunctions::_inScope($rdate, $workStart, $rdate, $fcnEnd, $format) && !isset($exdatelist[$rdate->key])) {
                             $recurlist[$rdate->key . $compStartHis] = $compDuration;
                         }
                         // set start date for recurrence + DateInterval/FALSE (+opt His)
                     } else {
                         // start DATETIME
                         $rdate = iCaldateTime::factory($theRdate, $params, $theRdate, $dtstartTz);
                         if (iCalUtilityFunctions::_inScope($rdate, $workStart, $rdate, $fcnEnd, $format) && !isset($exdatelist[$rdate->key])) {
                             $recurlist[$rdate->key] = $compDuration;
                         }
                         // set start datetime for recurrence DateInterval/FALSE
                     }
                     // end DATETIME
                 }
                 // end foreach( $prop as $theRdate )
             }
             // end while( FALSE !== ( $prop = $component->getProperty( 'rdate', FALSE, TRUE )))
             unset($prop, $workStart, $format, $theRdate, $rdate, $rend);
             foreach ($recurridList as $rKey => $rVal) {
                 // check for recurrence-id, i.e. alter recur Ymd[His] and duration
                 if (isset($recurlist[$rKey])) {
                     unset($recurlist[$rKey]);
                     $recurlist[$rVal[0]->key] = FALSE !== $rVal[2] ? $rVal[2] : $compDuration;
                     // echo "alter recurfrom {$rKey} to {$rVal[0]->key} ";if(FALSE!==$dur)echo " ({$dur->format( '%a days, %h-%i-%s' )})";echo "<br>\n"; // test ###
                 }
             }
             ksort($recurlist, SORT_STRING);
             // echo 'recurlist='   .implode(', ', array_keys( $recurlist ))   ."<br>\n"; // test ###
             // echo 'recurridList='   .implode(', ', array_keys( $recurridList ))   ."<br>\n"; // test ###
             /* *************************************************************
                output all remaining components in recurlist
                *********************************************************** */
             if (0 < count($recurlist)) {
                 $component2 = $component->copy();
                 $compUID = $component2->getProperty('UID');
                 $workStart = clone $fcnStart;
                 $workStart->sub($compDuration ? $compDuration : $intervalP1D);
                 $YmdOld = null;
                 foreach ($recurlist as $recurkey => $durvalue) {
                     if ($YmdOld == substr($recurkey, 0, 8)) {
                         // skip overlapping recur the same day, i.e. RDATE before RRULE
                         continue;
                     }
                     $YmdOld = substr($recurkey, 0, 8);
                     $rstart = clone $compStart;
                     $rstart->setDate((int) substr($recurkey, 0, 4), (int) substr($recurkey, 4, 2), (int) substr($recurkey, 6, 2));
                     $rstart->setTime((int) substr($recurkey, 8, 2), (int) substr($recurkey, 10, 2), (int) substr($recurkey, 12, 2));
                     // echo "recur start=".$rstart->format( iCalUtilityFunctions::$fmt['YmdHis2e'] )."<br>\n"; // test ###;
                     /* add recurring components within valid dates to output array, only start date set */
                     if ($flat) {
                         if (!isset($result[$compUID])) {
                             // only one comp
                             $result[$compUID] = $component2->copy();
                         }
                         // copy to output
                     } elseif ($split) {
                         $rend = clone $rstart;
                         if (FALSE !== $durvalue) {
                             $rend->add($durvalue);
                         }
                         if ($rend->format(iCalUtilityFunctions::$fmt['Ymd2']) > $fcnEnd->format(iCalUtilityFunctions::$fmt['Ymd2'])) {
                             $rend = clone $fcnEnd;
                         }
                         // echo "recur 1={$recurkey}, start=".$rstart->format( iCalUtilityFunctions::$fmt['YmdHis2e'] ).", end=".$rend->format( iCalUtilityFunctions::$fmt['YmdHis2e'] );if($durvalue) echo ", duration=".$durvalue->format( iCalUtilityFunctions::$fmt['durDHis'] );echo "<br>\n"; // test ###
                         $xRecurrence += 1;
                         $cnt = 0;
                         $occurenceDays = 1 + (int) $rstart->diff($rend)->format('%a');
                         // count the days (incl start day)
                         while ($rstart->format(iCalUtilityFunctions::$fmt['Ymd2']) <= $rend->format(iCalUtilityFunctions::$fmt['Ymd2'])) {
                             // iterate.. .
                             $cnt += 1;
                             if ($rstart->format(iCalUtilityFunctions::$fmt['Ymd2']) < $fcnStart->format(iCalUtilityFunctions::$fmt['Ymd2'])) {
                                 // date before dtstart
                                 // echo "recur 3, start=".$rstart->format( 'Y-m-d H:i:s' )." &gt;= fcnStart=".$fcnStart->format( 'Y-m-d H:i:s' )."<br>\n"; // test ###
                                 $rstart->add($intervalP1D);
                                 $rstart->setTime(0, 0, 0);
                                 continue;
                             } elseif (2 == $cnt) {
                                 $rstart->setTime(0, 0, 0);
                             }
                             $component2->setProperty('X-RECURRENCE', $xRecurrence);
                             if (1 < $occurenceDays) {
                                 $component2->setProperty('X-OCCURENCE', sprintf(iCalUtilityFunctions::$fmt['dayOfDays'], $cnt, $occurenceDays));
                             } else {
                                 $component2->deleteProperty('X-OCCURENCE');
                             }
                             $component2->setProperty('X-CURRENT-DTSTART', $rstart->format($compStart->dateFormat));
                             $propName = isset($compEnd->SCbools['dueExist']) ? 'X-CURRENT-DUE' : 'X-CURRENT-DTEND';
                             if (FALSE !== $durvalue) {
                                 if ($cnt < $occurenceDays) {
                                     $rstart->setTime(23, 59, 59);
                                 } else {
                                     $His = $rend->getTime();
                                     // set end time
                                     $rstart->setTime($His[0], $His[1], $His[2]);
                                 }
                                 $component2->setProperty($propName, $rstart->format($compEnd->dateFormat));
                                 // echo "checking date, (day {$cnt} of {$occurenceDays}), _end_=".$rstart->format( 'Y-m-d H:i:s e' )."<br>"; // test ###;
                             } else {
                                 $component2->deleteProperty($propName);
                             }
                             $result[(int) $rstart->format('Y')][(int) $rstart->format('m')][(int) $rstart->format('d')][$compUID] = $component2->copy();
                             // copy to output
                             $rstart->add($intervalP1D);
                         }
                         // end while( $rstart->format( 'Ymd' ) <= $rend->format( 'Ymd' ))
                         unset($rstart, $rend);
                     } elseif ($rstart->format(iCalUtilityFunctions::$fmt['Ymd2']) >= $fcnStart->format(iCalUtilityFunctions::$fmt['Ymd2'])) {
                         $xRecurrence += 1;
                         // date within period  //* flat=FALSE && split=FALSE => one comp every recur startdate *//
                         $component2->setProperty('X-RECURRENCE', $xRecurrence);
                         $component2->setProperty('X-CURRENT-DTSTART', $rstart->format($compStart->dateFormat));
                         $propName = isset($compEnd->SCbools['dueExist']) ? 'X-CURRENT-DUE' : 'X-CURRENT-DTEND';
                         if (FALSE !== $durvalue) {
                             $rstart->add($durvalue);
                             $component2->setProperty($propName, $rstart->format($compEnd->dateFormat));
                         } else {
                             $component2->deleteProperty($propName);
                         }
                         $result[(int) $rstart->format('Y')][(int) $rstart->format('m')][(int) $rstart->format('d')][$compUID] = $component2->copy();
                         // copy to output
                     }
                     // end elseif( $rstart >= $fcnStart )
                     unset($rstart);
                 }
                 // end foreach( $recurlist as $recurkey => $durvalue )
                 unset($component2, $xRecurrence, $compUID, $workStart, $rstart);
             }
             // end if( 0 < count( $recurlist ))
         }
         // end if( TRUE === $any )
         unset($component);
     }
     // end foreach ( $this->components as $cix => $component )
     unset($recurrid, $recurridList, $fcnStart, $fcnEnd, $compStart, $compEnd, $exdatelist, $recurlist);
     // clean up
     if (0 >= count($result)) {
         return FALSE;
     } elseif (!$flat) {
         foreach ($result as $y => $yeararr) {
             foreach ($yeararr as $m => $montharr) {
                 foreach ($montharr as $d => $dayarr) {
                     if (empty($result[$y][$m][$d])) {
                         unset($result[$y][$m][$d]);
                     } else {
                         $result[$y][$m][$d] = array_values($dayarr);
                         // skip tricky UID-index
                         if (1 < count($result[$y][$m][$d])) {
                             foreach ($result[$y][$m][$d] as &$c) {
                                 // sort
                                 iCalUtilityFunctions::_setSortArgs($c);
                             }
                             usort($result[$y][$m][$d], array('iCalUtilityFunctions', '_cmpfcn'));
                         }
                     }
                 }
                 // end foreach( $montharr as $d => $dayarr )
                 if (empty($result[$y][$m])) {
                     unset($result[$y][$m]);
                 } else {
                     ksort($result[$y][$m]);
                 }
             }
             // end foreach( $yeararr as $m => $montharr )
             if (empty($result[$y])) {
                 unset($result[$y]);
             } else {
                 ksort($result[$y]);
             }
         }
         // end foreach( $result as $y => $yeararr )
         if (empty($result)) {
             unset($result);
         } else {
             ksort($result);
         }
     }
     // end elseif( !$flat )
     if (0 >= count($result)) {
         return FALSE;
     }
     return $result;
 }
예제 #3
0
 /**
  * creates formatted output for calendar component property data value type duration
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.15.8 - 2012-10-30
  * @param array $duration, array( week, day, hour, min, sec )
  * @return string
  */
 public static function _format_duration($duration)
 {
     return iCalUtilityFunctions::_duration2str($duration);
 }
예제 #4
0
/**
 * Add children to a SimpleXMLelement
 *
 * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
 * @since 2.18.10 - 2013-09-04
 * @param object $parent   reference to a SimpleXMLelement node
 * @param string $name     new element node name
 * @param string $type     content type, subelement(-s) name
 * @param string $content  new subelement content
 * @param array  $params   new element 'attributes'
 * @uses iCalUtilityFunctions::_duration2str()
 * @uses iCalUtilityFunctions::_geo2str2()
 * @uses iCalUtilityFunctions::$geoLatFmt
 * @uses iCalUtilityFunctions::$geoLongFmt
 * @return void
 */
function _addXMLchild(&$parent, $name, $type, $content, $params = array())
{
    static $fmtYmd = '%04d-%02d-%02d';
    static $fmtYmdHis = '%04d-%02d-%02dT%02d:%02d:%02d';
    /** create new child node */
    $name = strtolower($name);
    $child = $parent->addChild($name);
    if (!empty($params)) {
        $parameters = $child->addChild('parameters');
        foreach ($params as $param => $parVal) {
            if ('VALUE' == $param) {
                continue;
            }
            $param = strtolower($param);
            if ('x-' == substr($param, 0, 2)) {
                $p1 = $parameters->addChild($param);
                $p2 = $p1->addChild('unknown', htmlspecialchars($parVal));
            } else {
                $p1 = $parameters->addChild($param);
                switch ($param) {
                    case 'altrep':
                    case 'dir':
                        $ptype = 'uri';
                        break;
                    case 'delegated-from':
                    case 'delegated-to':
                    case 'member':
                    case 'sent-by':
                        $ptype = 'cal-address';
                        break;
                    case 'rsvp':
                        $ptype = 'boolean';
                        break;
                    default:
                        $ptype = 'text';
                        break;
                }
                if (is_array($parVal)) {
                    foreach ($parVal as $pV) {
                        $p2 = $p1->addChild($ptype, htmlspecialchars($pV));
                    }
                } else {
                    $p2 = $p1->addChild($ptype, htmlspecialchars($parVal));
                }
            }
        }
    }
    // end if( !empty( $params ))
    if (empty($content) && '0' != $content || !is_array($content) && ('-' != substr($content, 0, 1) && 0 > $content)) {
        return;
    }
    /** store content */
    switch ($type) {
        case 'binary':
            $v = $child->addChild($type, $content);
            break;
        case 'boolean':
            break;
        case 'cal-address':
            $v = $child->addChild($type, $content);
            break;
        case 'date':
            if (array_key_exists('year', $content)) {
                $content = array($content);
            }
            foreach ($content as $date) {
                $str = sprintf($fmtYmd, (int) $date['year'], (int) $date['month'], (int) $date['day']);
                $v = $child->addChild($type, $str);
            }
            break;
        case 'date-time':
            if (array_key_exists('year', $content)) {
                $content = array($content);
            }
            foreach ($content as $dt) {
                if (!isset($dt['hour'])) {
                    $dt['hour'] = 0;
                }
                if (!isset($dt['min'])) {
                    $dt['min'] = 0;
                }
                if (!isset($dt['sec'])) {
                    $dt['sec'] = 0;
                }
                $str = sprintf($fmtYmdHis, (int) $dt['year'], (int) $dt['month'], (int) $dt['day'], (int) $dt['hour'], (int) $dt['min'], (int) $dt['sec']);
                if (isset($dt['tz']) && 'Z' == $dt['tz']) {
                    $str .= 'Z';
                }
                $v = $child->addChild($type, $str);
            }
            break;
        case 'duration':
            $output = 'trigger' == $name && FALSE !== $content['before'] ? '-' : '';
            $v = $child->addChild($type, $output . iCalUtilityFunctions::_duration2str($content));
            break;
        case 'geo':
            if (!empty($content)) {
                $v1 = $child->addChild('latitude', iCalUtilityFunctions::_geo2str2($content['latitude'], iCalUtilityFunctions::$geoLatFmt));
                $v1 = $child->addChild('longitude', iCalUtilityFunctions::_geo2str2($content['longitude'], iCalUtilityFunctions::$geoLongFmt));
            }
            break;
        case 'integer':
            $v = $child->addChild($type, (string) $content);
            break;
        case 'period':
            if (!is_array($content)) {
                break;
            }
            foreach ($content as $period) {
                $v1 = $child->addChild($type);
                $str = sprintf($fmtYmdHis, (int) $period[0]['year'], (int) $period[0]['month'], (int) $period[0]['day'], (int) $period[0]['hour'], (int) $period[0]['min'], (int) $period[0]['sec']);
                if (isset($period[0]['tz']) && 'Z' == $period[0]['tz']) {
                    $str .= 'Z';
                }
                $v2 = $v1->addChild('start', $str);
                if (array_key_exists('year', $period[1])) {
                    $str = sprintf($fmtYmdHis, (int) $period[1]['year'], (int) $period[1]['month'], (int) $period[1]['day'], (int) $period[1]['hour'], (int) $period[1]['min'], (int) $period[1]['sec']);
                    if (isset($period[1]['tz']) && 'Z' == $period[1]['tz']) {
                        $str .= 'Z';
                    }
                    $v2 = $v1->addChild('end', $str);
                } else {
                    $v2 = $v1->addChild('duration', iCalUtilityFunctions::_duration2str($period[1]));
                }
            }
            break;
        case 'recur':
            $content = array_change_key_case($content);
            foreach ($content as $rulelabel => $rulevalue) {
                switch ($rulelabel) {
                    case 'until':
                        if (isset($rulevalue['hour'])) {
                            $str = sprintf($fmtYmdHis, (int) $rulevalue['year'], (int) $rulevalue['month'], (int) $rulevalue['day'], (int) $rulevalue['hour'], (int) $rulevalue['min'], (int) $rulevalue['sec']) . 'Z';
                        } else {
                            $str = sprintf($fmtYmd, (int) $rulevalue['year'], (int) $rulevalue['month'], (int) $rulevalue['day']);
                        }
                        $v = $child->addChild($rulelabel, $str);
                        break;
                    case 'bysecond':
                    case 'byminute':
                    case 'byhour':
                    case 'bymonthday':
                    case 'byyearday':
                    case 'byweekno':
                    case 'bymonth':
                    case 'bysetpos':
                        if (is_array($rulevalue)) {
                            foreach ($rulevalue as $vix => $valuePart) {
                                $v = $child->addChild($rulelabel, $valuePart);
                            }
                        } else {
                            $v = $child->addChild($rulelabel, $rulevalue);
                        }
                        break;
                    case 'byday':
                        if (isset($rulevalue['DAY'])) {
                            $str = isset($rulevalue[0]) ? $rulevalue[0] : '';
                            $str .= $rulevalue['DAY'];
                            $p = $child->addChild($rulelabel, $str);
                        } else {
                            foreach ($rulevalue as $valuePart) {
                                if (isset($valuePart['DAY'])) {
                                    $str = isset($valuePart[0]) ? $valuePart[0] : '';
                                    $str .= $valuePart['DAY'];
                                    $p = $child->addChild($rulelabel, $str);
                                } else {
                                    $p = $child->addChild($rulelabel, $valuePart);
                                }
                            }
                        }
                        break;
                    case 'freq':
                    case 'count':
                    case 'interval':
                    case 'wkst':
                    default:
                        $p = $child->addChild($rulelabel, $rulevalue);
                        break;
                }
                // end switch( $rulelabel )
            }
            // end foreach( $content as $rulelabel => $rulevalue )
            break;
        case 'rstatus':
            $v = $child->addChild('code', number_format((double) $content['statcode'], 2, '.', ''));
            $v = $child->addChild('description', htmlspecialchars($content['text']));
            if (isset($content['extdata'])) {
                $v = $child->addChild('data', htmlspecialchars($content['extdata']));
            }
            break;
        case 'text':
            if (!is_array($content)) {
                $content = array($content);
            }
            foreach ($content as $part) {
                $v = $child->addChild($type, htmlspecialchars($part));
            }
            break;
        case 'time':
            break;
        case 'uri':
            $v = $child->addChild($type, $content);
            break;
        case 'utc-offset':
            if (in_array(substr($content, 0, 1), array('-', '+'))) {
                $str = substr($content, 0, 1);
                $content = substr($content, 1);
            } else {
                $str = '+';
            }
            $str .= substr($content, 0, 2) . ':' . substr($content, 2, 2);
            if (4 < strlen($content)) {
                $str .= ':' . substr($content, 4);
            }
            $v = $child->addChild($type, $str);
            break;
        case 'unknown':
        default:
            if (is_array($content)) {
                $content = implode('', $content);
            }
            $v = $child->addChild('unknown', htmlspecialchars($content));
            break;
    }
}