list.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. use app\models\base\Comments;
  3. use app\helpers\Tree;
  4. /**
  5. * @var $model \app\models\News
  6. */
  7. if($model->comments!="Y") return "";
  8. $query = $model->getCommentsAll()->orderBy(['created_at'=>SORT_ASC])->andWhere(['visible'=>'Y']);
  9. ?>
  10. <h2 class="comments-title">
  11. Комментарии&nbsp;<sup><small><?=$query->count()?></small></sup>
  12. </h2>
  13. <div class="comments-list">
  14. <?
  15. $rows = $query->all();
  16. $items = [];
  17. $islike = Comments::getUserLike($model->id);
  18. if( is_array($islike) ){
  19. $islike = array_flip($islike);
  20. }else{
  21. $islike = array();
  22. }
  23. foreach( $rows as $item ){
  24. $citem = [];
  25. $citem['id'] = $item->id;
  26. if( isset($items[$item->parent_id]) ){
  27. $citem['parent_id'] = $item->parent_id;
  28. }else{
  29. // потеряный предок перенести в корень но можно и убить
  30. $citem['parent_id'] = 0;
  31. }
  32. $citem['children'] = 0;
  33. $item->islike = isset($islike[$item->id]);
  34. $citem['item'] = $item;
  35. $items[$item->id] = $citem;
  36. }
  37. // yii\helpers\VarDumper::dump($items,2);;
  38. $otree = new Tree();
  39. $tree = $otree->buildTree($items);
  40. $otree->drawTree($tree, $this);
  41. /*
  42. foreach( $rows as $item ){
  43. echo $this->render('_item', [
  44. 'model' => $item,
  45. ]);
  46. }
  47. */
  48. /*
  49. \yii\widgets\ListView::widget([
  50. "dataProvider" => new \yii\data\ActiveDataProvider([
  51. "query"=>$query,
  52. "pagination" =>[
  53. "pageSize"=>0
  54. ],
  55. ]),
  56. 'options' => [
  57. 'tag' => "ul",
  58. 'class' => 'list-wrapper',
  59. 'id' => 'list-wrapper',
  60. ],
  61. "itemOptions"=>[
  62. "tag"=>"li"
  63. ],
  64. "itemView" => function ($model, $key, $index, $widget) {
  65. return $this->render('_item', [
  66. 'model' => $model,
  67. 'key' => $key,
  68. 'index' => $index,
  69. 'widget' => $widget,
  70. ]);},
  71. "layout" => "{items}"
  72. ]);*/
  73. ?>
  74. </div>
  75. <?
  76. if( (new Comments())::$conf->isActive() ){
  77. ?>
  78. <div class="comments-form" id="commentsForm">
  79. <?=\app\widgets\Alert::widget()?>
  80. <form method="post">
  81. <input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->csrfToken?>">
  82. <input type="hidden" name="parent_id" value="0" id="commentParentId">
  83. <div class="form-group">
  84. <div class="row">
  85. <label for="fakename" class="control-label ml-4">Имя</label>
  86. <div class="col-sm mb-3"><input type="text" class="form-control" name="fakename" id="fakename" value="<?=Yii::$app->request->post("fakename")?>" maxlength="30"></div>
  87. </div>
  88. <textarea class="form-control" name="message" id="commentFormTextarea" cols="30" rows="5" aria-label="Сообщение"><?=Yii::$app->request->post("message")?></textarea>
  89. </div>
  90. <div class="form-group">
  91. <div class="">
  92. <?php
  93. $a = rand(1,9);
  94. $b = rand(1,9);
  95. $answer = $a + $b;
  96. Yii::$app->session->setFlash('capcha_a',$a);
  97. Yii::$app->session->setFlash('capcha_b',$b);
  98. ?>
  99. <label for="capcha">Решите пример: <i style="opacity: 0;">1</i><?=$a?><i style="opacity: 0;">0</i>+<i style="opacity: 0;">2</i><?=$b?><i style="opacity: 0;">0</i>=</label>
  100. <input type="text" name="capcha" style="outline: none;border: 1px solid #212529;width: 25px;" aria-label="capcha">
  101. </div>
  102. <button type="submit" class="btn btn-primary">Отправить</button>
  103. </div>
  104. </form>
  105. </div>
  106. <script>
  107. window.answer = function (elem){
  108. const author = elem.parentNode.parentNode.parentNode.dataset.author;
  109. const time = elem.parentNode.parentNode.parentNode.dataset.time;
  110. const parentId = elem.parentNode.parentNode.parentNode.dataset.id;
  111. const message = $(elem.parentNode.parentNode).find('p:not(.text-muted)').html().replace(/<span>.*<\/span>\n?/m, '').substring(0,60);
  112. let text = '[[' + author + " ("+ time +") " + message + "... ]]\n";
  113. $("#commentFormTextarea").html(text).focus()[0].setSelectionRange(text.length + 2, text.length+ 2);
  114. $("#commentParentId").val(parentId);
  115. }
  116. window.clike = function (g,id,el){
  117. $('#comment-'+id+' .btnlike').addClass('disabled').attr('aria-disabled',true);
  118. t = Number( $( '#cnt_'+id ).text() );
  119. $( '#cnt_'+id ).text(t+g);
  120. $.post( "clike", { id:id, grd:g, <?=Yii::$app->request->csrfParam?>:'<?=Yii::$app->request->csrfToken?>', nid:<?=$model->id?>}, function( data ) {
  121. console.log(data);
  122. });
  123. }
  124. </script>
  125. <?
  126. }