2017 © Pedro Peláez
 

yii2-extension comments

Yii2 module for comments management

image

demi/comments

Yii2 module for comments management

  • Saturday, February 10, 2018
  • by demi
  • Repository
  • 4 Watchers
  • 5 Stars
  • 147 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 15 Versions
  • 14 % Grown

The README.md

Yii2-comments

Yii2 module for comments management
Live frontend demo, (*1)

Installation

Run, (*2)

composer require "demi/comments" "~1.0"

# Configurations

Create comments table:, (*3)

./yii migrate --migrationPath=@vendor/demi/comments/console/migrations

Create config file: /common/config/comments.php, (*4)

 'demi\comments\common\components\Comment',
    // Material types list: [materialTypeIntIndex => 'Material type name']
    'types' => [
        1 => 'Publication',
        2 => 'Product',
    ],
    // User model class name (by default "common\models\User")
    'userModelClass' => 'common\models\User',
    // Anonymous function to get user display name
    'getUsername' => function (Comment $comment) {
        // By default anon comment user name
        $name = $comment->user_name;
        // If comment author is registered user
        if ($comment->user) {
            // $comment->user by default relation to your \common\models\User
            $name = $comment->user->first_name . ' ' . $comment->user->last_name;
        }

        return Html::encode($name);
    },
    // Anonymous function to get user profile view url
    'getUserProfileUrl' => function (Comment $comment) {
        // You can check if app is backend and return url to user profile edit
        return $comment->isAnonymous ? null : ['/user/view', 'id' => $comment->user_id];
    },
    // Anonymous function to get user photo image src
    'getUserPhoto' => function (Comment $comment) {
        if ($comment->isAnonymous) {
            return Yii::$app->request->baseUrl . '/images/user_noimage.png';
        }

        // $comment->user by default relation to your \common\models\User
        return $comment->user->avatar_url;
    },
    // Anonymous function to get comment text
    // By default: nl2br(Html::encode($comment->text))
    'getCommentText' => function (Comment $comment) {
        return nl2br(Html::encode($comment->text));
    },
    // Anonymous function to get comment create time
    // By default: Yii::$app->formatter->asDatetime($comment->created_at)
    'getCommentDate' => function (Comment $comment) {
        return Yii::$app->formatter->asDatetime($comment->created_at);
    },
    // Anonymous function to get comment permalink.
    // By default: '#comment-' . $comment->id
    'getPermalink' => function (Comment $comment) {
        $url = '#comment-' . $comment->id;

        // If you have "admin" subdomain, you can specify absolute url path for use "goToComment" from admin page
        if ($comment->material_type == 1) {
            // http://site.com/publication/3221#comment-4
            $url = 'http://site.com/publication/' . $comment->material_id . $url;
        } elseif ($comment->material_type == 2) {
            // http://site.com/product/44212#comment-2
            $url = 'http://site.com/product/' . $comment->material_id . $url;
        }

        return $url;
    },
    'canDelete' => function (Comment $comment) {
        // Only admin can delete comment
        return Yii::$app->has('user') && Yii::$app->user->can('admin');
    },
    'canUpdate' => function (Comment $comment) {
        if (Yii::$app->has('user') && Yii::$app->user->can('admin')) {
            // Admin can edit any comment
            return true;
        } elseif ($comment->isAnonymous) {
            // Any non-admin user cannot edit any anon comment
            return false;
        }

        // Comment can be edited by author at anytime
        // todo You can calc $comment->created_at and eg. allow comment editing by author within X hours after posting
        return Yii::$app->has('user') && Yii::$app->user->id == $comment->user_id;
    },
    // Anonymous function to set siteKey for reCAPTCHA widget
    // @see https://www.google.com/recaptcha/admin
    // You can set string value instead function
    'reCaptchaSiteKey' => function () {
        return Yii::$app->params['reCAPTCHA.siteKey'];
    },
    'reCaptchaSecretKey' => function () {
        return Yii::$app->params['reCAPTCHA.secretKey'];
    },

    // FOR FIRST RECOMMENDED USE DEFAULT VIEW FILES WITH EXAMPLE CSS STYLES, CUSTOMIZE TEMPLATES AFTER TESTING

    // But after checking extension is working you can customize view templates, useful bash copy commands in project root dir:
    /*
        mkdir "frontend/views/comment"
        cp "vendor/demi/comments/frontend/widgets/views/comments.php" "frontend/views/comment/myCustomCommentsListView.php"
        cp "vendor/demi/comments/frontend/widgets/views/_comment.php" "frontend/views/comment/_myCustomCommentItem.php"
        cp "vendor/demi/comments/frontend/widgets/views/form.php" "frontend/views/comment/myCustomCommentForm.php"
    */
    // ALSO: WHILE CUSTOMIZING YOU SHOULD SAVE SOME HTML CLASS SELECTORS!
    // SEE ALL SELECTORS HERE: vendor/demi/comments/frontend/widgets/assets/js/comments.js:61
    // OR YOU CAN SET YOUR CUSTOM SELECTORS FOR WIDGET CONFIG 'clientOptions': \demi\comments\frontend\widgets\Comments::$clientOptions

    // Path to view file for render comments list (
    and
  • tags + nested) // 'listView' => '@frontend/views/comment/myCustomCommentsListView', // Path to view file for render each comment item (inside the
  • tag) // 'itemView' => '@frontend/views/comment/_myCustomCommentItem', // Path to view file for render new comment form // 'formView' => '@frontend/views/comment/myCustomCommentForm', ]; ``` Include config file to `/common/config/main.php`: ```php [ 'comment' => require(__DIR__ . '/comments.php'), ], ]; ``` Configure frontend `/frontend/config/main.php`: ```php return [ 'modules' => [ 'comment' => [ 'class' => 'demi\comments\frontend\CommentModule', ], ], 'components' => [ 'assetManager' => [ 'bundles' => [ // Extend default CommentsAsset: attaching included example css-styles for comments tree. // After testing you can remove this lines and make own css-styles you want. 'demi\comments\frontend\widgets\CommentsAsset' => [ 'css' => [ 'css/comments.css', ], ], ], ], ], ]; ``` Configure backend `/backend/config/main.php`: ```php return [ 'modules' => [ 'comment' => [ 'class' => 'demi\comments\backend\CommentModule', ], ], ]; ``` # Usage --- For example, you wish add comments to model "Publication", so append publication view file `/frontend/views/publication/view.php`: ```php

    Comments:

    = \demi\comments\frontend\widgets\Comments::widget([ // From config file "types" array key (1 => 'Publication') 'materialType' => 1, // You Publication model unique identifier // If you don't have this value simply type your unique key eg: "123", // for clarify code you can make const ABOUT_PAGE_COMMENTS_ID = 123 'materialId' => $model->id, // RECOMENDED FOR FIRST RUN COMMENTED OPTIONS BELOW AND CUSTOMIZED IT AFTER TESTING // HTML-options for main comments ul-tag 'options' => [ 'class' => 'comments list-unstyled', ], // HTML-options for nested comments ul-tag 'nestedOptions' => [ 'class' => 'comments reply list-unstyled', ], // jQuery-plugin options, see all options: vendor/demi/comments/frontend/widgets/assets/js/comments.js:55 'clientOptions' => [ 'deleteComfirmText' => 'Are you sure you want to delete this comment?', 'updateButtonText' => 'Update', 'cancelUpdateButtonText' => 'Cancel', ], // Maximum nested level. If level reached - nested comments will be outputted without ul-tag. 'maxNestedLevel' => 6, // Url for permalink (without '#') 'materialViewUrl' => Url::to(['view', 'id' => $model->id]), // ActiveForm configuration 'formConfig' => [ // This is required config attributes, you should save it 'enableClientValidation' => true, 'enableAjaxValidation' => false, ], ]) ?>

    # Backedn (admin page)

    Navigate to http://admin.site.com/comment/manage/index
    ie: <you backend url>/comment/manage/index, (*5)

The Versions

10/02 2018

dev-master

9999999-dev https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU GPL-3.0-or-later

The Requires

 

yii2 module widget comments management comment

10/02 2018

1.0.5

1.0.5.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GPL-3.0-or-later

The Requires

 

yii2 module widget comments management comment

18/06 2017

1.0.4

1.0.4.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

17/06 2017

1.0.3

1.0.3.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

17/06 2017

1.0.2

1.0.2.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

16/06 2017

1.0.1

1.0.1.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

15/06 2017

1.0.0

1.0.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

06/08 2015

0.6.0

0.6.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

05/08 2015

0.5.0

0.5.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

11/07 2015

0.4.2

0.4.2.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

11/07 2015

0.4.1

0.4.1.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

11/07 2015

0.4.0

0.4.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

10/07 2015

0.3.0

0.3.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

09/07 2015

0.2.0

0.2.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment

06/07 2015

0.1.0

0.1.0.0 https://github.com/demisang/yii2-comments#readme

Yii2 module for comments management

  Sources   Download

GNU

The Requires

 

yii2 module widget comments management comment