title) . "\"\n");
fwrite($fp, " time=\"" . xmlenc($this->time) . "\"\n");
fwrite($fp, " flyer=\"" . xmlenc($this->flyer) . "\">\n");
fwrite($fp, " ".xmlenc($this->description)."\n");
if($this->refs) $this->refs->write($fp, " ");
fwrite($fp, " \n");
}
public function show($eventpage)
{
$content = Markdown(htmlspecialchars_decode($this->description));
$str = "
\n";
$str .= "
" . date("D M jS Y", $this->time) . "
\n";
$str .= "
\n";
$str .= "
" . $content . "
\n";
/*
if($this->flyer) {
global $ICONS_DIR;
$icons = new Icons($ICONS_DIR."/");
$icon = $icons->icons[$this->flyer];
$str .= "
prefix.$icon->file."\"/>\n";
}
*/
if($this->refs) $str .= $this->refs->show();
$str .= "
\n";
return $str;
}
public function addRefs($refs)
{
$this->refs = $refs;
}
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();
public $eventpage;
// Admin config
public $admin_title = "Events";
public $admin_submodules = array("Add Event" => "add",
"Edit Event" => "edit",
"Delete Event" => "delete",
"Config" => "config");
public function admin_config($action, $vars)
{
switch($action) {
case "update":
$this->eventpage = $vars["eventpage"];
// echo "The title has now been changed to \"". $this->value("title") . "\"";
$this->write();
break;
default:
$form = new Form("update");
$form->addWidget(new LineEdit("Event page:", "eventpage", $this->eventpage));
$form->addWidget(new Button("Update"));
$form->render();
break;
}
}
public function admin_add($action, $vars)
{
global $ICONS_DIR;
switch($action) {
case "add":
$event = new Event($vars["title"], DateTimeEdit::toTimestamp($vars, "time"),
$vars["description"], $vars["icon"]);
echo "\"" .$event->title . "\" has now been added.";
$this->add($event);
$this->write();
break;
case "preview":
$event = new Event($vars["title"], DateTimeEdit::toTimestamp($vars, "time"),
$vars["description"], $vars["icon"]);
echo "\n";
echo "
\n";
echo $event->show($this->eventpage);
echo "
\n";
echo "
\n";
echo "Looking ok?
";
$form = new Form("add");
$form->addWidget(new Hidden($vars));
$form->addWidget(new Button("yes"));
$form->render();
$form = new Form("retry");
$form->addWidget(new Hidden($vars));
$form->addWidget(new Button("no"));
$form->render();
break;
case "retry":
$title = $vars["title"];
$time = DateTimeEdit::toTimestamp($vars, "time");
$description = $vars["description"];
$flyer = $vars["icon"];
default:
$form = new Form("preview");
$form->addWidget(new LineEdit("Title", "title", $title));
$form->addWidget(new DateTimeEdit("Time", "time", $time));
$form->addWidget(new TextEdit("Description", "description", $description));
$form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/")));
$form->addWidget(new Button("Add event"));
$form->render();
break;
}
}
public function admin_edit($action, $vars)
{
global $ICONS_DIR;
switch($action) {
case "add":
$this->events[$vars["eventid"]]->title = $vars["title"];
$this->events[$vars["eventid"]]->time = DateTimeEdit::toTimestamp($vars, "time");
$this->events[$vars["eventid"]]->description = $vars["description"];
$this->events[$vars["eventid"]]->flyer = $vars["icon"];
$refs = refsGetRefsFromHidden($vars);
$this->events[$vars["eventid"]]->refs = $refs;
$this->write();
echo "\"" . $this->events[$vars["eventid"]]->title . "\" has now been edited.";
break;
case "preview":
$event = new Event($vars["title"], DatetimeEdit::toTimestamp($vars, "time"),
$vars["description"], $vars["icon"]);
$refs = refsGetRefsFromWidget();
$event->addRefs($refs);
echo "\n";
echo "
\n";
echo $event->show($this->eventpage);
echo "
\n";
echo "
\n";
echo "Looking ok?
";
$form = new Form("add");
$form->addWidget(new Hidden($vars));
refsAddHiddenWidget($form, $refs);
$form->addWidget(new Button("yes"));
$form->render();
$form = new Form("retry");
$form->addWidget(new Hidden($vars));
refsAddHiddenWidget($form, $refs);
$form->addWidget(new Button("no"));
$form->render();
break;
case "edit":
case "retry":
if(isset($vars["title"])) $title = $vars["title"];
else $title = $this->events[$vars["eventid"]]->title;
if(isset($vars["time_year"])) $time = DateTimeEdit::toTimestamp($vars, "time");
else $time = $this->events[$vars["eventid"]]->time;
if(isset($vars["description"])) $description = $vars["description"];
else $description = $this->events[$vars["eventid"]]->description;
if(isset($vars["icon"])) $icon = $vars["icon"];
else $icon = $this->events[$vars["eventid"]]->flyer;
if(isset($vars["refslist_hidden"])) $refs = refsGetFromHidden($vars);
else {
$refs = refsGetCommaListFromRefs($this->events[$vars["eventid"]]->refs);
}
$form = new Form("preview");
$form->addWidget(new Hidden($vars));
$form->addWidget(new LineEdit("Title", "title", $title));
$form->addWidget(new DateTimeEdit("Time", "time", $time));
$form->addWidget(new TextEdit("Description", "description", $description));
$form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/")));
refsAddWidget($form, $refs);
$form->addWidget(new Button("Update event"));
$form->render();
break;
case "select":
default:
$eventlist = array();
foreach($this->events as $event) {
$eventlist[date("Y-m/d h:i:s", $event->time) . ": " . $event->title] = $event->time;
}
krsort($eventlist);
$form = new Form("edit");
$form->addWidget(new ComboBox("Edit this event:", "eventid", "", $eventlist));
$form->addWidget(new Button("Edit..."));
$form->render();
break;
}
}
public function admin_delete($action, $vars)
{
switch($action) {
case "delete":
echo "\"". $this->events[$vars["eventid"]]->title . "\" has now been deleted.";
unset($this->events[$vars["eventid"]]);
$this->write();
break;
case "confirm":
echo "Really delete: " . $this->events[$vars["eventid"]]->title . "?";
$form = new Form("delete");
$form->addWidget(new Hidden($vars));
$form->addWidget(new Button("yes"));
$form->render();
$form = new Form("select");
$form->addWidget(new Hidden($vars));
$form->addWidget(new Button("no"));
$form->render();
break;
case "select":
default:
$eventlist = array();
foreach($this->events as $event) {
$eventlist[date("Y-m/d h:i:s", $event->time) . ": " . $event->title] = $event->time;
}
krsort($eventlist);
$form = new Form("confirm");
$form->addWidget(new ComboBox("Delete this event:", "eventid", "", $eventlist));
$form->addWidget(new Button("Delete..."));
$form->render();
break;
}
}
public function admin($sub, $action, $vars)
{
switch($sub) {
case "config":
$this->admin_config($action, $vars);
break;
case "add":
$this->admin_add($action, $vars);
break;
case "edit":
$this->admin_edit($action, $vars);
break;
case "delete":
$this->admin_delete($action, $vars);
break;
}
}
public function run($params)
{
global $eventid;
$single = false;
$show = "all";
$number = -1;
$str = "";
$str .= "\n";
foreach($params as $param => $value) {
switch($param) {
case "show":
$show = $value;
break;
case "number":
$number = $value;
break;
case "single":
$single = true;
break;
}
}
if($single == true) {
if($eventid && $this->events[$eventid]) {
$str .= $this->events[$eventid]->show($this->eventpage);
} else {
$str .= "No such show!\n";
}
} else {
switch($show) {
case "coming":
$str .= $this->showcoming($number);
break;
case "old":
$str .= $this->showold($number);
break;
case "all":
default:
$str .= $this->showall($number);
}
}
$str .= "
\n";
return $str;
}
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($this->eventpage);
$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($this->eventpage);
$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($this->eventpage);
$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, "\n");
fwrite($fp, "eventpage)."\">\n");
foreach($this->events as $event) {
$event->write($fp);
}
fwrite($fp, "\n");
fclose($fp);
}
private function read()
{
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load($this->file);
$params = $dom->getElementsByTagName('event');
$events = $dom->documentElement;
$this->eventpage = $events->getAttribute('eventpage');
foreach ($params as $param) {
$description = "";
$dess = $param->getElementsByTagName('description');
foreach($dess as $des) {
$description = $des->textContent;
}
$event = new Event($param->getAttribute('title'),
$param->getAttribute('time'),
$description,
$param->getAttribute('flyer'));
$rs = $param->getElementsByTagName('refs');
foreach($rs as $r) {
$refs = new Refs($r);
$event->addRefs($refs);
break;
}
$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");
}
?>