예제 #1
0
 /**
  * transforms a dateTime from a timezone to another using PHP DateTime and DateTimeZone class (PHP >= PHP 5.2.0)
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.15.1 - 2012-10-17
  * @param mixed  $date,   date to alter
  * @param string $tzFrom, PHP valid 'from' timezone
  * @param string $tzTo,   PHP valid 'to' timezone, default 'UTC'
  * @param string $format, date output format, default 'Ymd\THis'
  * @return bool
  */
 public static function transformDateTime(&$date, $tzFrom, $tzTo = 'UTC', $format = 'Ymd\\THis')
 {
     if (is_array($date) && isset($date['timestamp'])) {
         try {
             $d = new \DateTime("@{$date['timestamp']}");
             // set UTC date
             $d->setTimezone(new \DateTimeZone($tzFrom));
             // convert to 'from' date
         } catch (Exception $e) {
             return FALSE;
         }
     } else {
         if (ICalUtilityFunctions::_isArrayDate($date)) {
             if (isset($date['tz'])) {
                 unset($date['tz']);
             }
             $date = ICalUtilityFunctions::_date2strdate(ICalUtilityFunctions::_chkDateArr($date));
         }
         if ('Z' == substr($date, -1)) {
             $date = substr($date, 0, strlen($date) - 2);
         }
         try {
             $d = new \DateTime($date, new \DateTimeZone($tzFrom));
         } catch (Exception $e) {
             return FALSE;
         }
     }
     try {
         $d->setTimezone(new \DateTimeZone($tzTo));
     } catch (Exception $e) {
         return FALSE;
     }
     $date = $d->format($format);
     return TRUE;
 }
예제 #2
0
파일: VCalendar.php 프로젝트: ephp/cal
 /**
  * sort iCal compoments
  *
  * ascending sort on properties (if exist) x-current-dtstart, dtstart,
  * x-current-dtend, dtend, x-current-due, due, duration, created, dtstamp, uid if called without arguments,
  * otherwise sorting on specific (argument) property values
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.16.4 - 2012-12-17
  * @param string $sortArg, optional
  * @return void
  *
  */
 function sort($sortArg = FALSE)
 {
     if (is_array($this->components)) {
         if ($sortArg) {
             $sortArg = strtoupper($sortArg);
             if (!in_array($sortArg, array('ATTENDEE', 'CATEGORIES', 'CONTACT', 'DTSTAMP', 'LOCATION', 'ORGANIZER', 'PRIORITY', 'RELATED-TO', 'RESOURCES', 'STATUS', 'SUMMARY', 'UID', 'URL'))) {
                 $sortArg = FALSE;
             }
         }
         /* set sort parameters for each component */
         foreach ($this->components as $cix => &$c) {
             $c->srtk = array('0', '0', '0', '0');
             if ('VTimeZone' == $c->objName) {
                 if (FALSE === ($c->srtk[0] = $c->getProperty('tzid'))) {
                     $c->srtk[0] = 0;
                 }
                 continue;
             } elseif ($sortArg) {
                 if ('ATTENDEE' == $sortArg || 'CATEGORIES' == $sortArg || 'CONTACT' == $sortArg || 'RELATED-TO' == $sortArg || 'RESOURCES' == $sortArg) {
                     $propValues = array();
                     $c->_getProperties($sortArg, $propValues);
                     if (!empty($propValues)) {
                         $sk = array_keys($propValues);
                         $c->srtk[0] = $sk[0];
                         if ('RELATED-TO' == $sortArg) {
                             $c->srtk[0] .= $c->getProperty('uid');
                         }
                     } elseif ('RELATED-TO' == $sortArg) {
                         $c->srtk[0] = $c->getProperty('uid');
                     }
                 } elseif (FALSE !== ($d = $c->getProperty($sortArg))) {
                     $c->srtk[0] = $d;
                     if ('UID' == $sortArg) {
                         if (FALSE !== ($d = $c->getProperty('recurrence-id'))) {
                             $c->srtk[1] = ICalUtilityFunctions::_date2strdate($d);
                             if (FALSE === ($c->srtk[2] = $c->getProperty('sequence'))) {
                                 $c->srtk[2] = PHP_INT_MAX;
                             }
                         } else {
                             $c->srtk[1] = $c->srtk[2] = PHP_INT_MAX;
                         }
                     }
                 }
                 continue;
             }
             if (FALSE !== ($d = $c->getProperty('X-CURRENT-DTSTART'))) {
                 $c->srtk[0] = ICalUtilityFunctions::_strdate2date($d[1]);
                 unset($c->srtk[0]['unparsedtext']);
             } elseif (FALSE === ($c->srtk[0] = $c->getProperty('dtstart'))) {
                 $c->srtk[1] = 0;
             }
             // sortkey 0 : dtstart
             if (FALSE !== ($d = $c->getProperty('X-CURRENT-DTEND'))) {
                 $c->srtk[1] = ICalUtilityFunctions::_strdate2date($d[1]);
                 // sortkey 1 : dtend/due(/dtstart+duration)
                 unset($c->srtk[1]['unparsedtext']);
             } elseif (FALSE === ($c->srtk[1] = $c->getProperty('dtend'))) {
                 if (FALSE !== ($d = $c->getProperty('X-CURRENT-DUE'))) {
                     $c->srtk[1] = ICalUtilityFunctions::_strdate2date($d[1]);
                     unset($c->srtk[1]['unparsedtext']);
                 } elseif (FALSE === ($c->srtk[1] = $c->getProperty('due'))) {
                     if (FALSE === ($c->srtk[1] = $c->getProperty('duration', FALSE, FALSE, TRUE))) {
                         $c->srtk[1] = 0;
                     }
                 }
             }
             if (FALSE === ($c->srtk[2] = $c->getProperty('created'))) {
                 // sortkey 2 : created/dtstamp
                 if (FALSE === ($c->srtk[2] = $c->getProperty('dtstamp'))) {
                     $c->srtk[2] = 0;
                 }
             }
             if (FALSE === ($c->srtk[3] = $c->getProperty('uid'))) {
                 // sortkey 3 : uid
                 $c->srtk[3] = 0;
             }
         }
         // end foreach( $this->components as & $c
         /* sort */
         usort($this->components, array('ICalUtilityFunctions', '_cmpfcn'));
     }
 }
