This commit is contained in:
vision 2015-06-03 18:04:47 +03:00
parent 3ba0069ce7
commit a31994263b
5 changed files with 144 additions and 43 deletions

View file

@ -44,6 +44,12 @@ class MessageApiAction extends Action {
}
protected function getNewMessage() {
$from_id = \Yii::$app->request->get('from_id', false);
return \Yii::$app->mymessages->getNewMessages(\Yii::$app->user->identity->id, $from_id);
}
protected function updateMessage() {
$id_last_message = \Yii::$app->request->post('id_last_message');
while(true){

View file

@ -73,6 +73,11 @@ class MyMessages extends Component {
}
public function getNewMessages($whom_id, $from_id) {
return $this->getMessages($whom_id, $from_id, 1);
}
/**
* Method to sendMessage.
*
@ -229,17 +234,12 @@ class MyMessages extends Component {
$query = new \yii\db\Query();
$query
->select(['msg.created_at', 'msg.id', 'msg.status', 'msg.message', "usr1.$this->attributeNameUser as from_name", "usr2.$this->attributeNameUser as whom_name"])
->select(['msg.created_at', 'msg.id', 'msg.status', 'msg.message', "usr1.id as from_id", "usr1.$this->attributeNameUser as from_name", "usr2.id as whom_id", "usr2.$this->attributeNameUser as whom_name"])
->from("$table_name as msg")
->leftJoin("$this->userTableName as usr1", 'usr1.id = msg.from_id')
->leftJoin("$this->userTableName as usr2", 'usr2.id = msg.whom_id')
->where(['msg.whom_id' => $whom_id]);
if($from_id) {
$query->andWhere(['msg.from_id' => $from_id]);
} else {
$query->orWhere(['msg.from_id' => $whom_id]);
}
->where(['msg.whom_id' => $whom_id, 'msg.from_id' => $from_id])
->orWhere(['msg.from_id' => $whom_id, 'msg.whom_id' => $from_id]);
if($type) {
$query->andWhere(['=', 'msg.status', $type]);
@ -251,10 +251,10 @@ class MyMessages extends Component {
$query->andWhere(['>', 'msg.id', $last_id]);
}
$return = Array();
$return = $query->orderBy('msg.id')->all();
$ids = Array();
foreach($query->all() as $m) {
$return[$m['from_name']][] = $m;
foreach($return as $m) {
//$return[$m['from_name']][] = $m;
$ids[] = $m['id'];
}
@ -263,9 +263,11 @@ class MyMessages extends Component {
Messages::updateAll(['status' => Messages::STATUS_READ], ['in', 'id', $ids]);
}
return $return;
$user_id = \Yii::$app->user->getId();
return array_map(function ($r) use ($user_id) { $r['i_am_sender'] = $r['from_id'] == $user_id; return $r;}, $return);
}
public static function unixToDate ($arr){
if(!is_array($arr) && !is_object($arr)) {
return $arr;

View file

@ -1,7 +1,8 @@
div.message-container {
width: 450px;
width: 250px;
height: 250px;
overflow-y: auto;
overflow-x: hidden;
font-size: 10px;
padding: 5px;
}

View file

@ -1,35 +1,96 @@
$(window).ready(function() {
var base_url = 'http://fitbato.com/admin/battle/private-messages';
var messages = {
box : $("#message-container"),
lastId: 0,
console:function(m) {
console.log(m);
var messages = (function() {
var di = {};
return function(id_block_) {
var id_block = typeof id_block_ != "undefined" ? id_block_ : '#message-container';
if(id_block in di) {
return di[id_block];
}
};
var self = this;
this.base_url = 'http://fitbato.com/admin/battle/private-messages';
this.mainBox = $(id_block);
this.box = this.mainBox.find('.message-container').eq(0);
this.inputText = this.mainBox.find('input[name="input_message"]');
this.inputFromId = this.mainBox.find('input[name="message_id_user"]');
this.lastId = 0;
this.log = function(m) {
console.log(m);
};
var getAllMessages = function (from_id) {
$.ajax({
type: "POST",
url: base_url + '?action=getMessage&from_id=' + from_id,
data: {from_id:from_id},
success: function(msg){
if(msg.status) {
updateBox(msg.data);
}else{
messages.console(msg);
}
this.getAllMessages = function () {
var from_id = self.inputFromId.val();
if(!from_id) {
return null;
}
});
};
$.ajax({
type: "GET",
url: self.base_url,
data: {from_id:from_id, action:'getMessage'},
success: function(msg){
if(msg.status) {
self.updateBox(msg.data);
} else {
self.log('error: getAllMessages');
self.log(msg);
}
}
});
};
var updateBox = function(m) {
var html = '';
messages.console(m);
};
this.getNewMessages = function () {
var from_id = self.inputFromId.val();
if(!from_id) {
return null;
}
$.ajax({
type: "GET",
url: self.base_url,
data: {from_id:from_id, action:'getNewMessage'},
success: function(msg){
if(msg.status) {
self.updateBox(msg.data);
} else {
self.log('error: getNewMessage');
self.log(msg);
}
}
});
};
});
this.clearBox = function() {
self.box.html('');
self.lastId = 0;
};
this.createHtmlMessage = function(n) {
var html = '';
html += '<div class="bubble ' + (n['i_am_sender'] ? 'bubble-alt green' : '') + '">';
html += '<p>' + n['message'] + '</p>';
html += '</div>';
return html;
};
this.updateBox = function(m) {
var html = '';
m.forEach(function(h){
if(h['id'] > self.lastId) {
html += self.createHtmlMessage(h);
self.lastId = h['id'];
}
});
self.box.prepend(html);
self.box.scrollTop(self.box.height());
};
di[id_block] = this;
}
})();

View file

@ -11,16 +11,27 @@ class PrivateMessage extends Widget {
public $buttonName = "Отправить";
protected $html;
protected $uniq_id;
public function init() {
parent::init();
$this->uniq_id = $this->createId();
}
public function run(){
MessageAssets::register($this->view);
$this->html = '';
$this->addJs();
$this->html = '<div id="' . $this->uniq_id . '">';
$this->html .= $this->getListUsers();
$this->html .= $this->getBoxMessages();
$this->html .= $this->getFormInput();
$this->html .= '</div>';
return $this->html;
}
protected function getListUsers() {
$users = \Yii::$app->mymessages->getAllUsers();
$html = '<ul class="list_users">';
@ -37,18 +48,38 @@ class PrivateMessage extends Widget {
protected function getBoxMessages() {
$html = '';
$html .= '<div class="message-container" id="message-container"></div>';
$html .= '<div class="message-container"></div>';
return $html;
}
protected function getFormInput() {
$html = '<form action="#" method="POST">';
$html = '<form action="#" id="message-form" method="POST">';
$html .= '<input type="text" name="input_message">';
$html .= '<input type="hidden" name="message_id_user">';
$html .= '<input type="hidden" name="message_id_user" value="10">';
$html .= '<button type="submit">' . $this->buttonName . '</button>';
$html .= '</form>';
return $html;
}
protected function addJs() {
$var_name = 'mess_' . $this->uniq_id;
$script = 'var ' . $var_name . ' = new messages("#'. $this->uniq_id .'");';
$script .= "$var_name.getAllMessages();";
$view = $this->getView();
$view->registerJs($script, $view::POS_READY);
}
protected function createId() {
$length = 5;
$chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return 'messag_'.$string;
}
}