/**
  * 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;
 }
 /**
  * return iCaldateTime object instance based on date array and timezone(s)
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.21.11 - 2015-03-28
  * @param array  $date
  * @param array  $params
  * @param array  $tz
  * @param string $dtstartTz
  * @uses iCalUtilityFunctions::$fmt
  * @uses iCaldateTime::getTimezoneName()
  * @uses iCaldateTime::$dateFormat
  * @uses iCaldateTime::$key
  * @return object instance
  */
 public static function factory(array $date, $params = null, $tz = null, $dtstartTz = null)
 {
     if (isset($params['TZID']) && !empty($params['TZID'])) {
         $tz = 'Z' == $params['TZID'] ? 'UTC' : $params['TZID'];
     } elseif (isset($tz['tz']) && !empty($tz['tz'])) {
         $tz = 'Z' == $tz['tz'] ? 'UTC' : $tz['tz'];
     } else {
         $tz = ini_get('date_default_timezone_set');
     }
     $strdate = sprintf(iCalUtilityFunctions::$fmt['Ymd'], (int) $date['year'], (int) $date['month'], (int) $date['day']);
     if (isset($date['hour'])) {
         $strdate .= 'T' . sprintf(iCalUtilityFunctions::$fmt['His'], (int) $date['hour'], (int) $date['min'], (int) $date['sec']);
     }
     try {
         $timezone = new DateTimeZone($tz);
         $d = new iCaldateTime($strdate, $timezone);
     } catch (Exception $e) {
         $d = new iCaldateTime($strdate);
     }
     if (!empty($dtstartTz)) {
         if ('Z' == $dtstartTz) {
             $dtstartTz = 'UTC';
         }
         if ($dtstartTz != $d->getTimezoneName()) {
             // set the same timezone as dtstart
             try {
                 $timezone = new DateTimeZone($dtstartTz);
                 $d->setTimezone($timezone);
             } catch (Exception $e) {
             }
         }
     }
     unset($timezone, $strdate);
     if (isset($params['VALUE']) && 'DATE' == $params['VALUE']) {
         $d->dateFormat = 'Y-m-d';
         $d->key = $d->format('Ymd');
     } else {
         $d->key = $d->format('YmdHis');
     }
     return $d;
 }