}
        if (cw_send_survey_invitation($survey_id, $e, $user['customer_id'], true) === true) {
            $cnt++;
        }
    }
    if ($cnt > 0) {
        $top_message = array("content" => cw_get_langvar_by_name("lbl_survey_invitations_are_sent"));
    }
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=maillist");
} elseif ($js_tab == 'maillist' && $action == 'send_all' && !empty($survey_id)) {
    # Send all unsent survey invitations
    $emails = db_query("SELECT email, customer_id FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}' ORDER BY date DESC");
    $cnt = 0;
    if ($emails) {
        while ($row = db_fetch_array($emails)) {
            if (cw_send_survey_invitation($survey_id, $row['email'], $row['customer_id']) === true) {
                $cnt++;
            }
        }
        db_free_result($emails);
    }
    if ($cnt > 0) {
        $top_message = array("content" => cw_get_langvar_by_name("lbl_survey_invitations_are_sent"));
    }
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=maillist");
} elseif ($js_tab == 'instances' && $action == 'delete' && !empty($check)) {
    # Delete survey instance(s)
    db_query("DELETE FROM {$tables['survey_results']} WHERE survey_id = '{$survey_id}' AND survey_result_id IN ('" . implode("','", $check) . "')");
    db_query("DELETE FROM {$tables['survey_result_answers']} WHERE survey_result_id IN ('" . implode("','", $check) . "')");
    $top_message = array("content" => cw_get_langvar_by_name("txt_survey_instances_are_deleted"));
    cw_header_location("index.php?target={$target}&survey_id=" . $survey_id . "&js_tab=instances");
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;
}