/** * @brief Import DeviceParts in a Device * * @note This function uses database transactions. If an error occurs, all changes will be rolled back. * * @note If there are already parts in the device, which are also in the import data array, * the quantity and the mountnames of the part to import will be added to the already existing device-part. * * @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 integer $device_id The ID of the device where the parts should be imported * @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! * * @throws Exception if there was an error (maybe the passed data is not valid) */ function import_device_parts(&$database, &$current_user, &$log, $device_id, $data, $only_check_data = false) { try { $transaction_id = $database->begin_transaction(); // start transaction foreach ($data as $row) { $part_id = $row['devicepart_part_id']; $quantity = $row['devicepart_mount_quantity']; $mountnames = $row['devicepart_mount_names']; if ($quantity > 0) { $new_devicepart = DevicePart::add($database, $current_user, $log, $device_id, $part_id, $quantity, $mountnames, true); } } if ($only_check_data) { $database->rollback(); } else { $database->commit($transaction_id); } // commit transaction } catch (Exception $e) { $database->rollback(); // rollback transaction throw new Exception($e->getMessage()); } }
$html->set_variable('searched_parts_rowcount', count($searched_parts), 'integer'); $html->set_variable('no_searched_parts_found', count($searched_parts) == 0, 'integer'); } catch (Exception $e) { $messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red'); } break; case 'assign_by_selected': // add some parts (which were listed by part search) to this device for ($i = 0; $i < $searched_parts_rowcount; $i++) { $part_id = isset($_REQUEST['id_' . $i]) ? (int) $_REQUEST['id_' . $i] : 0; $quantity = isset($_REQUEST['quantity_' . $i]) ? abs((int) $_REQUEST['quantity_' . $i]) : 0; $mountname = isset($_REQUEST['mountnames_' . $i]) ? trim((string) $_REQUEST['mountnames_' . $i]) : ''; if ($quantity > 0) { try { // if there is already such Part in this Device, the quantity will be increased $device_part = DevicePart::add($database, $current_user, $log, $device_id, $part_id, $quantity, $mountname, true); } catch (Exception $e) { $messages[] = array('text' => nl2br($e->getMessage()), 'strong' => true, 'color' => 'red'); } } } if (count($messages) == 0) { $reload_site = true; } break; case 'device_parts_apply': // apply new quantities and new mountnames, or remove parts from this device for ($i = 0; $i < $device_parts_rowcount; $i++) { $part_id = isset($_REQUEST['id_' . $i]) ? (int) $_REQUEST['id_' . $i] : 0; $quantity = isset($_REQUEST['quantity_' . $i]) ? abs((int) $_REQUEST['quantity_' . $i]) : 0; $mountname = isset($_REQUEST['mountnames_' . $i]) ? trim((string) $_REQUEST['mountnames_' . $i]) : '';
/** * @brief Create a new Device as a copy from this one. All DeviceParts will be copied too. * * @param string $name The name of the new device * @param integer $parent_id The ID of the new device's parent device * @param boolean $with_subdevices If true, all subdevices will be copied too * * @throws Exception if there was an error */ public function copy($name, $parent_id, $with_subdevices = false) { try { if ($with_subdevices && $parent_id > 0) { // check if $parent_id is NOT a child of this device $parent_device = new Device($this->database, $this->current_user, $this->log, $parent_id); if ($parent_device->get_id() == $this->get_id() || $parent_device->is_child_of($this)) { throw new Exception('Eine Baugruppe kann nicht in sich selber kopiert werden!'); } } $transaction_id = $this->database->begin_transaction(); // start transaction $new_device = Device::add($this->database, $this->current_user, $this->log, $name, $parent_id); $device_parts = $this->get_parts(); foreach ($device_parts as $part) { $new_part = DevicePart::add($this->database, $this->current_user, $this->log, $new_device->get_id(), $part->get_part()->get_id(), $part->get_mount_quantity(), $part->get_mount_names()); } if ($with_subdevices) { $subdevices = $this->get_subelements(false); foreach ($subdevices as $device) { $device->copy($device->get_name(), $new_device->get_id(), true); } } $this->database->commit($transaction_id); // commit transaction } catch (Exception $e) { $this->database->rollback(); // rollback transaction throw new Exception("Die Baugruppe \"" . $this->get_name() . "\"konnte nicht kopiert werden!\nGrund: " . $e->getMessage()); } }