diff --git a/application/controllers/api/v1/content/Dms.php b/application/controllers/api/v1/content/Dms.php index e9aa32811..9a2017f33 100644 --- a/application/controllers/api/v1/content/Dms.php +++ b/application/controllers/api/v1/content/Dms.php @@ -22,10 +22,8 @@ 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"); + // Load library DmsLib + $this->load->library("DmsLib"); } /** @@ -38,15 +36,7 @@ class Dms extends APIv1_Controller 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; - } - } + $result = $this->dmslib->read($dms_id, $version); $this->response($result, REST_Controller::HTTP_OK); } @@ -65,50 +55,7 @@ class Dms extends APIv1_Controller if ($this->_validate($dms)) { - $result = null; - - if(isset($dms["new"]) && $dms["new"] == true) - { - // Remove new parameter to avoid DB insert errors - unset($dms["new"]); - - if (($filename = $this->_saveFileOnInsert($dms)) !== false) - { - if(isset($dms["dms_id"]) && $dms["dms_id"] != "") - { - $result = $this->DmsVersionModel->insert( - $this->DmsVersionModel->filterFields($dms, $dms["dms_id"], $filename) - ); - } - else - { - $result = $this->DmsModel->insert($this->DmsModel->filterFields($dms)); - if ($result->error == EXIT_SUCCESS) - { - $result = $this->DmsVersionModel->insert( - $this->DmsVersionModel->filterFields($dms, $result->retval, $filename) - ); - } - } - } - } - else - { - 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) - ); - } - } - } + $result = $this->dmslib->save($dms); $this->response($result, REST_Controller::HTTP_OK); } @@ -118,78 +65,6 @@ class Dms extends APIv1_Controller } } - /** - * - */ - 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"] == "")) diff --git a/application/libraries/DmsLib.php b/application/libraries/DmsLib.php new file mode 100644 index 000000000..083dab2d4 --- /dev/null +++ b/application/libraries/DmsLib.php @@ -0,0 +1,150 @@ +ci =& get_instance(); + + $this->ci->load->model("content/Dms_model", "DmsModel"); + $this->ci->load->model("content/DmsVersion_model", "DmsVersionModel"); + $this->ci->load->model("content/DmsFS_model", "DmsFSModel"); + } + + public function read($dms_id, $version = null) + { + $result = null; + + if (isset($dms_id)) + { + $this->ci->DmsModel->addJoin("campus.tbl_dms_version", "dms_id"); + $this->ci->DmsModel->addOrder("version", "DESC"); + $this->ci->DmsModel->addLimit(1); + + if (!isset($version)) + { + $result = $this->ci->DmsModel->load($dms_id); + } + else + { + $result = $this->ci->DmsModel->loadWhere(array("dms_id" => $dms_id, "version" => $version)); + } + } + + if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) + { + $resultFS = $this->ci->DmsFSModel->read($result->retval[0]->filename); + if (is_object($resultFS) && $resultFS->error == EXIT_SUCCESS) + { + $result->retval[0]->{DmsLib::FILE_CONTENT_PROPERTY} = $resultFS->retval; + } + else + { + $result = $resultFS; + } + + } + + return $result; + } + + public function save($dms) + { + $result = null; + + if(isset($dms["new"]) && $dms["new"] == true) + { + // Remove new parameter to avoid DB insert errors + unset($dms["new"]); + + $result = $this->_saveFileOnInsert($dms); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + $filename = $result->retval; + if(isset($dms["dms_id"]) && $dms["dms_id"] != "") + { + $result = $this->ci->DmsVersionModel->insert( + $this->ci->DmsVersionModel->filterFields($dms, $dms["dms_id"], $filename) + ); + } + else + { + $result = $this->ci->DmsModel->insert($this->ci->DmsModel->filterFields($dms)); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + $result = $this->ci->DmsVersionModel->insert( + $this->ci->DmsVersionModel->filterFields($dms, $result->retval, $filename) + ); + } + } + } + } + else + { + $result = $this->_saveFileOnUpdate($dms); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + $result = $this->ci->DmsModel->update($dms["dms_id"], $this->ci->DmsModel->filterFields($dms)); + if ($result->error == EXIT_SUCCESS) + { + $result = $this->ci->DmsVersionModel->update( + array( + $dms["dms_id"], + $dms["version"] + ), + $this->ci->DmsVersionModel->filterFields($dms) + ); + } + } + } + + return $result; + } + + /** + * + */ + private function _saveFileOnInsert($dms) + { + $filename = uniqid() . "." . pathinfo($dms["name"], PATHINFO_EXTENSION); + + return $this->ci->DmsFSModel->write($filename, $dms["file_content"]); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + $result->retval = $filename; + } + + return $result; + } + + /** + * + */ + private function _saveFileOnUpdate($dms) + { + $result = null; + + if(isset($dms["version"])) + { + $result = $this->read($dms["dms_id"], $dms["version"]); + + if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) + { + $result = $this->ci->DmsFSModel->write($result->retval[0]->filename, $dms["file_content"]); + } + } + + return $result; + } +} \ No newline at end of file