PHP code example of songyue / yii2-message

1. Go to this page and download the library: Download songyue/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/ */

    

songyue / yii2-message example snippets


'modules' => [
    'message' => [
        'class' => 'songyue\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="glyphicon glyphicon-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 (glyphicons)
    'items' => [
    // ...
    [
      'label' => $messagelabel,
      'url' => '',
      'visible' => !Yii::$app->user->isGuest, 'items' => [
        ['label' => 'Inbox', 'url' => ['/message/message/inbox']],
        ['label' => 'Sent', 'url' => ['/message/message/sent']],
        ['label' => 'Compose a Message', 'url' => ['/message/message/compose']],
        ['label' => 'Manage your Ignorelist', 'url' => ['/message/message/ignorelist']],
      ]
    ],
    // ...
  ]);

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="glyphicon glyphicon-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();
    });
");