function sendMessage($id_author, $to_users, $subject, $body, $time, $thread_id = false, $type = 0) { global $current_user; if (!is_array($to_users)) { throw new Exception('$to_users must be an array'); } Database::query('START TRANSACTION'); $query = 'INSERT INTO `users_messages` SET `id_author`=' . (int) $id_author . ', `time`=' . $time . ', `subject`=' . Database::escape($subject) . ', `html`=' . Database::escape($body); Database::query($query); // если есть тред - пишем в тот же тред $lastId = Database::lastInsertId(); $thread_id = $thread_id ? $thread_id : $lastId; if ($thread_id) { $q = array(); foreach ($to_users as $receiver_id) { if (!(int) $receiver_id) { continue; } $to_user = new User($receiver_id); $to_user->reloadNewMessagesCount(); $is_new = $receiver_id == $id_author ? 0 : 1; $q[] = '(' . (int) $lastId . ',' . (int) $thread_id . ',' . (int) $receiver_id . ',' . (int) $is_new . ',0,' . (int) $type . ')'; } if (count($q)) { $query = 'INSERT INTO `users_messages_index`(message_id,thread_id,id_recipient,is_new,is_deleted,type) VALUES ' . implode(',', $q); Database::query($query); } } // increase counters $receivers = Users::getByIdsLoaded($to_users); foreach ($receivers as $receiver) { /* @var $receiver User */ if ($type == 0) { $receiver->setCounter('new_messages', $receiver->getCounter('new_messages') + 1); } else { $receiver->setCounter('new_notifications', $receiver->getCounter('new_notifications') + 1); } $receiver->save(); } if ($type == 0 && $current_user) { // не нотифай Notify::notifyNewInbox($to_users, $id_author); } Database::query('COMMIT'); }