function cw_clone_survey($survey_id)
{
    global $tables;
    if (!is_array($survey_id)) {
        $survey_id = array($survey_id);
    }
    $i = 0;
    foreach ($survey_id as $id) {
        $data = cw_query_first("SELECT * FROM {$tables['surveys']} WHERE survey_id = '{$id}'");
        if (empty($data)) {
            continue;
        }
        # Clone survey details
        unset($data['survey_id']);
        $data['survey_type'] = 'D';
        cw_addslashes($data);
        $newid = cw_array2insert("surveys", $data);
        if (!$newid) {
            continue;
        }
        # Clone survey multilanguage variables
        $vars = array("name", "header", "footer", "complete");
        foreach ($vars as $v) {
            $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'survey_" . $v . "_" . $id . "'", "code", false, true);
            if (empty($lang)) {
                continue;
            }
            foreach ($lang as $c => $l) {
                if ($v == 'name') {
                    $l .= " (CLONE)";
                }
                cw_languages_alt_insert("survey_" . $v . "_" . $newid, addslashes($l), $c);
            }
        }
        # Clone survey questions
        $questions = cw_query_hash("SELECT * FROM {$tables['survey_questions']} WHERE survey_id = '{$id}'", "question_id", false);
        if (!empty($questions)) {
            foreach ($questions as $qid => $q) {
                $q = cw_addslashes($q);
                $q['survey_id'] = $newid;
                $newqid = cw_array2insert("survey_questions", $q);
                if (empty($newqid)) {
                    continue;
                }
                # Clone question names
                $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'question_name_" . $qid . "'", "code", false, true);
                if (!empty($lang)) {
                    foreach ($lang as $c => $l) {
                        cw_languages_alt_insert("question_name_" . $newqid, addslashes($l), $c);
                    }
                }
                if ($q['answers_type'] == 'N') {
                    continue;
                }
                # Clone question answers
                $answers = cw_query_hash("SELECT * FROM {$tables['survey_answers']} WHERE question_id = '{$qid}'", "answer_id", false);
                if (empty($answers)) {
                    continue;
                }
                foreach ($answers as $aid => $a) {
                    $a = cw_addslashes($a);
                    $a['question_id'] = $newqid;
                    $newaid = cw_array2insert("survey_answers", $a);
                    if (empty($newaid)) {
                        continue;
                    }
                    # Clone answer names
                    $lang = cw_query_hash("SELECT code, value FROM {$tables['languages_alt']} WHERE name = 'answer_name_" . $aid . "'", "code", false, true);
                    if (!empty($lang)) {
                        foreach ($lang as $c => $l) {
                            cw_languages_alt_insert("answer_name_" . $newaid, addslashes($l), $c);
                        }
                    }
                }
            }
        }
        # Clone events
        $events = cw_query("SELECT * FROM {$tables['survey_events']} WHERE survey_id = '{$id}'");
        if (!empty($events)) {
            foreach ($events as $e) {
                $e['survey_id'] = $newid;
                cw_array2insert("survey_events", $e);
            }
        }
        $i++;
    }
    return $i;
}
<?php

# Add title
if ($action == "add" && !empty($add['title'])) {
    if (empty($add['orderby'])) {
        $add['orderby'] = cw_query_first_cell("SELECT MAX(orderby) FROM {$tables['titles']}") + 1;
    }
    cw_languages_alt_insert("title_" . $id, $v['title'], $current_language);
    cw_array2insert("titles", $add);
    # Update title(s)
} elseif ($action == "update" && !empty($data)) {
    foreach ($data as $id => $v) {
        $v['active'] = $v['active'];
        cw_languages_alt_insert("title_" . $id, $v['title'], $current_language);
        if ($current_language != $config['default_admin_language']) {
            unset($v['title']);
        }
        cw_array2update("titles", $v, "titleid = '{$id}'");
    }
    # Delete title(s)
} elseif ($action == "delete" && !empty($ids)) {
    $string = "titleid IN ('" . implode("','", $ids) . "')";
    db_query("DELETE FROM {$tables['titles']} WHERE " . $string);
    db_query("DELETE FROM {$tables['languages_alt']} WHERE name IN ('title_" . implode("','title_", $ids) . "')");
}
if (!empty($action)) {
    cw_header_location("index.php?target=titles");
}
$titles = cw_query("SELECT * FROM {$tables['titles']} ORDER BY orderby, title");
if (!empty($titles)) {
    foreach ($titles as $k => $v) {