'; $chosenjslink = ''; if (!$ajax) { $headerinsert .= $chosencsslink; $headerinsert .= $chosenjslink; } $new_node_record_form_action = generateURL("{$baseurl}/pages/admin/admin_manage_field_options.php", ['field' => $field]); $activation_action_label_for = fn(array $node): string => node_is_active($node) ? $lang['userpreference_disable_option'] : $lang['userpreference_enable_option']; // Process form requests if ($ajax === 'true' && trim($node_ref) != "" && 0 < $node_ref) { $option_name = trim(getval('option_name', '')); $option_parent = trim(getval('option_parent', '')); $option_new_index = getval('node_order_by', '', true); if ($option_new_index != "") { $option_new_index -= 1; } $node_action = getval('node_action', ''); // [Save Option] if ('save' === $node_action && enforcePostRequest($ajax)) { $response['refresh_page'] = false; $node_ref_data = array(); // If node baing saved has a parent and the parent changes then we need to reload $existing_parent = ""; if (get_node($node_ref, $node_ref_data)) { $existing_parent = $node_ref_data["parent"]; } if ($option_parent != '') { // Incoming parent is populated if ($option_parent == $existing_parent) { // Parent unchanged; no need to refresh } else { // Parent being changed; refresh $response['refresh_page'] = true; } } else { // Incoming parent is blank if ($option_parent == $existing_parent) { // No parent being established; no need to refresh } else { // Parent being removed; refresh $response['refresh_page'] = true; } } // Option order_by is not being sent because that can be asynchronously changed and we might not know about it, // thus this will be checked upon saving the data. If order_by is null / empty string, then we will use the current value set_node($node_ref, $field, $option_name, $option_parent, $option_new_index); // update value of corresponding fieldx update_fieldx($field); echo json_encode($response); clear_query_cache("schema"); exit(); } // [Move Option] if (('movedown' === $node_action || 'moveup' === $node_action || 'moveto' === $node_action) && enforcePostRequest($ajax)) { $response['error'] = null; $response['sibling'] = null; $response['refresh_page'] = false; $current_node = array(); if (!get_node($node_ref, $current_node)) { $response['error'] = 'No node found!'; exit(json_encode($response)); } // Locate current node position within its siblings $siblings = get_nodes($field, $current_node['parent']); $current_node_siblings_index = array_search($node_ref, array_column($siblings, 'ref')); $pre_sibling = 0; $post_sibling = 0; if ($option_new_index > count($siblings)) { $option_new_index = count($siblings); } $allow_reordering = false; $new_nodes_order = array(); $url_parameters = array('field' => $field, 'filter_by_name' => $filter_by_name); // Get pre & post siblings of current node // Note: these can be 0 if current node is either first/ last in the list if (1 < count($siblings) && isset($current_node_siblings_index)) { if (isset($siblings[$current_node_siblings_index - 1])) { $pre_sibling = $siblings[$current_node_siblings_index - 1]['ref']; } if (isset($siblings[$current_node_siblings_index + 1])) { $post_sibling = $siblings[$current_node_siblings_index + 1]['ref']; } } // Create the new order for nodes based on direction switch ($node_action) { case 'moveup': $response['sibling'] = $pre_sibling; move_array_element($siblings, $current_node_siblings_index, $current_node_siblings_index - 1); // This is the first node in the list so we can't reorder upwards if (0 < $pre_sibling) { $allow_reordering = true; } break; case 'movedown': $response['sibling'] = $post_sibling; move_array_element($siblings, $current_node_siblings_index, $current_node_siblings_index + 1); // This is the last node in the list so we can't reorder downwards if (0 < $post_sibling) { $allow_reordering = true; } break; case 'moveto': move_array_element($siblings, $current_node_siblings_index, $option_new_index); # issue here // This node is already in this position if ($current_node_siblings_index != $option_new_index) { $allow_reordering = true; $response['refresh_page'] = true; if ($field_data['type'] != 7) { // Not a category tree $per_page = (int) getval('per_page_list', $default_perpage_list, true); $move_to_page_offset = floor($option_new_index / $per_page) * $per_page; $url_parameters['offset'] = $move_to_page_offset; } } break; } $move_to_page_url = generateURL("{$baseurl_short}pages/admin/admin_manage_field_options.php", $url_parameters); $response['url'] = $move_to_page_url; // Create the new array of nodes order foreach ($siblings as $sibling) { $new_nodes_order[] = $sibling['ref']; } if ($allow_reordering) { reorder_node($new_nodes_order); } echo json_encode($response); exit(); } // [Delete Option] if ('delete' === $node_action && enforcePostRequest($ajax)) { delete_node($node_ref); } clear_query_cache("schema"); } // [Toggle tree node] if ('true' === $ajax && 'true' === getval('draw_tree_node_table', '') && 7 == $field_data['type']) { $nodes = get_nodes($field, $node_ref, false, null, null, '', true); $nodes_counter = count($nodes); $i = 0; $node_index = 0; foreach ($nodes as $node) { $last_node = false; $node_index++; if (++$i === $nodes_counter) { $last_node = true; } draw_tree_node_table($node['ref'], $node['resource_type_field'], $node['name'], $node['parent'], $node['order_by'], $last_node, $node['use_count']); } exit(); } // [New Option] $submit_new_option = getval('submit_new_option', ''); if ('true' === $ajax && '' != trim($submit_new_option) && 'add_new' === $submit_new_option && enforcePostRequest($ajax)) { $new_option_name = trim(getval('new_option_name', '')); $new_option_parent = getval('new_option_parent', ''); $new_option_order_by = get_node_order_by($field, 7 == $field_data['type'], $new_option_parent); $new_node_index = $new_option_order_by / 10; $new_record_ref = set_node(null, $field, $new_option_name, $new_option_parent, $new_option_order_by); clear_query_cache("schema"); if (getval("reload", "") == "") { if (isset($new_record_ref) && trim($new_record_ref) != "") { if (FIELD_TYPE_CATEGORY_TREE != $field_data['type'] && (trim($new_option_parent) == "")) { ?>