/** 
     * @return string
     */
    function SelectAllAccounts($pageNumber, $accountPerPage, $sortField, $sortOrder, $searchText)
    {
        $nom = $pageNumber > 0 ? ($pageNumber - 1) * $accountPerPage : 0;
        $dopstr = '';
        $search = trim($searchText);
        if (strlen($searchText) > 0) {
            $search = ' AND (id_acct LIKE %1$s OR email LIKE %1$s OR %2$s LIKE %1$s
							OR logins_count LIKE %1$s OR mail_inc_host LIKE %1$s
							OR mail_out_host LIKE %1$s) ';
            $search = sprintf($search, $this->_escapeString('%' . $searchText . '%'), CDateTime::GetMsSqlDateFormat('last_login'));
        }
        if ($nom > 0) {
            $dopstr = ' AND id_acct NOT IN
						(SELECT id_acct FROM
						(SELECT TOP %d id_acct, acct1.id_user as id_user, deleted, email,
						mail_inc_host, mail_out_host, %s AS nlast_login, logins_count,
						mailbox_size, mailbox_limit, def_acct
						FROM %sawm_accounts AS acct1
						INNER JOIN %sawm_settings AS sett1 ON acct1.id_user = sett1.id_user
						WHERE id_acct > -1 %s
						ORDER BY %s %s, def_acct DESC) AS stable) ';
            $dopstr = sprintf($dopstr, $nom, CDateTime::GetMsSqlDateFormat('last_login'), $this->_settings->DbPrefix, $this->_settings->DbPrefix, $search, $sortField, $sortOrder ? 'DESC' : 'ASC');
        }
        $sql = 'SELECT TOP %d id_acct, acct.id_user as id_user, deleted, email,
						mail_inc_host, mail_out_host, %s AS nlast_login, logins_count,
						mailbox_size, mailbox_limit, def_acct
					FROM %sawm_accounts AS acct
					INNER JOIN %sawm_settings AS sett ON acct.id_user = sett.id_user
					WHERE id_acct > -1 %s%s
					ORDER BY %s %s, def_acct DESC';
        return sprintf($sql, $accountPerPage, CDateTime::GetMsSqlDateFormat('last_login'), $this->_settings->DbPrefix, $this->_settings->DbPrefix, $search, $dopstr, $sortField, $sortOrder ? 'DESC' : 'ASC');
    }
    /**
     * @param int $pageNumber
     * @param string $condition
     * @param FolderCollection $folders
     * @param bool $inHeadersOnly
     * @param Account $account
     * @return WebMailMessageCollection
     */
    function SearchMessages($pageNumber, $condition, &$folders, $inHeadersOnly, &$account)
    {
        $tempstr = '';
        $foldersId = array();
        $_foldersKeys = array_keys($folders->Instance());
        foreach ($_foldersKeys as $key) {
            $folder =& $folders->Get($key);
            if (!$folder->Hide) {
                $foldersId[] = $folder->IdDb;
            }
            unset($folder);
        }
        unset($folders, $_foldersKeys);
        $filter = '';
        $asc = true;
        $this->_setSortOrder($account->DefaultOrder, $filter, $asc);
        $condition = str_replace('[', '[[]', $condition);
        $condition = $this->_escapeString('%' . $condition . '%');
        $str_foldersId = implode(',', $foldersId);
        if ($inHeadersOnly) {
            if (($pageNumber - 1) * $account->MailsPerPage > 0) {
                $tempstr = ' AND id_msg NOT IN 
								(SELECT TOP %d id_msg FROM %sawm_messages 
								WHERE id_acct = %d AND id_folder_db ' . $this->_inOrNot($foldersId) . ' AND	
								(from_msg LIKE %s OR to_msg LIKE %s OR cc_msg LIKE %s OR bcc_msg
								LIKE %s OR subject LIKE %s) ORDER BY %s %s)';
                $tempstr = sprintf($tempstr, ($pageNumber - 1) * $account->MailsPerPage, $this->_settings->DbPrefix, $account->Id, $str_foldersId, $condition, $condition, $condition, $condition, $condition, $filter, $asc ? 'ASC' : 'DESC');
            }
            $sql = 'SELECT TOP %d id_msg, %s AS uid, id_folder_db, from_msg, to_msg, cc_msg,
							bcc_msg, subject, %s AS nmsg_date, size, priority, x_spam,
							attachments, seen, flagged, deleted, replied, forwarded, grayed, charset, sensitivity
						FROM %sawm_messages
						WHERE id_acct = %d AND id_folder_db ' . $this->_inOrNot($foldersId) . ' AND	
							(from_msg LIKE %s OR to_msg LIKE %s OR cc_msg LIKE %s OR bcc_msg
							LIKE %s OR subject LIKE %s)%s
						ORDER BY %s %s';
            return sprintf($sql, $account->MailsPerPage, $this->_getMsgIdUidFieldName(true, $account->MailProtocol), CDateTime::GetMsSqlDateFormat('msg_date'), $this->_settings->DbPrefix, $account->Id, $str_foldersId, $condition, $condition, $condition, $condition, $condition, $tempstr, $filter, $asc ? 'ASC' : 'DESC');
        } else {
            if (($pageNumber - 1) * $account->MailsPerPage > 0) {
                $tempstr = ' AND id_msg NOT IN 
							(SELECT TOP %d id_msg FROM %sawm_messages
							WHERE id_acct = %d AND id_folder_db ' . $this->_inOrNot($foldersId) . ' AND	
								(from_msg LIKE %s OR to_msg LIKE %s OR cc_msg LIKE %s OR bcc_msg
								LIKE %s OR subject LIKE %s OR body_text LIKE %s) ORDER BY %s %s)';
                $tempstr = sprintf($tempstr, ($pageNumber - 1) * $account->MailsPerPage, $this->_settings->DbPrefix, $account->Id, $str_foldersId, $condition, $condition, $condition, $condition, $condition, $condition, $filter, $asc ? 'ASC' : 'DESC');
            }
            $sql = 'SELECT TOP %d id_msg, %s AS uid, id_folder_db, from_msg, to_msg, cc_msg,
							bcc_msg, subject, %s AS nmsg_date, size, priority, x_spam,
							attachments, seen, flagged, deleted, replied, forwarded, grayed, charset, sensitivity
						FROM %sawm_messages
						WHERE id_acct = %d AND id_folder_db ' . $this->_inOrNot($foldersId) . ' AND	
							(from_msg LIKE %s OR to_msg LIKE %s OR cc_msg LIKE %s OR bcc_msg
							LIKE %s OR subject LIKE %s OR body_text LIKE %s)%s
						ORDER BY %s %s';
            return sprintf($sql, $account->MailsPerPage, $this->_getMsgIdUidFieldName(true, $account->MailProtocol), CDateTime::GetMsSqlDateFormat('msg_date'), $this->_settings->DbPrefix, $account->Id, $str_foldersId, $condition, $condition, $condition, $condition, $condition, $condition, $tempstr, $filter, $asc ? 'ASC' : 'DESC');
        }
    }