add level calculation
This commit is contained in:
		
							parent
							
								
									018a42a3dc
								
							
						
					
					
						commit
						6d458f8fa4
					
				
					 2 changed files with 10 additions and 13 deletions
				
			
		| 
						 | 
					@ -1,5 +1,7 @@
 | 
				
			||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Grav\Plugin;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Comment 
 | 
					class Comment 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private $id = 0;
 | 
					    private $id = 0;
 | 
				
			||||||
| 
						 | 
					@ -22,11 +24,11 @@ class Comment
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getContent($level = 0) {
 | 
					    public function getContent($level = 0) {
 | 
				
			||||||
		$comments = $this->value;
 | 
							$this->value['level'] = $level;
 | 
				
			||||||
		$comments['level'] = $level;
 | 
							$comments[] = $this->value;
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		foreach($this->children as $child) {
 | 
							foreach($this->children as $child) {
 | 
				
			||||||
			array_merge($comments, $child->getContent($level + 1));
 | 
								$comments[] = $child->getContent($level + 1);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return $comments;
 | 
							return $comments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										15
									
								
								comments.php
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								comments.php
									
										
									
									
									
								
							| 
						 | 
					@ -16,7 +16,8 @@ use RocketTheme\Toolbox\Event\Event;
 | 
				
			||||||
use RocketTheme\Toolbox\File\File;
 | 
					use RocketTheme\Toolbox\File\File;
 | 
				
			||||||
use Symfony\Component\Yaml\Yaml;
 | 
					use Symfony\Component\Yaml\Yaml;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require_once 'class\Comment.php';
 | 
					require_once '/html/apps/grav/user/plugins/comments/class/Comment.php';
 | 
				
			||||||
 | 
					use Grav\Plugin\Comment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CommentsPlugin extends Plugin
 | 
					class CommentsPlugin extends Plugin
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -404,7 +405,7 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
        $filename .= $this->grav['uri']->path() . '.yaml';
 | 
					        $filename .= $this->grav['uri']->path() . '.yaml';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $comments = $this->getDataFromFilename($filename)['comments'];
 | 
					        $comments = $this->getDataFromFilename($filename)['comments'];
 | 
				
			||||||
		$comments = setCommentLevels($comments);
 | 
							$comments = $this->setCommentLevels($comments);
 | 
				
			||||||
        //save to cache if enabled
 | 
					        //save to cache if enabled
 | 
				
			||||||
        $cache->save($this->comments_cache_id, $comments);
 | 
					        $cache->save($this->comments_cache_id, $comments);
 | 
				
			||||||
        return $comments;
 | 
					        return $comments;
 | 
				
			||||||
| 
						 | 
					@ -414,11 +415,8 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
     * Return the latest commented pages
 | 
					     * Return the latest commented pages
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private function setCommentLevels($comments) {
 | 
					    private function setCommentLevels($comments) {
 | 
				
			||||||
		$levels = array();
 | 
					 | 
				
			||||||
		$levelsflat = array();
 | 
							$levelsflat = array();
 | 
				
			||||||
		foreach($comments as $key => $comment) {
 | 
							foreach($comments as $key => $comment) {
 | 
				
			||||||
			//$comments[$key]['level'] = 0;
 | 
					 | 
				
			||||||
			$levels[$comment['parent']][] = $comment['id'];
 | 
					 | 
				
			||||||
			$levelsflat[$comment['id']]['parent'] = $comment['parent'];
 | 
								$levelsflat[$comment['id']]['parent'] = $comment['parent'];
 | 
				
			||||||
			$levelsflat[$comment['id']]['class'] = new Comment($comment['id'], $comments[$key]);
 | 
								$levelsflat[$comment['id']]['class'] = new Comment($comment['id'], $comments[$key]);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -426,11 +424,8 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
		$leveltree = array();
 | 
							$leveltree = array();
 | 
				
			||||||
		foreach($levelsflat as $id => $parent) {
 | 
							foreach($levelsflat as $id => $parent) {
 | 
				
			||||||
			$parent_id = $parent['parent'];
 | 
								$parent_id = $parent['parent'];
 | 
				
			||||||
			if(!isset($levelsflat[$parent_id]){
 | 
								if(!isset($levelsflat[$parent_id])){
 | 
				
			||||||
				$leveltree[$id] = $levelsflat[$id]['class'];
 | 
									$leveltree[$id] = $levelsflat[$id]['class'];
 | 
				
			||||||
				//$leveltree[$id] = array();
 | 
					 | 
				
			||||||
				//$leveltree[$id]['level'] = 0;
 | 
					 | 
				
			||||||
				//$leveltree[$id]['children'] = array();
 | 
					 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				$currentParent = $levelsflat[$parent_id]['class'];
 | 
									$currentParent = $levelsflat[$parent_id]['class'];
 | 
				
			||||||
				$currentChild = $levelsflat[$id]['class'];
 | 
									$currentChild = $levelsflat[$id]['class'];
 | 
				
			||||||
| 
						 | 
					@ -441,7 +436,7 @@ class CommentsPlugin extends Plugin
 | 
				
			||||||
		//reset comment values to nested order
 | 
							//reset comment values to nested order
 | 
				
			||||||
		$comments = array();
 | 
							$comments = array();
 | 
				
			||||||
		foreach($leveltree as $id => $comment) {
 | 
							foreach($leveltree as $id => $comment) {
 | 
				
			||||||
			array_merge($comments, $comment->getContent);
 | 
								$comments = array_merge($comments, $comment->getContent());
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return $comments;
 | 
							return $comments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue