} else {
    # Get surveys list
    $now = time();
    $allow_ids = array();
    if (!empty($allowed_surveys) && is_array($allowed_surveys)) {
        $allow_ids = cw_array_merge($allow_ids, array_values($allowed_surveys));
    }
    if (!empty($filled_surveys) && is_array($filled_surveys)) {
        $allow_ids = cw_array_merge($allow_ids, $filled_surveys);
    }
    $surveys = cw_query_hash("SELECT {$tables['surveys']}.* FROM {$tables['surveys']}, {$tables['survey_questions']} WHERE {$tables['surveys']}.valid_from_date < {$now} AND {$tables['surveys']}.expires_data > {$now} AND {$tables['surveys']}.survey_id = {$tables['survey_questions']}.survey_id AND ({$tables['surveys']}.survey_type = 'P'" . (empty($customer_id) ? "" : " OR {$tables['surveys']}.survey_type = 'R'") . " OR ({$tables['surveys']}.survey_type = 'H' AND {$tables['surveys']}.survey_id IN ('" . implode("','", $allow_ids) . "'))) GROUP BY {$tables['surveys']}.survey_id ORDER BY {$tables['surveys']}.orderby", "survey_id", false);
    $count_surveys = 0;
    $count_filled = 0;
    if (!empty($surveys)) {
        foreach ($surveys as $sid => $v) {
            list($is_valid, $messages) = cw_check_survey($sid);
            if (!$is_valid) {
                unset($surveys[$sid]);
                continue;
            }
            $count_surveys++;
            if (cw_check_survey_filling($sid)) {
                $surveys[$sid]['is_filled'] = true;
                $count_filled++;
            }
            if (!empty($filled_surveys) && in_array($sid, $filled_surveys) && $v['publish_results'] == 'Y') {
                $id = array_search($sid, $filled_surveys);
                $rid = cw_query_first_cell("SELECT survey_result_id FROM {$tables['survey_results']} WHERE survey_result_id = '{$id}' AND survey_id = '{$sid}'");
                if (!empty($rid)) {
                    $surveys[$sid]['is_view_results'] = true;
                } else {
function cw_check_surveys_events($event, $data = array(), $user_customer_id = false)
{
    global $survey_events, $customer_id, $config, $tables, $allowed_surveys;
    if (!in_array($event, $survey_events)) {
        return false;
    }
    if (empty($user_customer_id)) {
        $user_customer_id = $customer_id;
    }
    # Get survey's IDs
    $now = time();
    $ids = cw_query_hash("SELECT {$tables['surveys']}.survey_id, {$tables['survey_events']}.param, {$tables['survey_events']}.id FROM {$tables['surveys']} LEFT JOIN {$tables['survey_events']} ON {$tables['surveys']}.survey_id = {$tables['survey_events']}.survey_id WHERE {$tables['surveys']}.event_type = '{$event}' AND {$tables['surveys']}.survey_type != 'D' AND {$tables['surveys']}.valid_from_date < {$now} AND {$tables['surveys']}.expires_data > {$now}", "survey_id");
    if (empty($ids)) {
        return false;
    }
    $i = 0;
    foreach ($ids as $id => $params) {
        if (!empty($params) && !empty($params[0]['param'])) {
            # Check by event conditions
            $is_or = cw_query_first_cell("SELECT event_logic FROM {$tables['surveys']} WHERE survey_id = '{$id}'") == 'O';
            $params_count = count(cw_query_column("SELECT COUNT(*) FROM {$tables['survey_events']} WHERE survey_id = '{$id}' GROUP BY param"));
            $avail = array();
            foreach ($params as $p) {
                if (empty($p['id'])) {
                    continue;
                }
                switch ($p['param']) {
                    case "T":
                        if ($data['order']['total'] > $p['id']) {
                            $avail[$p['param']]++;
                        }
                        break;
                    case "P":
                        foreach ($data['products'] as $product) {
                            if ($product['product_id'] == $p['id']) {
                                $avail[$p['param']]++;
                                break;
                            }
                        }
                        break;
                    case "D":
                        foreach ($data['products'] as $product) {
                            if (cw_query_first_cell("SELECT COUNT(*) FROM {$tables['products_categories']} WHERE product_id = '{$product['product_id']}' AND category_id = '{$p['id']}'") > 0) {
                                $avail[$p['param']]++;
                                break;
                            }
                        }
                        break;
                }
            }
            if (count($avail) == 0 || count($avail) < $params_count && !$is_or) {
                continue;
            }
        }
        # Check survey availability
        list($valid, $tmp) = cw_check_survey($id);
        if (!$valid) {
            continue;
        }
        # Get user email
        $email = cw_query_first_cell("SELECT email FROM {$tables['customers']} WHERE BINARY customer_id='" . addslashes($user_customer_id) . "'");
        if (empty($email)) {
            continue;
        }
        # Check login and email
        $mail_logins = cw_query_column("SELECT customer_id FROM {$tables['customers']} WHERE email = '" . addslashes($email) . "'");
        $login_exists = false;
        if (!empty($mail_logins)) {
            $login_exists = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['survey_results']} WHERE customer_id IN ('" . implode("','", cw_array_map("addslashes", $mail_logins)) . "') AND survey_id = '{$id}'") > 0;
        }
        if (cw_check_unique_email($email, $id) && !$login_exists) {
            $as_result = '';
            if (in_array($event, array("OPL", "OPC", "OPP", "OPB"))) {
                $as_result = $event . $data['order']['doc_id'];
            }
            # Add unique email
            $query_data = array("email" => addslashes($email), "customer_id" => addslashes($user_customer_id), "survey_id" => $id, "as_result" => $as_result, "date" => time());
            cw_array2insert("survey_maillist", $query_data);
            if ($config['survey']['survey_send_after_event'] == 'Y') {
                if ($config['survey']['survey_event_sent_delay'] > 0) {
                    cw_array2update("survey_maillist", array("delay_date" => time() + $config['survey']['survey_event_sent_delay'] * 3600), "email = '" . addslashes($email) . "' AND survey_id = '{$id}'");
                } else {
                    cw_send_survey_invitation($id, $email, $user_customer_id);
                }
            }
        }
        $allowed_surveys[$id] = $id;
        $i++;
    }
    return $i;
}