public function save_permissions() { if (!$this->checkParams(array('ModuleComponent', 'ModuleObject'))) { sendBack(); } if (!$this->loadData()) { sendBack(); } $moduleobject = $this->_uses[$this->modeltype]; $idField = $moduleobject->idField; $idValue = $moduleobject->{$moduleobject->idField}; $errors = array(); $flash = Flash::Instance(); $db = DB::Instance(); $db->Debug(); $db->StartTrans(); $components = $this->_data['ModuleComponent']; $current_components = array(); foreach ($components as $key => $component) { // delete any registered components that are de-registered if (!isset($component['register'])) { if (!empty($component['id'])) { $modulecomponent = new ModuleComponent(); $modulecomponent->delete($component['id']); } unset($components[$key]); } else { // create list of current registered components if (!empty($component['id'])) { $current_components[$component['id']] = $key; } } } // delete any entries that no longer exist in the file system foreach ($moduleobject->module_components as $module_component) { if (!isset($current_components[$module_component->id])) { $module_component->delete(); } } foreach ($components as $data) { $component = DataObject::Factory($data, $errors, 'ModuleComponent'); if (!$component || !$component->save()) { $errors[] = 'Failed to save components'; break; } } if (count($errors) > 0) { $flash->addErrors($errors); $db->FailTrans(); $db->CompleteTrans(); $this->_data['id'] = $idValue; $this->refresh(); return; } else { $moduleobject->update($idValue, 'registered', true); $flash->addMessage('components added'); } $db->CompleteTrans(); sendTo($this->name, 'index', $this->_modules); }