summaryrefslogtreecommitdiff
path: root/src/views/Template.class.php
blob: 9d55b754a4a0d5509d3366a9af528d6ea2a8559c (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
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
<?php
require_once('Singleton.class.php');
require_once('Site.class.php');

require_once('Login.class.php');// used to see if logged in
require_once('Auth.class.php');// used to get username if we are

class Template extends Singleton {
	private $indent = 0;
	private $ret = false;
	
	public function status($status) {
		header($_SERVER["SERVER_PROTOCOL"]." $status"); 
		header("Status: $status");
	}
	
	public function setRet($ret) {
		$this->ret = $ret;
	}
	
	private function tabs() {
		$str = '';
		for ($i=0;$i<$this->indent;$i++) { $str .= "\t"; }
		return $str;
	}

	private function attr($attr='') {
		$tags='';
		if (is_array($attr)) {
			foreach($attr as $key=>$value) {
				$tags .= " $key=\"$value\"";
			}
		}
		return $tags;
	}
	
	public function tag($tag, $attr='', $content=false) {
		$tags = $this->attr($attr);
		$str = $this->tabs()."<$tag$tags";
		if ($content===false) {
			$str.= " />";
		} else {
			$str.= ">$content</$tag>";
		}
		$str.= "\n";
		if ($this->ret) return $str;
		echo $str;
	}
	
	public function openTag($tag, $attr='') {
		$tags = $this->attr($attr);
		$str = $this->tabs()."<$tag$tags>\n";
		$this->indent++;
		if ($this->ret) return $str;
		echo $str;
	}
	
	public function closeTag($tag) {
		$this->indent--;
		$str = $this->tabs()."</$tag>\n";
		if ($this->ret) return $str;
		echo $str;
	}

	public function text($text) {
		$str = $this->tabs().$text."\n";
		if ($this->ret) return $str;
		echo $str;
	}

	public function paragraph($text, $attr='', $return=false) {
		$tabs = $this->tabs();
		$tags = $this->attr($attr);
		$str = $tabs."<p$tags>";
		$str.= wordwrap($text, 78-($this->indent*8), "\n$tabs  ");
		$str.= "</p>\n";
		if ($this->ret||$return) return $str;
		echo $str;
	}

	public function link($target, $text, $return=false) {
		$ret = $this->ret;
		$this->ret |= $return;
		$str = $this->tag('a', array('href'=>$target), $text);
		$this->ret = $ret;
		if ($this->ret||$return) return $str;
		echo $str;		
	}
	public function url($page) {
		return Site::getInstance()->baseUrl().$page;
	}

	public function row($cells) {
		$str = $this->openTag('tr');
		foreach ($cells as $cell)
			$str.= $this->tag('td', array(), $cell);
		$str.= $this->closeTag('tr');
		if ($this->ret) return $str;
		echo $str;
	}
	private function css($file, $media) {
		$str.= $this->tag('link', array('rel'=>"stylesheet",
		                                'type'=>"text/css",
		                                'href'=>$this->url($file),
		                                'media'=>$media));
		if ($this->ret) return $str;
		echo $str;
	}
	public function header($title) {
		// username=false if not logged in or not connected to DB
		$username = Auth::getInstance(Login::isLoggedIn())->getName();

		$ret = $this->ret;
		$this->ret = true;
		
		$logged_in = ($username!==false);
		
		$str = '<?xml version="1.0" encoding="utf-8"?>'."\n";
		$str.= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"'."\n";
		$str.= '"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'."\n";
		
		$xmlns = "http://www.w3.org/1999/xhtml";
		$str.= $this->openTag('html', array('xmlns'=>$xmlns,
		                                    'lang'=>"en-us",
		                                    'dir'=>"ltr"));
		$this->indent = 0; // don't indent for the <html> tag
		
		$str.= $this->openTag('head');
		$str.= $this->tag('title', array(), htmlspecialchars($title));
		$str.= $this->css('style.css', 'all');
		$str.= $this->css('screen.css', 'screen');
		$str.= $this->css('print.css', 'print');
		$str.= $this->closeTag('head');
		
		$body_class = 'logged'.($logged_in?'in':'out');
		$str.= $this->openTag('body', array('class'=>$body_class));

		$str.= $this->openTag('div', array('class'=>'infobar'));
		if ($logged_in) {
			$user = htmlentities($username);
			
			$str.= $this->link($this->url(''), "Home");
			$str.= $this->link($this->url("users/$user"),"@$user");
			$str.= $this->logout_button('Logout');
		} else {
			$url=$_SERVER['REQUEST_URI'];
			$str.= $this->openTag('form',
			                      array('action'=>$this->url('auth'),
			                            'method'=>'post'));
			$str.= $this->tag('input', array('type'=>'hidden',
			                          'name'=>'action',
			                          'value'=>'login'));
			$str.= $this->tag('input', array('type'=>'hidden',
			                          'name'=>'url',
			                          'value'=>$url));
			$str.= $this->tag('label',
			                  array('for'=>'username'),'Username:');
			$str.= $this->tag('input', array('type'=>'text',
			                          'name'=>'username',
			                          'id'=>'username'));
			$str.= $this->tag('label',
			                  array('for'=>'password'),'Password:');
			$str.= $this->tag('input', array('type'=>'password',
			                          'name'=>'password',
			                          'id'=>'password'));
			$str.= $this->tag('input', array('type'=>'submit',
			                          'value'=>'Login'));
			$str.= $this->link($this->url("users/new"),'New Account');
			$str.= $this->closeTag('form');
		}
		$str.= $this->closeTag('div');

		$str.= $this->openTag('div',array('class'=>'main'));
		$str.= $this->openTag('div',array('class'=>'main_sub'));

		$this->ret = $ret;
		if ($this->ret) return $str;
		echo $str;
	}

	public function footer() {
		$str = $this->closeTag('div');
		$str.= $this->closeTag('div');
		$str.= $this->closeTag('body');
		$str.= $this->closeTag('html');
		
		if ($this->ret) return $str;
		echo $str;
	}
	
	public function openFieldset($name, $lock=false) {
		$class = ($lock?' class="readonly"':'');
		$str = $this->text("<fieldset$class><legend>$name</legend><ul>");
		$this->indent++;
		if ($this->ret) return $str;
		echo $str;
	}
	
	public function closeFieldset() {
		$this->indent--;
		$str = $this->text("</ul></fieldset>");
		if ($this->ret) return $str;
		echo $str;
	}
	
	public function input($id, $label, $hint, $html, $tags=null) {
		if ($tags===null) { $tags=array(); }
		$str = $this->openTag('li', $tags);
		$str.= $this->tag('label', array('for'=>$id), $label);
		$str.= $this->text($html);
		if (strlen($hint)>0) {
			$str.=$this->paragraph($hint,
			                       Array('class'=>'form_data'));
		}
		$str.= $this->closeTag('li');
		if ($this->ret) return $str;
		echo $str;
	}
	
	private function inputStr($type, $id, $default, $lock) {
		$value = htmlentities($default);
		$tag = ($lock?"readonly='readonly' ":'');
		return "<input type='$type' name='$id' id='$id' value=\"$value\" $tag/>";
	}
	public function inputTextArea($id, $label, $hint='', $default='', $lock=FALSE) {
		$value = htmlentities($default);
		$tag = ($lock?"readonly='readonly' ":'');
		return $this->input($id, $label, $hint,
		                    "<textarea name='$id' id='$id' $tag>$value</textarea>",
		                    array('class'=>'wide'));
	}
		
	public function inputText($id, $label, $hint='', $default='', $lock=FALSE) {
		return $this->input($id, $label, $hint,
		                    $this->inputStr('text', $id, $default, $lock));
	}
	
	public function inputPassword($id, $label, $hint='', $default='', $lock=FALSE) {
		return $this->input($id, $label, $hint,
		                    $this->inputStr('password', $id, $default, $lock));
	}
	
	public function inputNewPassword($id, $label, $default='', $lock=FALSE) {
		return $this->input($id, $label,
		    "Type the same password twice, to make sure you don't mistype.",
		    $this->inputStr('password', $id, $default, $lock).
		    "\n".$this->tabs()."\t".
		    $this->inputStr('password', $id.'_verify', $default,$lock));
	}
	public function inputBool($id, $label, $hint='', $default=FALSE, $lock=FALSE) {
		$tag = '';
		if ($lock) $tag.= "readonly='readonly' ";
		if ($default) $tag.= "checked='checked' ";
		return $this->input($id, $label, $hint,
		                    "<input type='hidden' name='$id' value='false' />".
		                    "<input type='checkbox' id='$id' name='$id' value='true' $tag>");
		                    
		$attrib =  array('type'=>'checkbox',
		                 'id'=>$id,
		                 'name'=>$name.'[]',
		                 'value'=>$value);
		if ($default) $attrib['checked']='checked';
		if ($lock   ) $attrib['readonly']='readonly';
		
		$str = $this->openTag('li');
		$str.= $this->tag('input', $attrib);
		$str.= $this->tag('label', array('for'=>$id), $label);
		$str.= $this->closeTag('li');
		
		if ($this->ret) return $str;
		echo $str;

	}

	public function inputBoolArray($name, $value, $label, $default=FALSE, $lock=FALSE) {
		$id = $name.'_'.$value;
		$attrib =  array('type'=>'checkbox',
		                 'id'=>$id,
		                 'name'=>$name.'[]',
		                 'value'=>$value);
		if ($default) $attrib['checked']='checked';
		if ($lock   ) $attrib['readonly']='readonly';
		
		$str = $this->openTag('li');
		$str.= $this->tag('input', $attrib);
		$str.= $this->tag('label', array('for'=>$id), $label);
		$str.= $this->closeTag('li');
		
		if ($this->ret) return $str;
		echo $str;

	}
	
	public function inputP($text, $error=false) {
		$str = $this->openTag('li');
		$str.=$this->paragraph($text,
		                       array('class'=>($error?' error':'')));
		$str.= $this->closeTag('li');
		if ($this->ret) return $str;
		echo $str;		
	}
	
	public function logout_button($text) {
		$str = $this->openTag('form',array('action'=>$this->url('auth'),
		                                   'method'=>"post",
		                                   'style'=>'display:inline'));
		$str.= $this->tag('input', array('type'=>'hidden',
		                           'name'=>'action',
		                           'value'=>'logout'));
		$str.= $this->tag('input', array('type'=>'submit',
		                                 'value'=>$text));
		$str.= $this->closeTag('form');
		if ($this->ret) return $str;
		echo $str;		
	}
}