getSingleBaseName(); } public function new() { $factory = $this->getFactory(); $obj = $factory->createNew(); $this->saveToDatabase($obj, 'new'); } public function change() { $app = Factory::getApplication(); $input = $app->input; $id = (int) $input->post->getInt('id'); $factory = $this->getFactory(); $obj = $factory->loadById($id); $this->saveToDatabase($obj, $id); } /** * @param AbstractCommonClubsModel $obj * @param int $id */ protected function saveToDatabase($obj, $id) { try { // Fetch the posted data $values = $this->loadData($this->additionalData()); $this->filterRaw($values); // Check the input data if( ! $this->requiredDataIsAvailable($values) ) throw new DataParsingException(); if( ! $this->rawDataIsValid($values) ) throw new DataParsingException(); $obj->applyAndMergeValues($values, true); $this->filterObject($obj, $values); // Do some additional tests by the controller if( ! $this->objectValid($obj) ) throw new DataInvalidException(); // Check if the object complains about valitity if( ! $obj->dataIsValid() ) throw new DataInvalidException(); // Do the actual work $obj->save(); $this->saveAssocs($obj, $values); // Redirect to the list of objects $view = $this->getSingleViewName(); $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false)); } catch(DataParsingException $e) { // FIXME Make this robust (are external refs already dereferenced?) $view = $this->getSingleViewName(); $obj->setValues($values, true); $urldata = $obj->pack(); $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false)); } catch(DataInvalidException $e) { $view = $this->getSingleViewName(); $urldata = $obj->pack(); $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}&id={$id}&data={$urldata}", false)); } } protected function saveAssocs($obj, $vlaues){} protected function additionalData() { return array(); } protected function loadData($additionalData) { $values = array(); $factory = $this->getFactory(); $input = Factory::getApplication()->input->post; foreach($factory->getAttributes() as $column) { $values[$column->getAlias()] = $column->getFilter()->getFilteredValue($input, $column->getAlias()); } foreach($additionalData as $k => $v) { $values[$k] = $v->getFilteredValue($input, $k); } return $values; } protected function filterRaw(&$values){} protected function filterObject($obj){} protected function objectValid($obj) { return true; } private function requiredDataIsAvailable($values) { $ok = true; foreach($this->getFactory()->getAttributes() as $column) { $filter = $column->getFilter(); if(! $filter->requiredDataAvailable($values[$column->getAlias()])) { $fname = $filter->getName(); Factory::getApplication()->enqueueMessage("Das Feld $fname ist obligatorisch.", 'error'); $ok = false; } } return $ok; } /** * @param array $values * @param AbstractCommonClubsModel $obj * @return boolean */ protected function rawDataIsValid($values) { $error = false; $factory = $this->getFactory(); foreach($factory->getAttributes() as $column) { if(! $column->getFilter()->rawValueValid($values[$column->getAlias()])) { $fname = $column->getFilter()->getName(); Factory::getApplication()->enqueueMessage("Das Feld $fname ist fehlerhaft.", 'error'); $error = true; } } return ! $error; } protected function fieldValid($name, $value, $options) { if(empty($value)) return false; if(isset($options['filter'])) { switch($options['filter']) { case 'string': if(empty(trim($value))) return false; } } return true; } function delete() { $app = Factory::getApplication(); $id = $app->input->get->getInt('id'); $name = $this->getSingleBaseName(); $app->enqueueMessage("Removal of $name with id $id."); $factory = $this->getFactory(); $element = $factory->loadById($id); $element->delete(); $view = $this->getSingleViewName(); $this->setRedirect(Route::_("index.php?option=com_clubs&view={$view}s", false)); } }