function ProcessFolder($folder, $version_dir, &$resource_array, &$resource_error)
{
    global $lang, $syncdir, $nogo, $staticsync_max_files, $count, $done, $modtimes, $lastsync, $ffmpeg_preview_extension, $staticsync_autotheme, $staticsync_folder_structure, $staticsync_extension_mapping_default, $staticsync_extension_mapping, $staticsync_mapped_category_tree, $staticsync_title_includes_path, $staticsync_ingest, $staticsync_mapfolders, $staticsync_alternatives_suffix, $theme_category_levels, $staticsync_defaultstate, $additional_archive_states, $staticsync_extension_mapping_append_values, $image_alternatives, $exclude_resize, $post_host, $media_endpoint, $image_required_height, $sync_bucket, $aws_key, $aws_secret_key;
    $collection = 0;
    echo "Processing Folder: {$folder}" . PHP_EOL;
    #$alt_path = get_resource_path(59, TRUE, '', FALSE, 'png', -1, 1, FALSE, '', 4);
    # List all files in this folder.
    $dh = opendir($folder);
    while (($file = readdir($dh)) !== false) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        $filetype = filetype($folder . "/" . $file);
        $fullpath = $folder . "/" . $file;
        $shortpath = str_replace($syncdir . "/", '', $fullpath);
        # Work out extension
        $extension = explode(".", $file);
        if (count($extension) > 1) {
            $extension = trim(strtolower($extension[count($extension) - 1]));
        } else {
            //No extension
            $extension = "";
        }
        if (strpos($fullpath, $nogo)) {
            echo "This directory is to be ignored." . PHP_EOL;
            continue;
        }
        if ($staticsync_mapped_category_tree) {
            $path_parts = explode("/", $shortpath);
            array_pop($path_parts);
            touch_category_tree_level($path_parts);
        }
        # -----FOLDERS-------------
        if (($filetype == "dir" || $filetype == "link") && strpos($nogo, "[{$file}]") === false && strpos($file, $staticsync_alternatives_suffix) === false) {
            # Get current version direcotries.
            if (preg_match("/[0-9]{2}-[0-9]{2}-[0-9]{4}\$/", $file)) {
                if (!in_array($file, $version_dir)) {
                    array_push($version_dir, $file);
                }
                if (preg_match('/in_progress*/', $file)) {
                    echo "The Barcode is still being processed." . PHP_EOL;
                    continue;
                }
            }
            # Recurse
            ProcessFolder($folder . "/" . $file, $version_dir, $resource_array, $resource_error);
        }
        $psd_files = array();
        if (preg_match('/images/', $fullpath)) {
            $path_array = explode('/', $fullpath);
            $psd_array = array_splice($path_array, 0, array_search('images', $path_array));
            $psd_path = implode('/', $psd_array) . '/psd/';
            $psd_files = array_diff(scandir($psd_path), array('..', '.'));
            foreach ($psd_files as $index => $psd_file) {
                $psd_files[$index] = pathinfo($psd_file, PATHINFO_FILENAME);
            }
        }
        # -------FILES---------------
        if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
            /* Below Code Adapted  from CMay's bug report */
            global $banned_extensions;
            # Check to see if extension is banned, do not add if it is banned
            if (array_search($extension, $banned_extensions)) {
                continue;
            }
            /* Above Code Adapted from CMay's bug report */
            $count++;
            if ($count > $staticsync_max_files) {
                return true;
            }
            $last_sync_date = sql_value("select value from sysvars where name = 'last_sync'", "");
            $file_creation_date = date("Y-m-d H:i:s", filectime($fullpath));
            if (isset($last_sync_date) && $last_sync_date > $file_creation_date) {
                echo "No new file found.." . PHP_EOL;
                continue;
            }
            # Already exists?
            if (!isset($done[$shortpath])) {
                echo "Processing file: {$fullpath}" . PHP_EOL;
                if ($collection == 0 && $staticsync_autotheme) {
                    # Make a new collection for this folder.
                    $e = explode("/", $shortpath);
                    $theme = ucwords($e[0]);
                    $themesql = "theme='" . ucwords(escape_check($e[0])) . "'";
                    $themecolumns = "theme";
                    $themevalues = "'" . ucwords(escape_check($e[0])) . "'";
                    if ($staticsync_folder_structure) {
                        for ($x = 0; $x < count($e) - 1; $x++) {
                            if ($x != 0) {
                                $themeindex = $x + 1;
                                if ($themeindex > $theme_category_levels) {
                                    $theme_category_levels = $themeindex;
                                    if ($x == count($e) - 2) {
                                        echo PHP_EOL . PHP_EOL . "UPDATE THEME_CATEGORY_LEVELS TO {$themeindex} IN CONFIG!!!!" . PHP_EOL . PHP_EOL;
                                    }
                                }
                                $th_name = ucwords(escape_check($e[$x]));
                                $themesql .= " AND theme{$themeindex} = '{$th_name}'";
                                $themevalues .= ",'{$th_name}'";
                                $themecolumns .= ",theme{$themeindex}";
                            }
                        }
                    }
                    $name = count($e) == 1 ? '' : $e[count($e) - 2];
                    echo "Collection {$name}, theme={$theme}" . PHP_EOL;
                    $ul_username = $theme;
                    $escaped_name = escape_check($name);
                    $collection = sql_value("SELECT ref value FROM collection WHERE name='{$escaped_name}' AND {$themesql}", 0);
                    if ($collection == 0) {
                        sql_query("INSERT INTO collection (name,created,public,{$themecolumns},allow_changes)\n                                                   VALUES ('{$escaped_name}', NOW(), 1, {$themevalues}, 0)");
                        $collection = sql_insert_id();
                    }
                }
                # Work out a resource type based on the extension.
                $type = $staticsync_extension_mapping_default;
                reset($staticsync_extension_mapping);
                foreach ($staticsync_extension_mapping as $rt => $extensions) {
                    if (in_array($extension, $extensions)) {
                        $type = $rt;
                    }
                }
                $modified_type = hook('modify_type', 'staticsync', array($type));
                if (is_numeric($modified_type)) {
                    $type = $modified_type;
                }
                # Formulate a title
                if ($staticsync_title_includes_path) {
                    $title_find = array('/', '_', ".{$extension}");
                    $title_repl = array(' - ', ' ', '');
                    $title = ucfirst(str_ireplace($title_find, $title_repl, $shortpath));
                } else {
                    $title = str_ireplace(".{$extension}", '', $file);
                }
                $modified_title = hook('modify_title', 'staticsync', array($title));
                if ($modified_title !== false) {
                    $title = $modified_title;
                }
                # Import this file
                #$r = import_resource($shortpath, $type, $title, $staticsync_ingest);
                #Check for file name containing the psd.
                if (!empty($psd_files)) {
                    $image_file_array = explode('/', $fullpath);
                    $image_file = $image_file_array[count($image_file_array) - 1];
                    $image_psd_name = explode('_', $image_file)[0];
                    if (array_search($image_psd_name, $psd_files)) {
                        #Image name is in right format.
                        if (!validate_image_size($fullpath, $image_required_height)) {
                            $resource_error['size'][$file] = $fullpath;
                        }
                        $r = import_resource($fullpath, $type, $title, $staticsync_ingest);
                        sql_query("INSERT INTO resource_data (resource,resource_type_field,value)\n                               VALUES ('{$r}', (SELECT ref FROM resource_type_field WHERE name = 'logical_id'), '{$image_psd_name}')");
                        $original_filepath = sql_query("SELECT value FROM resource_data WHERE resource = '{$r}' AND\n                                                     resource_type_field = (SELECT ref FROM resource_type_field where name = 'original_filepath')");
                        if (isset($original_filepath)) {
                            sql_query("INSERT INTO resource_data (resource,resource_type_field,value)\n                                 VALUES ('{$r}',(SELECT ref FROM resource_type_field WHERE name = 'original_filepath'), '{$fullpath}')");
                        }
                    } else {
                        echo "Filename '{$fullpath}' is not in right format.." . PHP_EOL;
                        $resource_error['name'][$file] = $fullpath;
                        continue;
                    }
                } elseif (word_in_string($exclude_resize, explode('/', $fullpath))) {
                    $r = import_resource($fullpath, $type, $title, $staticsync_ingest);
                }
                if ($r !== false) {
                    array_push($resource_array, $r);
                    # Create current version for resource.
                    #print_r($version_dir);
                    if (count($version_dir) == 1) {
                        sql_query("INSERT into resource_data (resource,resource_type_field,value)\n                                    VALUES ('{$r}',(SELECT ref FROM resource_type_field WHERE name = 'current'), 'TRUE')");
                    }
                    $sync_status = sync_to_s3($syncdir, $sync_bucket, $aws_key, $aws_secret_key);
                    if (!$sync_status) {
                        echo "Failed to sync";
                    }
                    # Add to mapped category tree (if configured)
                    if (isset($staticsync_mapped_category_tree)) {
                        $basepath = '';
                        # Save tree position to category tree field
                        # For each node level, expand it back to the root so the full path is stored.
                        for ($n = 0; $n < count($path_parts); $n++) {
                            if ($basepath != '') {
                                $basepath .= "~";
                            }
                            $basepath .= $path_parts[$n];
                            $path_parts[$n] = $basepath;
                        }
                        update_field($r, $staticsync_mapped_category_tree, "," . join(",", $path_parts));
                    }
                    #This is an override to add user data to the resouces
                    if (!isset($userref)) {
                        $ul_username = ucfirst(strtolower($ul_username));
                        $current_user_ref = sql_query("Select ref from user where username = '******' ");
                        if (!empty($current_user_ref)) {
                            $current_user_ref = $current_user_ref[0]['ref'];
                            sql_query("UPDATE resource SET created_by='{$current_user_ref}' where ref = {$r}");
                        }
                    }
                    # default access level. This may be overridden by metadata mapping.
                    $accessval = 0;
                    # StaticSync path / metadata mapping
                    # Extract metadata from the file path as per $staticsync_mapfolders in config.php
                    if (isset($staticsync_mapfolders)) {
                        foreach ($staticsync_mapfolders as $mapfolder) {
                            $match = $mapfolder["match"];
                            $field = $mapfolder["field"];
                            $level = $mapfolder["level"];
                            if (strpos("/" . $shortpath, $match) !== false) {
                                # Match. Extract metadata.
                                $path_parts = explode("/", $shortpath);
                                if ($level < count($path_parts)) {
                                    // special cases first.
                                    if ($field == 'access') {
                                        # access level is a special case
                                        # first determine if the value matches a defined access level
                                        $value = $path_parts[$level - 1];
                                        for ($n = 0; $n < 3; $n++) {
                                            # if we get an exact match or a match except for case
                                            if ($value == $lang["access" . $n] || strtoupper($value) == strtoupper($lang['access' . $n])) {
                                                $accessval = $n;
                                                echo "Will set access level to " . $lang['access' . $n] . " ({$n})" . PHP_EOL;
                                            }
                                        }
                                    } else {
                                        if ($field == 'archive') {
                                            # archive level is a special case
                                            # first determin if the value matches a defined archive level
                                            $value = $mapfolder["archive"];
                                            $archive_array = array_merge(array(-2, -1, 0, 1, 2, 3), $additional_archive_states);
                                            if (in_array($value, $archive_array)) {
                                                $archiveval = $value;
                                                echo "Will set archive level to " . $lang['status' . $value] . " ({$archiveval})" . PHP_EOL;
                                            }
                                        } else {
                                            # Save the value
                                            #print_r($path_parts);
                                            $value = $path_parts[$level - 1];
                                            if ($staticsync_extension_mapping_append_values) {
                                                $given_value = $value;
                                                // append the values if possible...not used on dropdown, date, categroy tree, datetime, or radio buttons
                                                $field_info = get_resource_type_field($field);
                                                if (in_array($field['type'], array(0, 1, 2, 4, 5, 6, 7, 8))) {
                                                    $old_value = sql_value("select value value from resource_data where resource={$r} and resource_type_field={$field}", "");
                                                    $value = append_field_value($field_info, $value, $old_value);
                                                }
                                            }
                                            update_field($r, $field, trim($value));
                                            if (strtotime(trim($value))) {
                                                add_keyword_mappings($r, trim($value), $field, false, true);
                                            } else {
                                                add_keyword_mappings($r, trim($value), $field);
                                            }
                                            if ($staticsync_extension_mapping_append_values) {
                                                $value = $given_value;
                                            }
                                            echo " - Extracted metadata from path: {$value}" . PHP_EOL;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #Resize only original images.
                    if (!word_in_string($exclude_resize, explode('/', $fullpath))) {
                        echo "Creating preview..";
                        create_previews($r, false, $extension, false, false, -1, false, $staticsync_ingest);
                    }
                    # update access level
                    sql_query("UPDATE resource SET access = '{$accessval}',archive='{$staticsync_defaultstate}' WHERE ref = '{$r}'");
                    # Add any alternative files
                    $altpath = $fullpath . $staticsync_alternatives_suffix;
                    if ($staticsync_ingest && file_exists($altpath)) {
                        $adh = opendir($altpath);
                        while (($altfile = readdir($adh)) !== false) {
                            $filetype = filetype($altpath . "/" . $altfile);
                            if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
                                # Create alternative file
                                # Find extension
                                $ext = explode(".", $altfile);
                                $ext = $ext[count($ext) - 1];
                                $description = str_replace("?", strtoupper($ext), $lang["originalfileoftype"]);
                                $file_size = filesize_unlimited($altpath . "/" . $altfile);
                                $aref = add_alternative_file($r, $altfile, $description, $altfile, $ext, $file_size);
                                $path = get_resource_path($r, true, '', true, $ext, -1, 1, false, '', $aref);
                                rename($altpath . "/" . $altfile, $path);
                                # Move alternative file
                            }
                        }
                    }
                    # Add to collection
                    if ($staticsync_autotheme) {
                        $test = '';
                        $test = sql_query("SELECT * FROM collection_resource WHERE collection='{$collection}' AND resource='{$r}'");
                        if (count($test) == 0) {
                            sql_query("INSERT INTO collection_resource (collection, resource, date_added)\n                                            VALUES ('{$collection}', '{$r}', NOW())");
                        }
                    }
                } else {
                    # Import failed - file still being uploaded?
                    echo " *** Skipping file - it was not possible to move the file (still being imported/uploaded?)" . PHP_EOL;
                }
            } else {
                # check modified date and update previews if necessary
                $filemod = filemtime($fullpath);
                if (array_key_exists($shortpath, $modtimes) && $filemod > strtotime($modtimes[$shortpath])) {
                    # File has been modified since we last created previews. Create again.
                    $rd = sql_query("SELECT ref, has_image, file_modified, file_extension FROM resource\n                                        WHERE file_path='" . escape_check($shortpath) . "'");
                    if (count($rd) > 0) {
                        $rd = $rd[0];
                        $rref = $rd["ref"];
                        echo "Resource {$rref} has changed, regenerating previews: {$fullpath}" . PHP_EOL;
                        extract_exif_comment($rref, $rd["file_extension"]);
                        # extract text from documents (e.g. PDF, DOC).
                        global $extracted_text_field;
                        if (isset($extracted_text_field)) {
                            if (isset($unoconv_path) && in_array($extension, $unoconv_extensions)) {
                                // omit, since the unoconv process will do it during preview creation below
                            } else {
                                extract_text($rref, $extension);
                            }
                        }
                        # Store original filename in field, if set
                        global $filename_field;
                        if (isset($filename_field)) {
                            update_field($rref, $filename_field, $file);
                        }
                        create_previews($rref, false, $rd["file_extension"], false, false, -1, false, $staticsync_ingest);
                        sql_query("UPDATE resource SET file_modified=NOW() WHERE ref='{$rref}'");
                    }
                }
            }
        }
    }
}
 function save_resource_data_multi($collection)
 {
     # Save all submitted data for collection $collection, this is for the 'edit multiple resources' feature
     # Loop through the field data and save (if necessary)
     $list = get_collection_resources($collection);
     $errors = array();
     $tmp = hook("altercollist", "", array("save_resource_data_multi", $list));
     if (is_array($tmp)) {
         if (count($tmp) > 0) {
             $list = $tmp;
         } else {
             return true;
         }
     }
     // alter the collection list to spare some when saving multiple, if you need
     $ref = $list[0];
     $fields = get_resource_field_data($ref, true);
     global $auto_order_checkbox, $auto_order_checkbox_case_insensitive;
     $expiry_field_edited = false;
     for ($n = 0; $n < count($fields); $n++) {
         if (getval("editthis_field_" . $fields[$n]["ref"], "") != "" || hook("save_resource_data_multi_field_decision", "", array($fields[$n]["ref"]))) {
             if ($fields[$n]["type"] == 2) {
                 # construct the value from the ticked boxes
                 $val = ",";
                 # Note: it seems wrong to start with a comma, but this ensures it is treated as a comma separated list by split_keywords(), so if just one item is selected it still does individual word adding, so 'South Asia' is split to 'South Asia','South','Asia'.
                 $options = trim_array(explode(",", $fields[$n]["options"]));
                 if ($auto_order_checkbox) {
                     if ($auto_order_checkbox_case_insensitive) {
                         natcasesort($options);
                     } else {
                         sort($options);
                     }
                 }
                 for ($m = 0; $m < count($options); $m++) {
                     $name = $fields[$n]["ref"] . "_" . md5($options[$m]);
                     if (getval($name, "") == "yes") {
                         if ($val != ",") {
                             $val .= ",";
                         }
                         $val .= $options[$m];
                     }
                 }
             } elseif ($fields[$n]["type"] == 4 || $fields[$n]["type"] == 6 || $fields[$n]["type"] == 10) {
                 # date/expiry date type, construct the value from the date dropdowns
                 $val = sprintf("%04d", getvalescaped("field_" . $fields[$n]["ref"] . "-y", ""));
                 if ((int) $val <= 0) {
                     $val = "";
                 } elseif (($field = getvalescaped("field_" . $fields[$n]["ref"] . "-m", "")) != "") {
                     $val .= "-" . $field;
                     if (($field = getvalescaped("field_" . $fields[$n]["ref"] . "-d", "")) != "") {
                         $val .= "-" . $field;
                         if (($field = getval("field_" . $fields[$n]["ref"] . "-h", "")) != "") {
                             $val .= " " . $field . ":";
                             if (($field = getvalescaped("field_" . $fields[$n]["ref"] . "-i", "")) != "") {
                                 $val .= $field;
                             } else {
                                 $val .= "00";
                             }
                         }
                     }
                 }
             } elseif ($fields[$n]["type"] == 3) {
                 $val = getvalescaped("field_" . $fields[$n]["ref"], "");
                 // if it doesn't already start with a comma, add one
                 if (substr($val, 0, 1) != ',') {
                     $val = ',' . $val;
                 }
             } else {
                 $val = getvalescaped("field_" . $fields[$n]["ref"], "");
             }
             $origval = $val;
             # Loop through all the resources and save.
             for ($m = 0; $m < count($list); $m++) {
                 $ref = $list[$m];
                 $resource_sql = "";
                 # Work out existing field value.
                 $existing = escape_check(sql_value("select value from resource_data where resource='{$ref}' and resource_type_field='" . $fields[$n]["ref"] . "'", ""));
                 # Find and replace mode? Perform the find and replace.
                 if (getval("modeselect_" . $fields[$n]["ref"], "") == "FR") {
                     $val = str_replace(getvalescaped("find_" . $fields[$n]["ref"], ""), getvalescaped("replace_" . $fields[$n]["ref"], ""), $existing);
                 }
                 # Append text/option(s) mode?
                 if (getval("modeselect_" . $fields[$n]["ref"], "") == "AP") {
                     $val = append_field_value($fields[$n], $origval, $existing);
                 }
                 # Prepend text/option(s) mode?
                 if (getval("modeselect_" . $fields[$n]["ref"], "") == "PP") {
                     global $filename_field;
                     if ($fields[$n]["ref"] == $filename_field) {
                         $val = rtrim($origval, "_") . "_" . trim($existing);
                         // use an underscore if editing filename.
                     } else {
                         # Automatically append a space when appending text types.
                         $val = $origval . " " . $existing;
                     }
                 }
                 # Remove text/option(s) mode?
                 if (getval("modeselect_" . $fields[$n]["ref"], "") == "RM") {
                     $val = str_replace($origval, "", $existing);
                 }
                 # Possibility to hook in and alter the value - additional mode support
                 $hookval = hook("save_resource_data_multi_extra_modes", "", array($ref, $fields[$n]));
                 if ($hookval !== false) {
                     $val = $hookval;
                 }
                 $val = strip_leading_comma($val);
                 #echo "<li>existing=$existing, new=$val";
                 if ($existing !== str_replace("\\", "", $val)) {
                     # This value is different from the value we have on record.
                     # Write this edit to the log.
                     resource_log($ref, 'm', $fields[$n]["ref"], "", $existing, $val);
                     # Expiry field? Set that expiry date(s) have changed so the expiry notification flag will be reset later in this function.
                     if ($fields[$n]["type"] == 6) {
                         $expiry_field_edited = true;
                     }
                     # If this is a 'joined' field we need to add it to the resource column
                     $joins = get_resource_table_joins();
                     if (in_array($fields[$n]["ref"], $joins)) {
                         sql_query("update resource set field" . $fields[$n]["ref"] . "='" . escape_check($val) . "' where ref='{$ref}'");
                     }
                     # Purge existing data and keyword mappings, decrease keyword hitcounts.
                     sql_query("delete from resource_data where resource='{$ref}' and resource_type_field='" . $fields[$n]["ref"] . "'");
                     # Insert new data and keyword mappings, increase keyword hitcounts.
                     sql_query("insert into resource_data(resource,resource_type_field,value) values('{$ref}','" . $fields[$n]["ref"] . "','" . escape_check($val) . "')");
                     $oldval = $existing;
                     $newval = $val;
                     if ($fields[$n]["type"] == 3) {
                         # Prepend a comma when indexing dropdowns
                         $newval = "," . $val;
                         $oldval = "," . $oldval;
                     }
                     if ($fields[$n]["keywords_index"] == 1) {
                         # Date field? These need indexing differently.
                         $is_date = $fields[$n]["type"] == 4 || $fields[$n]["type"] == 6;
                         $is_html = $fields[$n]["type"] == 8;
                         remove_keyword_mappings($ref, i18n_get_indexable($oldval), $fields[$n]["ref"], $fields[$n]["partial_index"], $is_date, '', '', $is_html);
                         add_keyword_mappings($ref, i18n_get_indexable($newval), $fields[$n]["ref"], $fields[$n]["partial_index"], $is_date, '', '', $is_html);
                     }
                     # Add any onchange code
                     if ($fields[$n]["onchange_macro"] != "") {
                         eval($fields[$n]["onchange_macro"]);
                     }
                 }
             }
         }
     }
     # Also save related resources field
     if (getval("editthis_related", "") != "") {
         $related = explode(",", getvalescaped("related", ""));
         # Make sure all submitted values are numeric
         $ok = array();
         for ($n = 0; $n < count($related); $n++) {
             if (is_numeric(trim($related[$n]))) {
                 $ok[] = trim($related[$n]);
             }
         }
         for ($m = 0; $m < count($list); $m++) {
             $ref = $list[$m];
             sql_query("delete from resource_related where resource='{$ref}' or related='{$ref}'");
             # remove existing related items
             if (count($ok) > 0) {
                 sql_query("insert into resource_related(resource,related) values ({$ref}," . join("),(" . $ref . ",", $ok) . ")");
             }
         }
     }
     # Also update archive status
     global $user_resources_approved_email, $email_notify;
     if (getval("editthis_status", "") != "") {
         $notifyrefs = array();
         $usernotifyrefs = array();
         for ($m = 0; $m < count($list); $m++) {
             $ref = $list[$m];
             if (!hook('forbidsavearchive', '', array($errors))) {
                 # Also update archive status
                 $oldarchive = sql_value("select archive value from resource where ref='{$ref}'", "");
                 $setarchivestate = getvalescaped("status", $oldarchive, true);
                 // We used to get the 'archive' value but this conflicts with the archiveused for searching
                 if ($setarchivestate != $oldarchive && !checkperm("e" . $setarchivestate)) {
                     $setarchivestate = $oldarchive;
                 }
                 if ($setarchivestate != $oldarchive) {
                     sql_query("update resource set archive='" . $setarchivestate . "' where ref='{$ref}'");
                     if ($setarchivestate != $oldarchive && $ref > 0) {
                         resource_log($ref, "s", 0, "", $oldarchive, $setarchivestate);
                     }
                     # Check states to see if notifications are necessary
                     if ($oldarchive == -2 && $setarchivestate == -1 || $oldarchive == -1 && $setarchivestate == -2 || $user_resources_approved_email && ($oldarchive == -2 || $oldarchive == -1) && $setarchivestate == 0) {
                         $notifyrefs[] = $ref;
                     }
                 }
             }
         }
         if (count($notifyrefs) > 0) {
             if ($user_resources_approved_email && ($oldarchive == -2 || $oldarchive == -1) && $setarchivestate == 0) {
                 debug("Emailing approval notification for submitted resources to users");
                 notify_user_resources_approved($notifyrefs);
             }
             if ($oldarchive == -2 && $setarchivestate == -1) {
                 debug("Emailing notification of submitted resources to " . $email_notify);
                 notify_user_contributed_submitted($notifyrefs);
             }
             if ($oldarchive == -1 && $setarchivestate == -2) {
                 debug("Emailing notification of unsubmitted resources to " . $email_notify);
                 notify_user_contributed_unsubmitted($notifyrefs);
             }
         }
     }
     # Expiry field(s) edited? Reset the notification flag so that warnings are sent again when the date is reached.
     if ($expiry_field_edited) {
         if (count($list) > 0) {
             sql_query("update resource set expiry_notification_sent=0 where ref in (" . join(",", $list) . ")");
         }
     }
     # Also update access level
     if (getval("editthis_access", "") != "") {
         for ($m = 0; $m < count($list); $m++) {
             $ref = $list[$m];
             $access = getvalescaped("access", 0);
             $oldaccess = sql_value("select access value from resource where ref='{$ref}'", "");
             if ($access != $oldaccess) {
                 sql_query("update resource set access='{$access}' where ref='{$ref}'");
                 if ($oldaccess == 3) {
                     # Moving out of custom access - delete custom usergroup access.
                     delete_resource_custom_access_usergroups($ref);
                 }
                 resource_log($ref, "a", 0, "", $oldaccess, $access);
             }
             # For access level 3 (custom) - also save custom permissions
             if ($access == 3) {
                 save_resource_custom_access($ref);
             }
         }
     }
     # Update resource type?
     if (getval("editresourcetype", "") != "") {
         for ($m = 0; $m < count($list); $m++) {
             $ref = $list[$m];
             update_resource_type($ref, getvalescaped("resource_type", ""));
         }
     }
     # Update location?
     if (getval("editlocation", "") != "") {
         $location = explode(",", getvalescaped("location", ""));
         if (count($list) > 0) {
             if (count($location) == 2) {
                 $geo_lat = (double) $location[0];
                 $geo_long = (double) $location[1];
                 sql_query("update resource set geo_lat={$geo_lat},geo_long={$geo_long} where ref in (" . join(",", $list) . ")");
             } elseif (getvalescaped("location", "") == "") {
                 sql_query("update resource set geo_lat=null,geo_long=null where ref in (" . join(",", $list) . ")");
             }
         }
     }
     # Update mapzoom?
     if (getval("editmapzoom", "") != "") {
         $mapzoom = getvalescaped("mapzoom", "");
         if (count($list) > 0) {
             if ($mapzoom != "") {
                 sql_query("update resource set mapzoom={$mapzoom} where ref in (" . join(",", $list) . ")");
             } else {
                 sql_query("update resource set mapzoom=null where ref in (" . join(",", $list) . ")");
             }
         }
     }
     hook("saveextraresourcedata", "", array($list));
     # Update XML metadata dump file for all edited resources.
     for ($m = 0; $m < count($list); $m++) {
         update_xml_metadump($list[$m]);
     }
     hook("aftersaveresourcedata");
 }