if ($event['type'] === 'delete') {
    $deleteCond = "AND id <> $ID";
}

if (!$line['{$msg_link_field}']['raw'] && $line['{$msg_client_link_field}']['raw']) {
    $res = data_select_field({$user_table_id}, 'id, {$new_field} AS VK', '{$user_link_field} = ', $line['{$msg_client_link_field}']['raw']);
    $row = sql_fetch_assoc($res);
    $vk_id = $row['VK'];
    $line['{$msg_link_field}'] = $row['id'];
}

$pre = data_select_field(
    {$msg_table_id},
    "{$message_date_field} as nextdate, {$message_field}",
    "status = 0 " . $deleteCond . " AND {$msg_link_field} = " . $line['{$msg_link_field}']['raw'] .
    " AND {$message_date_field} != '0000-00-00 00:00:00' ORDER BY {$message_date_field} DESC"
);
$res = sql_fetch_assoc($pre);
$line['{$msg_link_field}']['{$msg_date_field}'] = $res['nextdate'];
$line['{$msg_link_field}']['{$last_message_field}'] = $res['{$message_field}'];
$line['{$msg_soc_logo_field}'] = '<img src="images/vk.png">';
if (!$line['{$msg_author_field}']) {
    $result = sql_select_field(USERS_TABLE,
        'fio',
        'id = ' . $user['id']
    );
    $row = sql_fetch_assoc($result);
    $line['{$msg_author_field}'] = $row['fio'];
}

if ($line['{$msg_author_field}'] == 'Клиент') {
    $line['{$msg_link_field}']['{$flag_field}'] = 1;
}