Commit b2cac3f9 authored by Marcello Pivanti's avatar Marcello Pivanti
Browse files

Initial Commit

parents
amos-comments
----------------
Extension for comment a content like news, events, etc...
Installation
------------
1 The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
composer require elitedivision/amos-comments
```
or add this row
```
"elitedivision/amos-comments": "dev-master"
```
to the require section of your `composer.json` file.
2 Add module to your main config in backend:
```php
<?php
'modules' => [
'comments' => [
'class' => 'elitedivision\amos\comments\AmosComments',
'modelsEnabled' => [
/**
* Add here the classnames of the models where you want the comments
* (i.e. 'elitedivision\amos\events\models\Event')
*/
],
// the following are mandatory fields
'displayNotifyCheckbox' => true, // if the notify checkbox in the accordion must be shown (if hidden, the notify checkbox is selected)
'accordionOpenedByDefault' => false, // if the accordion must be opened by default
],
],
```
Also, add these lines to your bootstrap:
```php
<?php
'bootstrap' => [
'comments',
],
```
3 Add the view component to your main config in common:
```php
<?php
'components' => [
'view' => [
'class' => 'elitedivision\amos\core\components\AmosView',
],
],
```
4 Apply migrations
```bash
php yii migrate/up --migrationPath=@vendor/elitedivision/amos-comments/src/migrations
```
or add this row to your migrations config in console:
```php
<?php
return [
'@vendor/elitedivision/amos-comments/src/migrations',
];
```
5 Implement the CommentInterface in your model
```php
<?php
use elitedivision\amos\comments\models\CommentInterface;
/**
* Implement the CommentInterface
*/
class MyClass implements CommentInterface
/**
* Add the required method that must return boolean
*/
public function isCommentable()
{
return true;
}
```
6 Add your model to the modulesEnables in the module config in backend/config/main.php
```php
<?php
'modules' => [
'comments' => [
'class' => 'elitedivision\amos\comments\AmosComments',
'modelsEnabled' => [
'class_namespace\MyClass'
]
],
],
```
7 disable mail notifications
```php
<?php
'modules' => [
'comments' => [
'class' => 'elitedivision\amos\comments\AmosComments',
'enableMailsNotification' => false,
'modelsEnabled' => [
'class_namespace\MyClass'
]
],
],
```
* **htmlMailContent** - string/array
change the content of the mail of notification when you insert a comment
you can insert an array
```php
'comments' => [
'htmlMailContent' => [
'elitedivision\amos\news\models\News' => '@backend/mail/comment/content_news',
'elitedivision\amos\discussioni\models\DiscussioniTopic' => '@backend/mail/comment/content_discussioni',
'elitedivision\amos\documenti\models\Documenti' => '@backend/mail/comment/content_documenti'
],
```
or a string if the conente is valid for all contents(news/discussioni/docuemnts/ecc..)
```php
'comments' => [
'htmlMailContent' => '@backend/mail/comment/content_news'
]
```
\ No newline at end of file
{
"name": "arter/amos-comments",
"description": "Comments for contents - plugin",
"keywords": [
"amos",
"yii2",
"comments"
],
"type": "plugin",
"license": "BSD-3-Clause",
"require": {
"php": ">=5.4.0",
"arter/amos-core": "^1.9.60",
"arter/amos-admin": ">=1.7",
"arter/amos-attachments": "^1.1.9"
},
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
},
"autoload": {
"psr-4": {
"elitedivision\\amos\\comments\\": "src"
}
}
}
\ No newline at end of file
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\comments
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\comments;
use elitedivision\amos\comments\components\CommentComponent;
use elitedivision\amos\comments\models\Comment;
use elitedivision\amos\comments\models\CommentReply;
use elitedivision\amos\core\components\AmosView;
use elitedivision\amos\core\module\AmosModule;
use elitedivision\amos\core\module\ModuleInterface;
use yii\base\BootstrapInterface;
use yii\base\Event;
/**
* Class AmosComments
* @package elitedivision\amos\comments
*/
class AmosComments extends AmosModule implements ModuleInterface, BootstrapInterface
{
public static $CONFIG_FOLDER = 'config';
/**
* @var string|boolean the layout that should be applied for views within this module. This refers to a view name
* relative to [[layoutPath]]. If this is not set, it means the layout value of the [[module|parent module]]
* will be taken. If this is false, layout will be disabled within this module.
*/
public $layout = 'main';
/**
* @inheritdoc
*/
public $controllerNamespace = 'elitedivision\amos\comments\controllers';
public $newFileMode = 0666;
public $name = 'Comments';
public $layoutInverted = false;
/**
* @var array $modelsEnabled
*/
public $modelsEnabled = [];
public $maxCommentAttachments = 5;
public $enableMailsNotification = true;
/**
* @var bool $enableUserSendMailCheckbox If true enable the checkbox in the comments forms with the user can select if send or not the notify mail.
*/
public $enableUserSendMailCheckbox = true;
/**
* This is the html used to render the subject of the e-mail. In the view is available the variable $profile
* that is an instance of 'elitedivision\amos\admin\models\UserProfile'
* @var string
*/
public $htmlMailContentTitle = '@vendor/elitedivision/amos-comments/src/views/comment/email/content_title';
/**
* This is the html used to render the message of the e-mail. In the view is available the variable $profile
* that is an instance of 'elitedivision\amos\admin\models\UserProfile'
* @var string|array
*/
public $htmlMailContent = '@vendor/elitedivision/amos-comments/src/views/comment/email/content';
// public $htmlMailContent = [
// 'elitedivision\amos\news\models\News' => '@vendor/elitedivision/amos-comments/src/views/comment/email/content_news',
// 'elitedivision\amos\discussioni\models\DiscussioniTopic' => '@vendor/elitedivision/amos-comments/src/views/comment/email/content_discussioni',
// 'elitedivision\amos\documenti\models\Documenti' => '@vendor/elitedivision/amos-comments/src/views/comment/email/content_documenti'
// ];
/**
* Sets if the notify checkbox must be visible into the comments accordion
* @var bool
*/
public $displayNotifyCheckbox = true;
/**
* Sets if the comments accordion must be opened by default
* @var bool
*/
public $accordionOpenedByDefault = true;
/**
* If true it enable the comments olny with the scope (in the community)
* @var boolean $enableCommentOnlyWithScope
*/
public $enableCommentOnlyWithScope = false;
/**
* If a true notify the context model (DiscussioniTopic) if is created a comment
* @var bool $enableNotifyCommentForDiscussions
*/
public $enableNotifyCommentForDiscussions = true;
/**
* @return string
*/
public static function getModuleName()
{
return 'comments';
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
\Yii::setAlias('@elitedivision/amos/' . static::getModuleName() . '/controllers', __DIR__ . '/controllers/');
// custom initialization code goes here
\Yii::configure($this, require(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php'));
}
/**
* @inheritdoc
*/
public function getWidgetGraphics()
{
return NULL;
}
/**
* @inheritdoc
*/
public function getWidgetIcons()
{
return [];
}
/**
* @inheritdoc
*/
protected function getDefaultModels()
{
return [];
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
Event::on(AmosView::className(), AmosView::AFTER_RENDER_CONTENT, [new CommentComponent(), 'showComments']);
}
/**
* @param $model
*/
public function countComments($model)
{
$query = Comment::find()
->joinWith('commentReplies', true, 'LEFT JOIN')
->andWhere(['context' => $model->className(), 'context_id' => $model->id])
->groupBy('comment.id');
/** @var \elitedivision\amos\comments\models\Comment $lastComment */
$countComment = $query->count();
$query = Comment::find()
->joinWith('commentReplies', true, 'LEFT JOIN')
->andWhere(['context' => $model->className(), 'context_id' => $model->id])
->andWhere(['is not', CommentReply::tableName() . '.id', null]);
$countComment += $query->count();
return $countComment;
}
}
<?php
/**
* Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
* OPEN 2.0
*
*
* @package elitedivision\amos\comments\assets
* @category CategoryName
* @author Elite Division S.r.l.
*/
namespace elitedivision\amos\comments\assets;
use yii\web\AssetBundle;
/**
* Class CommentsAsset
* @package elitedivision\amos\comments\assets
*/
class CommentsAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = '@vendor/elitedivision/amos-comments/src/assets/web';
/**
* @inheritdoc
*/
public $css = [
'less/comments.less'
];
/**
* @inheritdoc
*/
public $js = [
'js/comments.js'
];
/**
* @inheritdoc
*/
public $depends = [
'yii\web\JqueryAsset',
];
public function init()
{
$moduleL = \Yii::$app->getModule('layout');
if (!empty($moduleL)) {
$this->depends [] = 'elitedivision\amos\layout\assets\BaseAsset';
} else {
$this->depends [] = 'elitedivision\amos\core\views\assets\AmosCoreAsset';
}
parent::init(); // TODO: Change the autogenerated stub
}
}
var Comments = {};
Comments.comment = function (context_id) {
// Identify comment container of the content.
var comment_container = $("#bk-contribute");
// If it's hidden, it show it
if (comment_container.is(":hidden")) {
comment_container.show();
}
// In other case it hide it
else {
comment_container.hide();
}
};
Comments.beforeSendAjax = function () {
$('#comments-loader').toggleClass('hidden show');
$('#contribute-btn').prop('disabled', true);
$('#comments_anchor').prop('disabled', true);
};
Comments.completeAjax = function () {
$('#comments-loader').toggleClass('show hidden');
$('#contribute-btn').prop('disabled', false);
$('#comments_anchor').prop('disabled', false);
};
Comments.saveComment = function (context_id, context) {
// Identify comment container of the content.
var comment_container = $("#bk-contribute");
// It retrieves the comment textarea.
var comment_textarea = comment_container.find("[name='contribute-area']");
// It retrieves the comment text.
var comment_text = comment_textarea.val();
// If the comment text is not empty...
if (comment_text) {
// ...it build params.
var formData = new FormData();
formData.append('Comment[context]', context);
formData.append('Comment[context_id]', context_id);
formData.append('Comment[comment_text]', comment_text);
// It retrieves the comment attachments.
var commentAttachments = $('#commentAttachments').prop('files');
$.each(commentAttachments, function (key, value) {
formData.append('commentAttachments[]', value);
});
// Find the "send_notify_mail" inputs and add the values to form data.
var sendNotifyMailInputs = comment_container.find(":input[name^='send_notify_mail']");
sendNotifyMailInputs.each(function (index) {
if (($(this).attr('name') === 'send_notify_mail') && ($(this).attr('type') === 'checkbox')) {
// It retrieves the checkbox value.
var send_notify_mail_val = ($(this).is(":checked") ? 1 : 0);
formData.append('send_notify_mail', send_notify_mail_val);
} else {
formData.append($(this).attr('name'), $(this).val());
}
});
$.ajax({
url: '/comments/comment/create-ajax',
contentType: false,
processData: false,
data: formData,
type: 'post',
dataType: 'json',
beforeSend: function () {
Comments.beforeSendAjax();
},
complete: function (jjqXHR, textStatus) {
Comments.completeAjax();
},
success: function (response) {
if (response.error) {
alert(response.error.msg);
} else {
Comments.reloadComments();
comment_textarea.val('');
Comments.emptyRedactorEditor(comment_textarea.attr('id'));
$('#commentAttachments').fileinput('clear');
}
},
error: function (response) {
console.log(response);
$('#ajax-error-comment-modal-id').modal('show');
}
});
} else {
$('#empty-comment-modal-id').modal('show');
}
};
Comments.reply = function (reply_id) {
// Identify the container of the comment reply
var comment_reply_container = $("#bk-comment-reply-" + reply_id);
comment_reply_container.toggleClass('hidden show');
comment_reply_container.find('.redactor-editor').focus();
};
Comments.saveCommentReply = function (comment_id) {
// Identify the container of the comment reply
var comment_reply_container = $("#bk-comment-reply-" + comment_id);
// It retrieves the comment reply textarea.
var comment_reply_textarea = comment_reply_container.find("[name='comment-reply-area']");
// It retrieves the comment reply text.
var comment_reply_text = comment_reply_textarea.val();
// If the comment text is not empty...
if (comment_reply_text) {
// ...it build params.
var formData = new FormData();
formData.append('CommentReply[comment_id]', comment_id);
formData.append('CommentReply[comment_reply_text]', comment_reply_text);
// It retrieves the comment reply attachments.
var commentReplyAttachmentsId = '#commentReplyAttachments' + comment_id;
var commentReplyAttachments = $(commentReplyAttachmentsId).prop('files');
$.each(commentReplyAttachments, function (key, value) {
formData.append('commentReplyAttachments[]', value);
});
// Find the "send_reply_notify_mail" inputs and add the values to form data.
var sendNotifyMailInputs = comment_reply_container.find(":input[name^='send_reply_notify_mail']");
sendNotifyMailInputs.each(function (index) {
if ($(this).attr('name') === 'send_reply_notify_mail') {
// It retrieves the checkbox value.
var send_reply_notify_mail_val = ($(this).is(":checked") ? 1 : 0);
formData.append('send_reply_notify_mail', send_reply_notify_mail_val);
} else {
formData.append($(this).attr('name'), $(this).val());
}
});
$.ajax({
url: '/comments/comment-reply/create-ajax',
contentType: false,
processData: false,
data: formData,
type: 'post',
dataType: 'json',
beforeSend: function () {
Comments.beforeSendAjax();
},
complete: function (jjqXHR, textStatus) {
Comments.completeAjax();
},
success: function (response) {
if (response.error) {
alert(response.error.msg);
} else {
Comments.reloadComments();
}
},
error: function (response) {
console.log(response);
$('#ajax-error-comment-reply-modal-id').modal('show');
}
});
}
else {
$('#empty-comment-reply-modal-id').modal('show');
}
};
Comments.reloadComments = function () {
$.pjax.defaults.timeout = 15000;
$.pjax.reload({container: '#pjax-block-comments', async: false});
};
Comments.emptyRedactorEditor = function (editorId) {
tinyMCE.get(editorId).setContent('');
};
/**
IMPORT AMOS-LAYOUT VARIABLES
*/
@import (optional) "../../vendor/elitedivision/amos-layout/src/assets/resources/base/less/variables.less";
@import (optional) "../../vendor/elitedivision/amos-layout/src/assets/resources/base/less/utility.less";
/**
IMPORT STYLE-CUSTOM
*/
@import (optional) "../../backend/web/less/style_custom.less";
/**
COMMENTS WIDGET
*/
.contribute { /* form add comments */
.ui-accordion.ui-widget .ui-accordion-content.ui-widget-content {
height: 320px;
@media (max-width: 991px) {
height: 400px;
}
@media (max-width: 420px) {
height: 500px;
}
.contribute-container {
label {
margin: @margin-medium 0;
}
@media (min-width: 992px) {
+ div {
padding-left: @padding-default;
}