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

namespace Todo;

class Lists extends SessionHelper {
	public function home($f3) {
		$f3->set('content', ($this->user === NULL) ? 'login.html' : 'noListSelected.html');
	}
	
	private function populateListData($f3, $l) {
		$f3->set('listid', $l->id);
		$f3->set('listname', $l->name);
		$f3->set('listdata', $l->itemsToArray());
	}
	
	private function getList($f3, $id) {
		$l = new \Todo\Model\TodoList($f3->get('DB'));
		if((!$l->byID($id)) || $l->user !== $this->user->id) {
			$f3->set('content', 'blank.html');
			$f3->set('error', 'List not found');
			$f3->error(404); # TODO: Meh, this stops F3 from rendering the template....
			return NULL;
		}
		return $l;
	}
	
	public function deleteList($f3, $args) {
		if($this->needLogin($f3)) {
			return;
		}
		
		if(!($l = $this->getList($f3, $args['list']))) {
			return;
		}
		
		if(($f3->get('VERB') === 'POST') && ($f3->get('POST.confirm') === 'OK')) {
			$l->deleteList();
			
			$f3->set('success', 'List deleted');
			$f3->set('content', 'blank.html');
		} else {
			$this->populateListData($f3, $l);
			$f3->set('content', 'deleteList.html');
		}
	}
	
	public function newList($f3) {
		if($this->needLogin($f3)) {
			return;
		}
		
		if($f3->get('VERB') !== 'POST') {
			return;
		}
		
		if($f3->get('POST.listname') === '') {
			$f3->set('error', 'List name must not be empty');
			$f3->set('content', 'blank.html');
			return;
		}
		
		$l = new \Todo\Model\TodoList($f3->get('DB'));
		$l->user = $this->user->id;
		$l->name = $f3->get('POST.listname');
		$l->save();
		
		$f3->set('success', 'List created!');
		
		$f3->set('content', 'list.html');
		$this->populateListData($f3, $l);
	}
	
	public function showList($f3, $args) {
		if($this->needLogin($f3)) {
			return;
		}
		
		$f3->set('content', 'list.html');
		
		if(!($l = $this->getList($f3, $args['list']))) {
			return;
		}
		
		if($f3->get('VERB') === 'POST') {
			$ok = true;
			switch($f3->get('POST.action')) {
			case 'setname':
				if($f3->get('POST.name') === '') {
					$f3->set('error', 'List name must not be empty');
					$ok = false;
				} else {
					$l->name = $f3->get('POST.name');
					$l->save();
				}
				break;
			case 'additem':
				if($f3->get('POST.itemtext') === '') {
					$f3->set('error', 'Can not add empty list item');
					$ok = false;
				} else {
					$l->addItem($f3->get('POST.itemtext'));
				}
				break;
			case 'delitem':
				$l->delItem($f3->get('POST.id'));
				break;
			case 'setchecked':
				$it = $l->getItem($f3->get('POST.id'));
				if($it === NULL) {
					$f3->set('error', 'Invalid item ID');
					$ok = false;
				} else {
					$it->checked = ($f3->get('POST.checked') === 'y');
					$it->save();
				}
				break;
			case 'moveup':
				$l->moveItem($f3->get('POST.id'), -1);
				break;
			case 'movedown':
				$l->moveItem($f3->get('POST.id'), 1);
				break;
			case 'movex':
				$l->moveItem($f3->get('POST.id'), $f3->get('POST.move'));
				break;
			case 'delchecked':
				$l->deleteChecked();
				break;
			default:
				$ok = false;
				$f3->set('error', 'Unknown list action.');
			}
			
			if($ok) {
				$f3->set('success', 'List updated');
			}
		}
		
		$this->populateListData($f3, $l);
	}
}