/** * Execute the action * * @return void */ public function execute() { // call parent, this will probably add some general CSS/JS or other required files parent::execute(); // user is god? $isGod = BackendAuthentication::getUser()->isGod(); // get possible languages if ($isGod) { $possibleLanguages = array_unique(array_merge(BL::getWorkingLanguages(), BL::getInterfaceLanguages())); } else { $possibleLanguages = BL::getWorkingLanguages(); } // get parameters $language = SpoonFilter::getPostValue('language', array_keys($possibleLanguages), null, 'string'); $module = SpoonFilter::getPostValue('module', BackendModel::getModules(false), null, 'string'); $name = SpoonFilter::getPostValue('name', null, null, 'string'); $type = SpoonFilter::getPostValue('type', BackendModel::getDB()->getEnumValues('locale', 'type'), null, 'string'); $application = SpoonFilter::getPostValue('application', array('backend', 'frontend'), null, 'string'); $value = SpoonFilter::getPostValue('value', null, null, 'string'); // validate values if (trim($value) == '' || $language == '' || $module == '' || $type == '' || $application == '' || $application == 'frontend' && $module != 'core') { $error = BL::err('InvalidValue'); } // in case this is a 'act' type, there are special rules concerning possible values if ($type == 'act' && !isset($error)) { if (!SpoonFilter::isValidAgainstRegexp('|^([a-z0-9\\-\\_])+$|', $value)) { $error = BL::err('InvalidActionValue', $this->getModule()); } } // no error? if (!isset($error)) { // build item $item['language'] = $language; $item['module'] = $module; $item['name'] = $name; $item['type'] = $type; $item['application'] = $application; $item['value'] = $value; $item['edited_on'] = BackendModel::getUTCDate(); $item['user_id'] = BackendAuthentication::getUser()->getUserId(); // does the translation exist? if (BackendLocaleModel::existsByName($name, $type, $module, $language, $application)) { // add the id to the item $item['id'] = (int) BackendLocaleModel::getByName($name, $type, $module, $language, $application); // update in db BackendLocaleModel::update($item); } else { // insert in db BackendLocaleModel::insert($item); } // output OK $this->output(self::OK); } else { $this->output(self::ERROR, null, $error); } }
/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { $this->frm->cleanupFields(); // redefine fields $txtName = $this->frm->getField('name'); $txtValue = $this->frm->getField('value'); // name checks if ($txtName->isFilled(BL::err('FieldIsRequired'))) { // allowed regex (a-z and 0-9) if ($txtName->isValidAgainstRegexp('|^([a-z0-9])+$|i', BL::err('InvalidName'))) { // first letter does not seem to be a capital one if (!in_array(substr($txtName->getValue(), 0, 1), range('A', 'Z'))) { $txtName->setError(BL::err('InvalidName')); } else { // this name already exists in this language if (BackendLocaleModel::existsByName($txtName->getValue(), $this->frm->getField('type')->getValue(), $this->frm->getField('module')->getValue(), $this->frm->getField('language')->getValue(), $this->frm->getField('application')->getValue())) { $txtName->setError(BL::err('AlreadyExists')); } } } } // value checks if ($txtValue->isFilled(BL::err('FieldIsRequired'))) { // in case this is a 'act' type, there are special rules concerning possible values if ($this->frm->getField('type')->getValue() == 'act') { if (urlencode($txtValue->getValue()) != SpoonFilter::urlise($txtValue->getValue())) { $txtValue->addError(BL::err('InvalidValue')); } } } // module should be 'core' for any other application than backend if ($this->frm->getField('application')->getValue() != 'backend' && $this->frm->getField('module')->getValue() != 'core') { $this->frm->getField('module')->setError(BL::err('ModuleHasToBeCore')); } if ($this->frm->isCorrect()) { // build item $item['user_id'] = BackendAuthentication::getUser()->getUserId(); $item['language'] = $this->frm->getField('language')->getValue(); $item['application'] = $this->frm->getField('application')->getValue(); $item['module'] = $this->frm->getField('module')->getValue(); $item['type'] = $this->frm->getField('type')->getValue(); $item['name'] = $this->frm->getField('name')->getValue(); $item['value'] = $this->frm->getField('value')->getValue(); $item['edited_on'] = BackendModel::getUTCDate(); // update item $item['id'] = BackendLocaleModel::insert($item); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_add', array('item' => $item)); // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('index', null, null, null) . '&report=added&var=' . urlencode($item['name']) . '&highlight=row-' . $item['id'] . $this->filterQuery); } } }