2017 © Pedro PelĂĄez
 

yii2-extension yii2-comments

Comments module for Yii2

image

comments/yii2-comments

Comments module for Yii2

  • Wednesday, January 3, 2018
  • by Artem163397
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii 2 Comments Module, (*1)

Installation


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

Configuration

In config /protected/config/main.php, (*2)

<?php
return [
    // ...
    'modules' => [
        // ...
        'comments' => [
            'class' => 'teo_crm\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 "\teo_crm\yii\module\Comments\interfaces\CommentatorInterface", (*3)

class User extends \yii\db\ActiveRecord
    implements
        \yii\web\IdentityInterface,
        \teo_crm\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):, (*4)

<?php
use \teo_crm\yii\module\Comments\Permission;
use \teo_crm\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 teo_crm/yii2-comments, the last thing you need to do is updating your database schema by applying the migrations:, (*5)

In command line:, (*6)

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

Usage

In view, (*7)

<?php
// ...

use teo_crm\yii\module\Comments;

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

Parameters

 Module parameters

  • userIdentityClass (required, string) The user identity class that Yii2 uses to provide identity information about the users in the App., (*8)

  • useRbac (optional, boolean) Default TRUE. Defines if the comment system will use Rbac validation to check the comment permissions when trying to update, delete or add new comments., (*9)

  • modelClasses (optional, string[]) Stores the user defined model classes that will be used instead of the default ones in the comment system. Must have a key => classname format. e.g. 'Comment' => '@app\comments\CommentModel', (*10)

Widget parameters

  • entity (required, string) The entity that will identify the comments under on section from all the comments in this module., (*11)

  • theme (optional, string) In case you want to use a theme in your application you should define here it's location., (*12)

  • viewParams (optional, array) Data that will be sent directly into the widget view files. Must have a key => data format. The key will be the variable name in the view. The variable CommentsDataProvider it's already taken., (*13)

  • options (optional, array) Default ['class' => 'comments-widget']. Option data array that will be sent into the div holding the comment system in your views., (*14)

  • pagination (optional, array) Pagination configuration that will be used in the comment panel. Default data:, (*15)

public $pagination = 
    [
        'pageParam' => 'page',
        'pageSizeParam' => 'per-page',
        'pageSize' => 20,
        'pageSizeLimit' => [1, 50],
    ];
  • sort (optional, array) Type of sorting used to retrieve the comments into the panel. Can be sorted by any of the columns defined in the comment table. Default data:
        'defaultOrder' => [
            'id' => SORT_ASC,
        ],
  • showDeleted (optional, boolean) Default True. Defines if the comments panel will show a message indicating the deleted comments., (*16)

  • showCreateForm (optional, boolean) Default True. Will show or hide the form to add a comment in this panel., (*17)

Extending the package

 Extending Model files

Depending on which ones you need, you can set the modelMap config property:, (*18)


// ... 'modules' => [ // ... 'comments' => [ 'class' => 'teo_crm\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., (*19)

 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:, (*20)


// ... 'modules' => [ // ... 'comments' => [ 'class' => 'teo_crm\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., (*21)

// app/config/web.php

'components' => [
    'view' => [
        'theme' => [
            'pathMap' => [
                '@vendor/teo_crm/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., (*22)

The Versions

03/01 2018

dev-master

9999999-dev

Comments module for Yii2

  Sources   Download

MIT

The Requires

 

module yii widget comment