Skip to content

Instantly share code, notes, and snippets.

@franz-isx
Created April 16, 2012 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franz-isx/2398571 to your computer and use it in GitHub Desktop.
Save franz-isx/2398571 to your computer and use it in GitHub Desktop.
iCal Holiday import for TeamCal
Index: holidays.php
===================================================================
--- holidays.php (Revision 88)
+++ holidays.php (Arbeitskopie)
@@ -180,6 +180,71 @@
*/
$LOG->log("logHoliday",$L->checkLogin(),"Holiday deleted: ".$delname);
sendNotification("holidaydelete",$delname,"");
+} else if ( isset($_POST['btn_hol_ical_import']) ) {
+/**
+ * Import iCal
+ */
+ /**
+ * Parse iCal
+ */
+ $begin_of_ical = 999999999999999999999999999999;
+ $end_of_ical = 0;
+ $holidays = array();
+ # get all events of the ical file
+ preg_match_all("#(?sU)BEGIN:VEVENT.*END:VEVENT#", file_get_contents($_FILES['ical_file']['tmp_name']), $events);
+ foreach($events[0] as $event){
+ preg_match("#(?sU)DTSTART;.*DATE:([0-9]{8})#", $event, $start);
+ preg_match("#(?sU)DTEND;.*DATE:([0-9]{8})#", $event, $end);
+ $start = mktime (0,0,0, substr($start[1],4,2), substr($start[1],6,2), substr($start[1],0,4));
+ $end = mktime (0,0,0, substr($end[1],4,2), substr($end[1],6,2), substr($end[1],0,4));
+ # update begin and end of ical file (later used for purging)
+ if ($begin_of_ical > $start) $begin_of_ical = $start;
+ if ($end_of_ical < $end) $end_of_ical = $end;
+ # stort begin and end of this holiday period
+ $holidays[$start] = $end;
+ };
+ /**
+ * Purge database object and add new holiday periods
+ */
+ $H->findBySymbol($_POST['hol_symbolhidden']);
+
+ $current = $begin_of_ical;
+ while ($current < $end_of_ical){
+ $current_yearmonth = date("Ym", $current);
+ if ($M->yearmonth != $current_yearmonth){
+ $M->update("default", $M->yearmonth); // write changes to database
+ $M->findByName('default', $current_yearmonth); // load new month object
+ }
+ // purge old status of day
+ $dayno = date("j", $current);
+ if (substr($M->template, $dayno-1, 1) == $H->cfgsym){
+ if (date("w", $current) == 0 || date("w", $current) == 6){
+ $purge_sign = "1";
+ } else {
+ $purge_sign = "0";
+ }
+ $M->template = substr($M->template, 0, $dayno-1).$purge_sign.substr($M->template, $dayno);
+ }
+ // enter new state
+ $start_of_first_holiday_period = min(array_keys($holidays));
+ if ($start_of_first_holiday_period <= $current){
+ if ($holidays[$start_of_first_holiday_period] > $current){
+ // we are currently between begin and end of a holiday period
+ if (substr($M->template, $dayno-1, 1) != 1){
+ // don't change weekends
+ $M->template = substr($M->template, 0, $dayno-1).$H->cfgsym.substr($M->template, $dayno);
+ }
+ } else {
+ // we are behind the begin and behind the end of a holiday period!
+ // so the whole period is in the past
+ unset($holidays[$start_of_first_holiday_period]);
+ // but now we have to make sure the current day is also processesd
+ continue;
+ }
+ }
+ $current = strtotime("+1 day", $current);
+ }
+ $M->update("default", $M->yearmonth); // save last month
}
@@ -261,7 +326,7 @@
<!-- ".$H->dspname." -->
<tr>
<td>
- <form class=\"form\" name=\"form-hol-".$i."\" method=\"POST\" action=\"".$_SERVER['PHP_SELF']."?lang=".$CONF['options']['lang']."\">
+ <form class=\"form\" name=\"form-hol-".$i."\" method=\"POST\" action=\"".$_SERVER['PHP_SELF']."?lang=".$CONF['options']['lang']."\" enctype=\"multipart/form-data\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td class=\"dlg-row".$printrow."\" width=\"5%\">
@@ -273,13 +338,15 @@
<input name=\"hol_symbolhidden\" type=\"hidden\" class=\"text\" value=\"".$H->cfgsym."\">
<input name=\"hol_dspname\" size=\"28\" type=\"text\" class=\"text\" value=\"".$H->dspname."\">
</td>
- <td class=\"dlg-row".$printrow."\" width=\"15%\"><input name=\"hol_color\" size=\"6\" maxlength=\"6\" type=\"text\" class=\"text\" style=\"text-align: center;\" value=\"".$H->dspcolor."\"><a href=\"javascript:TCP.popup(document.forms['form-hol-".$i."'].elements['hol_color'])\"><img align=\"top\" border=\"0\" alt=\"".$LANG['color_picker']."\" src=\"themes/".$theme."/img/colorpicker.png\" style=\"padding-left: 2px;\" onmouseover=\"return overlib('".$LANG['color_picker_tt']."',".$CONF['ovl_tt_settings'].");\" onmouseout=\"return nd();\"></a></td>
- <td class=\"dlg-row".$printrow."\" width=\"15%\"><input name=\"hol_bgcolor\" size=\"6\" maxlength=\"6\" type=\"text\" class=\"text\" style=\"text-align: center;\" value=\"".$H->dspbgcolor."\"><a href=\"javascript:TCP.popup(document.forms['form-hol-".$i."'].elements['hol_bgcolor'])\"><img align=\"top\" border=\"0\" alt=\"".$LANG['color_picker']."\" src=\"themes/".$theme."/img/colorpicker.png\" style=\"padding-left: 2px;\" onmouseover=\"return overlib('".$LANG['color_picker_tt']."',".$CONF['ovl_tt_settings'].");\" onmouseout=\"return nd();\"></a></td>
+ <td class=\"dlg-row".$printrow."\" width=\"10%\"><input name=\"hol_color\" size=\"6\" maxlength=\"6\" type=\"text\" class=\"text\" style=\"text-align: center;\" value=\"".$H->dspcolor."\"><a href=\"javascript:TCP.popup(document.forms['form-hol-".$i."'].elements['hol_color'])\"><img align=\"top\" border=\"0\" alt=\"".$LANG['color_picker']."\" src=\"themes/".$theme."/img/colorpicker.png\" style=\"padding-left: 2px;\" onmouseover=\"return overlib('".$LANG['color_picker_tt']."',".$CONF['ovl_tt_settings'].");\" onmouseout=\"return nd();\"></a></td>
+ <td class=\"dlg-row".$printrow."\" width=\"10%\"><input name=\"hol_bgcolor\" size=\"6\" maxlength=\"6\" type=\"text\" class=\"text\" style=\"text-align: center;\" value=\"".$H->dspbgcolor."\"><a href=\"javascript:TCP.popup(document.forms['form-hol-".$i."'].elements['hol_bgcolor'])\"><img align=\"top\" border=\"0\" alt=\"".$LANG['color_picker']."\" src=\"themes/".$theme."/img/colorpicker.png\" style=\"padding-left: 2px;\" onmouseover=\"return overlib('".$LANG['color_picker_tt']."',".$CONF['ovl_tt_settings'].");\" onmouseout=\"return nd();\"></a></td>
<td class=\"dlg-row".$printrow."\" width=\"10%\"><input name=\"chkBusinessDay\" type=\"checkbox\" value=\"chkBusinessDay\" ".($H->checkOptions($CONF['H_BUSINESSDAY'])?'CHECKED':'')."></td>
- <td class=\"dlg-row".$printrow."\" width=\"25%\">
+ <td class=\"dlg-row".$printrow."\" width=\"35%\">
<input name=\"btn_hol_update\" type=\"submit\" class=\"button\" value=\"".$LANG['btn_update']."\" onmouseover=\"this.className='button-over';\" onmouseout=\"this.className='button';\">&nbsp;";
if ( $H->cfgsym!='0' && $H->cfgsym!='1' ) { // Business Day and Weekend Day cannot be deleted
- echo "<input name=\"btn_hol_delete\" type=\"submit\" class=\"button\" value=\"".$LANG['btn_delete']."\" onmouseover=\"this.className='button-over';\" onmouseout=\"this.className='button';\" onclick=\"return confirmSubmit('".$LANG['ed_delete_confirm']."')\">";
+ echo "<input name=\"btn_hol_delete\" type=\"submit\" class=\"button\" value=\"".$LANG['btn_delete']."\" onmouseover=\"this.className='button-over';\" onmouseout=\"this.className='button';\" onclick=\"return confirmSubmit('".$LANG['ed_delete_confirm']."')\">
+ <input type=\"file\" accept=\"text/calendar\" name=\"ical_file\">
+ <input type=\"submit\" class=\"button\" value=\"iCal importieren\" name=\"btn_hol_ical_import\"\>";
}
echo "
</td>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment