add recent comments widget as plugin template (partials/recentcomments.html.twig)
This commit is contained in:
		
							parent
							
								
									9179453c85
								
							
						
					
					
						commit
						bd1799cc7e
					
				
					 3 changed files with 124 additions and 39 deletions
				
			
		
							
								
								
									
										99
									
								
								comments.php
									
										
									
									
									
								
							
							
						
						
									
										99
									
								
								comments.php
									
										
									
									
									
								
							| 
						 | 
					@ -9,6 +9,7 @@ use Grav\Common\Filesystem\RecursiveFolderFilterIterator;
 | 
				
			||||||
use Grav\Common\Page\Page;
 | 
					use Grav\Common\Page\Page;
 | 
				
			||||||
use RocketTheme\Toolbox\Event\Event;
 | 
					use RocketTheme\Toolbox\Event\Event;
 | 
				
			||||||
use Symfony\Component\Yaml\Yaml;
 | 
					use Symfony\Component\Yaml\Yaml;
 | 
				
			||||||
 | 
					use Twig_SimpleFunction;
 | 
				
			||||||
require_once PLUGINS_DIR .  'comments/class/Comment.php';
 | 
					require_once PLUGINS_DIR .  'comments/class/Comment.php';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CommentsPlugin extends Plugin
 | 
					class CommentsPlugin extends Plugin
 | 
				
			||||||
