// Transfered chats to departments
$transferchatsDep = erLhcoreClassTransfer::getTransferChats(array('department_transfers' => true));
if (!empty($transferchatsDep)) {
    reset($transferchatsDep);
    $chatPending = current($transferchatsDep);
    if ($chatPending['transfer_id'] > $lastPendingTransferID) {
        $lastPendingTransferID = $chatPending['transfer_id'];
    }
    foreach ($transferchatsDep as &$transf) {
        $transf['time_front'] = date(erLhcoreClassModule::$dateDateHourFormat, $transf['time']);
    }
}
$ReturnMessages['transfer_chats'] = array('list' => array_values($transferchatsUser), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_dep_chats'] = array('list' => array_values($transferchatsDep), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
if ($canListOnlineUsers == true || $canListOnlineUsersAll == true) {
    $onlineOperators = erLhcoreClassModelUserDep::getOnlineOperators($currentUser, $canListOnlineUsersAll);
    foreach ($onlineOperators as &$onlineOperator) {
        erLhcoreClassChat::prefillGetAttributesObject($onlineOperator->user, array('lastactivity_ago'), array('username', 'password', 'email', 'filepath', 'filename', 'job_title', 'skype', 'xmpp_username'));
    }
    $ReturnMessages['online_op'] = array('list' => array_values($onlineOperators));
}
if ($unreadTabEnabled == true) {
    // Unread chats
    $unreadChats = erLhcoreClassChat::getUnreadMessagesChats(20, 0, array('ignore_fields' => erLhcoreClassChat::$chatListIgnoreField));
    $lastPendingChatID = 0;
    $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat', 'Visitor');
    $lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout', 'New unread message');
    if (!empty($unreadChats)) {
        $lastPendingChatID = max(array_keys($unreadChats));
        $chatRecent = reset($unreadChats);
        $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat', 'Unread message') . ' | ' . $chatRecent->nick . ' | ' . $chatRecent->department;
    if ($chatPending['transfer_id'] > $lastPendingTransferID) {
        $lastPendingTransferID = $chatPending['transfer_id'];
    }
    foreach ($transferchatsDep as &$transf) {
        $transf['time_front'] = date(erLhcoreClassModule::$dateDateHourFormat, $transf['time']);
    }
}
$ReturnMessages['transfer_chats'] = array('list' => array_values($transferchatsUser), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_dep_chats'] = array('list' => array_values($transferchatsDep), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
if ($canListOnlineUsers == true || $canListOnlineUsersAll == true) {
    $filter = array();
    if (is_array($Params['user_parameters_unordered']['operatord']) && !empty($Params['user_parameters_unordered']['operatord'])) {
        erLhcoreClassChat::validateFilterIn($Params['user_parameters_unordered']['operatord']);
        $filter['customfilter'][] = '(dep_id = 0 OR dep_id IN (' . implode(",", $Params['user_parameters_unordered']['operatord']) . '))';
    }
    $onlineOperators = erLhcoreClassModelUserDep::getOnlineOperators($currentUser, $canListOnlineUsersAll, $filter, is_numeric($Params['user_parameters_unordered']['limito']) ? (int) $Params['user_parameters_unordered']['limito'] : 10, $onlineTimeout);
    erLhcoreClassChat::prefillGetAttributes($onlineOperators, array('lastactivity_ago', 'user_id', 'id', 'name_support', 'active_chats', 'departments_names'), array(), array('remove_all' => true));
    $ReturnMessages['online_op'] = array('list' => array_values($onlineOperators));
}
if ($unreadTabEnabled == true) {
    $filter = array('ignore_fields' => erLhcoreClassChat::$chatListIgnoreField);
    $limitList = is_numeric($Params['user_parameters_unordered']['limitu']) ? (int) $Params['user_parameters_unordered']['limitu'] : 10;
    if (is_array($Params['user_parameters_unordered']['unreadd']) && !empty($Params['user_parameters_unordered']['unreadd'])) {
        erLhcoreClassChat::validateFilterIn($Params['user_parameters_unordered']['unreadd']);
        $filter['filterin']['dep_id'] = $Params['user_parameters_unordered']['unreadd'];
    }
    // Unread chats
    $unreadChats = erLhcoreClassChat::getUnreadMessagesChats($limitList, 0, $filter);
    $lastPendingChatID = 0;
    $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat', 'Visitor');
    $lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout', 'New unread message');
    if ($chatPending['transfer_id'] > $lastPendingTransferID) {
        $lastPendingTransferID = $chatPending['transfer_id'];
    }
    foreach ($transferchatsDep as &$transf) {
        $transf['time_front'] = date(erLhcoreClassModule::$dateDateHourFormat, $transf['time']);
    }
}
$ReturnMessages['transfer_chats'] = array('list' => array_values($transferchatsUser), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_dep_chats'] = array('list' => array_values($transferchatsDep), 'last_id_identifier' => 'transfer_chat', 'last_id' => $lastPendingTransferID);
if ($canListOnlineUsers == true || $canListOnlineUsersAll == true) {
    $filter = array();
    if (is_array($Params['user_parameters_unordered']['operatord']) && !empty($Params['user_parameters_unordered']['operatord'])) {
        erLhcoreClassChat::validateFilterIn($Params['user_parameters_unordered']['operatord']);
        $filter['customfilter'][] = '(dep_id = 0 OR dep_id IN (' . implode(",", $Params['user_parameters_unordered']['operatord']) . '))';
    }
    $onlineOperators = erLhcoreClassModelUserDep::getOnlineOperators($currentUser, $canListOnlineUsersAll, $filter, is_numeric($Params['user_parameters_unordered']['limito']) ? (int) $Params['user_parameters_unordered']['limito'] : 10, (int) erLhcoreClassModelChatConfig::fetch('sync_sound_settings')->data['online_timeout']);
    erLhcoreClassChat::prefillGetAttributes($onlineOperators, array('lastactivity_ago', 'user_id', 'id', 'name_support', 'active_chats', 'departments_names'), array(), array('remove_all' => true));
    $ReturnMessages['online_op'] = array('list' => array_values($onlineOperators));
}
if ($unreadTabEnabled == true) {
    $filter = array('ignore_fields' => erLhcoreClassChat::$chatListIgnoreField);
    $limitList = is_numeric($Params['user_parameters_unordered']['limitu']) ? (int) $Params['user_parameters_unordered']['limitu'] : 10;
    if (is_array($Params['user_parameters_unordered']['unreadd']) && !empty($Params['user_parameters_unordered']['unreadd'])) {
        erLhcoreClassChat::validateFilterIn($Params['user_parameters_unordered']['unreadd']);
        $filter['filterin']['dep_id'] = $Params['user_parameters_unordered']['unreadd'];
    }
    // Unread chats
    $unreadChats = erLhcoreClassChat::getUnreadMessagesChats($limitList, 0, $filter);
    $lastPendingChatID = 0;
    $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat', 'Visitor');
    $lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout', 'New unread message');