<?php
  //CMS - CMS Made Simple
  //(c)2004-2007 by Ted Kulp (ted@cmsmadesimple.org)
  //This project's homepage is: http://cmsmadesimple.org
  //
  //This program is free software; you can redistribute it and/or modify
  //it under the terms of the GNU General Public License as published by
  //the Free Software Foundation; either version 2 of the License, or
  //(at your option) any later version.
  //
  //This program is distributed in the hope that it will be useful,
  //but WITHOUT ANY WARRANTY; without even the implied warranty of
  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  //GNU General Public License for more details.
  //You should have received a copy of the GNU General Public License
  //along with this program; if not, write to the Free Software
  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  // 
  function smarty_cms_function_ics($params, &$smarty)
  {
  	$gCms = cmsms();
	
	
	
	$path = isset($params['path']) ? $params['path'] : 'ics';
	$file = isset($params['filename']) ? munge_string_to_url($params['filename']) : munge_string_to_url($params['subject']);
	$file .= '_' . md5(serialize($params));
	$subject = strip_tags($params['subject']);
	$summary = strip_tags($params['summary']);
    $detail = strip_tags($params['detail']);
	$where = strip_tags($params['where']);
	$target = isset($params['target']) ? $params['target'] : '_blank';
	$class = isset($params['class']) ? $params['class'] : 'cal';
	$url = isset($params['url']) ? $params['url'] : $gCms->config['root_url'];
	$linktext = isset($params['linktext']) ? $params['linktext'] : 'Add to Calendar';
	
  	$filename = $gCms->config['uploads_path'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file . ".ics";
	$start = $params['start'];
	$end = $params['end'] ? $params['end'] : $start;

      // create file
      if (!file_exists($filename)) {
          
          if (!file_exists($gCms->config['uploads_path'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR)) {
              @mkdir($gCms->config['uploads_path'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR , 0755);
          }
          $set_filename = $filename;
          $file_handle = fopen($set_filename, 'w') or die("can't open file");
          fclose($file_handle);
		  // enter information in ics file
          $event = "BEGIN:VCALENDAR
PRODID:" . $url . "
VERSION:2.0
BEGIN:VEVENT
BEGIN:VTIMEZONE
TZID:" . date('e') . "
LAST-MODIFIED:19870101T000000Z
X-LIC-LOCATION:" . date('e') . "
END:VTIMEZONE
URL:" . $url . "
DTSTART:" . gmstrftime('%Y%m%dT%H%M%SZ', strtotime($start)) . "
DTEND:" . gmstrftime('%Y%m%dT%H%M%SZ', strtotime($end)) . "
DTSTAMP:" . gmstrftime('%Y%m%dT%H%M%SZ') ."
LOCATION:" . $where . "
SUMMARY:" . $subject . "
SEQUENCE:0
DESCRIPTION:" . $summary  . ' ' . $detail . "
TRANSP:OPAQUE
UID:1
END:VEVENT
END:VCALENDAR
				   ";
          
          $len = strlen($event);
          $chk = file_put_contents($filename, $event);
          
          if ($len != $chk)
              die("LENGTH MISMATCH");
      } // create link
      $get_file = $gCms->config['uploads_url'] . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $file . ".ics";
	  	if(isset($params['assign'])) {
		$smarty -> assign(trim($params['assign']), $get_file);
		return ;
	 }
        return '<a href="' . $get_file . '" title="' . $linktext .': ' . $subject .'" class="' . $class . '" target="' . $target .'">' . $linktext . '</a>';
  } 
  
  function smarty_cms_help_function_ics()
  {
?>
<h3>What does this do?</h3>
<p>
  This plugin creates a simple .ics file for importing into outlook or other compatible softeware.
</p>
<h3>How do I use it?</h3>
<p>
  Just insert the tag into your template/page like:
  <code>
    {ics subject="Some event" summary="some short description about event" where="address of the event" linktext="Add this event"}
  </code>
</p>
<h3>What parameters does it take?</h3>
<p></p>
<ul>
  <li>
    <em><strong>(required)</strong> </em>
    <tt>subject </tt> - Subject is used as title of the event, also if <code>filename</code> isn't specified, <code>subject</code> will be used to generate the name of the file.
  </li>	
  <li>
    <em><strong>(required)</strong> </em>
    <tt>start </tt> - There is no event without date. You should enter a date and time using parameter <code>start</code>.
  </li>
  <li>
    <em>(optional) </em>
    <tt>end </tt> - If you want to use a different end date then use parameter <code>end</code>, if parameter is empty then a value of parameter <code>start</code> will be used.
  </li>  	  
  <li>
    <em>(optional) </em>
    <tt>path </tt> - Path relative to uploads folder, where you want your files save. If none specified a folder named ics will be created in uploads folder.
  </li>
  <li>
    <em>(optional) </em>
    <tt>filename </tt> - Subject 
  </li>
  <li>
    <em>(optional) </em>
    <tt>class </tt> - name of the class atribute to be used for generated link. Default is "cal"
  </li>
  <li>
    <em>(optional) </em>
    <tt>target </tt> -  target atribute for generated link. Default is "_blank".
  </li>
  <li>
    <em>(optional) </em>
    <tt>linktext </tt> - Text for generated link. Default is "Add to Calendar"
  </li>
  <li>
    <em>(optional) </em>
    <tt>url </tt> - URL address to be used as URL atribute of iCalendar. Default is "root_url".
  </li>
  <li>
    <em>(optional) </em>
    <tt>summary </tt> - Summary of the event.
  </li> 
  <li>
    <em>(optional) </em>
    <tt>detail </tt> - Detail text of the event.
  </li>   
  <li>
    <em>(optional) </em>
    <tt>where </tt> - Where does the event take place. Used for LOCATION atribute of iCalendar.
  </li>   
  <li>
    <em>(optional) </em>
    <tt>assign </tt> - This will assign {ics} tag to a variable and output only filepath, like <em>http://www.domain.com/some/folder/filename.ics</em>.
  </li>   
</ul>
<h3>Credits</h3>
<p>The idea about iCalendar isn't new, so i want to thank to Jeremy Bass (<a href="http://forum.cmsmadesimple.org/memberlist.php?mode=viewprofile&u=10367">JeremyBASS</a>) and iCal module, where the idea about plugin was born.<br />
I also wan't to thank to Jonathan Schimd (<a href="http://forum.cmsmadesimple.org/memberlist.php?mode=viewprofile&u=19004">Foaly*</a>) for helping me not being dumb ;-).</p>
<?php
} 
function smarty_cms_about_function_ics()
{
?>
<p>
  Author: Goran Ilic <a href="http://www.ich-mach-das.at" target="_blank">www.ich-mach-das.at</a>
</p>
<p>
  Version: 1.0
</p>
<?php
} 
?>