| 
						 | 
					@ -48,7 +49,10 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
    public function onTwigSiteVariables() {
 | 
					    public function onTwigSiteVariables() {
 | 
				
			||||||
        $this->grav['twig']->enable_comments_plugin = $this->enable;
 | 
					        $this->grav['twig']->enable_comments_plugin = $this->enable;
 | 
				
			||||||
        $this->grav['twig']->comments = $this->fetchComments();
 | 
					        $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')) {
 | 
					        if ($this->config->get('plugins.comments.built_in_css')) {
 | 
				
			||||||
            $this->grav['assets']
 | 
					            $this->grav['assets']
 | 
				
			||||||
                ->addCss('plugin://comments/assets/comments.css');
 | 
					                ->addCss('plugin://comments/assets/comments.css');
 | 
				
			||||||
| 
						 | 
					@ -153,22 +157,6 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public function onPluginsInitialized()
 | 
					    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()) {
 | 
					        if ($this->isAdmin()) {
 | 
				
			||||||
            $this->initializeAdmin();
 | 
					            $this->initializeAdmin();
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
| 
						 | 
					@ -534,24 +522,34 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private function getRecentComments($limit = 10) {
 | 
					    /**
 | 
				
			||||||
        //TODO
 | 
					     * Used to add a recent comments widget. Call {{ recent_comments(123,12) }} specifying an integer representing the result length.
 | 
				
			||||||
/*
 | 
					     *
 | 
				
			||||||
        //init cache id
 | 
					     * 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'];
 | 
					        $cache = $this->grav['cache'];
 | 
				
			||||||
        $comments_cache_id = md5('comments-data' . $cache->getKey() . '-' . $uri->url());
 | 
					        $options = array(
 | 
				
			||||||
        $uri = $this->grav['uri'];
 | 
					            'comments_limit' => $limit,
 | 
				
			||||||
        
 | 
					            'pages_limit' => $limit_pages,
 | 
				
			||||||
        //search in cache
 | 
					        );
 | 
				
			||||||
        if ($comments = $cache->fetch($comments_cache_id)) {
 | 
					        //use cached stats if possible
 | 
				
			||||||
            return $comments;
 | 
					        $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;
 | 
					        $path = PAGES_DIR;
 | 
				
			||||||
        $dirItr     = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
 | 
					        $dirItr     = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
 | 
				
			||||||
        $itrFilter = new \RecursiveIteratorIterator($dirItr, \RecursiveIteratorIterator::SELF_FIRST);
 | 
					        $itrFilter = new \RecursiveIteratorIterator($dirItr, \RecursiveIteratorIterator::SELF_FIRST);
 | 
				
			||||||
| 
						 | 
					@ -572,6 +570,10 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
            if ($file->isDir()) {
 | 
					            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 {
 | 
					            } 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(
 | 
					                $page_stats[$filepath] = array(
 | 
				
			||||||
                    'active_entries' => 0,
 | 
					                    'active_entries' => 0,
 | 
				
			||||||
                    'deleted_entries' => 0,
 | 
					                    'deleted_entries' => 0,
 | 
				
			||||||
| 
						 | 
					@ -580,6 +582,7 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
                    'active_replies' => 0,
 | 
					                    'active_replies' => 0,
 | 
				
			||||||
                    'deleted_replies' => 0,
 | 
					                    'deleted_replies' => 0,
 | 
				
			||||||
                    'latest_active_entry' => 0,
 | 
					                    'latest_active_entry' => 0,
 | 
				
			||||||
 | 
					                    'route' => $route,
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
                $localfile = CompiledYamlFile::instance($filepath);
 | 
					                $localfile = CompiledYamlFile::instance($filepath);
 | 
				
			||||||
                $localcomments = $localfile->content();
 | 
					                $localcomments = $localfile->content();
 | 
				
			||||||
| 
						 | 
					@ -609,6 +612,7 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
                            
 | 
					                            
 | 
				
			||||||
                            $comments[] = array_merge(array(
 | 
					                            $comments[] = array_merge(array(
 | 
				
			||||||
                                'path' => $filepath,
 | 
					                                'path' => $filepath,
 | 
				
			||||||
 | 
					                                'route' => $route,
 | 
				
			||||||
                                'time' => $time,
 | 
					                                'time' => $time,
 | 
				
			||||||
                            ), $comment);
 | 
					                            ), $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 0;
 | 
				
			||||||
            if ($a['time'] < $b['time']) return 1;
 | 
					            if ($a['time'] < $b['time']) return 1;
 | 
				
			||||||
            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)) {
 | 
					        if (!empty($limit) && $limit > 0 && $limit < count($comments)) {
 | 
				
			||||||
            return [$global_stats, $page_stats, array_slice($comments, 0, $limit)];
 | 
					            $comments = array_slice($comments, 0, $limit);
 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            return [$global_stats, $page_stats, $comments];
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        //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 = '') {
 | 
					    private function getFilesOrderedByModifiedDate($path = '') {
 | 
				
			||||||
| 
						 | 
					@ -723,7 +745,7 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Return the comments associated to the current route
 | 
					     * Return the comments associated to the current route
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private function fetchComments() {
 | 
					    public function fetchComments() {
 | 
				
			||||||
        $cache = $this->grav['cache'];
 | 
					        $cache = $this->grav['cache'];
 | 
				
			||||||
        //search in cache
 | 
					        //search in cache
 | 
				
			||||||
        if ($comments = $cache->fetch($this->comments_cache_id)) {
 | 
					        if ($comments = $cache->fetch($this->comments_cache_id)) {
 | 
				
			||||||
| 
						 | 
					@ -899,6 +921,9 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
        $obj = $event['object'];
 | 
					        $obj = $event['object'];
 | 
				
			||||||
        if ($obj instanceof Page) {
 | 
					        if ($obj instanceof Page) {
 | 
				
			||||||
            //TODO $this->deleteComment($obj);
 | 
					            //TODO $this->deleteComment($obj);
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            //clear cache
 | 
				
			||||||
 | 
					            $this->grav['cache']->delete(md5('comments-stats' . $this->grav['cache']->getKey()));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,6 +8,9 @@ de:
 | 
				
			||||||
    DELETE: Löschen
 | 
					    DELETE: Löschen
 | 
				
			||||||
    SUCCESS: "Der Kommentar wurde erfolgreich gespeichert."
 | 
					    SUCCESS: "Der Kommentar wurde erfolgreich gespeichert."
 | 
				
			||||||
    COMMENTS: Kommentare
 | 
					    COMMENTS: Kommentare
 | 
				
			||||||
 | 
					    COMMENTS_STATS: Kommentare
 | 
				
			||||||
 | 
					    RECENT_COMMENTS: Neue Kommentare
 | 
				
			||||||
 | 
					    RECENT_PAGES: Kommentierte Seiten
 | 
				
			||||||
    EMAIL_NOT_CONFIGURED: Email nicht konfiguriert
 | 
					    EMAIL_NOT_CONFIGURED: Email nicht konfiguriert
 | 
				
			||||||
    NEW_COMMENT_EMAIL_SUBJECT: 'Neuer Kommentar für %1$s'
 | 
					    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>'
 | 
					    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
 | 
					    DELETE: Delete
 | 
				
			||||||
    SUCCESS: "Comment has been saved successfully."
 | 
					    SUCCESS: "Comment has been saved successfully."
 | 
				
			||||||
    COMMENTS: Comments
 | 
					    COMMENTS: Comments
 | 
				
			||||||
 | 
					    COMMENTS_STATS: Comments
 | 
				
			||||||
 | 
					    RECENT_COMMENTS: Recent comments
 | 
				
			||||||
 | 
					    RECENT_PAGES: Commented pages
 | 
				
			||||||
    EMAIL_NOT_CONFIGURED: Email not configured
 | 
					    EMAIL_NOT_CONFIGURED: Email not configured
 | 
				
			||||||
    NEW_COMMENT_EMAIL_SUBJECT: 'New comment on %1$s'
 | 
					    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>'
 | 
					    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>'
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										54
									
								
								templates/partials/recentcomments.html.twig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								templates/partials/recentcomments.html.twig
									
										
									
									
									
										Normal 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 %}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue