diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7689142e8..13dd12983 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+# v0.9.10 beta
+## 12/12/2014
+
+1. [](#new)
+ * Added Facebook-style `nicetime` date Twig filter
+2. [](#improved)
+ * Moved `clear-cache` functionality into Cache object required for Admin plugin
+3. [](#bugfix)
+ * Fix for undefined index with previous/next buttons
+
# v0.9.9 beta
## 12/05/2014
diff --git a/system/defines.php b/system/defines.php
index 79219deed..87eee2afa 100644
--- a/system/defines.php
+++ b/system/defines.php
@@ -2,7 +2,7 @@
// Some standard defines
define('GRAV', true);
-define('GRAV_VERSION', '0.9.9');
+define('GRAV_VERSION', '0.9.10');
define('DS', '/');
// Directories and Paths
diff --git a/system/src/Grav/Common/Cache.php b/system/src/Grav/Common/Cache.php
index 9da3974a8..223693214 100644
--- a/system/src/Grav/Common/Cache.php
+++ b/system/src/Grav/Common/Cache.php
@@ -3,6 +3,7 @@
use \Doctrine\Common\Cache\Cache as DoctrineCache;
use Grav\Common\Config\Config;
+use Grav\Common\Filesystem\Folder;
/**
* The GravCache object is used throughout Grav to store and retrieve cached data.
@@ -37,6 +38,33 @@ class Cache extends Getters
protected $cache_dir;
+ protected static $standard_remove = [
+ 'cache/twig/',
+ 'cache/doctrine/',
+ 'cache/compiled/',
+ 'cache/validated-',
+ 'images/',
+ 'assets/',
+ ];
+
+ protected static $all_remove = [
+ 'cache/',
+ 'images/',
+ 'assets/'
+ ];
+
+ protected static $assets_remove = [
+ 'assets/'
+ ];
+
+ protected static $images_remove = [
+ 'images/'
+ ];
+
+ protected static $cache_remove = [
+ 'cache/'
+ ];
+
/**
* Constructor
*
@@ -169,4 +197,70 @@ public function getKey()
{
return $this->key;
}
+
+ /**
+ * Helper method to clear all Grav caches
+ *
+ * @param string $remove standard|all|assets-only|images-only|cache-only
+ *
+ * @return array
+ */
+ public static function clearCache($remove = 'standard')
+ {
+
+ $output = [];
+ $user_config = USER_DIR . 'config/system.yaml';
+
+ switch($remove) {
+ case 'all':
+ $remove_paths = self::$all_remove;
+ break;
+ case 'assets-only':
+ $remove_paths = self::$assets_remove;
+ break;
+ case 'images-only':
+ $remove_paths = self::$images_remove;
+ break;
+ case 'cache-only':
+ $remove_paths = self::$cache_remove;
+ break;
+ default:
+ $remove_paths = self::$standard_remove;
+ }
+
+
+ foreach ($remove_paths as $path) {
+
+ $anything = false;
+ $files = glob(ROOT_DIR . $path . '*');
+
+ foreach ($files as $file) {
+ if (is_file($file)) {
+ if (@unlink($file)) {
+ $anything = true;
+ }
+ } elseif (is_dir($file)) {
+ if (@Folder::delete($file)) {
+ $anything = true;
+ }
+ }
+ }
+
+
+ if ($anything) {
+ $output[] = 'Cleared: ' . $path . '*';
+ }
+ }
+
+ $output[] = '';
+
+ if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
+ touch($user_config);
+
+ $output[] = 'Touched: ' . $user_config;
+ $output[] = '';
+ }
+
+ return $output;
+ }
}
diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php
index 862b1f6e6..798034aa2 100644
--- a/system/src/Grav/Common/Page/Page.php
+++ b/system/src/Grav/Common/Page/Page.php
@@ -1186,7 +1186,7 @@ public function children($type = Page::STANDARD_PAGES)
*/
public function isFirst()
{
- $collection = $this->parent()->collection();
+ $collection = $this->parent()->collection('content', false);
return $collection->isFirst($this->path());
}
@@ -1197,7 +1197,7 @@ public function isFirst()
*/
public function isLast()
{
- $collection = $this->parent()->collection();
+ $collection = $this->parent()->collection('content', false);
return $collection->isLast($this->path());
}
@@ -1229,7 +1229,7 @@ public function nextSibling()
*/
public function adjacentSibling($direction = 1)
{
- $collection = $this->parent()->collection();
+ $collection = $this->parent()->collection('content', false);
return $collection->adjacentSibling($this->path(), $direction);
}
@@ -1318,10 +1318,11 @@ public function find($url)
* Get a collection of pages in the current context.
*
* @param string|array $params
+ * @param boolean $pagination
* @return Collection
* @throws \InvalidArgumentException
*/
- public function collection($params = 'content')
+ public function collection($params = 'content', $pagination = true)
{
if (is_string($params)) {
$params = (array) $this->value('header.'.$params);
@@ -1378,13 +1379,16 @@ public function collection($params = 'content')
// New Custom event to handle things like pagination.
$grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));
- $params = $collection->params();
+ // Slice and dice the collection if pagination is required
+ if ($pagination) {
+ $params = $collection->params();
- $limit = isset($params['limit']) ? $params['limit'] : 0;
- $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
+ $limit = isset($params['limit']) ? $params['limit'] : 0;
+ $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
- if ($limit && $collection->count() > $limit) {
- $collection->slice($start, $limit);
+ if ($limit && $collection->count() > $limit) {
+ $collection->slice($start, $limit);
+ }
}
return $collection;
diff --git a/system/src/Grav/Common/TwigExtension.php b/system/src/Grav/Common/TwigExtension.php
index 7764e1093..2d1a2a636 100644
--- a/system/src/Grav/Common/TwigExtension.php
+++ b/system/src/Grav/Common/TwigExtension.php
@@ -47,7 +47,8 @@ public function getFilters()
new \Twig_SimpleFilter('md5', [$this,'md5Filter']),
new \Twig_SimpleFilter('sort_by_key', [$this,'sortByKeyFilter']),
new \Twig_SimpleFilter('ksort', [$this,'ksortFilter']),
- new \Twig_SimpleFilter('contains', [$this, 'containsFilter'])
+ new \Twig_SimpleFilter('contains', [$this, 'containsFilter']),
+ new \Twig_SimpleFilter('nicetime', [$this, 'nicetimeFilter']),
];
}
@@ -255,6 +256,66 @@ public function containsFilter($haystack, $needle)
return (strpos($haystack, $needle) !== false);
}
+ /**
+ * displays a facebook style 'time ago' formatted date/time
+ *
+ * @param $date
+ * @param $long_strings
+ * @param String
+ *
+ * @return boolean
+ */
+ public function nicetimeFilter($date, $long_strings = true)
+ {
+ if (empty($date)) {
+ return "No date provided";
+ }
+
+ if ($long_strings) {
+ $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
+ } else {
+ $periods = array("sec", "min", "hr", "day", "wk", "mo", "yr", "dec");
+ }
+
+ $lengths = array("60","60","24","7","4.35","12","10");
+
+ $now = time();
+
+ // check if unix timestamp
+ if ((string)(int)$date == $date) {
+ $unix_date = $date;
+ } else {
+ $unix_date = strtotime($date);
+ }
+
+ // check validity of date
+ if (empty($unix_date)) {
+ return "Bad date";
+ }
+
+ // is it future date or past date
+ if ($now > $unix_date) {
+ $difference = $now - $unix_date;
+ $tense = "ago";
+
+ } else {
+ $difference = $unix_date - $now;
+ $tense = "from now";
+ }
+
+ for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
+ $difference /= $lengths[$j];
+ }
+
+ $difference = round($difference);
+
+ if ($difference != 1) {
+ $periods[$j].= "s";
+ }
+
+ return "$difference $periods[$j] {$tense}";
+ }
+
/**
* Repeat given string x times.
*
diff --git a/system/src/Grav/Console/Cli/ClearCacheCommand.php b/system/src/Grav/Console/Cli/ClearCacheCommand.php
index 148d24634..de48ca0d0 100644
--- a/system/src/Grav/Console/Cli/ClearCacheCommand.php
+++ b/system/src/Grav/Console/Cli/ClearCacheCommand.php
@@ -1,6 +1,8 @@
writeln('Clearing cache');
$output->writeln('');
- $user_config = USER_DIR . 'config/system.yaml';
-
- $anything = false;
-
if ($input->getOption('all')) {
- $remove_paths = $this->all_remove;
+ $remove = 'all';
} elseif ($input->getOption('assets-only')) {
- $remove_paths = $this->assets_remove;
+ $remove = 'assets-only';
} elseif ($input->getOption('images-only')) {
- $remove_paths = $this->images_remove;
+ $remove = 'images-only';
} elseif ($input->getOption('cache-only')) {
- $remove_paths = $this->cache_remove;
+ $remove = 'cache-only';
} else {
- $remove_paths = $this->standard_remove;
+ $remove = 'standard';
}
- foreach ($remove_paths as $path) {
-
- $files = glob(ROOT_DIR . $path . '*');
-
- foreach ($files as $file) {
- if (is_file($file)) {
- if (@unlink($file)) {
- $anything = true;
- }
- } elseif (is_dir($file)) {
- if (@Folder::delete($file)) {
- $anything = true;
- }
- }
- }
-
- if ($anything) {
- $output->writeln('Cleared: ' . $path . '*');
- }
- }
-
- if (file_exists($user_config)) {
- touch($user_config);
- $output->writeln('');
- $output->writeln('Touched: ' . $user_config);
- $output->writeln('');
+ foreach (Cache::clearCache($remove) as $result) {
+ $output->writeln($result);
}
-
- if (!$anything) {
- $output->writeln('Nothing to clear...');
- $output->writeln('');
- }
-
}
}