PHP code example of lyrasoft / feedback

1. Go to this page and download the library: Download lyrasoft/feedback library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lyrasoft / feedback example snippets


$this->lang->loadAllFromVendor('lyrasoft/feedback', 'ini');

// OR

$this->lang->loadAllFromVendor(\Lyrasoft\Feedback\FeedbackPackage::class, 'ini');

return [
    // ...

    __DIR__ . '/comment.seeder.php',
    __DIR__ . '/rating.seeder.php',

    // ...
];

foreach ($articleIds as $articleId) {
    foreach (range(1, random_int(2, 5)) as $i) {
        $userId = $faker->randomElement($userIds);

        $item = $commentService->addComment(
            $type,
            $articleId,
            $faker->paragraph(4),
            $userId,
            extra: function (Comment $item) use ($faker) {
                $item->title = $faker->sentence(2);
                $item->created = $faker->dateTimeThisYear();
                $item->ordering = $item->count() + 1;
            }
        );
    }
}

// Comment: Article
$menu->link('評論管理: 文章')
    ->to($nav->to('comment_list')->var('type', 'article'))
    ->icon('fal fa-comments');

// Or use translations

$menu->link($lang('feedback.comment.list.title', title: $lang('luna.article.title')))
    ->to($nav->to('comment_list')->var('type', 'article'))
    ->icon('fal fa-comments');

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */
$commentService->addComment(
    'flower', // Type
    $targetId, // Target ID
    'Comment Text...', // Content
    $user->id, // User ID
);

use Lyrasoft\Feedback\Entity\Comment;

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */
$commentService->addComment(
    'flower', // Type
    $targetId, // Target ID
    'Comment Text...', // Content
    $user->id, // User ID
    
    // The extra can be callback or array
    extra: function (Comment $comment) {
        $comment->rating = 5; // If user mark as 5 star
        $comment->nickname = 'Another nickname';
    }
);

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */
$newComment = $commentService->addComment(
    'flower', // Type
    $targetId, // Target ID
    'Comment Text...', // Content
    $user->id, // User ID or User entity
    extra: function (Comment $comment) use ($commentService) {
        $count = $commentService->countWith($comment);
        // OR
        $count = $comment->count();
    
        // This optional if you want to set ordering to one comment
        $comment->ordering = $count + 1;
    }
);

// Or reorder all comments of one target item.
$commentService->reorderComments(
    'flower', // Type
    $targetId, // Target ID
);

// OR

$commentService->reorderWith($newComment);

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */

$commentService->addInstantReply(
    $comment, // Can be ID or entity
    'Reply text...',
    $user->id, // User ID or User entity
);

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */

$childComment = $commentService->addSubReply(
    $parentComment, // Can be ID or entity
    'Reply text...',
    $user->id, // User ID or User entity
    extra: function (Comment $comment) {
        // Configure comment entity before save
    }
);

// Optional: if you want to reorder it.
$commentService->reorderComments(
    $parentComment->type, // Type
    $parentComment->targetId, // Target ID
    $parentComment->id, // Parent ID
);

// OR

$commentService->reorderWith($childComment);

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */
$newComment = $commentService->addComment(
    'flower', // Type
    $targetId, // Target ID
    'Comment Text...', // Content
    $user->id, // User ID or User entity
    extra: function (Comment $comment) use ($commentService) {
        $comment->rating = 4; // If user mark as 4 star
    }
);

// Update origin rated item
$orm->updateBatch(
    Target::class,
    [
        'rating_avg' => $commentService->calcAvgRatingWith($newComment),
        'rating_count' => $commentService->countWith($newComment),
    ],
    $targetId,
);

/** @var \Lyrasoft\Feedback\Service\CommentService $commentService */

// Create Comment Item
$comment = $commentService->createCommentItem($type, $targetId, 'text...', $user);

// count Comments
$count = $commentService->countComments($type, $targetId);

// Reorder 
$commentService->reorderComments($type, $targetId);

// Calc average rating
$avg = $commentService->calcAvgRating($type, $targetId);
$avg = $commentService->calcAvgRatingWith($comment);

// Check has commented
$comment = $commentService->getComment($type, $targetId);
$bool = $commentService->isCommented($type, $targetId);

// Remove comment
$commentService->removeComment($type, $targetId);

/** @var \Lyrasoft\Feedback\Service\RatingService $ratingService */
$ratingService->addRating(
    'flower', // Type
    $targetId, // Target ID
    $user->id, // User ID
);

use Lyrasoft\Feedback\Entity\Rating;

/** @var \Lyrasoft\Feedback\Service\RatingService $ratingService */
$ratingService->addRatingIfNotRated(
    'flower', // Type
    $targetId, // Target ID
    $user->id, // User ID
    
    // The extra can be callback or array
    extra: function (Rating $rating) {
        $rating->rank = 4.5; // If user mark as 4.5 star
    }
);

/** @var \Lyrasoft\Feedback\Service\RatingService $ratingService */
$newRating = $ratingService->addRating(
    'flower', // Type
    $targetId, // Target ID
    $user->id, // User ID or User entity
    extra: function (Rating $rating) use ($ratingService, $targetId) {
        $count = $ratingService->countWith($rating);
        // OR
        $count = $rating->count();
    
        // This optional if you want to set ordering to one comment
        $rating->ordering = $count + 1;
    }
);

// Update origin rated item
$orm->updateBatch(
    Target::class,
    ['rating' => $ratingService->calcAvgWith($rating)],
    $targetId,
);

// Or reorder all ratings of one target item.
$ratingService->reorderRatings(
    'flower', // Type
    $targetId, // Target ID
);
// OR
$ratingService->reorderWith($newRating);

/** @var \Lyrasoft\Feedback\Service\RatingService $ratingService */

// Calc average rank
$avg = $ratingService->calcAvgRank($type, $targetId);
$avg = $ratingService->calcAvgWith($rating);

// Get rating item or check is rated
$item = $ratingService->getRating($type, $targetId);
$bool = $ratingService->isRated($type, $targetId);

// Remove
$ratingService->removeRating($type, $targetId);

return [
    'feedback' => [
        // ...

        'rating' => [
            'ajax_type_protect' => true,
            'ajax_allow_types' => [
                'article',
                '...' // <-- Add your new types here
            ]
        ],
    ]
];

use Lyrasoft\Feedback\Repository\RatingRepository;

    // In any repository

    public function getFrontListSelector(?User $user = null): ListSelector
    {
        $selector = $this->getListSelector();

        if ($user && $user->isLogin()) {
            RatingRepository::joinRating(
                $selector,
                'item',
                $user->id,
                'item.id'
            );
        }
        
        // ...

php windwalker pkg:install lyrasoft/feedback -t lang