}
    }
    $top_message = array("content" => cw_get_langvar_by_name("txt_survey_respondents_are_added"));
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=" . $js_tab . "&show=respondents");
} elseif ($action == "import_users" && $js_tab == 'maillist') {
    # Import emails from CSV file
    if (empty($userfile)) {
        $top_message = array("type" => "E", "content" => cw_get_langvar_by_name("txt_survey_import_file_wasnt_assigned"));
        cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=" . $js_tab . "&show=import");
    }
    $userfile = cw_move_uploaded_file("userfile");
    $fp = cw_fopen($userfile, "r", true);
    if ($fp) {
        while ($em = fgets($fp, 255)) {
            $em = trim($em);
            if (!cw_check_unique_email($em, $survey_id)) {
                continue;
            }
            cw_array2insert("survey_maillist", array("survey_id" => $survey_id, "email" => $em, "date" => time()));
        }
        fclose($fp);
        $top_message = array("content" => cw_get_langvar_by_name("msg_adm_news_subscribers_imp"));
    }
    @unlink($userfile);
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=" . $js_tab . "&show=import");
} elseif ($js_tab == 'maillist' && !empty($check) && $action == 'send' && !empty($survey_id)) {
    # Send survey invitations
    $cnt = 0;
    foreach ($check as $e) {
        $user = cw_query_first("SELECT email, customer_id FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}' AND email = '{$e}'");
        if ($user['email'] != stripslashes($e)) {
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;
}