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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
<?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;
public $tagnames;
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\"><div class=\"member_tags_".$name."\">\n";
$str .= " <div class=\"member_tag_name\">".$this->tagnames[$name]."</div>\n";
$str .= " <div class=\"member_tag_value\">".$val."</div>\n";
$str .= " </div></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->stopyear . "\">\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, $tagnames)
{
$this->id = $id;
$this->name = $name;
$this->image = $image;
$this->startyear = $startyear;
$this->stopyear = $stopyear;
$this->tags = $tags;
$this->tagnames = $tagnames;
}
}
class Members {
private $file;
private $members = array();
private $tags = array();
// Admin config
public $admin_title = "Members";
public $admin_submodules = array("Add Member" => "add",
"Edit Member" => "edit",
"Tag Editor" => "tags");
public function admin_add($action, $vars)
{
global $MEMBERS_DIR;
switch($action) {
case "add":
$tags = $this->getTagsFromArray($vars, "tags_");
$member = new Member($this->getNextID(), $vars["name"], $vars["image"],
$vars["startyear"], $vars["stopyear"], $tags, $this->tags);
echo "\"" .$member->name . "\" has now been added.";
$this->add($member);
$this->write();
break;
case "preview":
$tags = $this->getTagsFromArray($vars, "tags_");
$member = new Member($this->getNextID(), $vars["name"], $vars["image"],
$vars["startyear"], $vars["stopyear"], $tags, $this->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 = $this->getTagsFromArray($vars, "tags_");
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));
foreach($this->tags as $id => $title) {
$form->addWidget(new LineEdit($title, "tags_".$id, $tags[$id]));
}
$form->addWidget(new ImageComboBox("Image", "image", $image, new Icons($MEMBERS_DIR."/")));
$form->addWidget(new Button("Add member"));
$form->render();
break;
}
}
public function admin_edit($action, $vars)
{
global $MEMBERS_DIR;
switch($action) {
case "add":
$tags = $this->getTagsFromArray($vars, "tags_");
$this->members[$vars["memberid"]]->show();
$this->members[$vars["memberid"]]->name = $vars["name"];
$this->members[$vars["memberid"]]->image = $vars["image"];
$this->members[$vars["memberid"]]->startyear = $vars["startyear"];
$this->members[$vars["memberid"]]->stopyear = $vars["stopyear"];
$this->members[$vars["memberid"]]->tags = $tags;
$this->write();
echo "\"" . $this->members[$vars["memberid"]]->name . "\" has now been edited.";
break;
case "preview":
$tags = $this->getTagsFromArray($vars, "tags_");
$member = new Member($vars["memberid"], $vars["name"], $vars["image"],
$vars["startyear"], $vars["stopyear"], $tags, $this->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 "edit":
case "retry":
if(isset($vars["name"])) $name = $vars["name"];
else $name = $this->members[$vars["memberid"]]->name;
if(isset($vars["image"])) $image = $vars["image"];
else $image = $this->members[$vars["memberid"]]->image;
if(isset($vars["startyear"])) $startyear = $vars["startyear"];
else $startyear = $this->members[$vars["memberid"]]->startyear;
if(isset($vars["stopyear"])) $stopyear = $vars["stopyear"];
else $stopyear = $this->members[$vars["memberid"]]->stopyear;
// HACK, this should work, but we should check on the tags_ vars instead.
if(isset($vars["name"])) $tags = $this->getTagsFromArray($vars, "tags_");
else $tags = $this->members[$vars["memberid"]]->tags;
$form = new Form("preview");
$form->addWidget(new Hidden($vars));
$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));
foreach($this->tags as $id => $title) {
$form->addWidget(new LineEdit($title, "tags_".$id, $tags[$id]));
}
$form->addWidget(new ImageComboBox("Image", "image", $image, new Icons($MEMBERS_DIR."/")));
$form->addWidget(new Button("Preview"));
$form->render();
break;
case "select":
default:
$memberlist = array();
foreach($this->members as $member) {
$memberlist[$member->name] = sprintf("%d", $member->id);
}
$form = new Form("edit");
$form->addWidget(new ComboBox("Edit this member:", "memberid", "", $memberlist));
$form->addWidget(new Button("Edit..."));
$form->render();
break;
}
}
public function admin_tags($action, $vars)
{
switch($action) {
case "update":
global $taglist;
$this->tags = ListEditor::splitValues($taglist);
$this->write();
break;
default:
$form = new Form("update");
$form->addWidget(new ListEditor("Tags", "taglist",
new LineEdit("Title (must not contain ':')", "title"),
new LineEdit("ID (must only contains letters a-z)", "id"),
$this->tags));
$form->addWidget(new Button("Update"));
$form->render();
break;
}
}
public function getTagsFromArray($array, $prefix)
{
$out = array();
foreach($array as $name => $value) {
if(strpos($name, $prefix) !== FALSE && strpos($name, $prefix) == 0)
$out[substr($name, strlen($prefix))] = $value;
}
return $out;
}
public function admin($sub, $action, $vars)
{
switch($sub) {
case "add":
$this->admin_add($action, $vars);
break;
case "edit":
$this->admin_edit($action, $vars);
break;
case "tags":
$this->admin_tags($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 tags=\"".htmlspecialchars(serialize($this->tags), ENT_QUOTES, "UTF-8")."\">\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);
$ts = $dom->getElementsByTagName('members');
foreach($ts as $t) {
$this->tags = unserialize(htmlspecialchars_decode($t->getAttribute('tags'), ENT_QUOTES));
}
$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->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");
}
?>
|