include "../include/resource_functions.php";
include "../include/search_functions.php";
$collection = getvalescaped("collection", "", true);
$errors = "";
$done = false;
# Fetch collection data
$cinfo = get_collection($collection);
if ($cinfo === false) {
    exit("Collection not found.");
}
# Check access
if (!$cinfo["request_feedback"]) {
    exit("Access denied.");
}
# Check that comments have been added.
$comments = get_collection_comments($collection);
if (count($comments) == 0 && $feedback_resource_select == false) {
    $errors = $lang["feedbacknocomments"];
}
if (getval("save", "") != "") {
    # Save comment
    $comment = trim(getvalescaped("comment", ""));
    send_collection_feedback($collection, $comment);
    # Stay on this page for external access users (no access to search)
    refresh_collection_frame();
    $done = true;
}
$headerinsert .= "<script src=\"../lib/lightbox/js/jquery.lightbox-0.5.min.js\" type=\"text/javascript\"></script>";
$headerinsert .= "<link type=\"text/css\" href=\"../lib/lightbox/css/jquery.lightbox-0.5.css?css_reload_key=" . $css_reload_key . "\" rel=\"stylesheet\">";
include "../include/header.php";
?>
function send_collection_feedback($collection, $comment)
{
    # Sends the feedback to the owner of the collection.
    global $applicationname, $lang, $userfullname, $userref, $k, $feedback_resource_select, $feedback_email_required, $regex_email;
    $cinfo = get_collection($collection);
    if ($cinfo === false) {
        exit("Collection not found");
    }
    $user = get_user($cinfo["user"]);
    $body = $lang["collectionfeedbackemail"] . "\n\n";
    if (isset($userfullname)) {
        $body .= $lang["user"] . ": " . $userfullname . "\n";
    } else {
        # External user.
        if ($feedback_email_required && !preg_match("/{$regex_email}/", getvalescaped("email", ""))) {
            $errors[] = $lang["youremailaddress"] . ": " . $lang["requiredfield"];
            return $errors;
        }
        $body .= $lang["fullname"] . ": " . getval("name", "") . "\n";
        $body .= $lang["email"] . ": " . getval("email", "") . "\n";
    }
    $body .= $lang["message"] . ": " . stripslashes(str_replace("\\r\\n", "\n", trim($comment)));
    $f = get_collection_comments($collection);
    for ($n = 0; $n < count($f); $n++) {
        $body .= "\n\n" . $lang["resourceid"] . ": " . $f[$n]["resource"];
        $body .= "\n" . $lang["comment"] . ": " . trim($f[$n]["comment"]);
        if (is_numeric($f[$n]["rating"])) {
            $body .= "\n" . $lang["rating"] . ": " . substr("**********", 0, $f[$n]["rating"]);
        }
    }
    if ($feedback_resource_select) {
        $body .= "\n\n" . $lang["selectedresources"] . ": ";
        $file_list = "";
        $result = do_search("!collection" . $collection);
        for ($n = 0; $n < count($result); $n++) {
            $ref = $result[$n]["ref"];
            if (getval("select_" . $ref, "") != "") {
                global $filename_field;
                $filename = get_data_by_field($ref, $filename_field);
                $body .= "\n" . $ref . " : " . $filename;
                # Append to a file list that is compatible with Adobe Lightroom
                if ($file_list != "") {
                    $file_list .= ", ";
                }
                $s = explode(".", $filename);
                $file_list .= $s[0];
            }
        }
        # Append Lightroom compatible summary.
        $body .= "\n\n" . $lang["selectedresourceslightroom"] . "\n" . $file_list;
    }
    $cc = getval("email", "");
    if (filter_var($cc, FILTER_VALIDATE_EMAIL)) {
        send_mail($user["email"], $applicationname . ": " . $lang["collectionfeedback"] . " - " . $cinfo["name"], $body, "", "", "", NULL, "", $cc);
    } else {
        send_mail($user["email"], $applicationname . ": " . $lang["collectionfeedback"] . " - " . $cinfo["name"], $body);
    }
    # Cancel the feedback request for this resource.
    /* - Commented out - as it may be useful to leave the feedback request in case the user wishes to leave
    	     additional feedback or make changes.
    	     
    	if (isset($userref))
    		{
    		sql_query("update user_collection set request_feedback=0 where collection='$collection' and user='******'");
    		}
    	else
    		{
    		sql_query("update external_access_keys set request_feedback=0 where access_key='$k'");
    		}
    	*/
}