$selected_footprint = NULL; } } catch (Exception $e) { $messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red'); $fatal_error = true; } /******************************************************************************** * * Execute actions * *********************************************************************************/ if (!$fatal_error) { switch ($action) { case 'add': try { $new_footprint = Footprint::add($database, $current_user, $log, $new_name, $new_parent_id, $new_filename); if (!$add_more) { $selected_footprint = $new_footprint; $selected_id = $selected_footprint->get_id(); } } catch (Exception $e) { $messages[] = array('text' => 'Der neue Footprint konnte nicht angelegt werden!', 'strong' => true, 'color' => 'red'); $messages[] = array('text' => 'Fehlermeldung: ' . nl2br($e->getMessage()), 'color' => 'red'); } break; case 'delete': try { if (!is_object($selected_footprint)) { throw new Exception('Es ist kein Footprint markiert oder es trat ein Fehler auf!'); } $parts = $selected_footprint->get_parts();
if (isset($_REQUEST['insert_footprints'])) { $start_time = microtime(true); try { $trans_id = $database->begin_transaction(); for ($i = 0; $i < $_REQUEST['count']; $i++) { $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10); $parent = isset($_REQUEST['with_error']) && $i == $_REQUEST['count'] / 2 ? -1 : 0; if ($i % 3 == 0) { $filename = '/IC_DIP02'; } elseif ($i % 3 == 1) { $filename = '/asdf'; } elseif ($i % 3 == 2) { $filename = ''; } // no filename $footprint = Footprint::add($database, $current_user, $log, $randomString, $parent, $filename); } $database->commit($trans_id); } catch (Exception $e) { $database->rollback(); $footprint_error = $e->getMessage(); } $footprint_milliseconds = (int) (1000 * (microtime(true) - $start_time)); } print 'Anzahl Footprints in der Tabelle: ' . $database->get_count_of_records('footprints') . '<br>'; print '<input type="text" size="6" name="count" value="' . (isset($_REQUEST['count']) ? $_REQUEST['count'] : 100) . '">'; print '<input type="submit" name="insert_footprints" value="Footprints hinzufügen">'; print '<input type="checkbox" name="with_error" ' . (isset($_REQUEST['with_error']) ? 'checked' : '') . '>Mit Fehler in der Hälfte<br>'; if (isset($footprint_milliseconds)) { print 'Zeit: ' . $footprint_milliseconds . 'ms<br>'; }
/** * @brief Import Parts (create Parts, and if neccessary, Categories, Footprints and so on) * * @note This function uses database transactions. If an error occurs, all changes will be rolled back. * * @param Database &$database reference to the database object * @param User &$current_user reference to the user which is logged in * @param Log &$log reference to the Log-object * @param array $data The import data array from "extract_import_data_from_request()" * @param boolean $only_check_data If true, this function will only check if all values in "$data" are valid. * In this case, no parts will be imported! * * @retval array All new Part objects (only if "$only_check_data == false") * * @throws Exception if there was an error (maybe the passed data is not valid) */ function import_parts(&$database, &$current_user, &$log, $data, $only_check_data = false) { $parts = array(); try { $transaction_id = $database->begin_transaction(); // start transaction // Get the category, footprint, storelocation, ... which are named "Import", or create them. // We need this elements as parent for new elements, which will be created while import parts. $import_categories = Category::search($database, $current_user, $log, 'Import', true); if (count($import_categories) > 0) { $import_category = $import_categories[0]; $import_category_created = false; } else { $import_category = Category::add($database, $current_user, $log, 'Import', NULL); $import_category_created = true; // we can delete it later if we didn't need it } $import_storelocations = Storelocation::search($database, $current_user, $log, 'Import', true); if (count($import_storelocations) > 0) { $import_storelocation = $import_storelocations[0]; $import_storelocation_created = false; } else { $import_storelocation = Storelocation::add($database, $current_user, $log, 'Import', NULL); $import_storelocation_created = true; // we can delete it later if we didn't need it } $import_footprints = Footprint::search($database, $current_user, $log, 'Import', true); if (count($import_footprints) > 0) { $import_footprint = $import_footprints[0]; $import_footprint_created = false; } else { $import_footprint = Footprint::add($database, $current_user, $log, 'Import', NULL); $import_footprint_created = true; // we can delete it later if we didn't need it } $import_suppliers = Supplier::search($database, $current_user, $log, 'Import', true); if (count($import_suppliers) > 0) { $import_supplier = $import_suppliers[0]; $import_supplier_created = false; } else { $import_supplier = Supplier::add($database, $current_user, $log, 'Import', NULL); $import_supplier_created = true; // we can delete it later if we didn't need it } $import_manufacturers = Manufacturer::search($database, $current_user, $log, 'Import', true); if (count($import_manufacturers) > 0) { $import_manufacturer = $import_manufacturers[0]; $import_manufacturer_created = false; } else { $import_manufacturer = Manufacturer::add($database, $current_user, $log, 'Import', NULL); $import_manufacturer_created = true; // we can delete it later if we didn't need it } $import_category_used = false; $import_storelocation_used = false; $import_footprint_used = false; $import_supplier_used = false; $import_manufacturer_used = false; // start import $row_index = 0; foreach ($data as $row) { $name = $row['part_name']; $description = $row['part_description']; $instock = $row['part_instock']; $mininstock = $row['part_mininstock']; $comment = $row['part_comment']; $category_name = $row['part_category_name']; $footprint_name = $row['part_footprint_name']; $storelocation_name = $row['part_storelocation_name']; $manufacturer_name = $row['part_manufacturer_name']; $supplier_name = $row['part_supplier_name']; $supplierpartnr = $row['part_supplierpartnr']; $price = $row['part_price']; // search elements / create them if they don't exist already if (strlen($category_name) > 0) { $categories = Category::search($database, $current_user, $log, $category_name, true); if (count($categories) > 0) { $category = $categories[0]; } else { $category = Category::add($database, $current_user, $log, $category_name, $import_category->get_id()); $import_category_used = true; } } else { throw new Exception('Jedes Bauteil muss eine Kategorie haben!'); } if (strlen($storelocation_name) > 0) { $storelocations = Storelocation::search($database, $current_user, $log, $storelocation_name, true); if (count($storelocations) > 0) { $storelocation = $storelocations[0]; } else { $storelocation = Storelocation::add($database, $current_user, $log, $storelocation_name, $import_storelocation->get_id()); $import_storelocation_used = true; } } if (strlen($manufacturer_name) > 0) { $manufacturers = Manufacturer::search($database, $current_user, $log, $manufacturer_name, true); if (count($manufacturers) > 0) { $manufacturer = $manufacturers[0]; } else { $manufacturer = Manufacturer::add($database, $current_user, $log, $manufacturer_name, $import_manufacturer->get_id()); $import_manufacturer_used = true; } } if (strlen($footprint_name) > 0) { $footprints = Footprint::search($database, $current_user, $log, $footprint_name, true); if (count($footprints) > 0) { $footprint = $footprints[0]; } else { $footprint = Footprint::add($database, $current_user, $log, $footprint_name, $import_footprint->get_id()); $import_footprint_used = true; } } if (strlen($supplier_name) > 0) { $suppliers = Supplier::search($database, $current_user, $log, $supplier_name, true); if (count($suppliers) > 0) { $supplier = $suppliers[0]; } else { $supplier = Supplier::add($database, $current_user, $log, $supplier_name, $import_supplier->get_id()); $import_supplier_used = true; } } else { if (strlen($supplierpartnr) > 0 || $price > 0) { throw new Exception('Ist eine Bestellnummer oder ein Preis angegeben, so muss auch ein Lieferant angegeben werden!'); } } $new_part = Part::add($database, $current_user, $log, $name, $category->get_id(), $description, $instock, $mininstock, isset($storelocation) ? $storelocation->get_id() : NULL, isset($manufacturer) ? $manufacturer->get_id() : NULL, isset($footprint) ? $footprint->get_id() : NULL, $comment); if (isset($supplier)) { $new_orderdetails = Orderdetails::add($database, $current_user, $log, $new_part->get_id(), $supplier->get_id(), $supplierpartnr); if ($price > 0) { $new_pricedetails = Pricedetails::add($database, $current_user, $log, $new_orderdetails->get_id(), $price); } } if (!$only_check_data) { $parts[] = $new_part; } $row_index++; } // delete all elements which were created in this function, but were not used if ($import_category_created && !$import_category_used) { $import_category->delete(); } if ($import_storelocation_created && !$import_storelocation_used) { $import_storelocation->delete(); } if ($import_footprint_created && !$import_footprint_used) { $import_footprint->delete(); } if ($import_supplier_created && !$import_supplier_used) { $import_supplier->delete(); } if ($import_manufacturer_created && !$import_manufacturer_used) { $import_manufacturer->delete(); } if ($only_check_data) { $database->rollback(); } else { $database->commit($transaction_id); } // commit transaction } catch (Exception $e) { $database->rollback(); // rollback transaction throw new Exception((isset($row_index) ? 'Nr. ' . ($row_index + 1) . ': ' : '') . $e->getMessage()); } return $parts; }