summaryrefslogtreecommitdiff
path: root/forum/utils/events.php
blob: 2f46f14c6e8a7022611373e14fa21512d85623d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php

include_once($UTIL_DIR . "/convert.php");

class Event {
  public $eid;
  public $title;
  public $starttime;
  public $duration;
  public $description;
	public $concert;
  public $user;

  public function show()
  {
    global $users, $GLOBALS, $client_is_mobile_device;

		$str = "";

    $user = $users->getUser($this->user);
    $str .=  "      <div class=\"event";
		if($this->concert == "true") $str .= " concert";
		$str .= "\">\n";
    $str .=  "        <div class=\"title\">". $this->title . "\n";
		//    $str .=  "          <a class=\"button\" href=\"\">Edit</a>\n";
    $str .=  "        </div>\n";
    $str .=  "        <div class=\"time\">" . date("G:i", $this->starttime) .
			" - " .  date("G:i", $this->starttime + $this->duration) .  "</div>\n";
    if(!$client_is_mobile_device) {
      $str .=  "        <div class=\"description\">". $this->description .
				"</div>\n";
    } else {
      $str .=  "        <div class=\"mobiledescription\">" .
				$this->description . "</div>\n";
    }
    $str .=  "        <div class=\"user\">By: ".$user->name . "</div>\n";

		$datestr = "";
		if(isset($GLOBALS['date'])) $datestr = "&amp;date=". $GLOBALS['date'];

		$str .=  "        <a href=\"?mode=calendar" . $datestr .
			"&amp;eid=" . $this->eid . "&amp;action=delete\">Delete</a>";
    $str .=  "      </div>\n";

		return $str;
  }
  
  public function show_simple()
  {
    global $users, $GLOBALS, $client_is_mobile_device;

		$str = "";

    $str .=  "      <div class=\"event_simple\">\n";
    $str .=  "        <span class=\"date\">" . date("D d M Y", $this->starttime) . "</span>\n";
    $str .=  "        <span class=\"title\">". $this->title . "\n";
    $str .=  "        </span>\n";
    $str .=  "        <span class=\"time\">" . date("G:i", $this->starttime) .
			" - " .  date("G:i", $this->starttime + $this->duration) .  "</span>\n";
    $str .=  "      </div>\n";

		return $str;
  }
  
  public function Event($eid, $title, $concert, $starttime, $duration,
												$description, $user)
  {
    $this->eid = $eid;
    $this->title = $title;
    $this->starttime = $starttime;
    $this->duration = $duration;
    $this->description = $description;
    $this->user = $user;
		$this->concert = $concert;
  }
}

class Events {

  private $file;
  public $events = array();
	
  public function add($event) {
    $key = $event->eid;
    $this->events[$key] = $event;
  }
	
	public function delete($eid) {
		if(isset($this->events[$eid])) {
			unset($this->events[$eid]);
			echo "DELETE";
		}
	}

  public function write()
  {
    $fp = fopen($this->file, "w");
    
    $block = TRUE;
    flock($fp, LOCK_EX, $block); // do an exclusive lock
    
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    
    fwrite($fp, "<events>\n");
    foreach($this->events as $event) {
      fwrite($fp, "  <event eid=\"" .
	     htmlspecialchars($event->eid, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         title=\"" . 
	     htmlspecialchars($event->title, ENT_QUOTES, "UTF-8") . "\"\n");

			if($event->concert == "true") $concert = "true";
			else $concert = "false";
			fwrite($fp, "         concert=\"" . $concert . "\"\n");

      fwrite($fp, "         starttime=\"" . 
	     htmlspecialchars($event->starttime, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         duration=\"" . 
	     htmlspecialchars($event->duration, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         description=\"" . 
	     htmlspecialchars($event->description, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         user=\"" . 
	     htmlspecialchars($event->user, ENT_QUOTES, "UTF-8") . "\">\n");
      fwrite($fp, "  </event>\n");
    }
    fwrite($fp, "</events>\n");
    
    fclose($fp);
  }

  /*
	public function deleteForumUser($id)
	{
		if($this->members[$id]) {
			unset($this->members[$id]); 
			//			$this->write();
		} else {
			$str .=  "<p>ERROR: User! <em>".$id."</em> does not exist!</p>\n";
			return false;
		}
		return true;
	}
  */

  public function show($starttime, $endtime)
  {
		$str = "";
    foreach($this->events as $event) {
      if($event->starttime > $starttime && $event->starttime < $endtime)
				$str .= $event->show();
    }
		return $str;
  }

  public function getEvent($eid)
  {
    $event = $this->events[$eid]; 
    return $event;
  }

  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $events = $dom->getElementsByTagName('event');
    
    foreach ($events as $e) {
      $event = new Event($e->getAttribute('eid'),
			 $e->getAttribute('title'),
			 $e->getAttribute('concert') == "true",
			 $e->getAttribute('starttime'),
			 $e->getAttribute('duration'),
			 $e->getAttribute('description'),
			 $e->getAttribute('user'));
      
      $this->add($event);
    }
    
  }

  public function Events($file)
  {
    $this->file = $file;
    if(file_exists($file)) $this->read();
  }

}
?>