summaryrefslogtreecommitdiff
path: root/autoload/Todo/SessionHelper.php
blob: b4811ab5f1ccab3851546aadfe407503b2ba9f6f (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
<?php

namespace Todo;

abstract class SessionHelper {
	protected $user = NULL;
	
	private function populateLists($f3) {
		$lists = array();
		for($l = $this->user->lists(); !$l->dry(); $l->next()) {
			$lists[] = array(
				'id' => $l->id,
				'name' => $l->name,
				'active' => ($l->id === $f3->get('listid')),
			);
		}
		$f3->set('lists', $lists);
	}
	
	public function beforeRoute($f3) {
		$id = $f3->get('SESSION.userID');
		if(is_numeric($id) && ($id > 0)) {
			$this->user = new \Todo\Model\User($f3->get('DB'));
			if((!$this->user->byID($id)) || (!$this->user->active)) {
				$this->user = NULL;
			}
		}
		$f3->set('listid', -1);
	}
	
	public function afterRoute($f3) {
		$f3->set('SESSION.userID', ($this->user === NULL) ? -1 : $this->user->id);
		if($this->user !== NULL) {
			$f3->set('user', $this->user->name);
			$this->populateLists($f3);
		} else {
			$f3->set('user', '');
		}
	}
	
	protected function needLogin($f3) {
		if($this->user === NULL) {
			$f3->set('error', 'You must be logged in to do that');
			$f3->set('content', 'blank.html');
			return true;
		}
		return false;
	}
}