From 1f4fd52d8412552493ce27443d3ea63623c1a71a Mon Sep 17 00:00:00 2001
From: Juanjo Menent <jmenent@2byte.es>
Date: Tue, 31 May 2011 16:50:54 +0000
Subject: [PATCH] task #9747: Can show content of external calendar (ical) into
 agenda

---
 htdocs/comm/action/class/ical.class.php | 12 ++---
 htdocs/comm/action/index.php            | 72 +++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/htdocs/comm/action/class/ical.class.php b/htdocs/comm/action/class/ical.class.php
index a912404cee0..b374bd9d6f3 100644
--- a/htdocs/comm/action/class/ical.class.php
+++ b/htdocs/comm/action/class/ical.class.php
@@ -81,7 +81,7 @@ class ical
         // read FILE text
         $this->file_text = $this->read_file($uri);
 
-        $this->file_text = split("[\n]", $this->file_text);
+        $this->file_text = preg_split("[\n]", $this->file_text);
         
         // is this text vcalendar standart text ? on line 1 is BEGIN:VCALENDAR
         if (!stristr($this->file_text[0],'BEGIN:VCALENDAR')) return 'error not VCALENDAR';
@@ -223,7 +223,7 @@ class ical
         $ical_date = str_replace('Z', '', $ical_date);
 
         // TIME LIMITED EVENT
-        ereg('([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})([0-9]{0,2})', $ical_date, $date);
+        preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})([0-9]{0,2})/', $ical_date, $date);
 
         // UNIX timestamps can't deal with pre 1970 dates
         if ($date[1] <= 1970)
@@ -244,15 +244,15 @@ class ical
     {
         $value = $this->ical_date_to_unix($value);
 
-        // zjisteni TZID
+        // Analyse TZID
         $temp = explode(";",$key);
         
-        if (empty($temp[1])) // neni TZID
+        if (empty($temp[1])) // not TZID
         {
-            $data = str_replace('T', '', $data);
+            $value = str_replace('T', '', $value);
             return array($key,$value);
         }
-        // pridani $value a $tzid do pole
+        // adding $value and $tzid 
         $key =     $temp[0];
         $temp = explode("=", $temp[1]);
         $return_value[$temp[0]] = $temp[1];
diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php
index b79873f9f23..927baac1ae8 100644
--- a/htdocs/comm/action/index.php
+++ b/htdocs/comm/action/index.php
@@ -484,6 +484,78 @@ if ($showbirthday)
 	}
 }
 
+//Exernal Calendars
+if ($conf->global->MAIN_FEATURES_LEVEL>=2)
+if ($conf->global->ENABLE_AGENDA_EXT==1 && $conf->global->AGENDA_EXT_NB>0)
+{
+	require_once(DOL_DOCUMENT_ROOT."/comm/action/class/ical.class.php");
+	$numcals= $conf->global->AGENDA_EXT_NB;
+	$i=1;
+	while ($i<=$numcals)
+	{
+		$paramkey='AGENDA_EXT_SRC'.$i;
+		$url=$conf->global->$paramkey;
+		$ical=new ical();
+		$ical->parse($url);
+		$icalevents= $ical->get_event_list();
+		if(count($icalevents)>0)
+		{
+			foreach($icalevents as $icalevent)
+			{
+				// Create a new object action
+				$event=new ActionComm($db);
+				$event->id=$icalevent->UID;
+				$event->datep=$icalevent->DTSTART;
+				$event->datef=$icalevent->DTEND;
+				$event->type_code="ICALEVENT";
+				$event->libelle=$icalevent->SUMMARY;
+	        	//$event->fulldayevent=$obj->fulldayevent;
+	        	
+				$event->date_start_in_calendar=$event->datep;
+				/*
+				if ($event->datef != '' && $event->datef >= $event->datep) $event->date_end_in_calendar=$event->datef;
+				else $event->date_end_in_calendar=$event->datep;
+			
+				// Define ponctual property
+				if ($event->date_start_in_calendar == $event->date_end_in_calendar)
+				{
+					$event->ponctuel=1;
+				}
+	
+				// Check values
+				if ($event->date_end_in_calendar < $firstdaytoshow || $event->date_start_in_calendar > $lastdaytoshow)
+				{
+					// This record is out of visible range
+				}
+				else
+				{
+					if ($event->date_start_in_calendar < $firstdaytoshow) $event->date_start_in_calendar=$firstdaytoshow;
+					if ($event->date_end_in_calendar > $lastdaytoshow) $event->date_end_in_calendar=$lastdaytoshow;
+	*/
+					// Add an entry in actionarray for each day
+					$daycursor=$event->date_start_in_calendar;
+					$annee = date('Y',$daycursor);
+					$mois = date('m',$daycursor);
+					$jour = date('d',$daycursor);
+	
+					// Loop on each day covered by action to prepare an index to show on calendar
+					$loop=true; $j=0;
+					$daykey=dol_mktime(0,0,0,$mois,$jour,$annee);
+					do
+					{
+						$eventarray[$daykey][]=$event;
+						$daykey+=60*60*24;
+						if ($daykey > $event->date_end_in_calendar) $loop=false;
+					}
+					while ($loop);
+				//}
+			}
+			       	
+		}
+		$i++;
+	}
+}
+
 $maxlength=16;
 $cachethirdparties=array();
 $cachecontacts=array();
-- 
GitLab