[EXTENSION]: private-messaging

http://www.yiiframework.com/extension/private-messaging/

Guys, hope you’ll find it useful.

Hi,

I’m fairly new to Yii but have years of PHP experience so I’m not finding it too tricky to run with it and implement some of the addons people have created.

I am however confused on how to implement the private-messaging module, which is a shame as it looks really cool from what I’ve read/seen.

Do you have any additional information not available here on Github.

Ideally a more step-by-step process on adding it to an existing Yii Project or even an example project running this module.

Thanks in advance for your time.

Cade

I use this extension by following the instructions on the extension page, so they re complete and on step by step basis. But could you perhaps tell us where things goes wrong? any errors?any part you dont understand what It means?

Hi Sampa,

I loaded all of the files into my project/protected directory however I wasn’t sure where the file “MessageModule.php” was meant to be loaded.

When trying to open one of the Controllers, say Inbox I got the following error:


Trying to get property of non-object 

$this->userModel = Yii::app()->getModule('message')->userModel;

This appears to relate to the calling of the MessageModule.php file and specifically userModel variable, which appears to get assigned during the construct of the class and I assume I need to use something like the following mentioned in the Extension:




return array(

    'modules' => array(

        'message' => array(

            'userModel' => 'User',

            'getNameMethod' => 'getFullName',

            'getSuggestMethod' => 'getSuggest',

        ),

    ),

);



My problem is I’m not sure where to store MessageModule.php or how to load it. I thought maybe it needs to be put into the /config/main.php, but I’m really not sure.

I would really appreciate any information you can give me on this and any corrections to any of my observations I have mentioned above.

The MessageModule.phpare suppossed to be in protected/modules/message/MessageModule.php (the module "root")

There you also put the view/controller/models/data folders.

'modules'=>array(

‘message’ => array(

        'userModel' => 'User', //make sure this is the same as the user model you use


        'getNameMethod' => 'getFullName',


        'getSuggestMethod' => 'getSuggest',


    ),

),

Yep, that did the trick.

Thanks for all your help

Just to recap for anyone else who is very new to Yii and wants to use this.

Step 1

Extract the contents of the ZIP to /protected/modules/message/

NOTE: the MessageModule.php file should be directly under this directory

Step 2

Modify the protected/config/main.php file and add the following in ‘modules’ (same location as ‘gii’=>array):


		'message' => array(

			'userModel' => 'User',

			'getNameMethod' => 'getFullName',

			'getSuggestMethod' => 'getSuggest',

			'viewPath' => '/message/fancy',

		),

NOTE: The viewPath line at the end is optional and uses the fancy style rather than default

Step 3

Create the database as per the /protected/modules/message/data/message.sql

Step 4

Update your User module (or what module you specify in step 2 to include a getFullName and getSuggest module

examples for this are in the Private Messaging Extension

Step 5

Access the module via: …/index.php/message

The above will depend on your URL structure

Issues

I did get 1 issue at step 5 where it complained it couldn’t find the database {{messages}}.

I had to edit the protected/modules/message/models/Message.php and change the line:

return ‘{{messages}}’;

To

return ‘messages’;

wow, I’m glad it solved it for you and amazing you wrote that “How-to”. Thats the spirit.

I think you can have helped some beginners.

Step 1

Extract the contents of the ZIP to /protected/modules/message/

NOTE: the MessageModule.php file should be directly under this directory

Step 2

Modify the protected/config/main.php file and add the following in ‘modules’ (same location as ‘gii’=>array):


		'message' => array(

			'userModel' => 'User',

			'getNameMethod' => 'getFullName',

			'getSuggestMethod' => 'getSuggest',

			'viewPath' => '/message/fancy',

		),

It is real step-by-step explanation.Thank you Cade.

Hi

thanks for this extension. worked out of box :)

can you suggest how to customize name-suggestions feature to show names near the text-field rather than at the top corner of the page ?

thanks again.

