aboutsummaryrefslogtreecommitdiff
path: root/app/models/EntriesGetter.php
blob: 803aad73213ed5c544c88d1ff4851e8a12b84190 (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
<?php

class EntriesGetter {
	private $type = array (
		'type' => 'all',
		'id' => 'all'
	);
	private $state = 'all';
	private $filter = array (
		'words' => array (),
		'tags' => array (),
	);
	private $order = 'high_to_low';
	private $entries = array ();

	private $nb = 1;
	private $first = '';
	private $next = '';

	public function __construct ($type, $state, $filter, $order, $nb, $first = '') {
		$this->_type ($type);
		$this->_state ($state);
		$this->_filter ($filter);
		$this->_order ($order);
		$this->nb = $nb;
		$this->first = $first;
	}

	public function type () {
		return $this->type;
	}
	public function state () {
		return $this->state;
	}
	public function filter () {
		return $this->filter;
	}
	public function order () {
		return $this->order;
	}
	public function entries () {
		return $this->entries;
	}

	public function _type ($value) {
		if (!is_array ($value) ||
		    !isset ($value['type']) ||
		    !isset ($value['id'])) {
			throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
		}

		$type = $value['type'];
		$id = $value['id'];

		if ($type != 'all' && $type != 'favoris' && $type != 'public' && $type != 'c' && $type != 'f') {
			throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
		}

		if (($type == 'all' || $type == 'favoris' || $type == 'public') &&
		    ($type != $id)) {
			throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
		}

		$this->type = $value;
	}
	public function _state ($value) {
		if ($value != 'all' && $value != 'not_read' && $value != 'read') {
			throw new EntriesGetterException ('Bad state line ' . __LINE__ . ' in file ' . __FILE__);
		}

		$this->state = $value;
	}
	public function _filter ($value) {
		$value = trim ($value);
		$terms = explode (' ', $value);

		foreach ($terms as $word) {
			if (!empty ($word) && $word[0] == '#' && isset ($word[1])) {
				$tag = substr ($word, 1);
				$this->filter['tags'][$tag] = $tag;
			} elseif (!empty ($word)) {
				$this->filter['words'][$word] = $word;
			}
		}
	}
	public function _order ($value) {
		if ($value != 'high_to_low' && $value != 'low_to_high') {
			throw new EntriesGetterException ('Bad order line ' . __LINE__ . ' in file ' . __FILE__);
		}

		$this->order = $value;
	}

	public function execute () {
		$entryDAO = new EntryDAO ();

		HelperEntry::$nb = $this->nb;	//TODO: Update: Now done in SQL
		HelperEntry::$first = $this->first;	//TODO: Update: Now done in SQL
		HelperEntry::$filter = $this->filter;

		$sqlLimit = (empty ($this->filter['words']) && empty ($this->filter['tags'])) ? $this->nb : '';	//Disable SQL LIMIT optimisation during search	//TODO: Do better!

		switch ($this->type['type']) {
		case 'all':
			list ($this->entries, $this->next) = $entryDAO->listEntries (
				$this->state,
				$this->order,
				$this->first,
				$sqlLimit
			);
			break;
		case 'favoris':
			list ($this->entries, $this->next) = $entryDAO->listFavorites (
				$this->state,
				$this->order,
				$this->first,
				$sqlLimit
			);
			break;
		case 'public':
			list ($this->entries, $this->next) = $entryDAO->listPublic (
				$this->state,
				$this->order,
				$this->first,
				$sqlLimit
			);
			break;
		case 'c':
			list ($this->entries, $this->next) = $entryDAO->listByCategory (
				$this->type['id'],
				$this->state,
				$this->order,
				$this->first,
				$sqlLimit
			);
			break;
		case 'f':
			list ($this->entries, $this->next) = $entryDAO->listByFeed (
				$this->type['id'],
				$this->state,
				$this->order,
				$this->first,
				$sqlLimit
			);
			break;
		default:
			throw new EntriesGetterException ('Bad type line ' . __LINE__ . ' in file ' . __FILE__);
		}
	}

	public function getPaginator () {
		$paginator = new RSSPaginator ($this->entries, $this->next);

		return $paginator;
	}
}