add level calculation
This commit is contained in:
		
							parent
							
								
									e8417c6a0e
								
							
						
					
					
						commit
						018a42a3dc
					
				
					 2 changed files with 80 additions and 0 deletions
				
			
		
							
								
								
									
										41
									
								
								class/Comment.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								class/Comment.php
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,41 @@
 | 
				
			||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Comment 
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    private $id = 0;
 | 
				
			||||||
 | 
					    private $value = array();
 | 
				
			||||||
 | 
					    private $parent = null;
 | 
				
			||||||
 | 
					    private $children = array();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public function __construct($id, $content) {
 | 
				
			||||||
 | 
							$this->id = $id;
 | 
				
			||||||
 | 
							$this->value = $content;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
					    public function addItem($obj, $key = null) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function deleteItem($key) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getItem($key) {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getContent($level = 0) {
 | 
				
			||||||
 | 
							$comments = $this->value;
 | 
				
			||||||
 | 
							$comments['level'] = $level;
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							foreach($this->children as $child) {
 | 
				
			||||||
 | 
								array_merge($comments, $child->getContent($level + 1));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return $comments;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public function setParent($parent) {
 | 
				
			||||||
 | 
							$this->parent = $parent;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    public function addSubComment($obj) {
 | 
				
			||||||
 | 
							$this->children[] = $obj;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										39
									
								
								comments.php
									
										
									
									
									
								
							
							
						
						
									
										39
									
								
								comments.php
									
										
									
									
									
								
							| 
						 | 
					@ -16,6 +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';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CommentsPlugin extends Plugin
 | 
					class CommentsPlugin extends Plugin
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    protected $route = 'comments';
 | 
					    protected $route = 'comments';
 | 
				
			||||||
| 
						 | 
					@ -402,11 +404,48 @@ 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);
 | 
				
			||||||
        //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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Return the latest commented pages
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    private function setCommentLevels($comments) {
 | 
				
			||||||
 | 
							$levels = array();
 | 
				
			||||||
 | 
							$levelsflat = array();
 | 
				
			||||||
 | 
							foreach($comments as $key => $comment) {
 | 
				
			||||||
 | 
								//$comments[$key]['level'] = 0;
 | 
				
			||||||
 | 
								$levels[$comment['parent']][] = $comment['id'];
 | 
				
			||||||
 | 
								$levelsflat[$comment['id']]['parent'] = $comment['parent'];
 | 
				
			||||||
 | 
								$levelsflat[$comment['id']]['class'] = new Comment($comment['id'], $comments[$key]);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							//get starting points (entries without valid parent = root element)
 | 
				
			||||||
 | 
							$leveltree = array();
 | 
				
			||||||
 | 
							foreach($levelsflat as $id => $parent) {
 | 
				
			||||||
 | 
								$parent_id = $parent['parent'];
 | 
				
			||||||
 | 
								if(!isset($levelsflat[$parent_id]){
 | 
				
			||||||
 | 
									$leveltree[$id] = $levelsflat[$id]['class'];
 | 
				
			||||||
 | 
									//$leveltree[$id] = array();
 | 
				
			||||||
 | 
									//$leveltree[$id]['level'] = 0;
 | 
				
			||||||
 | 
									//$leveltree[$id]['children'] = array();
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									$currentParent = $levelsflat[$parent_id]['class'];
 | 
				
			||||||
 | 
									$currentChild = $levelsflat[$id]['class'];
 | 
				
			||||||
 | 
									$levelsflat[$id]['class']->setParent($currentParent);
 | 
				
			||||||
 | 
									$levelsflat[$parent_id]['class']->addSubComment($currentChild);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							//reset comment values to nested order
 | 
				
			||||||
 | 
							$comments = array();
 | 
				
			||||||
 | 
							foreach($leveltree as $id => $comment) {
 | 
				
			||||||
 | 
								array_merge($comments, $comment->getContent);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return $comments;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Return the latest commented pages
 | 
					     * Return the latest commented pages
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue