예제 #1
0
    /**
     * @param int $iUserId
     * @param string $sSearch
     * @param int $iGroupId
     * @param int $iSharedTenantId = null
     * @param bool $bAll = false
     * @return string
     */
    public function GetContactItemsCount($iUserId, $sSearch, $sFirstCharacter, $iGroupId, $iSharedTenantId = null, $bAll = false)
    {
        $sGroupAdd = '';
        if (0 < $iGroupId) {
            $sGroupAdd = sprintf('
INNER JOIN %sawm_addr_groups_contacts AS gr_cnt ON gr_cnt.id_addr = book.id_addr AND gr_cnt.id_group = %d', $this->prefix(), $iGroupId);
        }
        $sSearchAdd = '';
        $sFirstCharacter = 0 < strlen(trim($sFirstCharacter)) ? trim($sFirstCharacter) : '';
        if (!empty($sFirstCharacter)) {
            $sSearch = '\'' . $this->escapeString($sFirstCharacter, true, true) . '%\'';
            $sSearchAdd = sprintf('book.fullname LIKE %s OR book.firstname LIKE %s OR book.surname LIKE %s OR book.nickname LIKE %s OR book.h_email LIKE %s OR book.b_email LIKE %s OR book.other_email LIKE %s', $sSearch, $sSearch, $sSearch, $sSearch, $sSearch, $sSearch, $sSearch);
        }
        if (0 < strlen($sSearch)) {
            $bPhone = api_Utils::IsPhoneSearch($sSearch);
            $sPhoneSearch = $bPhone ? api_Utils::ClearPhoneSearch($sSearch) : '';
            $sSearch = '\'%' . $this->escapeString($sSearch, true, true) . '%\'';
            $sMainSearch = sprintf('book.fullname LIKE %s OR book.firstname LIKE %s OR book.surname LIKE %s OR book.nickname LIKE %s OR book.h_email LIKE %s OR book.b_email LIKE %s OR book.other_email LIKE %s', $sSearch, $sSearch, $sSearch, $sSearch, $sSearch, $sSearch, $sSearch);
            if (0 < strlen($sPhoneSearch)) {
                $sPhoneSearch = '\'%' . $this->escapeString($sPhoneSearch, true, true) . '%\'';
                $sMainSearch .= sprintf(' OR ' . '(b_phone <> \'\' AND REPLACE(REPLACE(REPLACE(REPLACE(b_phone, \'(\', \'\'), \')\', \'\'), \'+\', \'\'), \' \', \'\') LIKE %s) OR ' . '(h_phone <> \'\' AND REPLACE(REPLACE(REPLACE(REPLACE(h_phone, \'(\', \'\'), \')\', \'\'), \'+\', \'\'), \' \', \'\') LIKE %s) OR ' . '(h_mobile <> \'\' AND REPLACE(REPLACE(REPLACE(REPLACE(h_mobile, \'(\', \'\'), \')\', \'\'), \'+\', \'\'), \' \', \'\') LIKE %s)', $sPhoneSearch, $sPhoneSearch, $sPhoneSearch);
            }
            $sSearchAdd = 0 === strlen($sSearchAdd) ? $sMainSearch : '(' . $sSearchAdd . ') AND (' . $sMainSearch . ')';
        }
        $sUserWhere = $this->sharedItemsSqlHelper($iUserId, $iGroupId, is_int($iSharedTenantId), 0 < $iGroupId, 'book.', $bAll);
        $sSql = 'SELECT COUNT(book.id_addr) AS cnt FROM %sawm_addr_book as book%s
WHERE %s AND book.deleted = 0 AND book.hide_in_gab = 0 AND book.auto_create = 0%s';
        return sprintf($sSql, $this->prefix(), $sGroupAdd, $sUserWhere, 0 < strlen($sSearchAdd) ? ' AND (' . $sSearchAdd . ')' : '');
    }