private function _handleDatabaseChange($model, $module, $data, $saveType, $id) { $ret = array('type' => null, 'message' => null, 'id' => $id); if ($model instanceof Phprojekt_Item_Abstract || $id == 0) { $databaseManager = new Phprojekt_DatabaseManager($model); $data = Zend_Json::decode($data); // Validate if ($databaseManager->recordValidate($data, $saveType)) { // Update Table Structure $tableData = $this->_getTableData($data); if (!$databaseManager->syncTable($data, $module, $tableData)) { $ret['type'] = 'error'; $ret['message'] = Phprojekt::getInstance()->translate('There was an error writing the table'); } else { // remove possible id's as we are not allowed to change id's foreach ($data as $key => $value) { unset($data[$key]['id']); } // Update DatabaseManager Table $databaseManager->saveData($module, $data, $tableData); $ret = null; } } else { $error = $databaseManager->getError(); $ret['message'] = $error['label'] . ': ' . $error['message']; $ret['type'] = 'error'; } } else { $ret = null; } return $ret; }
/** * Saves the design of all the fields in the module. * * If the request parameter "id" is null or 0, the function will add a new module, * if the "id" is an existing module, the function will update it. * * The save action will try to add or update the module table itself and the database_manager. * * REQUIRES request parameters: * <pre> * - integer <b>id</b> id of the module to save. * - string <b>designerData</b> Data of the fields. * - string <b>name</b> Name of the module. * - string <b>label</b> Display of the module. * </pre> * * The return is a string in JSON format with: * <pre> * - type => 'success' or 'error'. * - message => Success or error message. * - code => 0. * - id => id of the module. * </pre> * * @throws Phprojekt_PublishedException On error in the action save. * * @return void */ public function jsonSaveAction() { $id = (int) $this->getRequest()->getParam('id'); $data = $this->getRequest()->getParam('designerData'); $saveType = (int) $this->getRequest()->getParam('saveType'); $model = null; $module = Cleaner::sanitize('alnum', $this->getRequest()->getParam('name', null)); $this->setCurrentProjectId(); if (empty($module)) { $module = Cleaner::sanitize('alnum', $this->getRequest()->getParam('label')); } $module = ucfirst(str_replace(" ", "", $module)); if ($id > 0) { $model = Phprojekt_Loader::getModel($module, $module); } if ($model instanceof Phprojekt_Item_Abstract || $id == 0) { $databaseManager = new Phprojekt_DatabaseManager($model); $data = Zend_Json_Decoder::decode($data); // Validate if ($databaseManager->recordValidate($data, $saveType)) { // Update Table Structure $tableData = $this->_getTableData($data); if (!$databaseManager->syncTable($data, $module, $tableData)) { $type = 'error'; $message = Phprojekt::getInstance()->translate('There was an error writing the table'); } else { // Update DatabaseManager Table $databaseManager->saveData($module, $data, $tableData); if (empty($id)) { $message = Phprojekt::getInstance()->translate('The table module was created correctly'); } else { $message = Phprojekt::getInstance()->translate('The table module was edited correctly'); } $type = 'success'; } } else { $error = $databaseManager->getError(); $message = $error['label'] . ': ' . $error['message']; $type = 'error'; } } else { $type = 'success'; $message = null; } $return = array('type' => $type, 'message' => $message, 'code' => 0, 'id' => $id); Phprojekt_Converter_Json::echoConvert($return); }