primary; $download = false; if (array_key_exists('download', $params)) { if ($params['download'] != 0) { $download = true; } } $activerow = $database->context->table('attachments')->insert([ 'mailing' => $mailing, 'luser' => $user, 'download' => $download, 'filename' => is_null(@$params['filename']) ? null : $params['filename'], 'filesize' => is_null(@$params['size']) ? null : $params['size'], 'contents' => is_null(@$params['contents']) ? null : base64_encode($params['contents']), ]); if ($download) { // Create at least a key $key = Attachmentkey::create($database, $activerow->getPrimary(true)); } return $database->createModel('Attachment', $activerow); } public static function retrieve(\App\Services\Database $database, $id) { $activerows = $database->context->table('attachments')->where('attachment', $id); foreach($activerows as $row); return $database->createModel('Attachment', $row); return null; } public function delete() { return $this->activerow->delete(); } public function getMailing() { return $this->activerow->mailing; } public function setMailing($value = null) { if(is_null($value)) return null; $this->activerow->update(['mailing' => $value]); return $value; } public function getFilename() { return $this->activerow->filename; } public function setFilename($value = null) { $this->activerow->update(['filename' => $value]); } public function getSize() { return $this->activerow->filesize; } public function setSize($value = null) { $this->activerow->update(['filesize' => $value]); } private $contents; public function getContents() { if (!is_null($this->contents)) return $this->contents; $stream = $this->activerow->contents; if ($stream) { $chunks = []; for(;;) { $chunk = fread($stream, 65536); if($chunk === false) throw new \ErrorException(error_get_last()); if($chunk === '') break; $chunks[] = $chunk; } return $this->contents = base64_decode(implode($chunks)); } return null; } public function setContents($value = null) { $this->activerow->update(['contents' => base64_encode($value)]); } public function getDownload() { return $this->activerow->download; } public function setDownload($value = null) { $this->activerow->update(['download' => !!$value]); } # public function createKey() { # return Attachmentkey::create($this->database, $this->primary); # } public function getKeys() { $rows = $this->activerow->related('attachmentkeys'); $keys = []; $database = $this->database; foreach($rows as $row) { $keys[] = $database->createModel('Attachmentkey', $row); } return $keys; } public function getTotaldownloads() { $downloads = 0; $keys = $this->getKeys(); foreach($keys as $key) { $downloads += $key->used; } return $downloads; } public function resetdownloadcounter() { $keys = $this->getKeys(); foreach($keys as $key) { $key->used = 0; } } public function attachattachment() { $this->setDownload(false); $keys = $this->getKeys(); foreach($keys as $key) { $key->delete(); } } public function detachattachment() { $this->setDownload(true); $key = Attachmentkey::create($this->database, $this->primary); } }