public static function presendCannedMsg($chat)
{
$session = erLhcoreClassChat::getSession();
$q = $session->createFindQuery('erLhcoreClassModelCannedMsg');
$q->where($q->expr->lOr($q->expr->eq('department_id', $q->bindValue($chat->dep_id)), $q->expr->lAnd($q->expr->eq('department_id', $q->bindValue(0)), $q->expr->eq('user_id', $q->bindValue(0))), $q->expr->eq('user_id', $q->bindValue($chat->user_id))), $q->expr->eq('auto_send', $q->bindValue(1)));
$q->limit(1, 0);
$q->orderBy('user_id DESC, position ASC, id ASC');
// Questions with matched URL has higher priority
$items = $session->find($q);
if (!empty($items)) {
$cannedMsg = array_shift($items);
$replaceArray = array('{nick}' => $chat->nick, '{email}' => $chat->email, '{phone}' => $chat->phone, '{operator}' => (string) $chat->user->name_support);
$additionalData = $chat->additional_data_array;
if (is_array($additionalData)) {
foreach ($additionalData as $row) {
if (isset($row->identifier) && $row->identifier != '') {
$replaceArray['{' . $row->identifier . '}'] = $row->value;
}
}
}
erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.workflow.canned_message_replace', array('chat' => $chat, 'replace_array' => &$replaceArray));
$cannedMsg->setReplaceData($replaceArray);
$msg = new erLhcoreClassModelmsg();
$msg->msg = $cannedMsg->msg_to_user;
$msg->chat_id = $chat->id;
$msg->user_id = $chat->user_id;
$msg->name_support = $chat->user->name_support;
$chat->last_user_msg_time = $msg->time = time();
erLhcoreClassChat::getSession()->save($msg);
if ($chat->last_msg_id < $msg->id) {
$chat->last_msg_id = $msg->id;
}
$chat->updateThis();
}
}