_controller = $controller; $this->_model = $controller->getMainModel(); if (!($this->_controller->_configs instanceof App_Service_ConfigManager)) throw new Exception("E' necessario instanziare nella classe " . get_class($this->_controller) . " la proprietà '_configs' come App_Service_ConfigManager"); $this->_COOKIE_EXPIRE_TIME = time() + (86400 * 30); $BASE_COOKIE_STRING = str_replace('#module#', strtolower($this->_controller->getRequest()->getModuleName()), str_replace('#site#', strtolower(Zend_Registry::get('ENVIROMENTS')->get('site_short_id')), str_replace('#resource#', strtolower($this->_resourceName), self::COOKIE_BASE_STRING_VARIABLE_NAME))); $this->_COUNT_VARIABLE_NAME = $BASE_COOKIE_STRING . "count"; $this->_ORDER_VARIABLE_NAME = $BASE_COOKIE_STRING . "order"; $this->_ORDERTYPE_VARIABLE_NAME = $BASE_COOKIE_STRING . "ordertype"; $this->_SEARCHPARAMS_VARIABLE_NAME = $BASE_COOKIE_STRING . "searchparams"; } /*************************************************************************************************************** ************************************** FUNZIONI CRUD *********************************************** **************************************************************************************************************/ public function lista() { $this->listPreDispatch(); $this->handleSearchListParams(); $this->checkIfSetJoinData($this->_SEARCH_PARAMS); $this->checkIfSetGroupBy($this->_SEARCH_PARAMS); $orderString = $this->_ORDERLIST . ' ' . $this->_ORDERLIST_TYPE; $where = $this->_controller->getHelper('FilterFunctions')->direct($this->_SEARCH_PARAMS, true); $list = $this->_model->getList($this->_resourceName, $this->_PAGE, $this->_COUNT, $where, $orderString, '*', $this->_joinData, $this->_groupBy); if ($list instanceof Zend_Paginator) { $list->setPageRange(5); $this->_controller->view->totFiltrati = $list->getTotalItemCount(); } else { $this->_controller->view->totFiltrati = count($list); } $this->_controller->view->totElementi = $this->_model->getCountTotal($this->_resourceName); $this->_controller->view->table = $list; $this->listPostDispatch(); } public function get() { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if ($this->_controller->getRequest()->isXmlHttpRequest()) { if (!$this->_controller->getParam('id')) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => false, 'message' => 'Id non ricevuto'))); } $element = $this->_model->getById($this->_resourceName, $this->_controller->getParam('id')); if (!$element) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => false, 'message' => $this->_resourceName . ' non trovato'), false)); } if (method_exists($this, 'getElement')) { $element = $this->getElement($element); } $response = Zend_Json::encode(array_merge($element, array('success' => true))); $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody($response); } } public function save() { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if ($this->_controller->getRequest()->isXmlHttpRequest()) { if (!$this->_controller->getRequest()->isPost()) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Errore durante la richiesta.'))); } $form = $this->getDefaultSaveFormClassName(); if (!$form->isValid($_POST)) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => 'invalid', 'message' => 'Controllare i dati inseriti nei campi sono presenti errori.', 'errors' => $form->getMessages()))); } try { $dati = $form->getValues(); $id = $this->_controller->getParam('id'); $op = $this->_controller->getParam('op'); $response = array(); $response['id'] = $id; $response['op'] = $op; if ($op == 'insert') { $dati = $this->preInsertFunction($dati); $dati['id'] = $this->insertFunction($dati); $dati = $this->afterInsertFunction($dati); } else { if (!$id) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Errore id non ricevuto'))); } $dati = $this->preUpdateFunction($dati, $id); $this->updateFunction($id, $dati); $dati['id'] = $id; $dati = $this->afterUpdateFunction($dati, $id); } $this->_controller->view->assign($dati); $this->_controller->render(strtolower($this->_resourceName) . '/td'); $response['text'] = $this->_controller->getResponse()->getBody(); $response['data'] = $dati; $response['success'] = true; $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode($response)); } catch (Exception $e) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('stato' => false, 'shortMessage' => 'Si è verificato un errore durante il salvataggio dei dati', 'message' => $e->getMessage()) )); } } } public function delete() { if ($this->_controller->getRequest()->isXmlHttpRequest()) { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if (!$this->_controller->getParam('id') || !$this->_controller->getParam('cod')) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode(array('success' => false, 'message' => 'Id o codice non trovato.'))); } try { $this->deleteSingleElement($this->_controller->getParam('id')); return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode(array('success' => true))); } catch (Exception $e) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode(array('success' => false, 'message' => 'Si è verificato un errore durante l\'eliminazione.' . $e->getMessage()))); } } } public function deleteSelect() { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if ($this->_controller->getRequest()->isXmlHttpRequest()) { if ($this->_controller->getParam('data')) { $data = $this->_controller->getParam('data'); if (count($data) <= 0) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Nessun elemento selezionato'))); } $response['success'] = 'TRUE'; $response['id'] = array(); foreach ($data as $key => $value) { array_push($response['id'], $value); $this->deleteSingleElement($value); } } else { $response['success'] = FALSE; $response['message'] = "Si è verificato un errore durante l'eliminazione"; } $response = Zend_Json::encode($response); $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody($response); } } public function setstate() { if ($this->_controller->getRequest()->isXmlHttpRequest()) { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if (!$this->_controller->getParam('id')) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Id non trovato.'))); } try { $this->_model->update('Errorlog', $this->_controller->getParam('id'), array('state' => $this->_controller->getParam('state'))); $this->_controller->view->assign($this->_model->getById('Errorlog', $this->_controller->getParam('id'))); $this->_controller->render('errorlog/td'); $response['text'] = $this->_controller->getResponse()->getBody(); $response['success'] = true; return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody(Zend_Json::encode($response)); } catch (Exception $e) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Si è verificato un errore durante l\'eliminazione.'))); } } } public function setstateSelect() { $this->_controller->getHelper('layout')->disableLayout(); $this->_controller->getHelper('viewRenderer')->setNoRender(); if ($this->_controller->getRequest()->isXmlHttpRequest()) { if ($this->_controller->getParam('data')) { $data = $this->_controller->getParam('data'); if (count($data) <= 0) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Nessun elemento selezionato'))); } $state = $this->_controller->getParam('state'); if (!$state) { return $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8') ->setBody(Zend_Json::encode(array('success' => FALSE, 'message' => 'Stato da impostare non selezionato'))); } $response['rows'] = array(); foreach ($data as $key => $value) { $this->_controller->getResponse()->setBody(""); $this->_model->update($this->_resourceName, $value, array('state' => $state)); $this->_controller->view->assign($this->_model->getById($this->_resourceName, $value)); $this->_controller->render('errorlog/td'); $tdRow = array( 'id' => $value, 'tdRow' => $this->_controller->getResponse()->getBody() ); array_push($response['rows'], $tdRow); } $response['success'] = true; } else { $response['success'] = FALSE; $response['message'] = "Si è verificato un errore durante l'aggiornamento dei dati"; } $response = Zend_Json::encode($response); $this->_controller->getResponse()->setHeader('Content-type', 'application/json;charset=UTF-8')->setBody($response); } } /*************************************************************************************************************** *********************************** FUNZIONI SPECIALIZZATE ***************************************** **************************************************************************************************************/ /** * Serve a manipolare i search params dopo che sono stati recuperati dallo storage e ad esempio a fissare dei parametri fissi */ public function handleSearchListParams(){ } /** * Serve a impostare il joinData i params vengono passati per essere utilizzati per eventuali condizioni * @param $params */ public function checkIfSetJoinData($params){ } /** * Serve a impostare il groupBy i params vengono passati per essere utilizzati per eventuali condizioni * @param $params */ public function checkIfSetGroupBy($params){ } //Funzione di eliminazione singolo elemento personalizzarabile definendo il metodo deleteElement protected function deleteSingleElement($id) { $this->preDeleteFunction($id); if (method_exists($this, 'deleteElement')) { $this->deleteElement($id); } else { $this->_model->delete($this->_resourceName, $id); } $this->afterDeleteFunction($id); } /** * Funzione da sovrascrivere che viene eseguita prima l'inserimento dell'elemento nel db; (deve restiuire i dati) * @param $data * @return mixed */ public function preInsertFunction($data) { return $data; } /** * Funzione che effettua il singolo inserimento dell'elemento * * @param array $dati * @return mixed */ public function insertFunction(array $dati){ return $this->_model->insert($this->_resourceName, $dati); } /** * Funzione da sovrascrivere che viene eseguita dopo l'inserimento dell'elemento nel db; (deve restiuire i dati) * @param $data * @return mixed */ public function afterInsertFunction($data) { return $data; } /** * Funzione da sovrascrivere che viene eseguita prima l'aggiornamento dei dati nel db; (deve restiuire i dati) * @param $data * @return mixed */ public function preUpdateFunction($data, $id) { return $data; } /** * Funzione che effettua il singolo update dell'elemento * @param $id * @param array $dati */ public function updateFunction($id, array $dati){ $this->_model->update($this->_resourceName, $id, $dati); } /** * Funzione da sovrascrivere che viene eseguita dopo l'aggiornamento dei dati nel db; (deve restiuire i dati) * @param $data * @return mixed */ public function afterUpdateFunction($data, $id) { return $data; } /** * Funzione da sovrascrivere che viene eseguita prima dell'eliminazione dell'elemento nel db; (più che altro è una funzione di controllo) * @param $id * @return mixed */ public function preDeleteFunction($id) { return null; } /** Funzione che viene eseguita appena dopo l'eliminazione * @param $id * @return null */ public function afterDeleteFunction($id) { return null; } /** * @return mixed * * Restituisce la classe Form generica "Add" per la sezione */ public function getDefaultSaveFormClassName() { $class = implode('_', array( 'Application', 'Form', ucfirst($this->_controller->getRequest()->getModuleName()), ucfirst($this->_controller->getRequest()->getControllerName()), $this->_resourceName, 'Add')); return new $class(); } private function listPreDispatch() { $this->_controller->view->resourceName = strtolower($this->_resourceName); $this->_controller->view->isActiveModalDetail = $this->_enableModalDetail; $this->_controller->view->isActiveFilterModal = $this->_enableModalFilter; $this->_controller->view->isActiveNewInsert = $this->_enableNewInsert; $this->_controller->view->customJsScript = $this->_customJsSectionScripts; $this->_controller->view->customCssScript = $this->_customCssSectionScripts; $count = $this->_controller->getParam('count'); if ($count !== null && intval($count) != 0) { $this->_COUNT = intval($count); setcookie($this->_COUNT_VARIABLE_NAME, intval($count), $this->_COOKIE_EXPIRE_TIME, "/"); } else { if (isset($_COOKIE[$this->_COUNT_VARIABLE_NAME])) { $this->_COUNT = intval($_COOKIE[$this->_COUNT_VARIABLE_NAME]); } else { $this->_COUNT = $this->_defaultCount; } } if ($this->_controller->getParam('reset')) { $this->_PAGE = 1; //$this->_COUNT = $this->_defaultCount; $this->_ORDERLIST = $this->_model->getResource($this->_resourceName)->getDefaultOrder(); $this->_ORDERLIST_TYPE = $this->_model->getResource($this->_resourceName)->getDefaultTipoOrder(); $this->_SEARCH_PARAMS = array(); //Resetto anche i cookie //setcookie($this->_COUNT_VARIABLE_NAME, intval($this->_COUNT), $this->_COOKIE_EXPIRE_TIME, "/"); setcookie($this->_ORDER_VARIABLE_NAME, $this->_ORDERLIST, $this->_COOKIE_EXPIRE_TIME, "/"); setcookie($this->_ORDERTYPE_VARIABLE_NAME, $this->_ORDERLIST_TYPE, $this->_COOKIE_EXPIRE_TIME, "/"); setcookie($this->_SEARCHPARAMS_VARIABLE_NAME, Zend_Json::encode($this->_SEARCH_PARAMS), $this->_COOKIE_EXPIRE_TIME, "/"); return; } $this->_PAGE = $this->_controller->getParam('page', 1); if (isset($_COOKIE[$this->_ORDER_VARIABLE_NAME]) && !$this->_controller->getParam('order')) { $this->_ORDERLIST = $_COOKIE[$this->_ORDER_VARIABLE_NAME]; //Zend_Registry::get('log')->info(print_r("ORDERLIST_COOKIE_PARAM: ".$this->_ORDERLIST,true)); } else { $this->_ORDERLIST = $this->_controller->getParam('order', $this->_model->getResource($this->_resourceName)->getDefaultOrder()); //Zend_Registry::get('log')->info(print_r("ORDERLIST_NEW_PARAM: ".$this->_ORDERLIST,true)); setcookie($this->_ORDER_VARIABLE_NAME, $this->_ORDERLIST, $this->_COOKIE_EXPIRE_TIME, "/"); } if (isset($_COOKIE[$this->_ORDERTYPE_VARIABLE_NAME]) && !$this->_controller->getParam('tipoOrder')) { $this->_ORDERLIST_TYPE = $_COOKIE[$this->_ORDERTYPE_VARIABLE_NAME]; //Zend_Registry::get('log')->info(print_r("ORDERLISTTYPE_COOKIE_PARAM: ".$this->_ORDERLIST_TYPE,true)); } else { $this->_ORDERLIST_TYPE = $this->_controller->getParam('tipoOrder', $this->_model->getResource($this->_resourceName)->getDefaultTipoOrder()); //Zend_Registry::get('log')->info(print_r("ORDERLISTTYPE_NEW_PARAM: ".$this->_ORDERLIST_TYPE,true)); setcookie($this->_ORDERTYPE_VARIABLE_NAME, $this->_ORDERLIST_TYPE, $this->_COOKIE_EXPIRE_TIME, "/"); } if (isset($_COOKIE[$this->_SEARCHPARAMS_VARIABLE_NAME])) { $this->_SEARCH_PARAMS = Zend_Json::Decode($_COOKIE[$this->_SEARCHPARAMS_VARIABLE_NAME]); } else { $this->_SEARCH_PARAMS = array(); } //Aggiungo eventuali nuovi parametri $params = $this->_controller->getHelper('FilterFunctions')->cleanParams($this->_controller->getAllParams(), false); //Riunisco i parametri gia esistenti con i nuovi $this->_SEARCH_PARAMS = array_replace($this->_SEARCH_PARAMS, $params); } private function listPostDispatch() { $this->_controller->view->moduleName = $this->_controller->getRequest()->getModuleName(); $this->_controller->view->count = $this->_COUNT; $this->_controller->view->page = $this->_PAGE; $this->_controller->view->order = $this->_ORDERLIST; $this->_controller->view->tipoOrder = $this->_ORDERLIST_TYPE; $this->_controller->view->search_params = $this->_SEARCH_PARAMS; //Elimino i parametri vuoti dalle searchparams $this->_SEARCH_PARAMS = $this->_controller->getHelper('FilterFunctions')->cleanParams($this->_controller->getAllParams()); setcookie($this->_SEARCHPARAMS_VARIABLE_NAME, Zend_Json::encode($this->_SEARCH_PARAMS), $this->_COOKIE_EXPIRE_TIME, '/'); //Cambio il nome dello script view in 'list' Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setScriptAction(strtolower($this->_resourceName) . '/list'); } }