示例#1
0
    /**
     * Output tree of comments
     * @param array $comments Array of Comment
     * @param int $parent_id 
     * @param array $options 
     */
    public static function outCommentsTree($comments, $parent_id, $options)
    {
        if (!isset($options) || !is_object($options)) {
            $showReplies = isset($options['showReplies']) ? $options['showReplies'] : true;
            $showReplyButton = isset($options['showReplyButton']) ? $options['showReplyButton'] : true;
            $postID = isset($options['postID']) ? $options['postID'] : false;
            $options = (object) compact('showReplies', 'showReplyButton', 'postID');
        }
        if (isset($comments[$parent_id])) {
            foreach ($comments[$parent_id] as $comment) {
                if (is_null($comment->user)) {
                    $username = '******';
                    $avatar = new Asset();
                    $avatar->assetable_type = Asset::ASSETABLE_USER;
                    $imageUrl = $avatar->getDefaultFileUrl();
                    $userUrl = false;
                } else {
                    $username = $comment->user->getDisplayName();
                    $avatar = $comment->user->getAsset();
                    $imageUrl = $avatar->getFileUrl();
                    $userUrl = $comment->user->getUrl();
                }
                $commentDate = Yii::$app->formatter->asDate(strtotime($comment->created_at), 'd MMMM Y HH:mm');
                $repliesCommentsCount = isset($comments[$comment->id]) ? count($comments[$comment->id]) : 0;
                $classRepliesCount = $repliesCommentsCount == 0 ? 'no-replies' : '';
                $textRepliesCount = $repliesCommentsCount == 0 ? '' : $repliesCommentsCount;
                $isReply = $parent_id == 0 ? false : true;
                $own = isset(Yii::$app->user->id) && Yii::$app->user->id == $comment->user_id ? 'yes' : 'no';
                $rating = $comment->getRating();
                $ratingUpClass = '';
                $ratingDownClass = '';
                if (!Yii::$app->user->isGuest && Yii::$app->user->id != $comment->user_id) {
                    $userRating = $comment->getUserVote();
                    if ($userRating == 1) {
                        $ratingUpClass = 'voted';
                    } elseif ($userRating == -1) {
                        $ratingDownClass = 'voted';
                    }
                } else {
                    $ratingUpClass = 'disable';
                    $ratingDownClass = 'disable';
                }
                $commentLevelClass = $parent_id == 0 ? 'lvl-one' : '';
                if ($parent_id == 0 && $options->postID && $comment->getCommentableType() == Comment::COMMENTABLE_POST) {
                    $post = Post::findOne($comment->commentable_id);
                    if (isset($post->id) && $post->id !== $options->postID) {
                        $options->postID = $post->id;
                        ?>
                        <div class="comment-theme">
                            <div class="theme-label">Комментарии по теме:</div>
                            <div class="theme-link">
                                <a href="<?php 
                        echo $post->getUrl();
                        ?>
" data-pjax="0"><?php 
                        echo $post->title;
                        ?>
</a>
                            </div>
                        </div>
                        <?php 
                    }
                }
                $adminLink = '';
                if (Yii::$app->user->can('admin')) {
                    $adminLink = '<a class="admin-view-link" target="_blank" href="/admin/comment/' . $comment->id . '" data-pjax="0"></a>';
                }
                ?>
                <div id="comment-<?php 
                echo $comment->id;
                ?>
" class="comment <?php 
                echo $commentLevelClass;
                ?>
" 
                    data-own="<?php 
                echo $own;
                ?>
"
                    data-comment-id="<?php 
                echo $comment->id;
                ?>
"
                    data-commentable-type="<?php 
                echo $comment->getCommentableType();
                ?>
"
                    data-commentable-id="<?php 
                echo $comment->commentable_id;
                ?>
" >
                    <div class="comment-user">
                        <div class="user-photo">
                            <?php 
                if ($userUrl) {
                    ?>
                                <a href="<?php 
                    echo $userUrl;
                    ?>
" data-pjax="0">
                            <?php 
                }
                ?>
                                <img src="<?php 
                echo $imageUrl;
                ?>
">
                            <?php 
                if ($userUrl) {
                    ?>
                                </a>
                            <?php 
                }
                ?>

                        </div>
                        <div class="user-info">
                            <div class="user-name">
                                <?php 
                if ($userUrl) {
                    ?>
                                    <a href="<?php 
                    echo $userUrl;
                    ?>
" data-pjax="0">
                                <?php 
                }
                ?>
                                    <?php 
                echo $username;
                ?>
                                <?php 
                if ($userUrl) {
                    ?>
                                    </a>
                                <?php 
                }
                ?>
                            </div>
                            <div class="post-time"><?php 
                echo $commentDate;
                ?>
</div>
                        </div>
                    </div>
                    <div class="comment-links">
                        <div class="rating-counter">
                            <a href="javascript:void(0)" class="rating-up <?php 
                echo $ratingUpClass;
                ?>
" data-id="<?php 
                echo $comment->id;
                ?>
" data-type="comment"></a>
                            <div class="rating-count <?php 
                echo $rating >= 0 ? 'blue' : 'red';
                ?>
"><?php 
                echo $rating;
                ?>
</div>
                            <a href="javascript:void(0)" class="rating-down <?php 
                echo $ratingDownClass;
                ?>
" data-id="<?php 
                echo $comment->id;
                ?>
" data-type="comment"></a>
                        </div>
                        <?php 
                if (!Yii::$app->user->isGuest) {
                    ?>
                            <a href="<?php 
                    echo Url::to('/complain/' . $comment->id);
                    ?>
" class="button-complain" title="Пожаловаться" data-pjax="0"></a>
                        <?php 
                }
                ?>
                        <?php 
                if (!Yii::$app->user->isGuest && $options->showReplyButton) {
                    ?>
                            <a href="javascript:void(0)" class="button-reply" title="Ответить"></a>
                        <?php 
                }
                ?>
                        <?php 
                echo $adminLink;
                ?>
                    </div>
                    <div class="comment-body">
                        <?php 
                echo $comment->getContent();
                ?>
                    </div>
                    <?php 
                if ($repliesCommentsCount > 0) {
                    ?>
                    <div class="comment-replies">
                        <a class="replies-toggle-btn toggle-button toggle-<?php 
                    echo $options->showReplies ? 'hide' : 'show';
                    ?>
" data-target="comment-replies-content-<?php 
                    echo $comment->id;
                    ?>
" href="javascript:void(0)">
                            <div class="toggle-text">
                                <span><?php 
                    echo $options->showReplies ? 'Скрыть' : 'Показать';
                    ?>
</span> ответы
                            </div>
                            <div class="toggle-icon"></div>
                        </a>
                        <div id="comment-replies-content-<?php 
                    echo $comment->id;
                    ?>
" class="toggle-content <?php 
                    echo $options->showReplies ? 'visible' : '';
                    ?>
">
                        <?php 
                    self::outCommentsTree($comments, $comment->id, $options);
                    ?>
                        </div>
                    </div>
                    <?php 
                }
                ?>
                </div>
                <?php 
            }
        }
    }