Initial Commit
This commit is contained in:
@@ -0,0 +1,572 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class App_Controller_Rest_Controller
|
||||
* Versione 1.0.1 - Data: 12/08/2020
|
||||
*/
|
||||
abstract class App_Controller_Rest_Controller extends Zend_Rest_Controller
|
||||
{
|
||||
public $_resourceName;
|
||||
public $_resourceFormName;
|
||||
public $_patchResourceName;
|
||||
|
||||
public $_responseData;
|
||||
public $_status;
|
||||
public $_inArray;
|
||||
|
||||
public $_genericModel;
|
||||
public $_dbAdapter;
|
||||
|
||||
public $_joinData = null;
|
||||
public $_groupBy = null;
|
||||
|
||||
public $_defaultPageNumber = null;
|
||||
public $_defaultPageCount = null;
|
||||
public $_defaultOrder = null;
|
||||
|
||||
public $_returnWithDataObject = true;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->_helper->viewRenderer->setNoRender(true);
|
||||
$this->_helper->AjaxContext()
|
||||
->addActionContext('get', 'json')
|
||||
->addActionContext('post', 'json')
|
||||
->addActionContext('put', 'json')
|
||||
->addActionContext('patch', 'json')
|
||||
->addActionContext('delete', 'json')
|
||||
->addActionContext('options', 'json')
|
||||
->addActionContext('head', 'json')
|
||||
->initContext('json');
|
||||
|
||||
$this->_responseData = new stdClass();
|
||||
$this->_status = false;
|
||||
$this->_inArray = false;
|
||||
|
||||
if (!$this->_resourceFormName) {
|
||||
$this->_resourceFormName = $this->_resourceName;
|
||||
}
|
||||
|
||||
if (!$this->_patchResourceName) {
|
||||
$this->_patchResourceName = $this->_resourceName;
|
||||
}
|
||||
|
||||
$this->_genericModel = new Application_Model_Generic();
|
||||
$this->_dbAdapter = $this->_genericModel->getResource($this->_resourceName)->getDbAdapter();
|
||||
}
|
||||
|
||||
public function preDispatch()
|
||||
{
|
||||
$this->_response->setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Authorization,x-requested-with,Content-Type,Cache-Control,X-As-Dev-On,X-As-Dev-Pw,MongoDbName');
|
||||
$this->_response->setHeader('Access-Control-Expose-Headers', 'X-Tot-Items,X-Page,X-Count,X-Tot-Filtered');
|
||||
$this->_response->setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, PATCH, DELETE');
|
||||
$this->_response->setHeader('Access-Control-Allow-Origin', '*');
|
||||
$this->_response->setHeader('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
// Controllo sui metodi di prefetch
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
return $this->optionsAction();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
|
||||
return $this->optionsAction();
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'PATCH') {
|
||||
return $this->patchAction();
|
||||
}
|
||||
}
|
||||
|
||||
//##################################### CRUD FUNCTIONS #############################################################
|
||||
|
||||
public function indexAction()
|
||||
{
|
||||
$this->getAction();
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
try {
|
||||
$this->switchMethodFunction($this->_getParam('method'));
|
||||
} catch (Exception $e) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function postAction()
|
||||
{
|
||||
|
||||
try {
|
||||
|
||||
$this->_dbAdapter->beginTransaction();
|
||||
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Inserimento di batch
|
||||
if (is_array($params[0])) {
|
||||
$failedElements = array();
|
||||
foreach ($params as $key => $riga) {
|
||||
if ($form->isValid($riga)) {
|
||||
$dati = $form->getValues();
|
||||
$this->insertSingleElement($dati);
|
||||
} else {
|
||||
array_push($failedElements, $key);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
|
||||
$this->_status = true;
|
||||
$this->_responseData->data = array('failed_elements_index' => $failedElements);
|
||||
return $this->sendResponse();
|
||||
}
|
||||
|
||||
//Inserimento di un singolo elemento
|
||||
if (!$form->isValid($params)) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $form->getValues();
|
||||
|
||||
$dati = $this->preInsertElement($dati);
|
||||
|
||||
$dati['id'] = $this->insertSingleElement($dati);
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
|
||||
$dati = $this->afterInsertElement($dati);
|
||||
|
||||
$this->_responseData->data = ($this->_returnWithDataObject) ? array('data' => $dati) : $dati;
|
||||
$this->_status = true;
|
||||
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
try{
|
||||
$this->_dbAdapter->rollBack();
|
||||
}catch (Exception $e2){}
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function putAction()
|
||||
{
|
||||
try {
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
$this->_dbAdapter->beginTransaction();
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Aggiornamento di batch
|
||||
if (is_array($params[0])) {
|
||||
$failedElements = array();
|
||||
$resolvedElements = array();
|
||||
foreach ($params as $key => $riga) {
|
||||
if ($form->isValid($riga) && $riga['id']) {
|
||||
$this->updateSingleElement($riga['id'], $form->getValues());
|
||||
array_push($resolvedElements, $riga['id']);
|
||||
} else {
|
||||
array_push($failedElements, $riga['id']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
|
||||
$this->_status = true;
|
||||
$this->_responseData->data = array('failed_elements_id' => $failedElements, 'resolved_elements_id' => $resolvedElements);
|
||||
return $this->sendResponse();
|
||||
}
|
||||
|
||||
//Aggiornamento singolo
|
||||
if (!$form->isValidPartial($params)) {
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $form->getValues();
|
||||
|
||||
if (!$this->_getParam('id')) {
|
||||
$this->_responseData->message = 'Id elemento non trovato';
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $this->preUpdateElement($dati, $this->_getParam('id'));
|
||||
|
||||
$this->updateSingleElement($this->_getParam('id'), $dati);
|
||||
$dati['id'] = $this->_getParam('id');
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
|
||||
$dati = $this->afterUpdateElement($dati);
|
||||
|
||||
$this->_responseData->data = ($this->_returnWithDataObject) ? array('data' => $dati) : $dati;
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
try{
|
||||
$this->_dbAdapter->rollBack();
|
||||
}catch (Exception $e2){}
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function patchAction()
|
||||
{
|
||||
try {
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
$this->_dbAdapter->beginTransaction();
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Aggiornamento singolo
|
||||
if (!$form->isValidPartial($params)) {
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$formData = $form->getValues();
|
||||
|
||||
//rimuovo i valori vuoti che non devono essere sovrascritti
|
||||
foreach ($formData as $key => $val) {
|
||||
if (!$val) unset($formData[$key]);
|
||||
}
|
||||
|
||||
$id = $this->_getParam('id');
|
||||
if (!$id) {
|
||||
$this->_responseData->message = 'Id elemento non trovato';
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$oldElement = $this->_genericModel->getById($this->_resourceName, $id);
|
||||
$dati = array_merge($oldElement, $formData);
|
||||
|
||||
$dati = $this->preUpdateElement($dati, $this->_getParam('id'));
|
||||
|
||||
$dati = $this->updateSingleElement($this->_getParam('id'), $dati);
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
|
||||
$dati = $this->afterUpdateElement($dati);
|
||||
|
||||
$this->_responseData->data = $dati;
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
try{
|
||||
$this->_dbAdapter->rollBack();
|
||||
}catch (Exception $e2){}
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteAction()
|
||||
{
|
||||
try {
|
||||
|
||||
$this->_dbAdapter->beginTransaction();
|
||||
|
||||
if (!$this->_getParam('id')) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->message = "Errore id non trovato";
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_NOIDERROR;
|
||||
return $this->sendResponse();
|
||||
}
|
||||
|
||||
$toDelete = explode(',', $this->_getParam('id'));
|
||||
//Eliminazione batch
|
||||
if (is_array($toDelete) && count($toDelete) > 1) {
|
||||
$failedElements = array();
|
||||
$resolvedElements = array();
|
||||
foreach ($toDelete as $key => $id) {
|
||||
try {
|
||||
$this->deleteSingleElement($id);
|
||||
array_push($resolvedElements, $id);
|
||||
} catch (Exception $e) {
|
||||
array_push($failedElements, $id);
|
||||
}
|
||||
}
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
$this->_responseData->data = $resolvedElements;
|
||||
$this->_status = true;
|
||||
|
||||
return $this->sendResponse();
|
||||
}
|
||||
|
||||
$id = $this->_getParam('id', null);
|
||||
if (!$id) {
|
||||
$this->_responseData->message = 'Id non ricevuto';
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
// Pre delete
|
||||
$this->preDeleteElement($id);
|
||||
|
||||
//Eliminazione Singola
|
||||
$this->deleteSingleElement($id);
|
||||
|
||||
// After delete
|
||||
$this->afterDeleteElement($id);
|
||||
|
||||
$this->_dbAdapter->commit();
|
||||
$this->_status = true;
|
||||
return $this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
try{
|
||||
$this->_dbAdapter->rollBack();
|
||||
}catch (Exception $e2){}
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
return $this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function headAction()
|
||||
{
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
public function optionsAction()
|
||||
{
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
//##################################### SINGLE ELEMENT FUNCTIONS ###################################################
|
||||
|
||||
public function getSingleElement($id)
|
||||
{
|
||||
return $this->getSingleElementManipulate($this->_genericModel->getById($this->_resourceName, $id));
|
||||
}
|
||||
|
||||
public function insertSingleElement($dati)
|
||||
{
|
||||
return $this->_genericModel->insert($this->_resourceName, $dati);
|
||||
}
|
||||
|
||||
public function updateSingleElement($id, $dati)
|
||||
{
|
||||
return $this->_genericModel->update($this->_resourceName, $id, $dati);
|
||||
}
|
||||
|
||||
public function deleteSingleElement($id)
|
||||
{
|
||||
return $this->_genericModel->delete($this->_resourceName, $id);
|
||||
}
|
||||
|
||||
public function insertDataToRowList($list)
|
||||
{
|
||||
return $list;
|
||||
}
|
||||
|
||||
//##################################### SWITCH METHOD FUNCTIONS ###################################################
|
||||
|
||||
public function switchMethodFunction($method)
|
||||
{
|
||||
switch ($method) {
|
||||
case'bycolumn':
|
||||
$this->defaultGetByColumnName();
|
||||
break;
|
||||
default:
|
||||
$this->defaultGetList();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function defaultGetList()
|
||||
{
|
||||
if ($this->_getParam('id') != null) {
|
||||
$this->_responseData->data = $this->getSingleElement($this->_getParam('id'));
|
||||
} else {
|
||||
$this->_inArray = true;
|
||||
|
||||
$page = $this->_getParam('page', $this->_defaultPageNumber);
|
||||
$count = $this->_getParam('count', $this->_defaultPageCount);
|
||||
$order = $this->_getParam('order', $this->_defaultOrder);
|
||||
$select = $this->_getParam('select', null);
|
||||
|
||||
$this->checkIfSetJoinData($this->getAllParams());
|
||||
$this->checkIfSetGroupBy($this->getAllParams());
|
||||
|
||||
$where = $this->_helper->FilterFunctions($this->getAllParams());
|
||||
|
||||
$where = $this->setFixedSelectWhere($where);
|
||||
$data = $this->_genericModel->getList($this->_resourceName, $page, $count, $where, $order, $select, $this->_joinData, $this->_groupBy);
|
||||
|
||||
$data = $this->insertDataToRowList($data);
|
||||
|
||||
$this->_responseData->data = $data;
|
||||
}
|
||||
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
public function defaultGetByColumnName()
|
||||
{
|
||||
$columnName = $this->_getParam('name', '');
|
||||
$val = $this->_getParam('val', '');
|
||||
|
||||
$data = $this->_genericModel->getByColumnName($this->_resourceName, $columnName, $val);
|
||||
|
||||
$this->_responseData->data = $data;
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve per applicare eventuali filtri fissi
|
||||
* @param $where
|
||||
* @return mixed
|
||||
*/
|
||||
public function setFixedSelectWhere($where)
|
||||
{
|
||||
return $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve ad applicare un eventuale joinData
|
||||
* @param $params
|
||||
*/
|
||||
public function checkIfSetJoinData($params)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Serve ad applicare un eventuale groupBy
|
||||
* @param $params
|
||||
*/
|
||||
public function checkIfSetGroupBy($params)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//##################################### HELPERS FUNCTIONS ###################################################
|
||||
|
||||
public function getSingleElementManipulate($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preInsertElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function afterInsertElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preUpdateElement($data, $id)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function afterUpdateElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preDeleteElement($id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public function afterDeleteElement($id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//##################################### GENERIC FUNCTIONS ##########################################################
|
||||
|
||||
public function sendResponse()
|
||||
{
|
||||
|
||||
if (!$this->_status) {
|
||||
$message = (!$this->_responseData->message) ? "Errore non identificato sul web service risorsa: '$this->_resourceName'" : $this->_responseData->message;
|
||||
$errorCode = (!$this->_responseData->errorCode) ? App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR : $this->_responseData->errorCode;
|
||||
|
||||
$this->sendErrorResponse($message, $errorCode);
|
||||
}
|
||||
|
||||
if (isset($this->_responseData->data)) {
|
||||
if ($this->_responseData->data instanceof Zend_Paginator) {
|
||||
$this->_responseData->totalItems = $this->_genericModel->getCountTotal($this->_resourceName);
|
||||
$this->_responseData->currentItemsCount = $this->_responseData->data->getTotalItemCount();
|
||||
$this->_responseData->data = (array)$this->_responseData->data->getCurrentItems();
|
||||
} else if (is_array($this->_responseData->data)) {
|
||||
$this->_responseData->totalItems = $this->_genericModel->getCountTotal($this->_resourceName);
|
||||
$this->_responseData->currentItemsCount = count($this->_responseData->data);
|
||||
} else if (is_string($this->_responseData->data)) {
|
||||
$this->_responseData->data = array('message' => $this->_responseData->data);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->_inArray) {
|
||||
$content = $this->_responseData->data;
|
||||
$this->getResponse()
|
||||
->setHeader('X-Page', $this->getParam('page'))
|
||||
->setHeader('X-Count', $this->getParam('count'))
|
||||
->setHeader('X-Tot-Filtered', $this->_responseData->currentItemsCount)
|
||||
->setHeader('X-Tot-Items', $this->_responseData->totalItems);
|
||||
} else {
|
||||
$content = ($this->_responseData->data);
|
||||
}
|
||||
|
||||
$this->getResponse()
|
||||
->setHeader('Content-Type', 'application/json;charset=utf-8')
|
||||
->setBody($this->_helper->Globalfunction->safeJsonEncode($content))
|
||||
->sendResponse();
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
public function sendErrorResponse($message, $errorCode, $dbName = null)
|
||||
{
|
||||
|
||||
$message = (!$message) ? "Errore non identificato sul web service risorsa: '$this->_resourceName'" : $message;
|
||||
$errorCode = (!$errorCode) ? App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR : $errorCode;
|
||||
|
||||
throw new App_Controller_Errors_WebServiceError(utf8_encode($message), $dbName, $errorCode);
|
||||
}
|
||||
|
||||
public function disableCallAction()
|
||||
{
|
||||
// Disabilito la risorsa
|
||||
$this->sendErrorResponse("Funzione non disponibile", App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_NOPERMISSION);
|
||||
}
|
||||
|
||||
public function getAddFormForResource()
|
||||
{
|
||||
|
||||
$class = implode('_', array(
|
||||
'Application_Form',
|
||||
strtoupper($this->getRequest()->getModuleName()),
|
||||
ucfirst($this->_resourceFormName),
|
||||
'Add'));
|
||||
|
||||
if (!class_exists($class)) {
|
||||
throw new App_Controller_Errors_WebServiceError("Classe '$class' potrebbe non esistere");
|
||||
}
|
||||
|
||||
return new $class();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,393 @@
|
||||
<?php
|
||||
|
||||
abstract class App_Controller_Rest_MongoController extends Zend_Rest_Controller
|
||||
{
|
||||
|
||||
public $_resourceName;
|
||||
|
||||
public $_responseData;
|
||||
public $_status;
|
||||
public $_inArray;
|
||||
|
||||
public $_mongoModel;
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->_helper->viewRenderer->setNoRender(true);
|
||||
$this->_helper->AjaxContext()
|
||||
->addActionContext('get', 'json')
|
||||
->addActionContext('post', 'json')
|
||||
->addActionContext('put', 'json')
|
||||
->addActionContext('patch', 'json')
|
||||
->addActionContext('delete', 'json')
|
||||
->addActionContext('options', 'json')
|
||||
->addActionContext('head', 'json')
|
||||
->initContext('json');
|
||||
|
||||
}
|
||||
|
||||
public function preDispatch()
|
||||
{
|
||||
$this->_responseData = new stdClass();
|
||||
$this->_status = false;
|
||||
$this->_inArray = false;
|
||||
|
||||
$this->_mongoModel = new App_Model_Mongo_Client();
|
||||
|
||||
$this->_response->setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Authorization,x-requested-with,Content-Type,Cache-Control,X-As-Dev-On,X-As-Dev-Pw');
|
||||
$this->_response->setHeader('Access-Control-Expose-Headers', 'X-Tot-Items, X-Page,X-Count,X-Tot-Filtered');
|
||||
$this->_response->setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, PATCH, DELETE');
|
||||
$this->_response->setHeader('Access-Control-Allow-Origin', '*');
|
||||
$this->_response->setHeader('Access-Control-Allow-Credentials', 'true');
|
||||
|
||||
// Controllo sui metodi di prefetch
|
||||
if($_SERVER['REQUEST_METHOD'] === 'OPTIONS'){
|
||||
return $this->optionsAction();
|
||||
}
|
||||
|
||||
if($_SERVER['REQUEST_METHOD'] === 'HEAD'){
|
||||
return $this->optionsAction();
|
||||
}
|
||||
|
||||
if($_SERVER['REQUEST_METHOD'] === 'PATCH'){
|
||||
return $this->patchAction();
|
||||
}
|
||||
}
|
||||
|
||||
//##################################### CRUD FUNCTIONS #############################################################
|
||||
|
||||
public function indexAction()
|
||||
{
|
||||
$this->getAction();
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
|
||||
try {
|
||||
switch ($this->_getParam('method')) {
|
||||
default:
|
||||
if ($this->_getParam('id') != null) {
|
||||
$this->_responseData->data = $this->getSingleElement($this->_getParam('id'));
|
||||
} else {
|
||||
$this->_inArray = true;
|
||||
|
||||
$page = intval($this->_getParam('page', null));
|
||||
$count = intval($this->_getParam('count', null));
|
||||
$order = $this->_getParam('order', null);
|
||||
//$projection = $this->_helper->MongoProjectionFunctions($this->_getParam('select', null), $this->_defaultProjection);
|
||||
$projection = $this->_getParam('select', null);
|
||||
$addFields = null;
|
||||
|
||||
$where = $this->_helper->MongoFilterFunctions($this->getAllParams());
|
||||
|
||||
$where = $this->handleGetListFilters($where);
|
||||
|
||||
$data = $this->_mongoModel->getListAggregation($this->_resourceName, $page, $count, $where, $order, $projection, $addFields);
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($data,true));
|
||||
|
||||
$this->_responseData->data = $data;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function postAction()
|
||||
{
|
||||
|
||||
try {
|
||||
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Inserimento di un singolo elemento
|
||||
if (!$form->isValid($params)) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $form->getValues();
|
||||
|
||||
$dati = $this->preInsertElement($dati);
|
||||
|
||||
$dati['id'] = $this->insertSingleElement($dati);
|
||||
|
||||
$dati = $this->afterInsertElement($dati);
|
||||
|
||||
$this->_responseData->data = array('data' => $dati);
|
||||
$this->_status = true;
|
||||
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function putAction()
|
||||
{
|
||||
try {
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Aggiornamento singolo
|
||||
if (!$form->isValidPartial($params)) {
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $form->getValues();
|
||||
|
||||
if (!$this->_getParam('id')) {
|
||||
$this->_responseData->message = 'Id elemento non trovato';
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $this->preUpdateElement($dati, $this->_getParam('id'));
|
||||
|
||||
$this->updateSingleElement($this->_getParam('id'), $dati);
|
||||
$dati['id'] = $this->_getParam('id');
|
||||
|
||||
$dati = $this->afterUpdateElement($dati);
|
||||
|
||||
$this->_responseData->data = array('data' => $dati);
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function patchAction()
|
||||
{
|
||||
try {
|
||||
$params = Zend_Json::decode($this->getRequest()->getRawBody());
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($params,true));
|
||||
|
||||
$form = $this->getAddFormForResource();
|
||||
|
||||
//Aggiornamento singolo
|
||||
if (!$form->isValidPartial($params)) {
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_FORM_DATAINVALID;
|
||||
$this->_responseData->message = 'I dati inseriti non sono validi';
|
||||
$this->_responseData->errorDetails = $form->getMessages();
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
$dati = $form->getValues();
|
||||
|
||||
//rimuovo i valori vuoti che non devono essere sovrascritti
|
||||
foreach ($dati as $key => $val){
|
||||
if(!$val) unset($dati[$key]);
|
||||
}
|
||||
|
||||
$id = $this->_getParam('id');
|
||||
if (!$id) {
|
||||
$this->_responseData->message = 'Id elemento non trovato';
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($dati,true));
|
||||
|
||||
$oldElement = (array) $this->_mongoModel->getById($this->_resourceName, $id);
|
||||
$dati = array_merge($oldElement, $dati);
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($oldElement,true));
|
||||
|
||||
$dati = $this->preUpdateElement($dati, $id);
|
||||
|
||||
// Zend_Registry::get('log')->info(print_r($dati,true));
|
||||
|
||||
$this->updateSingleElement($id, $dati);
|
||||
$dati['id'] = $id;
|
||||
|
||||
$dati = $this->afterUpdateElement($dati);
|
||||
|
||||
$this->_responseData->data = array('data' => $dati);
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
$this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteAction()
|
||||
{
|
||||
try {
|
||||
|
||||
if (!$this->_getParam('id')) {
|
||||
$this->_status = false;
|
||||
$this->_responseData->message = "Errore id non trovato";
|
||||
$this->_responseData->errorCode = App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_NOIDERROR;
|
||||
return $this->sendResponse();
|
||||
}
|
||||
|
||||
//Eliminazione Singola
|
||||
$this->deleteSingleElement($this->_getParam('id'));
|
||||
|
||||
$this->_status = true;
|
||||
return $this->sendResponse();
|
||||
} catch (Exception $e) {
|
||||
$this->_responseData->message = $e->getMessage();
|
||||
return $this->sendResponse();
|
||||
}
|
||||
}
|
||||
|
||||
public function headAction()
|
||||
{
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
public function optionsAction()
|
||||
{
|
||||
$this->_status = true;
|
||||
$this->sendResponse();
|
||||
}
|
||||
|
||||
//##################################### SINGLE ELEMENT FUNCTIONS ###################################################
|
||||
|
||||
public function getSingleElement($id)
|
||||
{
|
||||
return $this->getSingleElementManipulate($this->_mongoModel->getById($this->_resourceName, $id));
|
||||
}
|
||||
|
||||
public function insertSingleElement($dati)
|
||||
{
|
||||
return $this->_mongoModel->insert($this->_resourceName, $dati);
|
||||
}
|
||||
|
||||
public function updateSingleElement($id, $dati)
|
||||
{
|
||||
return $this->_mongoModel->update($this->_resourceName, $id, $dati);
|
||||
}
|
||||
|
||||
public function deleteSingleElement($id)
|
||||
{
|
||||
return $this->_mongoModel->delete($this->_resourceName, $id);
|
||||
}
|
||||
|
||||
//##################################### HELPERS FUNCTIONS ###################################################
|
||||
|
||||
public function getSingleElementManipulate($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preInsertElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function afterInsertElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function preUpdateElement($data, $id)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function afterUpdateElement($data)
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function handleGetListFilters($where){
|
||||
return $where;
|
||||
}
|
||||
|
||||
//##################################### GENERIC FUNCTIONS ##########################################################
|
||||
|
||||
public function sendResponse()
|
||||
{
|
||||
|
||||
if (!$this->_status) {
|
||||
$message = (!$this->_responseData->message) ? "Errore non identificato sul web service risorsa: '$this->_resourceName'" : $this->_responseData->message;
|
||||
$errorCode = (!$this->_responseData->errorCode) ? App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR : $this->_responseData->errorCode;
|
||||
|
||||
$this->sendErrorResponse($message, $errorCode);
|
||||
}
|
||||
|
||||
if (isset($this->_responseData->data) && is_array($this->_responseData->data)) {
|
||||
$this->_responseData->totalItems = $this->_mongoModel->getCountTotal($this->_resourceName);
|
||||
|
||||
if (Zend_Registry::isRegistered(App_Model_Mongo_Client::WHERE_MONGO_REG_NAME)) {
|
||||
$this->_responseData->currentItemsCount = $this->_mongoModel->getCountFilteredTotal($this->_resourceName, Zend_Registry::get(App_Model_Mongo_Client::WHERE_MONGO_REG_NAME));
|
||||
} else {
|
||||
$this->_responseData->currentItemsCount = $this->_responseData->totalItems;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->_inArray) {
|
||||
$content = $this->_responseData->data;
|
||||
$this->getResponse()
|
||||
->setHeader('X-Page', $this->getParam('page'))
|
||||
->setHeader('X-Count', $this->getParam('count'))
|
||||
->setHeader('X-Tot-Filtered', $this->_responseData->currentItemsCount)
|
||||
->setHeader('X-Tot-Items', $this->_responseData->totalItems);
|
||||
} else {
|
||||
$content = ($this->_responseData->data);
|
||||
}
|
||||
|
||||
$this->getResponse()
|
||||
->setHeader('Content-Type', 'application/json;charset=utf-8')
|
||||
->setBody($this->_helper->Globalfunction->safeJsonEncode($content))
|
||||
->sendResponse();
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
public function sendErrorResponse($message, $errorCode, $dbName = null)
|
||||
{
|
||||
|
||||
$message = (!$message) ? "Errore non identificato sul web service risorsa: '$this->_resourceName'" : $message;
|
||||
$errorCode = (!$errorCode) ? App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR : $errorCode;
|
||||
|
||||
throw new App_Controller_Errors_WebServiceError(utf8_encode($message), $dbName, $errorCode);
|
||||
}
|
||||
|
||||
public function disableCallAction()
|
||||
{
|
||||
// Disabilito la risorsa
|
||||
$this->sendErrorResponse("Funzione non disponibile", App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_NOPERMISSION);
|
||||
}
|
||||
|
||||
protected function getAddFormForResource()
|
||||
{
|
||||
|
||||
$class = implode('_', array(
|
||||
'Application_Form',
|
||||
strtoupper($this->getRequest()->getModuleName()),
|
||||
ucfirst($this->_resourceName),
|
||||
'Add'));
|
||||
|
||||
if (!class_exists($class)) {
|
||||
throw new App_Controller_Errors_WebServiceError("Classe '$class' potrebbe non esistere");
|
||||
}
|
||||
|
||||
return new $class();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user