_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(); } }