In month view, sort events on a day by starttime so that an event that
[squirrelmail.git] / plugins / calendar / calendar_data.php
1 <?php
2
3 /**
4 * calendar_data.php
5 *
6 * Copyright (c) 2002-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Originally contrubuted by Michal Szczotka <michal@tuxy.org>
10 *
11 * functions to operate on calendar data files.
12 *
13 * $Id$
14 */
15
16 // this is array that contains all events
17 // it is three dimensional array with fallowing structure
18 // $calendardata[date][time] = array(length,priority,title,message);
19 $calendardata = array();
20
21 //read events into array
22 //data is | delimited, just like addresbook
23 //files are structured like this:
24 //date|time|length|priority|title|message);
25 //files are divide by year for performance increase
26 function readcalendardata() {
27 global $calendardata, $username, $data_dir, $year;
28
29 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
30
31 if (file_exists($filename)){
32 $fp = fopen ($filename,'r');
33
34 if ($fp){
35 while ($fdata = fgetcsv ($fp, 4096, '|')) {
36 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
37 'priority' => $fdata[3],
38 'title' => htmlspecialchars($fdata[4],ENT_NOQUOTES),
39 'message' => htmlspecialchars($fdata[5],ENT_NOQUOTES),
40 'reminder' => $fdata[6] );
41 }
42 fclose ($fp);
43 // this is to sort the events within a day on starttime
44 foreach($calendardata as $day => $data) {
45 ksort($data, SORT_NUMERIC);
46 $calendardata[$day] = $data;
47 }
48 }
49 }
50 }
51
52 //makes events persistant
53 function writecalendardata() {
54 global $calendardata, $username, $data_dir, $year;
55
56 $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp");
57 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
58 $fp = fopen ($filetmp,"w");
59 if ($fp) {
60 while ( $calfoo = each ($calendardata)) {
61 while ( $calbar = each ($calfoo['value'])) {
62 $calfoobar = $calendardata[$calfoo['key']][$calbar['key']];
63 $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n";
64 fwrite($fp, $calstr, 4096);
65 }
66
67 }
68 fclose ($fp);
69 @unlink($filename);
70 rename($filetmp,$filename);
71 }
72 }
73
74 //deletes event from file
75 function delete_event($date, $time) {
76 global $calendardata, $username, $data_dir, $year;
77
78 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
79 $fp = fopen ($filename,'r');
80 if ($fp){
81 while ($fdata = fgetcsv ($fp, 4096, "|")) {
82 if (($fdata[0]==$date) && ($fdata[1]==$time)){
83 // do nothing
84 } else {
85 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
86 'priority' => $fdata[3],
87 'title' => $fdata[4],
88 'message' => $fdata[5],
89 'reminder' => $fdata[6] );
90 }
91 }
92 fclose ($fp);
93 }
94 writecalendardata();
95
96 }
97
98 // same as delete but not saves calendar
99 // saving is done inside event_edit.php
100 function update_event($date, $time) {
101 global $calendardata, $username, $data_dir, $year;
102
103 $filename = getHashedFile($username, $data_dir, "$username.$year.cal");
104 $fp = fopen ($filename,'r');
105 if ($fp){
106 while ($fdata = fgetcsv ($fp, 4096, '|')) {
107 if (($fdata[0]==$date) && ($fdata[1]==$time)){
108 // do nothing
109 } else {
110 $calendardata[$fdata[0]][$fdata[1]] = array( 'length' => $fdata[2],
111 'priority' => $fdata[3],
112 'title' => $fdata[4],
113 'message' => $fdata[5],
114 'reminder' => $fdata[6] );
115 }
116 }
117 fclose ($fp);
118 }
119 }
120
121
122 ?>