Added upload function to DmsLib

Signed-off-by: cris-technikum <hainberg@technikum-wien.at>
This commit is contained in:
Cris
2021-01-11 15:24:12 +01:00
committed by cris-technikum
parent 1fefffcab3
commit 5cd163819d
+53 -1
View File
@@ -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);
}
}