presetshare/yii2-entity-like

User-like features for Yii2 Applications

Installs: 50

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Type:yii2-extension

dev-main 2023-02-06 11:16 UTC

This package is auto-updated.

Last update: 2024-11-06 14:48:58 UTC


README

Total Downloads License

❤️ User-like features for Yii2 Applications.

How yii2-entity-like works

Installation

The preferred way to install this extension is through composer.

Either run

php composer require --prefer-dist presetshare/yii2-entity-like "@dev"

or add

"presetshare/yii2-entity-like": "@dev"

to the require section of your composer.json file.

Usage

Migrations

php yii migrate/up --migrationPath=@vendor/presetshare/yii2-entity-like/migrations

Module config

'modules' => [
    'entityLike' => [
        'class' => \presetshare\yii2\likes\Module::class,
        'entities' => [
            \app\models\Post::class
        ],
    ],
],

Model behavior

class Post extends \yii\db\ActiveRecord
{
    ...
    public function behaviors()
    {
        return [
            [
                'class' => \presetshare\yii2\likes\behaviors\LikeableEntityBehavior::class,
                'authorAttribute' => 'user_id'
            ]
        ];
    }
    ...
}

Query trait

class PostQuery extends \yii\db\ActiveQuery
{
    use \presetshare\yii2\likes\traits\EntityLikeQueries;
    ...
}

Queries

$dataProvider = new ActiveDataProvider([
    'query' => Post::find()
        ->withLikesCount()
        ->withHasMyLike()
        //->onlyLikedByMe()

        ->orderBy(['likes_count' => SORT_DESC, 'post.id' => SORT_DESC]),
    'pagination' => [
        'pageSize' => 15,
        'defaultPageSize' => 15,
    ],
]);

$posts = $dataProvider->getModels();

$post = Post::find()->where(['post.id' => $id])->withLikesCount()->withHasMyLike()->one();

$likesCount = $post->likes_count;
$hasMyLike = $post->has_my_like;

Widget

<?= \presetshare\yii2\likes\widgets\EntityLikeWidget::widget([
    'model' => $post,
    'customClass' => 'my-entity-like-button', // default null
    'buttonText' => '❤️', // default 👍
    'registerJS' => true, // default true
    'registerCSS' => true // default false
]); ?>

Events

<?php
// EntityLike::EVENT_AFTER_FIRST_LIKE_TOGGLE
// EntityLike::EVENT_AFTER_LIKE_TOGGLE

\yii\base\Event::on(
    \presetshare\yii2\likes\models\EntityLike::class,
    \presetshare\yii2\likes\models\EntityLike::EVENT_AFTER_FIRST_LIKE_TOGGLE,
    function (\presetshare\yii2\likes\events\EntityLikeEvent $event) {

        $entityLike = $event->sender;

        $likerId = $entityLike->user_id;
        $entityAuthorId = $event->entityAuthorId;
        $entityId = $entityLike->entity_id;
        $action = $event->action; // like/unlike
        $entityClass = $event->entityClass; // e.g. \app\models\Post

        if ($likerId == $entityAuthorId) return;

        $liker = \app\models\User::findOne($likerId);
        $entityAuthor = \app\models\User::findOne($entityAuthorId);

        if ($entityClass == \app\models\Post::class) {
            $post = \app\models\Post::findOne($entityId);

            \Yii::info(
                'First ' . $action . ' for post ' . $post->name .
                ' from user ' . $liker->username . ' to user ' . $entityAuthor->username);
        }
    }
);

\yii\base\Event::on(
    \presetshare\yii2\likes\models\EntityLike::class,
    \presetshare\yii2\likes\models\EntityLike::EVENT_AFTER_LIKE_TOGGLE,
    function (\presetshare\yii2\likes\events\EntityLikeEvent $event) {

        $entityLike = $event->sender;

        $likerId = $entityLike->user_id;
        $entityAuthorId = $event->entityAuthorId;
        $entityId = $entityLike->entity_id;
        $action = $event->action; // like/unlike
        $entityClass = $event->entityClass; // e.g. \app\models\Post

        if ($likerId == $entityAuthorId) return;

        $liker = \app\models\User::findOne($likerId);
        $entityAuthor = \app\models\User::findOne($entityAuthorId);

        if ($entityClass == \app\models\Post::class) {
            $post = \app\models\Post::findOne($entityId);

            \Yii::info(
                $action . ' for post ' . $post->name .
                ' from user ' . $liker->username . ' to user ' . $entityAuthor->username);
        }
    }
);

Support

"Buy Me A Coffee"