Skip to content

Commit

Permalink
add OnHandleRequest event back to override global lexicon's language
Browse files Browse the repository at this point in the history
[#2] add redirector for translated alias
  • Loading branch information
goldsky committed Jul 8, 2014
1 parent 7455c9b commit 0444b22
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
1 change: 1 addition & 0 deletions _build/lingua/data/transport.plugin.events.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function setEventObjects(array $events = array()) {
'OnDocFormPrerender',
'OnDocFormSave',
'OnEmptyTrash',
'OnHandleRequest',
'OnInitCulture',
'OnMediaSourceFormSave',
'OnPluginFormSave',
Expand Down
9 changes: 7 additions & 2 deletions core/components/lingua/docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Lingua 2.0.0-beta2
Lingua 2.0.0-beta3 (July 8, 2014)
=================================
- add OnHandleRequest event back to override global lexicon's language.
- [#2] add redirector for translated alias

Lingua 2.0.0-beta2 (July 7, 2014)
=================================
- bugfix TVs' values overriding on front-end
- add cache clearing on all elements' form saving actions.

Lingua 2.0.0-beta1
Lingua 2.0.0-beta1 (July 6, 2014)
=================================
- Initialize manager language switcher
- replace options: lingua.get.key to lingua.request_key, lingua.code.field to lingua.code_field
Expand Down
80 changes: 71 additions & 9 deletions core/components/lingua/elements/plugins/lingua.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,69 @@
*/
$event = $modx->event->name;
switch ($event) {
case 'OnInitCulture':
case 'OnPageNotFound':
$lingua = $modx->getService('lingua', 'Lingua', MODX_CORE_PATH . 'components/lingua/model/lingua/');

if (!($lingua instanceof Lingua)) {
return '';
}
$modx->lexicon->load('lingua:default');

$search = $_SERVER['REQUEST_URI'];
$baseUrl = $modx->getOption('base_url',null,MODX_BASE_URL);
if(!empty($baseUrl) && $baseUrl != '/' && $baseUrl != ' ' && $baseUrl != '/'.$modx->context->get('key').'/') {
$search = str_replace($baseUrl,'',$search);
}

$search = ltrim($search,'/');
if(!empty($search)) {
$parts = @explode('/', $search);
$reversed = array_reverse($parts);
$aliases = @explode('.', $reversed[0]);
$reversedAliases = array_reverse($aliases);

$modContentTypes = $modx->getCollection('modContentType');
$contentTypes = array();
if ($modContentTypes) {
foreach ($modContentTypes as $modContentType) {
$contentTypes[] = $modContentType->get('file_extensions');
}
}
if (in_array('.' . $reversedAliases[0], $contentTypes)) {
$extension = array_pop($aliases);
}
$cleanAlias = @implode('.', $aliases);
if (!empty($cleanAlias)) {
$c = $modx->newQuery('linguaSiteContent');
$c->leftJoin('modResource', 'Resource', 'Resource.id = linguaSiteContent.resource_id');
$c->where(array(
'alias:LIKE' => $cleanAlias,
'Resource.published:=' => 1,
'Resource.deleted:!=' => 1,
));
$clone = $modx->getObject('linguaSiteContent', $c);
if ($clone) {
$resource = $modx->getObject('modResource', $clone->get('resource_id'));
if ($resource) {
$lang = $modx->getObject('linguaLangs', $clone->get('lang_id'));
if ($lang) {
$_SESSION['cultureKey'] = $lang->get('lang_code');
}
$modx->sendForward($resource->get('id'));
// $url = $modx->makeUrl($resource->get('id'));
// if (!empty($url)) {
// $options = array('responseCode' => 'HTTP/1.1 301 Moved Permanently');
// $modx->sendRedirect($url, $options);
// }
}
}
}
}

break;

case 'OnHandleRequest': // for global
case 'OnInitCulture': // for request class
if ($modx->context->key !== 'mgr') {
$langGetKey = $modx->getOption('lingua.request_key', $scriptProperties, 'lang');
$langGetKeyValue = filter_input(INPUT_GET, $langGetKey, FILTER_SANITIZE_STRING);
Expand Down Expand Up @@ -413,6 +475,7 @@

// update linguaSiteContent
$reverting = array();
$clearKeys = array();
foreach ($resource->_fields as $k => $v) {
if (!preg_match('/_lingua$/', $k)) {
continue;
Expand All @@ -423,11 +486,7 @@
}
$reverting[$a][preg_replace('/_lingua$/', '', $k)] = $b;
}
/**
* json seems has number of characters limit;
* that makes saving success report truncated and output modal hangs
*/
$resource->set($k, '');
$clearKeys[] = $k;
}

$resourceId = $resource->get('id');
Expand Down Expand Up @@ -521,15 +580,18 @@
break;
}
$reverting[$lang][$tvId] = $value;
$clearKeys[] = $k;
}

/**
* json seems has number of characters limit;
* json seems to have number of characters limit;
* that makes saving success report truncated and output modal hangs,
* TV's procces does this outside of reverting's loops
*/
foreach ($resource->_fields as $k => $value) {
$resource->set($k, '');
if (!empty($clearKeys)) {
foreach ($clearKeys as $k) {
$resource->set($k, '');
}
}

foreach ($reverting as $k => $tmplvars) {
Expand Down

0 comments on commit 0444b22

Please sign in to comment.