Commits

christiansalazar  committed a08a936

ENH. add user-defined data argument to the widget

  • Participants
  • Parent commits bd87961

Comments (0)

Files changed (4)

File IYiiChat.php

  * @license FREE BSD
  */
 interface IYiiChat {
-	public function yiichat_post($chat_id, $identity, $message);
-	public function yiichat_list_posts($chat_id, $identity, $last_id);
+	public function yiichat_post($chat_id, $identity, $message, $data);
+	public function yiichat_list_posts($chat_id, $identity, $last_id, $data);
 }
 ?>

File MyYiiChatHandler.php

  *	this class is invoked because you specify it in your YiiChatWidget 
  *	arguments passed to the widget.
  *
+ *	this is the object fields required: (as an indexed array)
+ *
+ *		'id'				the post unique id
+ *		'text'				the post text
+ *		'time'				the time stamp
+ *		'owner'				the name of the person who make this post
+ *		'post_identity'		the ID of the person who make this post
+ *
+ *	the both methods in this handler receive:
+ *
+ *		$chat_id			the id provided in the widget, to discrimine 
+ *							between various chats.
+ *
+ *		$identity			the identity (ID) of the person who is in chat
+ *							(the post_identity field is the same as $identity
+ *							only when we are creating a post: yiichat_post
+ *
+ *		$data				a user-defined value passed from the widget
+ *
  * @uses CWidget
  * @version 1.0 
  * @author Christian Salazar <christiansalazarh@gmail.com> 
 	/**
 	 	post a message into your database.
 	 */
-	public function yiichat_post($chat_id, $identity, $message){
+	public function yiichat_post($chat_id, $identity, $message, $data){
 		return array(
 			'id'=>time()+rand(10000,99999), 
 			'text'=>$message, 
 				this scenario occurs when the post widget refreshs using
 				a timer, in order to receive the new posts since last_id.
 	 */
-	public function yiichat_list_posts($chat_id, $identity, $last_id){
+	public function yiichat_list_posts($chat_id, $identity, $last_id, $data){
 		$ar = array();
 
 		// simulation: other posts since last_id, because is a sym you dont see
 											// MyYiiChatHandler is a demo
 											// you must create your own class 
 											// and implements IYiiChat interfac
-
+		'data'=>'any data',					// data passed to the handler
 		// success and error handlers, both optionals.
 		'onSuccess'=>new CJavaScriptExpression(
 			"function(code, text, post_id){   }"),

File YiiChatWidget.php

 	public	$maxPostLen=140;
 	public  $model; // a model instance, it must implements IYiiChat
 	public  $timerMs = 5000;
+	public  $data;	// public data passed to the model instance
 
 	private $_action;
 	private $_baseUrl;
 		));
 		$s = Yii::app()->session;
 		$s[$this->chat_id.'_model'] = $this->model;
+		$s[$this->chat_id.'_data'] = $this->data;
 		$var_id = rand(1000,9999);
 		Yii::app()->getClientScript()->registerScript("yii_chat_script_".$var_id
 		,"	var chat_{$var_id} = new YiiChat({$options});
 		$text = $this->_getPost('text');
 		$s = Yii::app()->session;
 		$model = $s[$chat_id.'_model'];
+		$data = $s[$chat_id.'_data'];
 		if(($action == 'sendpost') && $identity && $chat_id){
 			header("Content-type: application/json");
-			if($post = $model->yiichat_post($chat_id, $identity, $text)){
+			if($post = $model->yiichat_post($chat_id, $identity, $text, $data)){
 				if(!isset($post['chat_id']))
 					$post['chat_id']=$chat_id;
 				if(!isset($post['identity']))
 			}
 		}
 		if(($action == 'init') && $identity && $chat_id){
-			$posts = $model->yiichat_list_posts($chat_id, $identity, null);
+			$posts = $model->yiichat_list_posts($chat_id, $identity, null, $data);
 			if($posts==null)
 				$posts = array();
 			$data = array('chat_id'=>$chat_id, 'identity'=>$identity,
 		}
 		if(($action == 'timer') && $identity && $chat_id){
 			$posts = $model->yiichat_list_posts(
-					$chat_id, $identity, $this->_getPost('last_id'));
+					$chat_id, $identity, $this->_getPost('last_id'), $data);
 			if($posts==null)
 				$posts = array();
 			$data = array('chat_id'=>$chat_id, 'identity'=>$identity,