PHP code example of niciz / yii2-message
1. Go to this page and download the library: Download niciz/yii2-message 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/ */
niciz / yii2-message example snippets
'modules' => [
'message' => [
'class' => 'niciz\message\Module',
'userModelClass' => '\app\models\User', // your User model. Needs to be ActiveRecord.
],
],
'mailMessages' => function ($user) {
return $user->profile->receive_emails === true;
},
'useMailQueue' => true,
'recipientsFilterCallback' => function ($users) {
return array_filter($users, function ($user) {
return !$user->isAdmin;
});
},
$messagelabel = '<span class="fas fa-envelope"></span>';
$unread = Message::find()->where(['to' => $user->id, 'status' => 0])->count();
if ($unread > 0)
$messagelabel .= '(' . $unread . ')';
echo Nav::widget([
'encodeLabels' => false, // important to display HTML-code (fontawesome icons)
'items' => [
// ...
[
'label' => $messagelabel,
'url' => '',
'visible' => !Yii::$app->user->isGuest, 'items' => [
['label' => '<i class="fas fa-inbox"></i> Inbox', 'url' => ['/message/message/inbox']],
['label' => '<i class="fas fa-share-square"></i> Sent', 'url' => ['/message/message/sent']],
'<hr>',
['label' => '<i class="fas fa-firstdraft"></i> Drafts', 'url' => ['/message/message/drafts']],
['label' => '<i class="fas fa-clone"></i> Signature', 'url' => ['/message/message/signature']],
['label' => '<i class="fas fa-calendar-times"></i> Out of Office', 'url' => ['/message/message/out-of-office']],
['label' => '<i class="fas fa-ban"></i> Manage your Ignorelist', 'url' => ['/message/message/ignorelist']],
'<hr>',
['label' => '<i class="fas fa-plus"></i> Compose a Message', 'url' => ['/message/message/compose']],
]
],
// ...
]);
use kartik\growl\GrowlAsset;
use yii\bootstrap\Modal;
use yii\helpers\Url;
GrowlAsset::register($this);
Modal::begin(['id' => 'compose-message', 'header' => '<h2>Compose new Message</h2>']);
Modal::end();
$recipient_id = 1337; # write an message to user with id 1337
echo Html::a('<span class="fas fa-envelope"></span> Compose Message', '', [
'class' => 'btn btn-default btn-contact-user',
'data-recipient' => $recipient_id,
'data-pjax' => 0
]);
$message_url = Url::to(['//message/message/compose']);
$this->registerJs("
$('.modal-body').on('click', '.btn-send-message', function(event) {
if ($('#message-title').val()) {
$.post('".$message_url."', $('#message-form').serializeArray(), function() {
$.notify({message: 'Message has been sent successfully.'}, {type: 'success'});
$('#compose-message').modal('hide');
});
} else {
$('.modal-body').prepend('<div class=\"alert alert-warning\">Please enter a title at least.</div>');
}
event.preventDefault();
});
$('.modal-body').on('submit', '#message-form', function(event) {
$('.btn-send-message').click();
event.preventDefault();
});
$('body').on('click', '.btn-contact-user', function(event) {
$('#compose-message').modal();
recipient = $(this).data('recipient');
$.ajax('".$message_url."?to='+recipient+'&add_to_recipient_list=1', {
'success': function(result) {
$('.modal-body').html(result);
}
});
event.preventDefault();
});
");