rmrevin / yii2-comments

Yii 2 comments extension
MIT License
52 stars 19 forks source link

Yii 2 Comments Module

License Latest Stable Version Latest Unstable Version Total Downloads

Code Status

Scrutinizer Code Quality Code Coverage Travis CI Build Status Dependency Status

Installation

composer require "rmrevin/yii2-comments:~1.4"

Configuration

In config /protected/config/main.php

<?php
return [
    // ...
    'modules' => [
        // ...
        'comments' => [
            'class' => 'rmrevin\yii\module\Comments\Module',
            'userIdentityClass' => 'app\models\User',
            'useRbac' => true,
        ]
    ],
    // ...
];

In your User model (or another model implements the interface IdentityInterface) need to implement the interface "\rmrevin\yii\module\Comments\interfaces\CommentatorInterface"

class User extends \yii\db\ActiveRecord
    implements
        \yii\web\IdentityInterface,
        \rmrevin\yii\module\Comments\interfaces\CommentatorInterface
{
    // ...

    public function getCommentatorAvatar()
    {
        return $this->avatar_url;
    }

    public function getCommentatorName()
    {
        return $this->name;
    }

    public function getCommentatorUrl()
    {
        return ['/profile', 'id' => $this->id]; // or false, if user does not have a public page
    }

    // ...
}

In auth manager add rules (if Module::$useRbac = true):

<?php
use \rmrevin\yii\module\Comments\Permission;
use \rmrevin\yii\module\Comments\rbac\ItsMyComment;

$AuthManager = \Yii::$app->getAuthManager();
$ItsMyCommentRule = new ItsMyComment();

$AuthManager->add($ItsMyCommentRule);

$AuthManager->add(new \yii\rbac\Permission([
    'name' => Permission::CREATE,
    'description' => 'Can create own comments',
]));
$AuthManager->add(new \yii\rbac\Permission([
    'name' => Permission::UPDATE,
    'description' => 'Can update all comments',
]));
$AuthManager->add(new \yii\rbac\Permission([
    'name' => Permission::UPDATE_OWN,
    'ruleName' => $ItsMyCommentRule->name,
    'description' => 'Can update own comments',
]));
$AuthManager->add(new \yii\rbac\Permission([
    'name' => Permission::DELETE,
    'description' => 'Can delete all comments',
]));
$AuthManager->add(new \yii\rbac\Permission([
    'name' => Permission::DELETE_OWN,
    'ruleName' => $ItsMyCommentRule->name,
    'description' => 'Can delete own comments',
]));

Updating database schema

After you downloaded and configured rmrevin/yii2-comments, the last thing you need to do is updating your database schema by applying the migrations:

In command line:

php yii migrate/up --migrationPath=@vendor/rmrevin/yii2-comments/migrations/

Usage

In view

<?php
// ...

use rmrevin\yii\module\Comments;

echo Comments\widgets\CommentListWidget::widget([
    'entity' => (string) 'photo-15', // type and id
]);

Parameters

 Module parameters

Widget parameters

Extending the package

 Extending Model files

Depending on which ones you need, you can set the modelMap config property:


    // ...
    'modules' => [
        // ...
        'comments' => [
            'class' => 'rmrevin\yii\module\Comments\Module',
            'userIdentityClass' => 'app\models\User',
            'useRbac' => true,
            'modelMap' => [
                'Comment' => '@app\comments\CommentModel'
            ]
        ]
    ],
    // ...

Attention: keep in mind that if you are changing the Comment model, the new class should always extend the package's original Comment class.

 Attaching behaviors and event handlers

The package allows you to attach behavior or event handler to any model. To do this you can set model map like so:


    // ...
    'modules' => [
        // ...
        'comments' => [
            'class' => 'rmrevin\yii\module\Comments\Module',
            'userIdentityClass' => 'app\models\User',
            'useRbac' => true,
            'modelMap' => [
                'Comment' => [
                    'class' => '@app\comments\CommentModel',
                    'on event' => function(){
                        // code here
                    },
                    'as behavior' => 
                        ['class' => 'Foo'],
            ]
        ]
    ],
    // ...

Extending View files

You can extend the view files supplied by this package using the theme component in the config file.

// app/config/web.php

'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@vendor/rmrevin/yii2-comments/widgets/views' => '@app/views/comments', // example: @app/views/comment/comment-form.php
            ],
        ],
    ],
],

 Extending Widgets

To extend the widget code and behavior you only have to extend the widget classes and call them instead of the package's ones.