/** * @brief Delete this element * * @note This function overrides the same-named function from the parent class. * @note The associated orderdetails and attachements will be deleted too. * * @param boolean $delete_files_from_hdd if true, the attached files of this part will be deleted * from harddisc drive (!) * @param boolean $delete_device_parts @li if true, all DeviceParts with this part will be deleted * @li if false, there will be thrown an exception * if there are DeviceParts with this part * * @throws Exception if there are device parts and $delete_device_parts == false * @throws Exception if there was an error */ public function delete($delete_files_from_hdd = false, $delete_device_parts = false) { try { $transaction_id = $this->database->begin_transaction(); // start transaction $devices = $this->get_devices(); $orderdetails = $this->get_orderdetails(); $this->reset_attributes(); // set $this->devices ans $this->orderdetails to NULL // Check if there are no Devices with this Part (and delete them if neccessary) if (count($devices) > 0) { if ($delete_device_parts) { foreach ($devices as $device) { foreach ($device->get_parts() as $device_part) { if ($device_part->get_part()->get_id() == $this->get_id()) { $device_part->delete(); } } } } else { throw new Exception('Das Bauteil "' . $this->get_name() . '" wird noch in ' . count($devices) . ' Baugruppen verwendet und kann daher nicht gelöscht werden!'); } } // Delete all Orderdetails foreach ($orderdetails as $details) { $details->delete(); } // now we can delete this element + all attachements of it parent::delete($delete_files_from_hdd); $this->database->commit($transaction_id); // commit transaction } catch (Exception $e) { $this->database->rollback(); // rollback transaction // restore the settings from BEFORE the transaction $this->reset_attributes(); throw new Exception("Das Bauteil \"" . $this->get_name() . "\" konnte nicht gelöscht werden!\nGrund: " . $e->getMessage()); } }
/** * @brief Delete this element * * @note This function overrides the same-named method from the parent class. * (Because of the argument $delete_recursive, we need to redefine this method.) * * @param boolean $delete_recursive @li if true, all child elements (recursive) * will be deleted too (!!) * @li if false, the parent of the child nodes (not recursive) * will be changed to the parent element of this element * @param boolean $delete_files_from_hdd if true, all attached files from this element will be deleted * from harddisc drive (!!) * * @throws Exception if there was an error */ public function delete($delete_recursive = false, $delete_files_from_hdd = false) { if ($this->get_id() == NULL) { throw new Exception('Die Oberste Ebene kann nicht gelöscht werden!'); } try { $transaction_id = $this->database->begin_transaction(); // start transaction // first, we take all subelements of this element... $subelements = $this->get_subelements(false); // then we set $this->subelements to NULL, because if there was an error while deleting $this->reset_attributes(); // ant then we change the parent IDs of the subelments to the parent ID of this element foreach ($subelements as $element) { if ($delete_recursive) { $element->delete(true, $delete_files_from_hdd); } else { $element->set_parent_id($this->get_parent_id()); } // just change its parent } // now we can delete this element + all attachements of it parent::delete($delete_files_from_hdd); $this->database->commit($transaction_id); // commit transaction } catch (Exception $e) { $this->database->rollback(); // rollback transaction // restore the settings from BEFORE the transaction $this->reset_attributes(); throw new Exception("Das Element \"" . $this->get_name() . "\" konnte nicht gelöscht werden!\nGrund: " . $e->getMessage()); } }