/** * Validate the form */ private function validateForm() { if ($this->frm->isSubmitted()) { $this->frm->cleanupFields(); // redefine fields $fileFile = $this->frm->getField('file'); $chkOverwrite = $this->frm->getField('overwrite'); // name checks if ($fileFile->isFilled(BL::err('FieldIsRequired'))) { // only xml files allowed if ($fileFile->isAllowedExtension(array('xml'), sprintf(BL::getError('ExtensionNotAllowed'), 'xml'))) { // load xml $xml = @simplexml_load_file($fileFile->getTempFileName()); // invalid xml if ($xml === false) { $fileFile->addError(BL::getError('InvalidXML')); } } } if ($this->frm->isCorrect()) { // import $statistics = BackendLocaleModel::importXML($xml, $chkOverwrite->getValue()); // trigger event BackendModel::triggerEvent($this->getModule(), 'after_import', array('statistics' => $statistics)); // everything is imported, so redirect to the overview $this->redirect(BackendModel::createURLForAction('index') . '&report=imported&var=' . ($statistics['imported'] . '/' . $statistics['total']) . $this->filterQuery); } } }
/** * 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); } }
/** * Sets the filter based on the $_GET array. */ private function setFilter() { $this->filter['language'] = $this->getParameter('language', 'array') != '' ? $this->getParameter('language', 'array') : BL::getWorkingLanguage(); $this->filter['application'] = $this->getParameter('application'); $this->filter['module'] = $this->getParameter('module'); $this->filter['type'] = $this->getParameter('type', 'array'); $this->filter['name'] = $this->getParameter('name'); $this->filter['value'] = $this->getParameter('value'); $this->filterQuery = BackendLocaleModel::buildURLQueryByFilter($this->filter); }
/** * Sets the filter based on the $_GET array. */ private function setFilter() { // if no language is selected, set the working language as the selected if ($this->getParameter('language', 'array') == null) { $_GET['language'] = array(BL::getWorkingLanguage()); $this->parameters['language'] = array(BL::getWorkingLanguage()); } // if no type is selected, set labels as the selected type if ($this->getParameter('type', 'array') == null) { $_GET['type'] = array('lbl'); $this->parameters['type'] = array('lbl', 'act', 'err', 'msg'); } // set filter $this->filter['application'] = $this->getParameter('application') == null ? 'frontend' : $this->getParameter('application'); $this->filter['module'] = $this->getParameter('module', 'string', null); $this->filter['type'] = $this->getParameter('type', 'array'); $this->filter['language'] = $this->getParameter('language', 'array'); $this->filter['name'] = $this->getParameter('name') == null ? '' : $this->getParameter('name'); $this->filter['value'] = $this->getParameter('value') == null ? '' : $this->getParameter('value'); // build query for filter $this->filterQuery = BackendLocaleModel::buildURLQueryByFilter($this->filter); }
/** * Create the XML based on the locale items. * * @return void */ private function createXML() { // create XML $xmlOutput = BackendLocaleModel::createXMLForExport($this->locale); // xml headers $headers[] = 'Content-Disposition: attachment; filename="locale_' . BackendModel::getUTCDate('d-m-Y') . '.xml"'; $headers[] = 'Content-Type: application/octet-stream;charset=utf-8'; $headers[] = 'Content-Length: ' . strlen($xmlOutput); // set headers SpoonHTTP::setHeaders($headers); // output XML echo $xmlOutput; // stop script exit; }
/** * 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); } } }
/** * Build items array and group all items by application, module, type and name. * * @return void */ private function setItems() { // init $this->locale = array(); // get items $frontend = BackendLocaleModel::getNonExistingFrontendLocale($this->filter['language']); // group by application, module, type and name foreach ($frontend as $item) { $item['value'] = null; $this->locale[$item['application']][$item['module']][$item['type']][$item['name']][] = $item; } // no need to keep this around unset($frontend); // get items $backend = BackendLocaleModel::getNonExistingBackendLocale($this->filter['language']); // group by application, module, type and name foreach ($backend as $item) { $item['value'] = null; $this->locale[$item['application']][$item['module']][$item['type']][$item['name']][] = $item; } // no need to keep this around unset($backend); }
/** * Set locale * It will require the correct file and init the needed vars * * @param string $language The language to load. */ public static function setLocale($language) { $language = (string) $language; // require the BackendLocaleModel require_once BACKEND_MODULES_PATH . '/locale/engine/model.php'; // validate file, generate it if needed if (!SpoonFile::exists(BACKEND_CACHE_PATH . '/locale/en.php')) { BackendLocaleModel::buildCache('en', APPLICATION); } if (!SpoonFile::exists(BACKEND_CACHE_PATH . '/locale/' . $language . '.php')) { BackendLocaleModel::buildCache($language, APPLICATION); } // store self::$currentInterfaceLanguage = $language; // attempt to set a cookie try { // store in cookie CommonCookie::set('interface_language', $language); } catch (SpoonCookieException $e) { // settings cookies isn't allowed, because this isn't a real problem we ignore the exception } // init vars $err = array(); $lbl = array(); $msg = array(); // set English translations, they'll be the fallback require BACKEND_CACHE_PATH . '/locale/en.php'; self::$err = (array) $err; self::$lbl = (array) $lbl; self::$msg = (array) $msg; // overwrite with the requested language's translations require BACKEND_CACHE_PATH . '/locale/' . $language . '.php'; foreach ($err as $module => $translations) { if (!isset(self::$err[$module])) { self::$err[$module] = array(); } self::$err[$module] = array_merge(self::$err[$module], $translations); } foreach ($lbl as $module => $translations) { if (!isset(self::$lbl[$module])) { self::$lbl[$module] = array(); } self::$lbl[$module] = array_merge(self::$lbl[$module], $translations); } foreach ($msg as $module => $translations) { if (!isset(self::$msg[$module])) { self::$msg[$module] = array(); } self::$msg[$module] = array_merge(self::$msg[$module], $translations); } }
/** * Create an XML-string that can be used for export. * * @param array $items The items. * @return string */ public static function createXMLForExport(array $items) { $xml = new DOMDocument('1.0', SPOON_CHARSET); // set some properties $xml->preserveWhiteSpace = false; $xml->formatOutput = true; // locale root element $root = $xml->createElement('locale'); $xml->appendChild($root); // loop applications foreach ($items as $application => $modules) { // create application element $applicationElement = $xml->createElement($application); $root->appendChild($applicationElement); // loop modules foreach ($modules as $module => $types) { // create application element $moduleElement = $xml->createElement($module); $applicationElement->appendChild($moduleElement); // loop types foreach ($types as $type => $items) { // loop items foreach ($items as $name => $translations) { // create application element $itemElement = $xml->createElement('item'); $moduleElement->appendChild($itemElement); // attributes $itemElement->setAttribute('type', BackendLocaleModel::getTypeName($type)); $itemElement->setAttribute('name', $name); // loop translations foreach ($translations as $translation) { // create translation $translationElement = $xml->createElement('translation'); $itemElement->appendChild($translationElement); // attributes $translationElement->setAttribute('language', $translation['language']); // set content $translationElement->appendChild(new DOMCdataSection($translation['value'])); } } } } } return $xml->saveXML(); }
/** * Imports the locale XML file * * @param string $filename The full path for the XML-file. * @param bool[optional] $overwriteConflicts Should we overwrite when there is a conflict? */ protected function importLocale($filename, $overwriteConflicts = false) { $filename = (string) $filename; $overwriteConflicts = (bool) $overwriteConflicts; // load the file content and execute it $content = trim(SpoonFile::getContent($filename)); // file actually has content if (!empty($content)) { // load xml $xml = @simplexml_load_file($filename); // import if it's valid xml if ($xml !== false) { // import locale require_once BACKEND_MODULES_PATH . '/locale/engine/model.php'; BackendLocaleModel::importXML($xml, $overwriteConflicts, $this->getLanguages(), $this->getInterfaceLanguages(), $this->getDefaultUserID(), gmdate('Y-m-d H:i:s')); } } }
/** * Load the datagrids */ private function loadDataGrids() { /* * Frontend datagrid */ $this->dgFrontend = new BackendDataGridArray(BackendLocaleModel::getNonExistingFrontendLocale(BL::getWorkingLanguage())); // overrule default URL $this->dgFrontend->setURL(BackendModel::createURLForAction(null, null, null, array('offset' => '[offset]', 'order' => '[order]', 'sort' => '[sort]'), false)); // sorting columns $this->dgFrontend->setSortingColumns(array('language', 'application', 'module', 'type', 'name'), 'name'); // check if this action is allowed if (BackendAuthentication::isAllowedAction('add')) { // set colum URLs $this->dgFrontend->setColumnURL('name', BackendModel::createURLForAction('add') . '&language=[language]&application=[application]&module=[module]&type=[type]&name=[name]'); } // set column functions $this->dgFrontend->setColumnFunction(array(__CLASS__, 'formatFilesList'), '[used_in]', 'used_in', true); // check if this action is allowed if (BackendAuthentication::isAllowedAction('save_translation')) { // add columns $this->dgFrontend->addColumn('translation', null, null, null, BL::lbl('Add')); // add a class for the inline edit $this->dgFrontend->setColumnAttributes('translation', array('class' => 'translationValue')); // add attributes, so the inline editing has all the needed data $this->dgFrontend->setColumnAttributes('translation', array('data-id' => '{language: \'[language]\', application: \'[application]\', module: \'[module]\', name: \'[name]\', type: \'[type]\'}')); $this->dgFrontend->setColumnAttributes('translation', array('style' => 'width: 150px')); } // disable paging $this->dgFrontend->setPaging(false); /* * Backend datagrid */ $this->dgBackend = new BackendDataGridArray(BackendLocaleModel::getNonExistingBackendLocale(BL::getWorkingLanguage())); // overrule default URL $this->dgBackend->setURL(BackendModel::createURLForAction(null, null, null, array('offset' => '[offset]', 'order' => '[order]', 'sort' => '[sort]'), false)); // sorting columns $this->dgBackend->setSortingColumns(array('language', 'application', 'module', 'type', 'name'), 'name'); // check if this action is allowed if (BackendAuthentication::isAllowedAction('add')) { // set column URLs $this->dgBackend->setColumnURL('name', BackendModel::createURLForAction('add') . '&language=[language]&application=[application]&module=[module]&type=[type]&name=[name]'); } // set column functions $this->dgBackend->setColumnFunction(array(__CLASS__, 'formatFilesList'), '[used_in]', 'used_in', true); // check if this action is allowed if (BackendAuthentication::isAllowedAction('save_translation')) { // add columns $this->dgBackend->addColumn('translation', null, null, null, BL::lbl('Add')); // add a class for the inline edit $this->dgBackend->setColumnAttributes('translation', array('class' => 'translationValue')); // add attributes, so the inline editing has all the needed data $this->dgBackend->setColumnAttributes('translation', array('data-id' => '{language: \'[language]\', application: \'[application]\', module: \'[module]\', name: \'[name]\', type: \'[type]\'}')); $this->dgBackend->setColumnAttributes('translation', array('style' => 'width: 150px')); } // disable paging $this->dgBackend->setPaging(false); }
/** * Set locale * It will require the correct file and init the needed vars * * @return void * @param string $language The language to load. */ public static function setLocale($language) { // redefine $language = (string) $language; // check if file exists if (!SpoonFile::exists(BACKEND_CACHE_PATH . '/locale/' . $language . '.php')) { // require the BackendLocaleModel require_once BACKEND_MODULES_PATH . '/locale/engine/model.php'; // build locale file BackendLocaleModel::buildCache($language, APPLICATION); } // store self::$currentInterfaceLanguage = $language; // attempt to set a cookie try { // store in cookie SpoonCookie::set('interface_language', $language); } catch (SpoonCookieException $e) { // settings cookies isn't allowed, because this isn't a real problem we ignore the exception } // store in session for TinyMCE SpoonSession::set('tiny_mce_language', $language); SpoonSession::set('interface_language', $language); // init vars $err = array(); $lbl = array(); $msg = array(); // require file require BACKEND_CACHE_PATH . '/locale/' . $language . '.php'; // set language specific labels self::$err = (array) $err; self::$lbl = (array) $lbl; self::$msg = (array) $msg; }