From 5cd163819dae36eeb3bf84d33d36ba7760e95611 Mon Sep 17 00:00:00 2001 From: Cris Date: Mon, 11 Jan 2021 15:24:12 +0100 Subject: [PATCH] Added upload function to DmsLib Signed-off-by: cris-technikum --- application/libraries/DmsLib.php | 54 +++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/application/libraries/DmsLib.php b/application/libraries/DmsLib.php index f32df5fd1..04204012b 100644 --- a/application/libraries/DmsLib.php +++ b/application/libraries/DmsLib.php @@ -5,7 +5,10 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); class DmsLib { const FILE_CONTENT_PROPERTY = 'file_content'; - + + const FILE_INPUT_NAME = 'uploadfile'; // name of the HTML input tag containing the uploaded file + private $UPLOAD_PATH = DMS_PATH; // temporary directory to store the upload file + /** * Object initialization */ @@ -92,6 +95,42 @@ class DmsLib return $result; } + + /** + * Uploads a document and saves it to DMS + * @param $dms DMS assoc array + * @param array $allowed_types Default: all. Param example: array(jpg, pdf) + * @return array + */ + public function upload($dms, $allowed_types = array('*')) + { + // Init upload configs + $this->_loadUploadLibrary($allowed_types); + + if (!$this->ci->upload->do_upload(DmsLib::FILE_INPUT_NAME)) + { + return error($this->ci->upload->display_errors()); + } + + $upload_data = $this->ci->upload->data(); // data about the uploaded file + $filename = $upload_data['file_name']; + + // Insert to DMS table + if (!$result = $this->ci->DmsModel->insert($this->ci->DmsModel->filterFields($dms))) + { + return error('Failed inserting to DMS'); + } + + // Insert DMS version + if (!$result = $this->ci->DmsVersionModel->insert( + $this->ci->DmsVersionModel->filterFields($dms, $result->retval, $filename))) + { + return error('Failed inserting DMS version'); + } + + // return result of uploaded data + return success($upload_data); // data about the uploaded file + } /** * Saves a Document @@ -302,4 +341,17 @@ class DmsLib return $result; } + + /** + * Loads the upload library of CI + */ + private function _loadUploadLibrary($allowed_types) + { + $config['upload_path'] = $this->UPLOAD_PATH; + $config['allowed_types'] = implode('|', $allowed_types); + $config['overwrite'] = true; + + $this->ci->load->library('upload', $config); + $this->ci->upload->initialize($config); + } }