예제 #3
0
 /**
  * creates formatted output for calendar component property data value type recur
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.14.1 - 2012-10-06
  * @param array $recurlabel
  * @param array $recurdata
  * @return string
  */
 function _format_recur($recurlabel, $recurdata)
 {
     $output = null;
     foreach ($recurdata as $therule) {
         if (empty($therule['value'])) {
             if ($this->getConfig('allowEmpty')) {
                 $output .= $this->_createElement($recurlabel);
             }
             continue;
         }
         $attributes = isset($therule['params']) ? $this->_createParams($therule['params']) : null;
         $content1 = $content2 = null;
         foreach ($therule['value'] as $rulelabel => $rulevalue) {
             switch ($rulelabel) {
                 case 'FREQ':
                     $content1 .= "FREQ={$rulevalue}";
                     break;
                 case 'UNTIL':
                     $parno = isset($rulevalue['hour']) ? 7 : 3;
                     $content2 .= ';UNTIL=' . ICalUtilityFunctions::_date2strdate($rulevalue, $parno);
                     break;
                 case 'COUNT':
                 case 'INTERVAL':
                 case 'WKST':
                     $content2 .= ";{$rulelabel}={$rulevalue}";
                     break;
                 case 'BYSECOND':
                 case 'BYMINUTE':
                 case 'BYHOUR':
                 case 'BYMONTHDAY':
                 case 'BYYEARDAY':
                 case 'BYWEEKNO':
                 case 'BYMONTH':
                 case 'BYSETPOS':
                     $content2 .= ";{$rulelabel}=";
                     if (is_array($rulevalue)) {
                         foreach ($rulevalue as $vix => $valuePart) {
                             $content2 .= $vix ? ',' : null;
                             $content2 .= $valuePart;
                         }
                     } else {
                         $content2 .= $rulevalue;
                     }
                     break;
                 case 'BYDAY':
                     $content2 .= ";{$rulelabel}=";
                     $bydaycnt = 0;
                     foreach ($rulevalue as $vix => $valuePart) {
                         $content21 = $content22 = null;
                         if (is_array($valuePart)) {
                             $content2 .= $bydaycnt ? ',' : null;
                             foreach ($valuePart as $vix2 => $valuePart2) {
                                 if ('DAY' != strtoupper($vix2)) {
                                     $content21 .= $valuePart2;
                                 } else {
                                     $content22 .= $valuePart2;
                                 }
                             }
                             $content2 .= $content21 . $content22;
                             $bydaycnt++;
                         } else {
                             $content2 .= $bydaycnt ? ',' : null;
                             if ('DAY' != strtoupper($vix)) {
                                 $content21 .= $valuePart;
                             } else {
                                 $content22 .= $valuePart;
                                 $bydaycnt++;
                             }
                             $content2 .= $content21 . $content22;
                         }
                     }
                     break;
                 default:
                     $content2 .= ";{$rulelabel}={$rulevalue}";
                     break;
             }
         }
         $output .= $this->_createElement($recurlabel, $attributes, $content1 . $content2);
     }
     return $output;
 }