Пример #1
0
    static function Send_pm($sender_id, $sender_name, $receiver_user, $title, $content, $once_way = false, $ref_id = 0)
    {
        $num_send = 0;
        $new_row_msg = array('msg_date' => TIME_NOW, 'msg_post' => htmlspecialchars_decode($content), 'msg_author_id' => $sender_id, 'msg_author_name' => $sender_name, 'msg_ip_address' => AZLib::ip());
        // ***** CHÚ Ý *****
        // Newest = 0 - Mới nhất, = 1 - Cũ hơn (Do lỗi tạo giá trị mặc định trường này là 0, nên quy ước hơi bị ngược, để đỡ mất công chạy lệnh update database
        // ***** CHÚ Ý *****
        $msg_id = DB::insert('message_text', $new_row_msg);
        $inbox_ref_id = 0;
        if ($ref_id) {
            $msgText = DB::select('message_topics', 'mt_id = ' . $ref_id . ' AND mt_owner_id = ' . $sender_id);
            if ($msgText['mt_msg_id']) {
                // Kiểm tra xem người nhận còn tin nhắn gốc hay không
                $mtTopic = DB::select('message_topics', 'mt_msg_id = ' . $msgText['mt_msg_id'] . ' AND mt_id <> ' . $ref_id . ' AND mt_owner_id = ' . $receiver_user['id'] . ' AND mt_from_id <> ' . $receiver_user['id']);
                // Nế còn tin nhắn gốc
                if ($mtTopic['mt_id']) {
                    // Gán tin nhắn sắp gửi là tin nhắn mới nhất
                    // Lấy id tin nhắn gốc của người gửi làm ref_id cho tin nhắn sắp gửi
                    $inbox_ref_id = $mtTopic['mt_id'];
                }
            }
        }
        // Đặt cờ tin nhắn mới nhất (inbox) của các tin trong luồng của người nhận = 0
        if ($inbox_ref_id) {
            DB::query('UPDATE message_topics SET mt_newest = 1 WHERE mt_newest = 0 AND mt_vid_folder = \'inbox\' AND (mt_ref_id = ' . $inbox_ref_id . ' OR mt_id = ' . $inbox_ref_id . ') AND mt_owner_id = ' . $receiver_user["id"]);
        }
        // Đặt cờ tin nhắn mới nhất (sent) của các tin trong luồng của người gửi = 0
        if ($ref_id) {
            DB::query('UPDATE message_topics SET mt_newest = 1 WHERE mt_newest = 0 AND mt_vid_folder = \'sent\' AND (mt_ref_id = ' . $ref_id . ' OR mt_id = ' . $ref_id . ') AND mt_owner_id = ' . $sender_id);
        }
        $new_row_msg = array('mt_msg_id' => $msg_id, 'mt_date' => TIME_NOW, 'mt_title' => $title, 'mt_ref_id' => $inbox_ref_id, 'mt_newest' => 0, 'mt_to_name' => $receiver_user["user_name"], 'mt_read' => 0);
        $arrID = array($sender_id, $sender_name, $receiver_user["id"], $receiver_user["user_name"]);
        // Tạo tiêu đề tin nhắn mới
        $msg_id_inbox = AZLib::InserMessage($new_row_msg, $arrID, $status = 'inbox');
        if ($msg_id_inbox) {
            $num_send++;
            //insert vao bang feed
            $feed_sql = "(4,{$receiver_user['id']},{$msg_id_inbox},{$new_row_msg['mt_date']},{$sender_id},0)";
            $feed_sql = "INSERT INTO feed (`type`,`user_id`,`ref_id`,`time`,`act_user_id`,`item_id`) VALUES " . $feed_sql;
            DB::query($feed_sql);
        }
        if ($once_way == false) {
            $new_row_msg['mt_read'] = 1;
            $new_row_msg['mt_ref_id'] = $ref_id;
            $msg_id_sent = AZLib::InserMessage($new_row_msg, $arrID, $status = 'sent');
            $num_send++;
        } else {
            $msg_id_sent = true;
        }
        // Nếu tin được tạo mới là tin nhắn trả lời
        //if($inbox_ref_id)
        //{
        // Đặt cờ đã có tin nhắn trả lời cho tin nhắn gốc trong cả "inbox" và "sent"
        //DB::query('	UPDATE message_topics SET mt_has_reply = 1 WHERE mt_id = ' . $ref_id . ' AND mt_vid_folder = \'inbox\'');
        //	DB::query('	UPDATE message_topics SET mt_has_reply = 1 WHERE mt_id = ' . $inbox_ref_id);
        //}
        if ($msg_id_inbox && $msg_id_sent) {
            $count_pm = true;
            // Đối với tin trả lời, chỉ đếm những tin trả lời mới cho tin thuộc các luồng tin khác nhau
            if ($ref_id) {
                // Kiểm tra xem trong luồng tin hiện tại có tin trả lời nào chưa đọc hay không
                $re = DB::query('SELECT COUNT(*) AS unreadCount FROM message_topics
								WHERE mt_ref_id = ' . $ref_id . ' 
									AND mt_vid_folder = \'inbox\' AND mt_owner_id = ' . $receiver_user["id"] . '
									AND mt_read = 0');
                if ($re) {
                    $row = mysql_fetch_assoc($re);
                    // Nếu có thì không tính thêm tin này vào tổng pm
                    if ($row['unreadCount'] > 1) {
                        $count_pm = false;
                    }
                }
            }
            if ($count_pm) {
                DB::query('UPDATE user SET total_pm = total_pm + 1 WHERE id=' . $receiver_user["id"]);
            }
            User::getUser($receiver_user["id"], 0, 1);
            DB::query("UPDATE message_text SET msg_sent_to_count = {$num_send} WHERE msg_id = {$msg_id}");
            if ($receiver_user['email'] && $receiver_user['email_alert']) {
                $link = WEB_ROOT . Url::build('message');
                $content = trim(addslashes(AZLib::parseBBCode($title . ' - ' . $content, true)));
                AZLib::addCronJob('pm', $content, $receiver_user['id'], User::user_name(), '', 0, '', $link);
            }
            return true;
        } else {
            return false;
        }
    }