Skip to content

Commit

Permalink
Option to show only posts that have a specific tag or category.
Browse files Browse the repository at this point in the history
- add configuration description in README.markdown
- add unit test with testmore.php for this very feature
  • Loading branch information
Romain d'Alverny committed Jun 23, 2012
1 parent d928ffa commit 1af18af
Show file tree
Hide file tree
Showing 5 changed files with 478 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,25 @@ Web hosting with PHP5 (PHP4 will not work).
License
-------
Moonmoon is free software and is released under BSD license.


Configuration options
---------------------
After installation, configuration is kept in a YAML formatted ```custom/config.yml```:

```%yaml
url: http://planet.example.net # your planet base URL
name: My Planet # your planet front page name
locale: en # front page locale
items: 10 # how many items to show
refresh: 240 # feeds cache timeout (in seconds)
cache: 10 # front page cache timeout (in seconds)
cachedir: ./cache # where is cache stored
postmaxlength: 0 # deprecated
shuffle: 0 # deprecated
nohtml: 0 # deprecated
categories: # only list posts that have one
# of these (tag or category)
```

---
41 changes: 41 additions & 0 deletions app/classes/Planet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function __construct($config=null)
*/
public function getItems()
{
$this->items = $this->_filterItemsByCategory(
$this->items,
$this->config->getCategories());

return $this->items;
}

Expand Down Expand Up @@ -152,4 +156,41 @@ public function sort()
{
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));
}
}
17 changes: 17 additions & 0 deletions app/classes/PlanetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,25 @@ public function getPostMaxLength()
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;
}
}
Loading

0 comments on commit 1af18af

Please sign in to comment.