diff options
author | Romain d'Alverny <rda@hupstream.com> | 2012-06-23 22:31:35 +0200 |
---|---|---|
committer | Romain d'Alverny <rda@hupstream.com> | 2012-06-23 22:31:35 +0200 |
commit | 1af18af0150f51215f20cb3cd4c51c36e86145cd (patch) | |
tree | 25fdbb8fa72576381a216d1e25daf40ce2b07086 /app/classes | |
parent | d928ffae2e6d463336758d8a183b2ead87e0d433 (diff) | |
download | planet-1af18af0150f51215f20cb3cd4c51c36e86145cd.tar planet-1af18af0150f51215f20cb3cd4c51c36e86145cd.tar.gz planet-1af18af0150f51215f20cb3cd4c51c36e86145cd.tar.bz2 planet-1af18af0150f51215f20cb3cd4c51c36e86145cd.tar.xz planet-1af18af0150f51215f20cb3cd4c51c36e86145cd.zip |
Option to show only posts that have a specific tag or category.
- add configuration description in README.markdown
- add unit test with testmore.php for this very feature
Diffstat (limited to 'app/classes')
-rw-r--r-- | app/classes/Planet.class.php | 41 | ||||
-rw-r--r-- | app/classes/PlanetConfig.php | 17 |
2 files changed, 58 insertions, 0 deletions
diff --git a/app/classes/Planet.class.php b/app/classes/Planet.class.php index 4dcfbba..81a71fe 100644 --- a/app/classes/Planet.class.php +++ b/app/classes/Planet.class.php @@ -58,6 +58,10 @@ class Planet */ public function getItems() { + $this->items = $this->_filterItemsByCategory( + $this->items, + $this->config->getCategories()); + return $this->items; } @@ -152,4 +156,41 @@ class Planet { usort($this->items, array('PlanetItem','compare')); } + + /** + * Filter out items that do not match at least one + * of the defined categories. + * + * If there's no category, return all items. + * + * @param array $items to filter + * @param string $categories to filter against; may be a single word + * or a comma-separated list of words. + * + * @return array resulting list of items + */ + public function _filterItemsByCategory($items, $categories = null) + { + $categories = trim($categories); + + if (empty($categories)) + return $items; + + $categories = array_map('trim', explode(',', strtolower($categories))); + $cb_category_filter = + function ($item) use ($categories) + { + if (!is_array($item_categories = $item->get_categories())) + return false; + + $item_categories = array_map( + function ($i) { return strtolower($i->get_label()); }, + $item_categories + ); + + return array_intersect($categories, $item_categories); + }; + + return array_values(array_filter($items, $cb_category_filter)); + } } diff --git a/app/classes/PlanetConfig.php b/app/classes/PlanetConfig.php index a31938e..709bc2e 100644 --- a/app/classes/PlanetConfig.php +++ b/app/classes/PlanetConfig.php @@ -81,8 +81,25 @@ class PlanetConfig return $this->conf['postmaxlength']; } + public function getCategories() + { + return $this->conf['categories']; + } + public function toYaml() { return Spyc::YAMLDump($this->conf,4); } + + /** + * Generic accessor for config. + */ + public function __get($key) + { + $key = strtolower($key); + + return array_key_exists($key, $this->conf) ? + $this->conf[$key] : + null; + } } |