add recent comments widget as plugin template (partials/recentcomments.html.twig)

This commit is contained in:
codeshell 2017-10-30 12:18:50 +01:00
parent 9179453c85
commit bd1799cc7e
3 changed files with 124 additions and 39 deletions

View File

@ -9,6 +9,7 @@ use Grav\Common\Filesystem\RecursiveFolderFilterIterator;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use Symfony\Component\Yaml\Yaml;
use Twig_SimpleFunction;
require_once PLUGINS_DIR . 'comments/class/Comment.php';
class CommentsPlugin extends Plugin
@ -48,7 +49,10 @@ class CommentsPlugin extends Plugin
public function onTwigSiteVariables() {
$this->grav['twig']->enable_comments_plugin = $this->enable;
$this->grav['twig']->comments = $this->fetchComments();
$this->grav['twig']->recentComments = $this->getRecentComments();
//$this->grav['twig']->recent_comments = $this->getRecentComments(); //cannot be used for functions with arguments
$function = new Twig_SimpleFunction('recent_comments', [$this, 'getRecentComments']);
$this->grav['twig']->twig()->addFunction($function);
if ($this->config->get('plugins.comments.built_in_css')) {
$this->grav['assets']
->addCss('plugin://comments/assets/comments.css');
@ -153,22 +157,6 @@ class CommentsPlugin extends Plugin
*/
public function onPluginsInitialized()
{
if ('/recent' === $this->grav['uri']->path()) {
//TODO TEST
echo PAGES_DIR;
echo "<br />";
$test = $this->getRecentComments(25);
var_dump($test[0]);
echo '<br /><hr /><br />';
var_dump($test[1]);
echo '<br /><hr /><br />';
var_dump($test[2]);
echo '<br /><hr /><br />';
exit();
}
if ($this->isAdmin()) {
$this->initializeAdmin();
} else {
@ -181,7 +169,7 @@ class CommentsPlugin extends Plugin
*/
public function onPageInitialized()
{
$is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
$is_ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
//$callback = $this->config->get('plugins.comments.ajax_callback');
// Process comment if required
if ($is_ajax) {// || $callback === $this->grav['uri']->path()
@ -534,24 +522,34 @@ class CommentsPlugin extends Plugin
}
}
private function getRecentComments($limit = 10) {
//TODO
/*
//init cache id
/**
* Used to add a recent comments widget. Call {{ recent_comments(123,12) }} specifying an integer representing the result length.
*
* Returns three different arrays with stats and comments.
*
* @param integer $limit max amount of comments in result set
* @param integer $limit_pages max amount of pages in result set
*
* @return array|array|array global stats, page stats, list of recent comments, options
*/
public function getRecentComments($limit, $limit_pages)
{
$routes = $this->grav['pages']->routes(); //routes[route] => path
$paths = array_flip($routes);
$cache = $this->grav['cache'];
$comments_cache_id = md5('comments-data' . $cache->getKey() . '-' . $uri->url());
$uri = $this->grav['uri'];
//search in cache
if ($comments = $cache->fetch($comments_cache_id)) {
return $comments;
$options = array(
'comments_limit' => $limit,
'pages_limit' => $limit_pages,
);
//use cached stats if possible
$recent_comments_cache_id = md5('comments-stats' . $cache->getKey());
if ($recent_comments = $cache->fetch($recent_comments_cache_id)) {
//use cache only if limits are big enough
if($recent_comments['options']['comments_limit'] >= $options['comments_limit'] && $recent_comments['options']['pages_limit'] >= $options['pages_limit']) {
return $recent_comments;
}
}
$comments = $this->getDataFromFilename($filename)['comments'];
$comments = $this->setCommentLevels($comments);
//save to cache if enabled
$cache->save($this->comments_cache_id, $comments);
*/
$path = PAGES_DIR;
$dirItr = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
$itrFilter = new \RecursiveIteratorIterator($dirItr, \RecursiveIteratorIterator::SELF_FIRST);
@ -570,8 +568,12 @@ class CommentsPlugin extends Plugin
$comments = array();
foreach ($filesItr as $filepath => $file) {
if ($file->isDir()) {
// this should never trigger as we are looking vor yamls only
// this should never trigger as we are looking vor yamls only
} else {
$route = '';
$fileFolder = substr($filepath, 0, strlen($filepath) - strlen($file->getFilename()) - 1);
if (!empty($paths[str_replace('/', '\\', $fileFolder)])) $route = $paths[str_replace('/', '\\', $fileFolder)];
if (!empty($paths[str_replace('\\', '/', $fileFolder)])) $route = $paths[str_replace('\\', '/', $fileFolder)];
$page_stats[$filepath] = array(
'active_entries' => 0,
'deleted_entries' => 0,
@ -580,6 +582,7 @@ class CommentsPlugin extends Plugin
'active_replies' => 0,
'deleted_replies' => 0,
'latest_active_entry' => 0,
'route' => $route,
);
$localfile = CompiledYamlFile::instance($filepath);
$localcomments = $localfile->content();
@ -609,6 +612,7 @@ class CommentsPlugin extends Plugin
$comments[] = array_merge(array(
'path' => $filepath,
'route' => $route,
'time' => $time,
), $comment);
}
@ -619,16 +623,34 @@ class CommentsPlugin extends Plugin
}
}
}
usort($comments, function($a, $b) {
//most recent comments first
usort($comments, function ($a, $b) {
if ($a['time'] === $b['time']) return 0;
if ($a['time'] < $b['time']) return 1;
return -1;
});
//most recent pages first
usort($page_stats, function ($a, $b) {
if ($a['latest_active_entry'] === $b['latest_active_entry']) return 0;
if ($a['latest_active_entry'] < $b['latest_active_entry']) return 1;
return -1;
});
//reduce comments in output to limit
if (!empty($limit) && $limit > 0 && $limit < count($comments)) {
return [$global_stats, $page_stats, array_slice($comments, 0, $limit)];
} else {
return [$global_stats, $page_stats, $comments];
$comments = array_slice($comments, 0, $limit);
}
//reduce pages in output to limit
if (!empty($limit_pages) && $limit_pages > 0 && $limit_pages < count($page_stats)) {
$page_stats = array_slice($page_stats, 0, $limit_pages);
}
//save to cache if enabled
$cache->save($recent_comments_cache_id, ['global_stats' => $global_stats, 'pages' => $page_stats, 'comments' => $comments, 'options' => $options]);
return ['global_stats' => $global_stats, 'pages' => $page_stats, 'comments' => $comments, 'options' => $options];
}
private function getFilesOrderedByModifiedDate($path = '') {
@ -723,7 +745,7 @@ class CommentsPlugin extends Plugin
/**
* Return the comments associated to the current route
*/
private function fetchComments() {
public function fetchComments() {
$cache = $this->grav['cache'];
//search in cache
if ($comments = $cache->fetch($this->comments_cache_id)) {
@ -899,6 +921,9 @@ class CommentsPlugin extends Plugin
$obj = $event['object'];
if ($obj instanceof Page) {
//TODO $this->deleteComment($obj);
//clear cache
$this->grav['cache']->delete(md5('comments-stats' . $this->grav['cache']->getKey()));
}
return true;
}

View File

@ -8,6 +8,9 @@ de:
DELETE: Löschen
SUCCESS: "Der Kommentar wurde erfolgreich gespeichert."
COMMENTS: Kommentare
COMMENTS_STATS: Kommentare
RECENT_COMMENTS: Neue Kommentare
RECENT_PAGES: Kommentierte Seiten
EMAIL_NOT_CONFIGURED: Email nicht konfiguriert
NEW_COMMENT_EMAIL_SUBJECT: 'Neuer Kommentar für %1$s'
NEW_COMMENT_EMAIL_BODY: '<p>Ein neuer Kommentar am %1$s von %3$s (%4$s).</p><p>Seite: %2$s</p><p>Text: %5$s</p>'
@ -36,6 +39,9 @@ en:
DELETE: Delete
SUCCESS: "Comment has been saved successfully."
COMMENTS: Comments
COMMENTS_STATS: Comments
RECENT_COMMENTS: Recent comments
RECENT_PAGES: Commented pages
EMAIL_NOT_CONFIGURED: Email not configured
NEW_COMMENT_EMAIL_SUBJECT: 'New comment on %1$s'
NEW_COMMENT_EMAIL_BODY: '<p>A new comment was made on %1$s by %3$s (%4$s).</p><p>Page: %2$s</p><p>Text: %5$s</p>'

View File

@ -0,0 +1,54 @@
{# you may set options when using this partial. Example: include 'partials/recentcomments.html.twig' with {'limit': 5, 'pages_limit': 3} #}
{% if grav.twig.enable_comments_plugin %}
<h1>{'PLUGIN_COMMENTS.COMMENTS_STATS'|t}}</h1>
{% set stats = recent_comments(limit|default(5), pages_limit|default(3)) %}
{% if stats.global_stats.active_entries %}
<i class="fa fa-comments" title="active_entries"></i> {{stats.global_stats.active_entries}}
(<i class="fa fa-trash" title="deleted_entries"></i> {{stats.global_stats.deleted_entries}})
- <i class="fa fa-comment" title="active_comments"></i>{{stats.global_stats.active_comments}}
(<i class="fa fa-trash" title="deleted_comments"></i> {{stats.global_stats.deleted_comments}})
- <i class="fa fa-commenting" title="active_replies"></i>{{stats.global_stats.active_replies}}
(<i class="fa fa-trash" title="deleted_replies"></i> {{stats.global_stats.deleted_replies}})
- <i class="fa fa-files-o" title="pages_with_active_entries"></i> {{stats.global_stats.pages_with_active_entries}}
{% endif %}
{% for key, entry in stats.pages %}
{% if loop.first %}
<h2>{'PLUGIN_COMMENTS.RECENT_PAGES'|t}} (limit {{stats.options.pages_limit}})</h2>
<ul class="fa-ul">
{% endif %}
<li><i class="fa-li fa fa-file" title="{{entry.route}}"></i>
{% if entry.route %}
<a href="{{entry.route}}#comments">
{% endif %}
{{entry.active_entries}}
{% if entry.route %}
</a>
{% endif %}
</li>
{% if loop.last %}
</ul>
{% endif %}
{% endfor %}
{% for key, entry in stats.comments %}
{% if loop.first %}
<h2>{'PLUGIN_COMMENTS.RECENT_COMMENTS'|t}} (limit {{stats.options.comments_limit}})</h2>
<ul class="fa-ul">
{% endif %}
{% set entry_icon = 'fa-comment' %}
{% if not empty(entry.parent) %}
{% set entry_icon = 'fa-commenting' %}
{% endif %}
<li><i class="fa-li fa {{entry_icon}}" title="{{key}}: {{entry.id}}, {{entry.parent}}"></i>
{% if entry.route %}
<a href="{{entry.route}}#comments">
{% endif %}
{{entry.date}}, {{entry.author}}, {{entry.text|truncate(15)}}
{% if entry.route %}
</a>
{% endif %}
</li>
{% if loop.last %}
</ul>
{% endif %}
{% endfor %}
{% endif %}