summaryrefslogtreecommitdiff
path: root/utils/modules/events.php
blob: bdbe43e78de1e837822a76d0fde0d9d15730a6fc (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
include_once($UTIL_DIR . "/convert.php");

class Event {
  public $title;
  public $time;
  public $description;
  public $flyer;

  public function show()
  {
    $str = "<div class=\"event\">\n";
    $str .= "  <div class=\"event_title\">" . 
      htmlspecialchars_decode($this->title, ENT_QUOTES) . "</div>\n";
    $str .= "  <div class=\"event_time\">" . date("D M jS Y", $this->time) . "</div>\n";
    $str .= "  <div class=\"event_description\">" . 
      htmlspecialchars_decode($this->description, ENT_QUOTES) . "</div>\n";
    if($this->flyer) {
      $str .= "  <img class=\"event_flyer\" alt=\"flyer\" src=\"gfx/flyers/" . $this->flyer . "\"/>\n";
    }
    $str .= "</div>\n";
    return $str;
  }

  public function Event($title, $time, $description, $flyer = "")
  {
    $this->title = $title;
    $this->time = $time;
    $this->description = $description;
    $this->flyer = $flyer;
  }
}

class Events {

  private $file;
  private $events = array();

  // Admin config
  public $admin_title = "Events";
  public $admin_submodules = array("New Event" => "new",
																	 "Edit Event" => "edit",
																	 "Delete Event" => "delete");

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "new":
      echo "New";
      break;
    case "edit":
      echo "Edit";
      break;
    case "delete":
      echo "Delete";
      break;
    }
  }

  public function run($params)
  {
    foreach($params as $param) {
      switch($param) {
      case "coming":
				return $this->showcoming(-1);
				break;

      case "all":
        return $this->showall(-1);
        break;

      case "old":
      default:
				return $this->showold(-1);
				break;
      }
    }
  }

  public function showall($number)
  {
    $str = "";

    $foundany = false;

    // Key sort
    ksort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;

    foreach($this->events as $event) {
      $foundany = true;
      $str .= $event->show();
      $number--;
      if(!$number) return $str;
    }

    if($foundany == false) return "No shows available at the moment.";
    return $str;
  }

  public function showcoming($number)
  {
    $str = "";

    $foundany = false;

    // Key sort
    ksort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;
		
    foreach($this->events as $event) {
      if($event->time >= time()) {
				$foundany = true;
				$str .= $event->show();
				$number--;
      }
      if(!$number) return $str;
    }

    if($foundany == false) return "No shows available at the moment.";
    return $str;
  }

  public function showold($number)
  {
    $str = "";

    // Key sort
    krsort($this->events);

    // If number is -1 show all shows.
    if($number == -1) $number = 100000;

    foreach($this->events as $event) {
      if($event->time <= time()) {
				$str .= $event->show();
				$number--;
      }
      if(!$number) return $str;
    }
    return $str;
  }
	
  public function add($event) {
    $key = $event->time;
    //		array_push($this->events, $event);
    $this->events[$key] = $event;
  }
	
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<events>\n");
    foreach($this->events as $event) {
      fwrite($fp, "  <event title=\"" .
	     htmlspecialchars($event->title, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         time=\"" . $event->time . "\"\n");
      fwrite($fp, "         description=\"" .
	     htmlspecialchars($event->description, ENT_QUOTES, "UTF-8") . "\"\n");
      fwrite($fp, "         flyer=\"" . $event->flyer . "\">\n");
      fwrite($fp, "  </event>\n");
    }
    fwrite($fp, "</events>\n");

    fclose($fp);
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $params = $dom->getElementsByTagName('event');

    foreach ($params as $param) {
      $event = new Event($param->getAttribute('title'),
			 $param->getAttribute('time'),
			 $param->getAttribute('description'),
			 $param->getAttribute('flyer'));
      $this->add($event);
    }
  }

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

}

function events_init()
{
  global $DATA_DIR;
  return new Events($DATA_DIR . "/events.xml");
}

?>