Skip to content

Commit

Permalink
✨ allow field on site
Browse files Browse the repository at this point in the history
Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Feb 22, 2024
1 parent 3d83035 commit 743cc1d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 74 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Kirby 3 Plugin to view recently modified pages by current User

## Usage

Add the section to your site or page blueprint.
Add the section to your site or page blueprint to display a list of the most recently modified pages by the currently logged in user. The sections is not able to list the site itself since the section depends on a collection of pages for the query.

**site/blueprints/site.yml**
```yaml
Expand All @@ -47,9 +47,9 @@ sections:
# query
```

Optionally you can add the field to page blueprints to show the time and user that modified given page most recently.
Optionally you can add the field to the site or any page blueprint to show the time and user that modified given content most recently. In contrast to the section the field is able to show most recent modified information for the site (`content/site.txt`).

**site/blueprints/default.yml**
**site/blueprints/pages/default.yml**
```yaml
fields:
showWhichUserModifiedPage:
Expand Down Expand Up @@ -90,7 +90,7 @@ sections:
```
```php
return [
'bnomei.recently-modified.limit' => 25,
'bnomei.recently-modified.limit' => 25, // default: 7
'bnomei.recently-modified.info' => function ($page) {
return $page->id();
},
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Kirby 3 Section to display recently modified content pages",
"license": "MIT",
"type": "kirby-plugin",
"version": "4.0.0",
"version": "4.1.0",
"authors": [
{
"name": "Bruno Meilick",
Expand Down
55 changes: 52 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,44 @@
}
return pages($keys ?? []);
},
'modifiedTimestamp' => function() {
return filemtime(site()->storage()->contentFiles(site()->storage()->defaultVersion())[0]);
},
'trackModifiedByUser' => function (bool $add = true): bool {
if (!kirby()->user() || option('bnomei.recently-modified.hooks') !== true) {
return false;
}
$cacheKey = kirby()->user()->id();

$listKey = $this->id();
$list = kirby()->cache('bnomei.recently-modified')->get($cacheKey, []);
if ($add) {
$list[$listKey] = $this->modifiedTimestamp();
} elseif (array_key_exists($listKey, $list)) {
unset($list[$listKey]);
}
arsort($list);
// NOTE: do not limit list or field will not work beyond the limit
// $list = array_slice($list, 0, intval(option('bnomei.recently-modified.limit')));
kirby()->cache('bnomei.recently-modified')->set($cacheKey, $list);
return true;
},
'findRecentlyModifiedByUser' => function (): ?\Kirby\Cms\User {
$modifier = null;
$modified = null;
$id = 'site';
foreach (kirby()->users() as $user) {
$list = kirby()->cache('bnomei.recently-modified')->get($user->id(), []);
if (array_key_exists($id, $list)) {
$modifiedTS = $list[$id];
if (!$modified || $modified < $modifiedTS) {
$modifier = $user;
$modified = $modifiedTS;
}
}
}
return $modifier;
},
],
'pageMethods' => [
'trackModifiedByUser' => function (bool $add = true): bool {
Expand All @@ -99,10 +136,11 @@
'findRecentlyModifiedByUser' => function (): ?\Kirby\Cms\User {
$modifier = null;
$modified = null;
$id = $this->id();
foreach (kirby()->users() as $user) {
$list = kirby()->cache('bnomei.recently-modified')->get($user->id(), []);
if (array_key_exists($this->id(), $list)) {
$modifiedTS = $list[$this->id()];
if (array_key_exists($id, $list)) {
$modifiedTS = $list[$id];
if (!$modified || $modified < $modifiedTS) {
$modifier = $user;
$modified = $modifiedTS;
Expand All @@ -115,7 +153,7 @@
'pagesMethods' => [
'onlyModifiedByUser' => function (?\Kirby\Cms\User $user = null) {
$user = $user ?? kirby()->user();
$cacheKey = kirby()->user()->id();
$cacheKey = $user->id();
$list = kirby()->cache('bnomei.recently-modified')->get($cacheKey, []);
return $this->filterBy(function ($page) use ($list) {
return array_key_exists($page->id(), $list);
Expand All @@ -138,6 +176,9 @@
'page.delete:before' => function (\Kirby\Cms\Page $page, bool $force) {
$page->trackModifiedByUser(false);
},
'site.update:after' => function (\Kirby\Cms\Site $newSite, \Kirby\Cms\Site $oldSite) {
$newSite->trackModifiedByUser();
},
],
'api' => [
'routes' => [
Expand All @@ -146,6 +187,14 @@
'action' => function () {
$id = urldecode(get('id'));
$id = explode('?', ltrim(str_replace(['/pages/', '/_drafts/', '+', ' '], ['/', '/', '/', '/'], $id), '/'))[0];
if ($id === 'site') {
$user = site()->findRecentlyModifiedByUser();
$username = $user ? (string)$user->nameOrEmail() : '';
return [
'auser' => $username,
'datetime' => date(option('bnomei.recently-modified.format'), site()->modifiedTimestamp()),
];
}
if ($page = kirby()->page($id)) {
$user = $page->findRecentlyModifiedByUser();
$username = $user ? (string)$user->nameOrEmail() : '';
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit199df58202538e1993df18d1b41c38a5::getLoader();
return ComposerAutoloaderInit505107e9cd1d25194956b98f2c30aa00::getLoader();
Loading

0 comments on commit 743cc1d

Please sign in to comment.