PageRenderTime 72ms CodeModel.GetById 56ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/assets/snippets/jot/actions/treecomments.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 131 lines | 97 code | 18 blank | 16 comment | 25 complexity | 0cc4ae7550112406f78271ad7eff6613 MD5 | raw file
  1<?php
  2	// Display comments
  3	function treecomments_mode(&$object) {
  4		global $modx;
  5		
  6		$output_comments = NULL;
  7		// Check if viewing is allowed
  8		if($object->canView) {
  9			
 10			// View (Moderation)
 11			$view = 1;
 12			if ($object->isModerator) { 
 13				$view = $object->config["moderation"]["view"];
 14				$object->config["moderation"]["unpublished"] = $object->provider->GetCommentCount($object->config["docids"],$object->config["tagids"],0,$object->config["userids"]);
 15				$object->config["moderation"]["published"] = $object->provider->GetCommentCount($object->config["docids"],$object->config["tagids"],1,$object->config["userids"]);
 16				$object->config["moderation"]["mixed"] = $object->provider->GetCommentCount($object->config["docids"],$object->config["tagids"],2,$object->config["userids"]);
 17			}
 18			
 19			// Render Moderation Options
 20			if ($object->isModerator) { 
 21				$tpl = new CChunkie($object->templates["moderate"]);
 22				$tpl->AddVar('jot',$object->config);
 23				$object->config["html"]["moderate"] = $tpl->Render();
 24			}
 25			
 26			// Render subscription options
 27			$tpl = new CChunkie($object->templates["subscribe"]);
 28			$tpl->AddVar('jot',$object->config);
 29			$object->config["html"]["subscribe"] = $tpl->Render();
 30			
 31			// Get comments
 32			$array_comments = $object->provider->GetComments($object->config["docids"],$object->config["tagids"],$view,$object->config["upc"],$object->config["sortby"],0,0,$object->config["userids"]);	
 33			
 34			// Get comments count
 35			$count = count($array_comments);
 36			
 37			// Comment Numbering
 38			for ($i = 0; $i < $count; $i++) {
 39				$num = ($object->config["numdir"]) ? $count-$i :  $i+1;
 40				if (substr($object->config["sortby"], -1)=='a') $array_comments[$i]["postnumber"] = $count - $num + 1;
 41				else $array_comments[$i]["postnumber"] = $num;
 42			}
 43			
 44			// Get tree
 45			$tree = array();
 46			foreach ($array_comments as $row) {
 47				$tree[(int) $row['parent']][] = $row;
 48			}
 49			unset($array_comments);
 50			
 51			// Get first level comments count
 52			$commentTotal = count($tree[0]);
 53			
 54			// Get total number of comments
 55			$limit = $object->config["limit"];
 56			$commentTotal = ($limit>0 && $limit<$commentTotal) ? $limit : $commentTotal;
 57			$pagination = (isset($_GET[$object->config["querykey"]["navigation"]]) && $_GET[$object->config["querykey"]["navigation"]] == 0) ? 0 : $object->config["pagination"];
 58			
 59			// Apply pagination if enabled
 60			if ($pagination > 0) {
 61				$pageLength = ($limit>0 && $limit<$pagination) ? $limit : $pagination;
 62				$pageTotal = ceil($commentTotal / $pageLength);
 63				$pageCurrent = isset($_GET[$object->config["querykey"]["navigation"]]) ? intval($_GET[$object->config["querykey"]["navigation"]]): 1;
 64				if ( ($pageCurrent < 1) || ($pageCurrent > $pageTotal) ) { $pageCurrent = 1; };
 65				$pageOffset = (($pageCurrent*$pageLength)-$pageLength);
 66				$navStart = ($pageOffset+1);
 67				$navEnd = ($pageOffset+$pageLength) > $commentTotal ? $commentTotal : ($pageOffset+$pageLength);
 68			} else {
 69				$pageLength = $commentTotal;
 70				$pageOffset = 0;
 71				$pageTotal = 1;
 72				$pageCurrent = 1;
 73				$navStart = 0;
 74				$navEnd = $commentTotal;
 75			}
 76			
 77			if (is_array($tree[0]))  $tree[0] = array_slice($tree[0],$pageOffset,$pageLength);
 78			
 79			// Navigation
 80			$object->config['nav'] = array('total'=>$commentTotal,'start'=>$navStart,'end'=> $navEnd);
 81			$object->config['page'] = array('length'=>$pageLength,'total'=>$pageTotal,'current'=>$pageCurrent);
 82			
 83			// Render navigation
 84			if (($pagination > 0) && ($pageTotal > 1) ) {
 85				$tpl = new CChunkie($object->templates["navigation"]);
 86				$tplPage = $tpl->getTemplate($object->templates["navPage"]);
 87				$tplPageCur = $tpl->getTemplate($object->templates["navPageCur"]);
 88				$tplPageSpl = $tpl->getTemplate($object->templates["navPageSpl"]);
 89				$pages = '';
 90				for ($i = 1; $i <= $pageTotal; $i++) {
 91					$pages .= ($i == $pageCurrent) ? str_replace('[+jot.page.num+]',$i,$tplPageCur) : str_replace('[+jot.page.num+]',$i,$tplPage);
 92					if ($i< $pageTotal) $pages .= $tplPageSpl;
 93				}
 94				$tpl->template = str_replace('[+jot.pages+]',$pages,$tpl->template);
 95				$tpl->AddVar('jot',$object->config);
 96				$object->config["html"]["navigation"] = $tpl->Render();
 97			}
 98			
 99			function treeRender($tree, $pid, &$object, $depth) {
100				global $modx;
101				if (empty($tree[$pid])) return;
102				$res = '';
103				foreach ($tree[$pid] as $k=>$row) {
104					$chunk["rowclass"] = $object->getChunkRowClass($k+1,$row["createdby"]);
105					$tpl = new CChunkie($object->templates["comments"]);
106					$row["parentlink"] = $object->preserveUrl($modx->documentIdentifier,'',array_merge($object->_link,array('parent'=>$row["id"])));
107					$row["depth"] = $depth;
108					$tpl->AddVar('jot',$object->config);
109					$tpl->AddVar('comment',$row);
110					$tpl->AddVar('chunk',$chunk);
111					if (isset($tree[$row['id']]) &&  $object->config["depth"] > $depth) {
112						$tpl->AddVar('jot.wrapper',treeRender($tree, $row['id'], $object, $depth+1));
113					}
114					$res .= $tpl->Render();
115				}
116				return $res;
117			}
118			
119			/* Render comments */
120			$object->config["html"]["comments"] = treeRender($tree, 0 , $object, 1);
121			unset($tree);
122			
123			//onSetCommentsOutput event
124			if (null !== ($output = $object->doEvent("onSetCommentsOutput"))) return $output;
125			
126			$output_comments = $object->config["html"]["subscribe"] . $object->config["html"]["moderate"] .
127			$object->config["html"]["navigation"] . $object->config["html"]["comments"] . $object->config["html"]["navigation"];
128		}
129		if ($object->config["output"]) return $output_comments;
130	}
131?>