function inventories_load_file($objects_file)
{
    $file_handle = fopen($objects_file, "r");
    global $config;
    while (!feof($file_handle)) {
        $create = true;
        $line = fgets($file_handle);
        if ($line == '' || !isset($line)) {
            continue;
        }
        preg_match_all('/(.*),/', $line, $matches);
        $values = explode(',', $line);
        $id_object_type = $values[0];
        $owner = $values[1];
        $name = $values[2];
        $public = $values[3];
        $description = $values[4];
        $id_contract = $values[5];
        $id_manufacturer = $values[6];
        $id_parent = $values[7];
        $id_companies = $values[8];
        $id_users = $values[9];
        $status = $values[10];
        if ($id_companies != '') {
            $id_companies_arr = explode(';', $id_companies);
        } else {
            $id_companies_arr = array();
        }
        if ($id_users != '') {
            $id_users_arr = explode(';', $id_users);
        } else {
            $id_users_arr = array();
        }
        $value = array('id_object_type' => $id_object_type, 'owner' => $owner, 'name' => safe_input($name), 'public' => $public, 'description' => safe_input($description), 'id_contract' => $id_contract, 'id_manufacturer' => $id_manufacturer, 'id_parent' => $id_parent, 'status' => $status, 'last_update' => date("Y/m/d", get_system_time()));
        if ($name == '') {
            echo "<h3 class='error'>" . __('Inventory name empty') . "</h3>";
            $create = false;
        } else {
            $inventory_id = get_db_value('id', 'tinventory', 'name', $name);
            if ($inventory_id != false) {
                echo "<h3 class='error'>" . __('Inventory ') . $name . __(' already exists') . "</h3>";
                $create = false;
            }
        }
        if ($id_contract != 0 && $id_contract != '') {
            $exists = get_db_value('id', 'tcontract', 'id', $id_contract);
            if (!$exists) {
                echo "<h3 class='error'>" . __('Contract ') . $id_contract . __(' doesn\'t exist') . "</h3>";
                $create = false;
            }
        }
        if ($id_manufacturer != 0 && $id_manufacturer != '') {
            $exists = get_db_value('id', 'tmanufacturer', 'id', $id_manufacturer);
            if (!$exists) {
                echo "<h3 class='error'>" . __('Manufacturer ') . $id_manufacturer . __(' doesn\'t exist') . "</h3>";
                $create = false;
            }
        }
        if ($id_object_type != 0 && $id_object_type != '') {
            $exists_object_type = get_db_value('id', 'tobject_type', 'id', $id_object_type);
            if (!$exists_object_type) {
                echo "<h3 class='error'>" . __('Object type ') . $id_object_type . __(' doesn\'t exist') . "</h3>";
                $create = false;
            } else {
                //~ $all_fields = inventories_get_all_type_field ($id_object_type);
                $sql = "SELECT * FROM tobject_type_field WHERE id_object_type=" . $id_object_type;
                $all_fields = get_db_all_rows_sql($sql);
                if ($all_fields == false) {
                    $all_fields = array();
                }
                $value_data = array();
                $i = 11;
                $j = 0;
                foreach ($all_fields as $key => $field) {
                    $data = $values[$i];
                    switch ($field['type']) {
                        case 'combo':
                            $combo_val = explode(",", $field['combo_value']);
                            $k = array_search($data, $combo_val);
                            if ($k === false) {
                                echo "<h3 class='error'>" . __('Field ') . $field['label'] . __(' doesn\'t match. Valid values: ') . $field['combo_value'] . "</h3>";
                                $create = false;
                            }
                            break;
                        case 'numeric':
                            $res = is_numeric($data);
                            if (!$res) {
                                echo "<h3 class='error'>" . __('Field ') . $field['label'] . __(' must be numeric') . "</h3>";
                                $create = false;
                            }
                            break;
                        case 'external':
                            $table_ext = $field['external_table_name'];
                            $exists_table = get_db_sql("SHOW TABLES LIKE '{$table_ext}'");
                            if (!$exists_table) {
                                echo "<h3 class='error'>" . __('External table ') . $table_ext . __(' doesn\'t exist') . "</h3>";
                                $create = false;
                            }
                            $id = $field['external_reference_field'];
                            $exists_id = get_db_sql("SELECT {$id} FROM {$table_ext}");
                            if (!$exists_id) {
                                echo "<h3 class='error'>" . __('Id ') . $id . __(' doesn\'t exist') . "</h3>";
                                $create = false;
                            }
                            break;
                    }
                    if ($field['inherit']) {
                        $ok = inventories_check_unique_field($data, $field['type']);
                        if (!$ok) {
                            echo "<h3 class='error'>" . __('Field ') . $field['label'] . __(' must be unique') . "</h3>";
                            $create = false;
                        }
                    }
                    $value_data[$j]['id_object_type_field'] = $field['id'];
                    $value_data[$j]['data'] = safe_input($data);
                    $i++;
                    $j++;
                }
            }
        }
        if ($create) {
            $result_id = process_sql_insert('tinventory', $value);
            if ($result_id) {
                foreach ($value_data as $k => $val_data) {
                    $val_data['id_inventory'] = $result_id;
                    process_sql_insert('tobject_field_data', $val_data);
                }
                if (!empty($id_companies_arr)) {
                    foreach ($id_companies_arr as $id_company) {
                        $values_company['id_inventory'] = $result_id;
                        $values_company['id_reference'] = $id_company;
                        $values_company['type'] = 'company';
                        process_sql_insert('tinventory_acl', $values_company);
                    }
                }
                if (!empty($id_users_arr)) {
                    foreach ($id_users_arr as $id_user) {
                        $values_user['id_inventory'] = $result_id;
                        $values_user['id_reference'] = $id_user;
                        $values_user['type'] = 'user';
                        process_sql_insert('tinventory_acl', $values_user);
                    }
                }
            }
        }
    }
    //end while
    fclose($file_handle);
    echo "<h3 class='info'>" . __('File loaded') . "</h3>";
    return;
}
        }
    }
    // Does not exist
    echo json_encode(true);
    return;
} elseif ($search_existing_object_type_field_data) {
    require_once 'include/functions_db.php';
    require_once 'include/functions_inventories.php';
    $object_type_field_data = get_parameter('object_type_field_data');
    $object_type_field_id = get_parameter('object_type_field_id');
    $object_type_field_type = get_parameter('object_type_field_type');
    $inventory_id = get_parameter('inventory_id', 0);
    $unique = get_db_value("unique", "tobject_type_field", "id", $object_type_field_id);
    // Checks if the object type field data brokes any unique constraint in the db
    if ($unique) {
        if (!inventories_check_unique_field($object_type_field_data, $object_type_field_type)) {
            if ($inventory_id) {
                $query_result = get_db_value_filter("id", "from tobject_field_data", array('id_inventory' => $inventory_id, 'data' => $object_type_field_data));
                if (!$query_result) {
                    // Exists. Validation error
                    echo json_encode(false);
                    return;
                }
            } else {
                // Exists. Validation error
                echo json_encode(false);
                return;
            }
        }
    } else {
        if (!inventories_check_no_unique_field($object_type_field_data, $object_type_field_type)) {
Example #3
0
 //insert data to incident type fields
 if ($id_object_type != 0) {
     $sql_label = "SELECT `label`, `unique`, `type` FROM `tobject_type_field` WHERE id_object_type = {$id_object_type}";
     $labels = get_db_all_rows_sql($sql_label);
     if ($labels === false) {
         $labels = array();
     }
     foreach ($labels as $label) {
         $id_object_field = get_db_value_filter('id', 'tobject_type_field', array('id_object_type' => $id_object_type, 'label' => $label['label']), 'AND');
         $values_insert['id_inventory'] = $id;
         //~ $values_insert['data'] = get_parameter (base64_encode($label['label']));
         $data_name = get_parameter(base64_encode($label['label']));
         $data_name_arr = explode("#", $data_name);
         $values_insert['data'] = $data_name_arr[0];
         if ($label['unique']) {
             $is_unique = inventories_check_unique_field($values_insert['data'], $label['type']);
             if (!$is_unique) {
                 $msg_err .= '<h3 class="err">' . __(" Field '") . $label['label'] . __("' not created. Value must be unique") . '</h3>';
             }
         }
         $values_insert['id_object_type_field'] = $id_object_field;
         $id_object_type_field = get_db_value('id', 'tobject_type_field', 'id_object_type', $id_object_type);
         if ($is_unique) {
             process_sql_insert('tobject_field_data', $values_insert);
         }
     }
     inventory_tracking($id, INVENTORY_OBJECT_TYPE, $id_object_type);
 }
 //parent
 if ($id_parent != 0) {
     $id_object_type_inherit = get_db_value('id_object_type', 'tinventory', 'id', $id_parent);