mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-06-02 12:49:27 +00:00
5a5a22e0d5
- Added permission fs/dms to dump.sql and fhcomplete.php - Added FilesystemLib to read and write from/in filesystem - Added FS_Model to manage filesystem with the same permission system of DB_Model - Added more models to handle the Dms - Modified Dms controller to use the new models
190 lines
4.2 KiB
PHP
190 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* FH-Complete
|
|
*
|
|
* @package FHC-API
|
|
* @author FHC-Team
|
|
* @copyright Copyright (c) 2016, fhcomplete.org
|
|
* @license GPLv3
|
|
* @link http://fhcomplete.org
|
|
* @since Version 1.0
|
|
* @filesource
|
|
*/
|
|
// ------------------------------------------------------------------------
|
|
|
|
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
class Dms extends APIv1_Controller
|
|
{
|
|
/**
|
|
*
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// Load model PersonModel
|
|
$this->load->model('content/Dms_model', 'DmsModel');
|
|
$this->load->model('content/DmsVersion_model', 'DmsVersionModel');
|
|
$this->load->model('content/DmsFS_model', 'DmsFSModel');
|
|
// Set the uid of the model to let to check the permissions
|
|
$this->DmsModel->setUID($this->_getUID());
|
|
$this->DmsVersionModel->setUID($this->_getUID());
|
|
$this->DmsFSModel->setUID($this->_getUID());
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function getDms()
|
|
{
|
|
$dms_id = $this->get('dms_id');
|
|
$version = $this->get('version');
|
|
|
|
if (isset($dms_id))
|
|
{
|
|
$result = $this->_getDms($dms_id, $version);
|
|
if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0)
|
|
{
|
|
$resultFS = $this->DmsFSModel->read($result->retval[0]->filename);
|
|
if (is_object($resultFS) && $resultFS->error == EXIT_SUCCESS)
|
|
{
|
|
$result->retval[0]->file_content = $resultFS->retval;
|
|
}
|
|
}
|
|
|
|
$this->response($result, REST_Controller::HTTP_OK);
|
|
}
|
|
else
|
|
{
|
|
$this->response();
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public function postDms()
|
|
{
|
|
$dms = $this->_parseData($this->post());
|
|
|
|
if ($this->_validate($dms))
|
|
{
|
|
$result = null;
|
|
|
|
if (isset($dms['dms_id']))
|
|
{
|
|
if ($this->_saveFileOnUpdate($dms))
|
|
{
|
|
$result = $this->DmsModel->update($dms['dms_id'], $this->DmsModel->filterFields($dms));
|
|
if ($result->error == EXIT_SUCCESS)
|
|
{
|
|
$result = $this->DmsVersionModel->update(array($dms['dms_id'], $dms['version']), $this->DmsVersionModel->filterFields($dms));
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (($filename = $this->_saveFileOnInsert($dms)) !== false)
|
|
{
|
|
$result = $this->DmsModel->insert($this->DmsModel->filterFields($dms));
|
|
if ($result->error == EXIT_SUCCESS)
|
|
{
|
|
$result = $this->DmsVersionModel->insert($this->DmsVersionModel->filterFields($dms, $result->retval, $filename));
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->response($result, REST_Controller::HTTP_OK);
|
|
}
|
|
else
|
|
{
|
|
$this->response();
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private function _getDms($dms_id, $version = null)
|
|
{
|
|
$result = null;
|
|
|
|
if (isset($dms_id))
|
|
{
|
|
$result = $this->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id');
|
|
if ($result->error == EXIT_SUCCESS)
|
|
{
|
|
$result = $this->DmsModel->addOrder('version', 'DESC');
|
|
if ($result->error == EXIT_SUCCESS)
|
|
{
|
|
$result = $this->DmsModel->addLimit(1);
|
|
if ($result->error == EXIT_SUCCESS)
|
|
{
|
|
if (!isset($version))
|
|
{
|
|
$result = $this->DmsModel->loadWhere(array('dms_id' => $dms_id));
|
|
}
|
|
else
|
|
{
|
|
$result = $this->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private function _saveFileOnUpdate($dms)
|
|
{
|
|
if(isset($dms['version']))
|
|
{
|
|
$result = $this->_getDms($dms['dms_id'], $dms['version']);
|
|
|
|
if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0)
|
|
{
|
|
$result = $this->DmsFSModel->write($result->retval[0]->filename, $dms['file_content']);
|
|
if (is_object($result) && $result->error == EXIT_SUCCESS)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private function _saveFileOnInsert($dms)
|
|
{
|
|
$filename = uniqid() . '.' . pathinfo($dms['name'], PATHINFO_EXTENSION);
|
|
|
|
$result = $this->DmsFSModel->write($filename, $dms['file_content']);
|
|
if (is_object($result) && $result->error == EXIT_SUCCESS)
|
|
{
|
|
return $filename;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private function _validate($dms = NULL)
|
|
{
|
|
if (!isset($dms['file_content']) || (isset($dms['file_content']) && $dms['file_content'] == ''))
|
|
{
|
|
return false;
|
|
}
|
|
if (!isset($dms['name']) || (isset($dms['name']) && $dms['name'] == ''))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |