/** * @brief Delete this element * * @note This function overrides the same-named function from the parent class. * This is required to delete the attachements of an element too. * * @param boolean $delete_files_from_hdd @li if true, the attached files will be deleted from harddisc drive (!!) * If some files are used for other elements, they won't be deleted. * @li if false, the files will be deleted from database, * but not from the harddisc drive. * * @throws Exception if there was an error */ public function delete($delete_files_from_hdd = false) { try { $transaction_id = $this->database->begin_transaction(); // start transaction // first, we will delete all files of this element $attachements = $this->get_attachements(); $this->reset_attributes(); // set $this->attachements to NULL foreach ($attachements as $attachement) { $attachement->delete($delete_files_from_hdd); } parent::delete(); // now delete this element $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()); } }
/** * @brief Delete this attachement from database (and the associated file from harddisc if desired) * * @note This method overrides the same-named method from the parent class. * * @param boolean $delete_from_hdd if true, and the associated file isn't used in other file records, * the file will be deleted from harddisc drive too (!!) * * @throws Exception if the file exists and should be deleted, but cannot be deleted * (maybe not enought permissions) * @throws Exception if there was an error */ public function delete($delete_from_hdd = false) { $filename = $this->get_filename(); $must_file_delete = false; if ($delete_from_hdd && strlen($filename) > 0) { // we will delete the file only from HDD if there are no other "Attachement" objects with the same filename! $attachements = Attachement::get_attachements_by_filename($this->database, $this->current_user, $this->log, $filename); if (count($attachements) <= 1 && file_exists($filename)) { // check if there are enought permissions to delete the file if (!is_writable(dirname($filename))) { throw new Exception('Die Datei "' . $filename . '" kann nicht gelöscht werden, ' . 'da im übergeordneten Ordner keine Schreibrechte vorhanden sind!'); } // all OK, file must be deleted after deleting the database record successfully $must_file_delete = true; } } try { $transaction_id = $this->database->begin_transaction(); // start transaction // Set all "id_master_picture_attachement" in the table "parts" to NULL where the master picture is this attachement $query = 'SELECT id from parts WHERE id_master_picture_attachement=?'; $query_data = $this->database->query($query, array($this->get_id())); foreach ($query_data as $row) { $part = new Part($this->database, $this->current_user, $this->log, $row['id']); $part->set_master_picture_attachement_id(NULL); } $this->get_element()->set_attributes(array()); // save element attributes to update its "last_modified" // Now we can delete the database record of this attachement parent::delete(); // now delete the file (if desired) if ($must_file_delete) { if (!unlink($filename)) { throw new Exception('Die Datei "' . $filename . '" kann nicht von der Festplatte gelöscht ' . "werden! \nÜberprüfen Sie, ob die nötigen Rechte vorhanden sind."); } } $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("Der Dateianhang \"" . $this->get_name() . "\" konnte nicht entfernt werden!\nGrund: " . $e->getMessage()); } }