netliva/symfony-commenter

Netliva Symfony 评论库

安装: 58

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

开放性问题: 0

类型:symfony-bundle

v2.2.5 2024-04-24 14:16 UTC

README

用于在项目中使用的评论脚本。需要Jquery、Bootstrap和Font-awsome。

安装

$ composer require netliva/symfony-commenter

激活Bundle

在config/bundles.php中

return [
    // ...
    Netliva\CommentBundle\NetlivaCommentBundle::class => ['all' => true],
];

将资源包含到项目中

安装资源

使用以下命令执行资源的安装

$ php bin/console assets:install

此命令会将生成的js和css文件包含到您的项目中,位于public/bundles/netlivamedialib目录。

// assets/js/app.js
import '../../public/bundles/netlivacomment/comment.css'
import '../../public/bundles/netlivacomment/comment'

用户实体类设置

在用户类的implements中添加AuthorInterface并添加_toString函数

namespace App\Entity;

// ...
use Netliva\CommentBundle\Entity\AuthorInterface;

/**
 * Staff
 */
class Users implements UserInterface, \Serializable, AuthorInterface
{
    // ...
	public function __toString ()
	{
		return $this->getName();
	}
    // ...
}

添加必要的设置;

# config/packages/netliva_commenter.yaml
doctrine:
  orm:
    resolve_target_entities:
      Netliva\CommentBundle\Entity\AuthorInterface: App\Entity\Users

# Opsiyonel: yorumlara bırakılan ifadeleri özelleştirmek isterseniz aşağıdakileri ekleyin
netliva_comment:
  emotions:
    like :  { emoji: '👍🏼', color: '#8A6749', desc: 'Beğen' }
    love :  { emoji: '❤️',  color: '#DD2E44', desc: 'Muhteşem' }
    haha :  { emoji: '😂', color: '#DD9E00', desc: 'Hahaha' }
    wow :   { emoji: '😮', color: '#DD9E00', desc: 'İnanılmaz' }
    sad :   { emoji: '😔', color: '#DD9E00', desc: 'Üzgün' }
    angry : { emoji: '😡', color: '#DA2F47', desc: 'Kızgın' } 
    # key olarak herhangi bir değer girilebilir
# config/routes/netliva_commenter.yaml
netliva_comment_route:
    resource: "@NetlivaCommentBundle/Controller/"
    type: annotation
    prefix: /netliva

使用

如以下示例所示,在需要添加评论区域的地方添加twig函数。

使用方式为commentbox("kanal_tanimi")。频道评论用于分组。

// Belli bir sayfanın altında yorumlar
{{ commentbox("page_"~page.id, options) }}

// Ürün yorumları
{{ commentbox("product_"~product.id, options) }}

// Chat alanları
{{ commentbox("room_1", options) }}

Options变量是一个键值对数组,可以发送以下键和值:

symfony事件

以下列出了一些触发的事件。您可以为这些事件编写订阅者来执行自己的代码。

触发的事件

创建订阅者

# aşağıdaki kodu service dosyanıza ekleyin 
services:
    # ...
    my_comment_box_user_image_subscriber:
        class: App\EventListener\CommentBoxUserImageSubscriber
        arguments: [ "@service_container", "@doctrine.orm.entity_manager", "@security.token_storage" ]
        tags:
            - { name: kernel.event_subscriber }
namespace App\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use Netliva\CommentBundle\Event\NetlivaCommenterEvents;
use Netliva\CommentBundle\Event\UserImageEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class CommentBoxUserImageSubscriber implements EventSubscriberInterface
{
    public function __construct () { }

    public static function getSubscribedEvents ()
    {
        return [
            NetlivaCommenterEvents::USER_IMAGE => 'getUserImage'
        ];
    }

    public function getUserImage (UserImageEvent $event)
    {
        // Kullanıcı mülküne ulaşın
        $user = $event->getAuthor();

        // kullanıcının profil fotoğrafının resmine ulaşın
        $imgPath = $user->getPhoto();

        if (!$imgPath || !file_exists($imgPath))
            return null;

        // fotoğrafın yolunu set edin, böylece gerekli yerlerde kullanıcı fotoğrafları gösterilir
        $event->setImage($imgPath);
    }

}

Js事件

以下列出了一些触发的jQuery事件。您可以在这些事件触发时执行自己的代码。

触发的事件

netliva:commenter:init

在创建评论区域后立即触发。

$(document).on("netliva:commenter:init", function(event, $comment_area, commenter){
    // $comment_area : Yorum alanı jQuery öğesi
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});

netliva:commenter:initline

在创建评论行后立即触发。

$(document).on("netliva:commenter:initline", function(event, $comment_line, commenter){
    // $comment_line : Yorum satırı jQuery öğesi
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});

netliva:commenter:send:click

在发送评论时触发。

$(document).on("netliva:commenter:send:click", function(event, $comment_area, commenter){
    // $comment_area : Yorum alanı jQuery öğesi
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});

netliva:commenter:send:complete

在评论发送完成后触发。

$(document).on("netliva:commenter:send:complete", function(event, $comment_area, jqXHR, textStatus, commenter){
    // $comment_area : Yorum alanı jQuery öğesi
    // jqXHR : jQuery ajax sonucu dönen XMLHttpRequest nesnesi (https://api.jqueryjs.cn/jQuery.ajax/#jqXHR) 
    // textStatus : Dönen sonucun durumunu kategorize eden bir dize ("success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror")
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});

netliva:commenter:send:success

如果评论发送成功则触发。

$(document).on("netliva:commenter:send:success", function(event, $comment_area, response, textStatus, jqXHR, commenter){
    // $comment_area : Yorum alanı jQuery öğesi
    // response : ajax sonrası dönen yanıt
    // jqXHR : jQuery ajax sonucu dönen XMLHttpRequest nesnesi (https://api.jqueryjs.cn/jQuery.ajax/#jqXHR) 
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});

netliva:commenter:send:error

如果评论发送失败则触发。

$(document).on("netliva:commenter:send:error", function(event, $comment_area, jqXHR, textStatus, commenter){
    // $comment_area : Yorum alanı jQuery öğesi
    // jqXHR : jQuery ajax sonucu dönen XMLHttpRequest nesnesi (https://api.jqueryjs.cn/jQuery.ajax/#jqXHR) 
    // commenter : Yorum aksiyonlarının yer aldığı javascript nesnesi 
});