PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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