1<?php
2# =================================================================================================
3# gCalendar main-part
4# =================================================================================================
5
6/**
7 * main-part of the gcalendar-plugin
8 *
9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author  Frank Hinkel <frank@hi-sys.de>
11 *
12 * the options-array contains all parameters passed to the gcal statement
13 */
14
15include_once('gcal_read.php'); # responsible for reading the data into the gCal_data-array
16include_once('gcal_show.php'); # responsible for displaying the calendar-data
17
18
19/**
20 * Main render-function of the plugin.
21 * This function is called from the function "render" in "syntax.php"
22 *
23 * @author Frank Hinkel <frank.hinkel@hi-sys.de>
24 *
25 * @param  array $options contains a list of parameters passed to the plugin at the wiki-page
26 * @param  object $renderer
27 */
28function render_gcal(&$options) {
29  global $ID;
30  global $conf;
31
32  # generate style.css and print.css files
33  copy_css();
34
35  # set month as default mode
36  if(!isset($options['mode'])) $options['mode']='month';
37
38  # url-pars override pars on wiki-page
39  if(isset($_REQUEST['mode']))   $options['mode']   = $_REQUEST['mode'];
40  if(isset($_REQUEST['offset'])) $options['offset'] = $_REQUEST['offset'];
41  if(isset($_REQUEST['pages']))  $options['pages']  = $_REQUEST['pages'];
42
43  # if no page is given in the options use actual page
44  if(!isset($options['pages'])) $options['pages'] = '('.$ID.')';
45
46  # fetch parameter "pages" and explode the list "(page1,page2,...)" to an array
47
48  # get options "pages" and remove the parens
49  $pages = $options['pages'];
50  $pages = substr($pages,1,strlen($pages)-2);
51
52
53  # expand some macros
54  $user = $_SESSION[$conf['title']]['auth']['user'];
55  if($user=="") $user="guest";
56  $pages = str_replace('@USER@',$user,$pages);
57  $pages = str_replace('@ID@',$ID,$pages);
58
59  # explode the comma-seperated list to an array
60  $pages = explode(',',$pages);
61
62  # grab date from actual timestamp
63  $today = getdate();
64  $day   = $today["mday"];
65  $year  = $today["year"];
66  $month = $today["mon"];
67
68  # grab date from options (gCal-command in wiki-page)
69  if(isset($options["month"])) $month = $options["month"] + $options["offset"] + $_SESSION["offset"];
70  if(isset($options["year"]))  $year  = $options["year"];
71  if(isset($options["day"]))   $day   = $options["day"];
72
73  # grab date from request (url)
74  if(isset($_REQUEST["day"]))   $day   = $_REQUEST["day"];
75  if(isset($_REQUEST["month"])) $month = $_REQUEST["month"];
76  if(isset($_REQUEST["year"]))  $year  = $_REQUEST["year"];
77
78  # calculate given offset according to selected mode
79  if(isset($options["offset"])) {
80    switch($options["mode"]) {
81      case "day"   : $day   += $options["offset"];     break;
82      case "week"  : $day   += $options["offset"] * 7; break;
83      case "month" : $month += $options["offset"]    ; break;
84    }
85  }
86
87  # finally this is our reference-date for further calculations
88  $reference_date = mktime(0, 0, 0, $month, $day, $year);
89
90  # for debug only. add-dayshift if given
91  if(isset($options['dayshift'])) $reference_date += 60*60*24*$options['dayshift'];
92
93  # calculate the date-range to display according to selected mode
94  switch(strtolower($options["mode"])) {
95    case "day"   : $start_date = $reference_date;
96                   $end_date   = $reference_date;
97                   if(isset($options["days"])) {
98                     $end_date = strtotime ( "+".($options["days"]-1)." days", $end_date);
99                   }
100                   break;
101
102    case "week"  : $shift = date('w',$reference_date)-1;
103                   if($shift==-1) $shift=6;
104                   $start_date = strtotime( "-".$shift."days", $reference_date );
105                   if(isset($options["days"])) {
106                     $end_date = strtotime( "+".($options["days"]-1)." days", $start_date);
107                   } else {
108                     $end_date   = strtotime( "+1 week -1 day", $start_date );
109                   }
110                   break;
111
112    case "month" :;# month-view is the default case
113    default      : $start_date = mktime(0, 0, 0, $month, 1, $year);
114                   $end_date   = strtotime( "+1 month -1 day", $start_date );
115  }
116
117  # read every given wiki-page (pages-parameter) into the calendar-array
118  read_pages_into_calendar($options,$pages,$reference_date);
119
120  # start output --------------------------------------------------------------
121  show_gCal_page($options,$pages,$start_date,$end_date);
122}
123
124
125/**
126 * copy the $in_files over to style.css and print.css if
127 * at least one of the $in_files is newer than "style.css"
128 *
129 * @author Frank Hinkel <frank@hi-sys.de>
130 */
131function copy_css() {
132  $in_files=array(
133      "inc/standard.css",
134      "gCal_cell_cat_" => "user/background.css",
135      "gCal_cat_"      => "user/events.css",
136      "user/other.css"
137  );
138
139  $infile_time = 0;
140  foreach($in_files as $file) {
141      $t = filemtime(DOKU_GCAL.$file);
142      if($t>$infile_time) $infile_time = $t;
143  }
144  if(!file_exists(DOKU_GCAL."style.css") ||
145     (filemtime(DOKU_GCAL."style.css") < $infile_time)) {
146        append($in_files,"style.css");
147        $result = @copy(DOKU_GCAL."style.css",DOKU_GCAL."print.css");
148  }
149}
150
151
152function append($source_arr, $destfile) {
153    $out_handle = fopen (DOKU_GCAL.$destfile, "w");
154    if(!$out_handle) return;
155
156    fwrite($out_handle,"/*\n");
157    fwrite($out_handle," * WARNING:\n");
158    fwrite($out_handle," * This file was automatically created by the gCalendar-plugin.\n");
159    fwrite($out_handle," * Do NOT edit this file, because your changes will be overwritten !\n");
160    fwrite($out_handle," */\n");
161
162    foreach($source_arr as $class_prefix => $infile) {
163        if(file_exists(DOKU_GCAL.$infile)) {
164            $in_handle = fopen(DOKU_GCAL.$infile,"r");
165            if(!$in_handle) continue;
166
167            $text = " copied from file '$infile' ";
168            $text = str_pad ($text, 93, "#", STR_PAD_BOTH);
169            fwrite($out_handle,"\n/* $text */\n\n");
170
171            while (!feof($in_handle)) {
172              $line = fgets($in_handle, 4096);
173
174              if(is_string($class_prefix) && strlen($class_prefix)>0) {
175                # did I allready mentioned that I hate the complexity of reg-exps ?
176                # but I love its power ! Btw I dont think thats good coding-style.
177                $line = preg_replace('#((\,|^)\s*\.)(\w*)(\W)#Ue', "'\\1'.$class_prefix.strtoupper('\\3').'\\4'", $line);
178              }
179              fwrite($out_handle,$line);
180            }
181
182            fclose($in_handle);
183        }
184    }
185    fclose($out_handle);
186
187}
188