PHP code example of skeeks / cms-comments

1. Go to this page and download the library: Download skeeks/cms-comments library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

skeeks / cms-comments example snippets


yii migrate --migrationPath=@skeeks/cms/comments/migrations/

'bootstrap' => ['comments'],
'components' =>
[
'i18n' => [
    'translations'  =>
    [
        'skeeks/comments' =>
        [
            'class'             => 'yii\i18n\PhpMessageSource',
            'basePath'          => '@skeeks/cms/comments/messages',
            'fileMap' => [
                'skeeks/comments' => 'main.php',
            ],
        ]
    ],
],
],

'modules' =>
[
	'comments' =>
	[
	    'class' => 'skeeks\cms\comments\CommentsModule',
	    //'maxNestedLevel'          => 5
        //'onlyRegistered'          => false
        //'orderDirection'          => SORT_DESC
        //'nestedOrderDirection'    => SORT_ASC
        //'displayAvatar'           => true
        //'commentsPerPage'         => 5,
        'on commentAdded' => function(\skeeks\cms\comments\events\CommentEvent $e)
        {
            /**
             * @var $comment \skeeks\cms\comments\models\CmsComment
             */
            $comment = $e->comment;
            $comment->user;
            $comment->model;
            $comment->model_id;
        },

        'on commentDeleted' => function(\skeeks\cms\comments\events\CommentEvent $e)
        {
            //...
        }
	]
]

public function behaviors()
{
  return [
    'comments' => [
      'class' => 'skeeks\cms\comments\behaviors\CommentsBehavior'
    ]
  ];
}

'on commentAdded' => function(\skeeks\cms\comments\events\CommentEvent $e)
{
	/**
	 * @var $comment \skeeks\cms\comments\models\CmsComment
	 * @var $user \common\models\User
	 * @var $element \skeeks\cms\models\CmsContentElement
	 */
	$comment = $e->comment;
	$user = $comment->user;

	/*$user->appUser->total_comments = $user->appUser->total_comments + 1;
	if (!$user->appUser->save())
	{
	    \Yii::error("Not update user total comments: {$user->id}", 'project');
	}*/

	\Yii::error(\skeeks\cms\models\CmsContentElement::tableName(), 'project');
	\Yii::error(\yii\helpers\Json::encode($comment->toArray()), 'project');

	if ($comment->model == \skeeks\cms\models\CmsContentElement::tableName())
	{
	    $element = \skeeks\cms\models\CmsContentElement::findOne($comment->model_id);
	    if ($element && $element->relatedPropertiesModel->hasAttribute('comments'))
	    {
		$totalComments = \skeeks\cms\comments\models\CmsComment::find()->where([
		    'model_id' => $element->id,
		])->andWhere(['model' => \skeeks\cms\models\CmsContentElement::tableName()])->count();
		$element->relatedPropertiesModel->setAttribute('comments', $totalComments);
		//$element->relatedPropertiesModel->setAttribute('comments', ((int) $element->relatedPropertiesModel->getAttribute('comments') + 1));

		if (!$element->relatedPropertiesModel->save())
		{
		    \Yii::error("Not update element total comments: {$element->id}", 'project');
		}
	    } else
	    {
		\Yii::error("Element not found or not have property comments: {$element->id}", 'project');
	    }
	}
},

'on commentDeleted' => function(\skeeks\cms\comments\events\CommentEvent $e)
{
....
	    

use skeeks\cms\comments\widgets\CommentsWidget;

echo Comments::widget(['model' => $pageKey]);

echo Comments::widget(['model' => 'post', 'model_id' => 1]);

echo Post::findOne(10)->displayComments();

php yii lang/translate-app @skeeks/cms/comments/messages/ru/main.php @skeeks/cms/comments/messages/ main.php

php composer.phar