. */ /** * An interface to the file table in the database */ class File extends Model { public $table = 'file', $fillable_columns = ['filename']; /** * Get the full internal path to the file * * @see file::getFilenameURI To get the full external path to the file * * @return string The path */ public function getFilenamePath() { return Constants::files_folder . $this->filename; } /** * Get the full external path to the file * * @see file::getFilenamePath To get the full internal path to the file * * @return string The URI */ public function getFilenameURI() { return Constants::files_folder_external . $this->filename; } //------------------------------------------------------------------------------ // Other functions //------------------------------------------------------------------------------ /** * Move this file to the trash and delete all records for it * * Physically, this moves the file to a trash folder * Then, the file will be removed from the file table in the database * Any appendices linking to this file with fileId will have fileId NULL * Any offers linking to this file with invoice_fileId will have invoice_fileId NULL * * @throws PDOException If there's something wrong with the database * * @return bool True on success, false on failure */ public function delete() { // Try to move the file to trash $newname = pathinfo($this->filename, PATHINFO_FILENAME) . '--' . date('Y-m-d.H.i.s.') . pathinfo($this->filename, PATHINFO_EXTENSION); $newdir = pathinfo($this->getFilenamePath(), PATHINFO_DIRNAME) . '/' . Constants::files_folder_trash . '/'; if (!file_exists($newdir)) { if (!mkdir($newdir)) { return false; } } if (!(@rename($this->getFilenamePath(), $newdir . $newname))) { return false; } // Remove offers linked by invoice_fileId $this->pdo->query("UPDATE `".Constants::db_prefix."offer` SET `invoice_fileId`=NULL WHERE `invoice_fileId`={$this->id}"); return parent::delete(); } }