/
controller.php
136 lines (119 loc) · 4.26 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Concrete\Block\SwitchLanguage;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\Http\Response;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Permission\Checker;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use Cookie;
use Session;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends BlockController implements UsesFeatureInterface
{
protected $btInterfaceWidth = 500;
protected $btInterfaceHeight = 150;
protected $btTable = 'btSwitchLanguage';
public $helpers = ['form'];
/**
* @var string
*/
public $label;
public function getBlockTypeDescription()
{
return t('Adds a front-end language switcher to your website.');
}
public function getBlockTypeName()
{
return t('Switch Language');
}
public function getRequiredFeatures(): array
{
return [
Features::MULTILINGUAL
];
}
/**
* @param int $currentPageID
* @param int $sectionID
*
* @return \League\URL\URLInterface
*/
public function resolve_language_url($currentPageID, $sectionID)
{
$resolve = ['/'];
$lang = Section::getByID((int) $sectionID);
if (is_object($lang)) {
$resolve = [$lang];
$page = \Page::getByID((int) $currentPageID);
if (!$page->isError()) {
$relatedID = $lang->getTranslatedPageID($page);
if ($relatedID) {
$pc = \Page::getByID($relatedID);
$resolve = [$pc];
} elseif ($page->isGeneratedCollection()) {
$this->app->make('session')->set('multilingual_default_locale', $lang->getLocale());
$resolve = [$page];
}
}
}
return $this->app->make(ResolverManagerInterface::class)->resolve($resolve);
}
public function action_switch_language($currentPageID, $sectionID, $bID = false)
{
$to = $this->resolve_language_url($currentPageID, $sectionID);
$rf = $this->app->make(ResponseFactoryInterface::class);
return $rf->redirect($to, Response::HTTP_FOUND);
}
public function action_set_current_language()
{
if ($this->request->request->has('language')) {
$section = Section::getByID($this->request->request->get('language'));
if (is_object($section)) {
Session::set('multilingual_default_locale', $section->getLocale());
if ($this->request->request->get('remember')) {
Cookie::set('multilingual_default_locale', $section->getLocale(), time() + (60 * 60 * 24 * 365));
} else {
Cookie::clear('multilingual_default_locale');
}
}
}
return $this->action_switch_language($this->request->request->get('rcID'), $this->request->request->get('language'));
}
public function add()
{
$this->set('label', t('Choose Language'));
}
public function view()
{
$ml = Section::getList();
$c = \Page::getCurrentPage();
$al = Section::getBySectionOfSite($c);
$languages = [];
$locale = null;
if ($al !== null) {
$locale = $al->getLanguage();
}
if (!$locale) {
$locale = \Localization::activeLocale();
$al = Section::getByLocale($locale);
}
$mlAccessible = [];
foreach ($ml as $m) {
$pc = new Checker(\Page::getByID($m->getCollectionID()));
if ($pc->canRead()) {
$mlAccessible[] = $m;
$languages[$m->getCollectionID()] = $m->getLanguageText($m->getLocale());
}
}
$this->set('languages', $languages);
$this->set('languageSections', $mlAccessible);
$this->set('activeLanguage', $al ? $al->getCollectionID() : null);
$dl = $this->app->make('multilingual/detector');
$this->set('defaultLocale', $dl->getPreferredSection());
$this->set('locale', $locale);
$this->set('cID', $c->getCollectionID());
}
}