diff options
Diffstat (limited to 'utils/modules')
-rw-r--r-- | utils/modules/members.php | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/utils/modules/members.php b/utils/modules/members.php new file mode 100644 index 0000000..db0966d --- /dev/null +++ b/utils/modules/members.php @@ -0,0 +1,208 @@ +<?php +/* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ + +include_once($UTIL_DIR . "/convert.php"); +include_once($UTIL_DIR . "/forms.php"); +include_once($UTIL_DIR . "/markdown.php"); + +class Member { + public $id; + public $name; + public $image; + public $startyear; + public $stopyear; + public $tags = array("Favoritkage" => "Æbletærte"); + + public function show() + { + global $MEMBERS_DIR; + $str = "<div class=\"member_entry\">\n"; + $str .= " <div class=\"member_name\">" . htmlspecialchars_decode($this->name, ENT_QUOTES) . "</div>\n"; + $str .= " <img alt=\"member\" class=\"member_image\" src=\"" . $MEMBERS_DIR."/".$this->image."\"/>\n"; + $str .= " <div class=\"member_startyear\">Member since: " . $this->startyear . "</div>\n"; + foreach($this->tags as $name => $val) { + $str .= " <div class=\"member_tag\">\n"; + $str .= " <div class=\"member_tag_name\">".$name."</div>\n"; + $str .= " <div class=\"member_tag_value\">".$val."</div>\n"; + $str .= " </div>\n"; + } + $str .= "</div>\n"; + return $str; + } + + public function write($fp) + { + fwrite($fp, " <member id=\"" . $this->id . "\"\n"); + fwrite($fp, " name=\"" . $this->name . "\"\n"); + fwrite($fp, " image=\"" . $this->image . "\"\n"); + fwrite($fp, " startyear=\"" . $this->startyear . "\"\n"); + fwrite($fp, " stopyear=\"" . $this->stoptyear . "\">\n"); + foreach($this->tags as $name => $val) { + fwrite($fp, " <tag name=\"".$name."\" value=\"".$val."\"/>\n"); + } + fwrite($fp, " </member>\n"); + } + + public function Member($id, $name, $image, $startyear, $stopyear, $tags) + { + $this->id = $id; + $this->name = $name; + $this->image = $image; + $this->startyear = $startyear; + $this->stopyear = $stopyear; + $this->tags = $tags; + $this->tags = array("Favoritkage" => "Føtex snegl"); + } +} + +class Members { + + private $file; + private $members = array(); + + // Admin config + public $admin_title = "Members"; + public $admin_submodules = array("Add Member" => "add", + "Edit Member" => "edit"); + + public function admin_add($action, $vars) + { + global $MEMBERS_DIR; + + switch($action) { + case "add": + $tags = array(); + $member = new Member($this->getNextID(), $vars["name"], $vars["image"], + $vars["startyear"], $vars["stopyear"], $tags); + echo "\"" .$member->name . "\" has now been added."; + $this->add($member); + $this->write(); + break; + + case "preview": + $tags = array(); + $member = new Member($this->getNextID(), $vars["name"], $vars["image"], + $vars["startyear"], $vars["stopyear"], $tags); + echo "<div class=\"preview\">\n"; + echo "<div class=\"content\">\n"; + echo $member->show(); + echo "</div>\n"; + echo "</div>\n"; + echo "<p>Looking ok?</p>"; + $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": + $name = $vars["name"]; + $image = $vars["image"]; + $startyear = $vars["startyear"]; + $stopyear = $vars["stopyear"]; + $tags = array(); + default: + $form = new Form("preview"); + $form->addWidget(new LineEdit("Name", "name", $name)); + $form->addWidget(new LineEdit("Start Year", "startyear", $startyear)); + $form->addWidget(new LineEdit("Stop Year (if no longer a member)", "stopyear", $stopyear)); + $form->addWidget(new ImageComboBox("Image", "image", $image, new Icons($MEMBERS_DIR."/"))); + $form->addWidget(new Button("Add member")); + $form->render(); + break; + } + } + + public function admin($sub, $action, $vars) + { + switch($sub) { + case "add": + $this->admin_add($action, $vars); + break; + + case "edit": + // $this->admin_edit($action, $vars); + break; + } + } + + public function run($params) + { + $str = ""; + foreach($this->members as $member) { + $str .= $member->show(); + } + return $str; + } + + public function add($member) { + $key = $member->id; + $this->members[$key] = $member; + } + + public function getNextID() + { + $maxid = -1; + foreach($this->members as $member) { + if($member->id > $maxid) $maxid = $member->id; + } + return $maxid + 1; + } + + public function write() + { + $fp = fopen($this->file, "w"); + fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); + + fwrite($fp, "<members>\n"); + foreach($this->members as $member) { + $member->write($fp); + } + fwrite($fp, "</members>\n"); + + fclose($fp); + } + + private function read() + { + $dom = new DomDocument; + $dom->preserveWhiteSpace = FALSE; + $dom->load($this->file); + $params = $dom->getElementsByTagName('member'); + + foreach ($params as $param) { + $tags = array(); + $ps = $param->getElementsByTagName('tag'); + foreach ($ps as $p) { + $tags[$p->getAttribute('name')] = $p->getAttribute('value'); + } + + $member = new Member($param->getAttribute('id'), + $param->getAttribute('name'), + $param->getAttribute('image'), + $param->getAttribute('startyear'), + $param->getAttribute('stopyear'), + $tags); + $this->add($member); + } + } + + public function Members($file) + { + $this->file = $file; + if(file_exists($file)) $this->read(); + } + +} + +function members_init() +{ + global $DATA_DIR; + return new Members($DATA_DIR . "/members.xml"); +} +?> |