For anyone (like me) having problems with the suggest function, I would like to share how I did it (requires to change the SuggestController and compose view (I’ve also added filter and accessrules functions as I am using the Rights module for authentication):

SuggestController.php Controller




<?php


class SuggestController extends Controller {

	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return CMap::mergeArray(parent::filters(),array(

			'accessControl', // perform access control for CRUD operations

		));

	}

	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('user'),

				'users'=>array('@'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

        

        public function actionUser() {

                $res =array();


                if (isset($_GET['term'])) {

                        $q = Yii::app()->request->getParam('term');

                        $userModels = (array) call_user_func(array(

                                CActiveRecord::model(Yii::app()->getModule('message')->userModel),

                                Yii::app()->getModule('message')->getSuggestMethod

                        ), $q);                        

                        

                        if ($userModels) {

                            foreach ($userModels as $userModel) {

                                $fullName=call_user_func(array(

                                        $userModel, $this->getModule()->getNameMethod

                                ));

                                $users[] = array(

                                    'label' => $fullName,

                                    'value' => $fullName,

                                    'id' => $userModel->getPrimaryKey(),

                                );

                            }

                        }

                }


		header('Cache-Control: no-store');

		header('Pragma: no-store');

		header("Content-type: application/json");

                echo CJSON::encode($users);

                Yii::app()->end();

        }

}



Compose.php View




<?php $this->pageTitle=Yii::app()->name . ' - '.RE_t("Compose PMessages"); ?>

<?php

	$this->breadcrumbs=array(

		RE_t('Messages')=>array('#'),

		RE_t("Compose"),

	);

?>


<?php $this->renderPartial(Yii::app()->getModule('message')->viewPath . '/_navigation'); ?>


<h2><?php echo RE_t('Compose New PMessages'); ?></h2>


<div class="form">

	<?php $form = $this->beginWidget('CActiveForm', array(

		'id'=>'message-form',

		'enableAjaxValidation'=>false,

	)); ?>


	<p class="note"><?php echo RE_t('Fields with <span class="required">*</span> are required.'); ?></p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">                

                <?php echo $form->labelEx($model,'receiver_id'); ?>

                <?php

                    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

                            'name'=>'receiver',

                            'model'=>$model,

                            'value'=>"$receiverName",

                            'source'=>$this->createUrl('suggest/user'),

                            // additional javascript options for the autocomplete plugin

                            'options'=>array(

                                'showAnim'=>'fold',

                                'select'=>"js: function(event, ui) {

                                         console.log(ui.item);

                                         $('#PMessages_receiver_id').val(ui.item['id']);

                                }"

                            ),

                    ));                

                ?>

                <?php echo $form->hiddenField($model,'receiver_id'); ?>

		<?php echo $form->error($model,'receiver_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'subject'); ?>

		<?php echo $form->textField($model,'subject'); ?>

		<?php echo $form->error($model,'subject'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'body'); ?>

		<?php echo $form->textArea($model,'body',array('style'=>'width: 400px; height: 100px;','maxlength' => 500)); ?>

		<?php echo $form->error($model,'body'); ?>

	</div>


	<div class="buttons">

		<?php echo CHtml::submitButton(RE_t("Send"),array('class'=>'btn btn-primary')); ?>

	</div>


	<?php $this->endWidget(); ?>


</div>


<?php 

//TODO we are not using this jquery functions because Yii provides a better widget for autocompletion

//and it is part of the standard FrameWork: zii.widgets.jui.CJuiAutoComplete


//$this->renderPartial(Yii::app()->getModule('message')->viewPath . '/_suggest');



I hope this helps!

I follow the instructions of http://www.yiiframework.com/extension/private-messaging/ and @Cade but I see error as follows

"Active record "Message" has an invalid configuration for relation "receiver". It must specify the relation type, the related active record class and the foreign key."

My config/main.php:




'message' => array(

                        'userModel' => 'User',

                        'getNameMethod' => 'getFullName',

                        'getSuggestMethod' => 'getSuggest',

                        //'viewPath' => '/message/fancy',

                        

                        'receiverRelation' => array(

                        		CActiveRecord::BELONGS_TO,

                        		'User',

                        		'on' => 'User.id = receiver_id'

                        ),


                        

						)


                        'senderRelation' => array(

                        		CActiveRecord::BELONGS_TO,

                        		'User',

                        		'on' => 'User.id = sender_id'

                        ),


                		

        ),



Please help me !!!!

This is my post, it have error "Active record "Message" has an invalid configuration for relation "receiver"."

https://www.dropbox.com/s/oq1jpbfpftih6mo/message.rar

https://www.dropbox.com/s/oq1jpbfpftih6mo/message.rar

Hi, id like to know if it’s possible use this extension with CRUGE.

If anyone has done it, Could you explain how-to?

PD. i have integrated everything with CRUGE but it seems that doesnt work:

  • For example, doesnt find any user in suggest

  • I have inserted a message directly in table via phpmyadmin and then in menu it shows correctly that there’s a new message but when i go to inbox i get an error (it seems that try to get the sender username) :


Missing argument 2 for CrugeUser::__construct(), called in C:\wamp\www\yii\framework\db\ar\CActiveRecord.php on line 395 and defined 


public function __construct($username, $password, $authmode = 'default')

68     {

69         $this-&gt;username = $username;

70         $this-&gt;password = $password;

71         $this-&gt;authmode = $authmode;

72         $this-&gt;storeduser = null;

73 

74         Yii::log(__METHOD__ . &quot;\n&quot;, &quot;info&quot;);

75     }

I have got this error Message and its behaviors do not have a method or closure named "getCountUnreaded". how to solve this error?