Skip to content

Commit

Permalink
Fix host insert bug
Browse files Browse the repository at this point in the history
  • Loading branch information
NEMS Linux committed Mar 17, 2024
1 parent f0e0de6 commit 372dde9
Showing 1 changed file with 37 additions and 43 deletions.
80 changes: 37 additions & 43 deletions include/functions.php
Expand Up @@ -1222,7 +1222,6 @@ function add_attribute($id, $id_attr, $attr_value){
$attr = array();
$attr["key"] = $id_attr;
$attr["value"] = $attr_value;


# get class_id of item --> for function
$class_id = db_templates("get_classid_of_item", $id);
Expand Down Expand Up @@ -1269,49 +1268,43 @@ function add_attribute($id, $id_attr, $attr_value){
$attr_datatype = db_templates("attr_datatype", $attr["key"]);

# save assign_one/assign_many/assign_cust_order in ItemLinks
foreach ($attr["value"] as $many_attr) {
// Test if $many_attr is an array and that array contains a value
if (is_array($many_attr) && !empty(array_filter($many_attr, 'strlen'))) {
// Check if the "value" key exists and is not empty
if (!empty($many_attr["value"])) {
// Proceed with your code
$check = check_link_as_child_or_bidirectional($attr["key"], $class_id);
if ($check === TRUE) {
$query = 'INSERT INTO ItemLinks
(fk_id_item, fk_item_linked2, fk_id_attr, cust_order)
VALUES
('.$many_attr["value"].', '.$id.', '.$attr["key"].', '.$cust_order.')
';
} else {
$query = 'INSERT INTO ItemLinks
(fk_id_item, fk_item_linked2, fk_id_attr, cust_order)
VALUES
('.$id.', '.$many_attr["value"].', '.$attr["key"].', '.$cust_order.')
';
}

if (DB_NO_WRITES != 1) {
$result_insert = db_handler($query, "insert", "Insert");
if ($result_insert) {
history_add("assigned", $attr["key"], $many_attr["value"], $id, "resolve_assignment");
message('DEBUG', '', "ok");
} else {
message('ERROR', 'Error when linking '.$many_attr["value"].' with '.$attr["key"].':'.$query);
}
}

// Increase assign_cust_order if needed
if ($attr_datatype == "assign_cust_order") {
$cust_order++;
}
}
} else {
// Handle the case where $many_attr is not an array (perhaps log an error or skip)
// Generates false errors when inserting new check commands, so don't output error.
// message('ERROR', 'Invalid data format for $many_attr: ' . var_export($many_attr, true));
}
// }
foreach ($attr["value"] as $many_attr["value"]) {
# if value is empty go to next one
if (!$many_attr["value"]){
continue;
}else{

# create insert query
$check = check_link_as_child_or_bidirectional($attr["key"], $class_id);
if ( $check === TRUE ){
$query = 'INSERT INTO ItemLinks
(fk_id_item, fk_item_linked2, fk_id_attr, cust_order)
VALUES
('.$many_attr["value"].', '.$id.', '.$attr["key"].', '.$cust_order.')
';
}else{
$query = 'INSERT INTO ItemLinks
(fk_id_item, fk_item_linked2, fk_id_attr, cust_order)
VALUES
('.$id.', '.$many_attr["value"].', '.$attr["key"].', '.$cust_order.')
';
}

if (DB_NO_WRITES != 1) {
$result_insert = db_handler($query, "insert", "Insert");
if ( $result_insert ){
history_add("assigned", $attr["key"], $many_attr["value"], $id, "resolve_assignment");
message ('DEBUG', '', "ok");
//message ('DEBUG', 'Successfully linked "'.$many_attr["value"].'" with '.$attr["key"]);
}else{
message ('ERROR', 'Error when linking '.$many_attr["value"].' with '.$attr["key"].':'.$query);
}
}

# increase assign_cust_order if needed
if ($attr_datatype == "assign_cust_order") $cust_order++;

}
}
}
}
Expand All @@ -1321,6 +1314,7 @@ function add_attribute($id, $id_attr, $attr_value){




### read_attributes
#
# $config_class: name of class
Expand Down

0 comments on commit 372dde9

Please sign in to comment.