Automad
 All Classes Functions Variables Pages
pagelist.php
1 <?php
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  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
72 class Pagelist {
73 
74 
79  private $collection;
80 
81 
86  private $Context;
87 
88 
93  private $defaults = array(
94  'type' => false,
95  'parent' => AM_REQUEST,
96  'template' => false,
97  'sortItem' => false,
98  'sortOrder' => AM_LIST_DEFAULT_SORT_ORDER,
99  'offset' => 0,
100  'limit' => NULL
101  );
102 
103 
108  private $type;
109 
110 
115  private $parent;
116 
117 
122  private $template;
123 
124 
129  private $sortItem;
130 
131 
136  private $sortOrder;
137 
138 
143  private $filter = false;
144 
145 
150  private $search = false;
151 
152 
160  public function __construct($collection, $Context) {
161 
162  $this->collection = $collection;
163  $this->Context = $Context;
164  $this->config($this->defaults);
165 
166  }
167 
168 
177  public function config($options = array()) {
178 
179  Debug::log($options, 'Updating the configuration with the following options');
180 
181  // Turn all (but only) array items also existing in $defaults into class properties.
182  // Only items existing in $options will be changed and will override the existings values defined with the first call ($defaults).
183  foreach (array_intersect_key($options, $this->defaults) as $key => $value) {
184  $this->$key = $value;
185  }
186 
187  // Override settings with current query string options (filter, search and sort)
188  $overrides = Parse::queryArray();
189 
190  Debug::log($overrides, 'Use overrides from query string');
191 
192  foreach (array('filter', 'search', 'sortItem', 'sortOrder') as $key) {
193  if (isset($overrides[$key])) {
194  $this->$key = $overrides[$key];
195  }
196  }
197 
198  // Set sortOrder to the default order, if its value is invalid.
199  if (!in_array($this->sortOrder, array('asc', 'desc'))) {
200  $this->sortOrder = AM_LIST_DEFAULT_SORT_ORDER;
201  }
202 
203  $configArray = array_intersect_key((array)get_object_vars($this), $this->defaults);
204  Debug::log($configArray, 'Current config');
205 
206  return $configArray;
207 
208  }
209 
210 
223  private function getRelevant() {
224 
225  $Selection = new Selection($this->collection);
226 
227  // Filter by type
228  switch ($this->type) {
229  case 'children':
230  $Selection->filterByParentUrl($this->parent);
231  break;
232  case 'related':
233  $Selection->filterRelated($this->Context->get());
234  break;
235  case 'siblings':
236  $Selection->filterByParentUrl($this->Context->get()->parentUrl);
237  break;
238  case 'breadcrumbs':
239  $Selection->filterBreadcrumbs($this->Context->get()->url);
240  break;
241  }
242 
243  // Filter by template
244  $Selection->filterByTemplate($this->template);
245 
246  // Filter by keywords (for search results)
247  $Selection->filterByKeywords($this->search);
248 
249  return $Selection->getSelection(true, $this->offset, $this->limit);
250 
251  }
252 
253 
260  public function getTags() {
261 
262  $tags = array();
263 
264  foreach ($this->getRelevant() as $Page) {
265  $tags = array_merge($tags, $Page->tags);
266  }
267 
268  $tags = array_unique($tags);
269  sort($tags);
270 
271  return $tags;
272 
273  }
274 
275 
288  public function getPages($offset = 0, $limit = NULL) {
289 
290  $Selection = new Selection($this->getRelevant());
291  $Selection->filterByTag($this->filter);
292  $Selection->sortPages($this->sortItem, constant(strtoupper('sort_' . $this->sortOrder)));
293 
294  $pages = $Selection->getSelection(true, $offset, $limit);
295 
296  Debug::log(array_keys($pages));
297 
298  return $pages;
299 
300  }
301 
302 
303 }
304 
305 
306 ?>
static log($element, $description= '')
Definition: debug.php:113
__construct($collection, $Context)
Definition: pagelist.php:160
getPages($offset=0, $limit=NULL)
Definition: pagelist.php:288
config($options=array())
Definition: pagelist.php:177
static queryArray()
Definition: parse.php:270