I am creating an ics file parser on a site I'm working on and no matter what I try to write for checking whether or not the $icsEvent['LOCATION']
index is empty it keeps acting as if it has a value. I am trying to take an address that is entered into an ics event and break it up into location name, location street, location city, location state and location zip. The first event in the ics file has an address where as the second one does not. When it gets to the second event it is still acting as if the second event isn't empty and is causing it to throw an error for an undefined offset.
$file = "https://calendar.google.com/calendar/ical/nhfb76hhlt4ts86jmk4n9bsghk%40group.calendar.google.com/public/basic.ics";
/* Getting events from isc file */
$obj = new ics();
$icsEvents = $obj->getIcsEventsAsArray($file);
/* Here we are getting the timezone to get the event dates according to geo location */
$timeZone = trim($icsEvents[1]['X-WR-TIMEZONE']);
unset($icsEvents[1]);
unset($icsEvents[2]);
unset($icsEvents[3]);
unset($icsEvents[4]);
unset($icsEvents[5]);
unset($icsEvents[6]);
foreach($icsEvents as $icsEvent){
$eventKey = removeEmail($icsEvent['UID']);
/* Getting start date and time */
$start = isset($icsEvent['DTSTART;VALUE=DATE']) ? $icsEvent['DTSTART;VALUE=DATE'] : $icsEvent['DTSTART'];
/* Converting to datetime and apply the timezone to get proper date time */
$startDt = new DateTime($start);
$startDt->setTimezone(new DateTimeZone($timeZone));
$startDate = $startDt->format('Ymd H:i:s');
$eventDate = $startDt->format('m.d.Y @ g:iA');
$startSrch = $startDt->format('Ymd');
/* Getting end date with time */
$end = isset($icsEvent['DTEND;VALUE=DATE']) ? $icsEvent['DTEND;VALUE=DATE'] : $icsEvent['DTEND'];
$endDt = new DateTime($end);
$endDate = $endDt->format('Ymd H:i:s');
/* Determining if the event is more than one day */
if($endDt->format('Ymd') != $startDt->format('Ymd')){
$eventDays = 1;
}else{
$eventDays = 0;
}
/* Getting the name of event */
$eventName = $icsEvent['SUMMARY'];
/* Getting the description of event */
$eventDesc = $icsEvent['DESCRIPTION'];
/* Getting the location of event */
$eventLoc = $icsEvent['LOCATION'];
if($eventLoc){
$parts = explode("\, ",$eventLoc);
$locName = $parts[0];
$locStreet = $parts[1];
$locCity = $parts[2];
$locStateZ = $parts[3];
$parts = explode(" ",$locStateZ);
$locState = $parts[0];
$navState = convertState($locState);
$locZIP = $parts[1];
}else{
$locName = "Any Location Name";
$locStreet = "5200 Lake Gold Parkway";
$locCity = "Anytown";
$locState = "MN";
$navState = "Minnesota";
$locZIP = "80134";
}
echo $locName;
echo $locStreet;
echo $locCity;
echo $locState;
echo $locZIP;
echo $navState;
$find = " ";
$replace = "+";
$navStreet = str_replace($find,$replace,$locStreet);
echo $navStreet;
/* Getting the image of event */
if(array_key_exists("ATTACH",$icsEvent)){
$eventImg = imageCorrection($icsEvent['ATTACH']);
}else{
$eventImg = "/components/img/event-default";
}
/* Creating the url of event */
$eventURL = seoURL($eventName);
/* Creating the id of event */
$startSwtch = date('B',strtotime($startDate));
$eventID = $eventKey."{$startSwtch}";
$query = "SELECT * FROM calendar WHERE event_key='{$eventKey}' AND start_date LIKE '{$startSrch}%'";
$update = "UPDATE calendar SET event='".$connection->real_escape_string($eventName)."',url='".$connection->real_escape_string($eventURL)."',image='".$connection->real_escape_string($eventImg)."',description='".$connection->real_escape_string($eventDesc)."',locationName='".$connection->real_escape_string($locName)."',locationStreet='".$connection->real_escape_string($locStreet)."',locationCity='".$connection->real_escape_string($locCity)."',locationState='".$connection->real_escape_string($locState)."',locationZip='".$connection->real_escape_string($locZIP)."',start_date='{$startDate}',end_date='{$endDate}',multiDay='{$eventDays}',church='1' WHERE (event_key='{$eventKey}' AND start_date LIKE '{$start}%')";
$insert = "INSERT INTO calendar VALUES ('{$eventKey}','".$connection->real_escape_string($eventName)."','".$connection->real_escape_string($eventURL)."','".$connection->real_escape_string($eventImg)."','".$connection->real_escape_string($eventDesc)."','".$connection->real_escape_string($locName)."','".$connection->real_escape_string($locStreet)."','".$connection->real_escape_string($locCity)."','".$connection->real_escape_string($locState)."','".$connection->real_escape_string($locZIP)."','','','{$startDate}','{$endDate}','{$eventDays}','1','0','0','0','0','0','0','0','0','0','0'";
if($queryDB = $connection->query($query)){
$row_cnt = $queryDB->num_rows;
if($row_cnt != 0){
$updateDB = $connection->query($update);
}else{
$insertDB = $connection->query($insert);
}
}
Notice: Undefined offset: 1 in /home/user/mysite.com/components/data/icsparse.php on line 192
Notice: Undefined offset: 2 in /home/user/mysite.com/components/data/icsparse.php on line 193
Notice: Undefined offset: 3 in /home/user/mysite.com/components/data/icsparse.php on line 194
Notice: Undefined offset: 1 in /home/user/mysite.com/components/data/icsparse.php on line 198
ics calendar file
Here are the first few entries of the ics
file:
BEGIN:VEVENT
DTSTART:20191123T013000Z
DTEND:20191123T023000Z
DTSTAMP:20191122T205609Z
UID:7mg6p2bj83hadlununj7d9ov40@google.com
CREATED:20191122T064508Z
DESCRIPTION:
LAST-MODIFIED:20191122T064508Z
LOCATION:Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 508\, USA
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Basketball vs Birchwood Christian
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20190624
DTEND;VALUE=DATE:20190629
RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1
DTSTAMP:20191122T205609Z
UID:002fe82nhfs3qhfn8i84qe2dm5@google.com
CREATED:20181218T234159Z
DESCRIPTION:
LAST-MODIFIED:20191122T060518Z
LOCATION:
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:Soccer Camp (PreK-6th)
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20190617
DTEND;VALUE=DATE:20190622
RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1
DTSTAMP:20191122T205609Z
UID:4e27isqv5puk2ctc2r2pa6vuc9@google.com
CREATED:20181218T234147Z
DESCRIPTION:
LAST-MODIFIED:20191122T060517Z
LOCATION:
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:STEM Camp (K-8th)
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20191124T010000Z
DTEND:20191124T040000Z
DTSTAMP:20191122T205609Z
UID:4gi5mcte11iefak3rhba9l05sk@google.com
CREATED:20191119T232703Z
DESCRIPTION:
LAST-MODIFIED:20191119T232703Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Parents' Night Out
TRANSP:OPAQUE
END:VEVENT
Here is what var_export($icsEvent)
is returning for the first few entries:
array ( 'BEGIN' => 'VEVENT ', 'DTSTART' => '20191123T013000Z ', 'DTEND' => '20191123T023000Z ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '7mg6p2bj83hadlununj7d9ov40@google.com ', 'CREATED' => '20191122T064508Z ', 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T064508Z ', 'LOCATION' => 'Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 ', 'SEQUENCE' => '0 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'Basketball vs Birchwood Christian ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )Alaska Pacific University4101 University DrAnchorageAK99 AK4101+University+Drarray ( 'BEGIN' => 'VEVENT ', 'DTSTART;VALUE=DATE' => '20190624 ', 'DTEND;VALUE=DATE' => '20190629 ', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '002fe82nhfs3qhfn8i84qe2dm5@google.com ', 'CREATED' => '20181218T234159Z ', 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T060518Z ', 'LOCATION' => ' ', 'SEQUENCE' => '1 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'Soccer Camp (PreK-6th) ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )
: Undefined offset: 1 in ... on line 193
: Undefined offset: 2 in ... on line 194
: Undefined offset: 3 in ... on line 195
: Undefined offset: 1 in ... on line 199
array ( 'BEGIN' => 'VEVENT ', 'DTSTART;VALUE=DATE' => '20190617 ', 'DTEND;VALUE=DATE' => '20190622 ', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '4e27isqv5puk2ctc2r2pa6vuc9@google.com ', 'CREATED' => '20181218T234147Z ', 'DESCRIPTION' => ' ',
'LAST-MODIFIED' => '20191122T060517Z ', 'LOCATION' => ' ',
'SEQUENCE' => '1 ', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => 'STEM Camp (K-8th) ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )
Why is it doing this?