Initial import
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\services;
|
||||
|
||||
|
||||
use app\models\Articles;
|
||||
use app\models\Pages;
|
||||
use app\models\parsed\ArticleParsedInterface;
|
||||
use app\models\register\Partner;
|
||||
|
||||
class ViewReg
|
||||
{
|
||||
public static $pages = [
|
||||
'news' => 'novini',
|
||||
'events' => 'sabitia',
|
||||
'campaigns' => 'kampanii',
|
||||
'projects' => 'proekti',
|
||||
'collections' => 'kolektsii',
|
||||
'expositions' => 'ekspozitsii',
|
||||
'objects' => 'obekti',
|
||||
'partners' => 'partnjori',
|
||||
'kolektsii-obekti' => 'collections-objects',
|
||||
'collections-objects' => 'kolektsii-obekti',
|
||||
'ekspozitsii-obekti' => 'expositions-objects',
|
||||
'expositions-objects' => 'ekspozitsii-obekti',
|
||||
'obekti' => 'objects',
|
||||
'about-the-project' => 'otnosno-proekta',
|
||||
'e-library' => 'e-biblioteka'
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Pages | null $model
|
||||
*/
|
||||
public static function localeSwitch($model = null)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . ($model ? $model->slug_en . '/' : '') . self::appendQueryStr(),
|
||||
'en' => '/bg/' . ($model ? $model->slug . '/' : '') . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function localeSwitchArticle($table, Articles $model)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . $table . '/' . Formatter::encodeId($model->id) . '-' . Formatter::cyrillicTrans($model->article->title('en')) . '/' . self::appendQueryStr(),
|
||||
'en' => '/bg/' . self::$pages[$table] . '/' . Formatter::encodeId($model->id) . '-' . Formatter::cyrillicTrans($model->article->title('bg')) . '/' . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function localeSwitchPartnerArticle($table, Articles $model, $partnerSlug)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/partner/' . $partnerSlug . '/' . $table . '/' . Formatter::encodeId($model->id) . '-' . Formatter::cyrillicTrans($model->article->title('en')) . '/' . self::appendQueryStr(),
|
||||
'en' => '/bg/partnjor/' . $partnerSlug . '/' . self::$pages[$table] . '/' . Formatter::encodeId($model->id) . '-' . Formatter::cyrillicTrans($model->article->title('bg')) . '/' . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function localeSwitchUser()
|
||||
{
|
||||
$controllerAction = \Yii::$app->controller->id . '/' . \Yii::$app->controller->action->id . '/';
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . $controllerAction,
|
||||
'en' => '/bg/' . $controllerAction
|
||||
];
|
||||
}
|
||||
|
||||
public static function localeSwitchPage($table, $id, $title, $title_en)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . $table . '/' . Formatter::encodeId($id) . '-' . Formatter::cyrillicTrans($title_en) . '/' . self::appendQueryStr(),
|
||||
'en' => '/bg/' . self::$pages[$table] . '/' . Formatter::encodeId($id) . '-' . Formatter::cyrillicTrans($title) . '/' . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function localePartnerSwitchPage($table, $id, $title, $title_en)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . self::partnerSite('partner') . $table . '/' . Formatter::encodeId($id) . '-' . Formatter::cyrillicTrans($title_en) . '/' . self::appendQueryStr(),
|
||||
'en' => '/bg/' . self::partnerSite('partnjor') . self::$pages[$table] . '/' . Formatter::encodeId($id) . '-' . Formatter::cyrillicTrans($title) . '/' . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
/* [Partner locale switches] */
|
||||
/**
|
||||
* @param Pages | null $model
|
||||
*/
|
||||
// Locale switch for index page
|
||||
public static function partnerLocaleSwitch($partnerSlug, $model = null)
|
||||
{
|
||||
\Yii::$app->params['locale_switch'] = [
|
||||
'bg' => '/en/' . self::partnerSite('partner') . ($model ? $model->slug_en . '/' : '') . self::appendQueryStr(),
|
||||
'en' => '/bg/' . self::partnerSite('partnjor') . ($model ? $model->slug . '/' : '') . self::appendQueryStr(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function find($page)
|
||||
{
|
||||
|
||||
$slug = \Yii::$app->language == 'en' ? 'slug_en' : 'slug';
|
||||
$foundPage = Pages::find()->where([$slug => $page])->one();
|
||||
|
||||
if ($foundPage) {
|
||||
$file = $_SERVER['DOCUMENT_ROOT'] . '/views/site/inner-pages/' . $foundPage->slug . '.php';
|
||||
if (file_exists($file)) {
|
||||
return (object)[
|
||||
'view' => "inner-pages/" . $foundPage->slug,
|
||||
'model' => $foundPage
|
||||
];
|
||||
} else {
|
||||
return (object)[
|
||||
'view' => "template-pages/page",
|
||||
'model' => $foundPage
|
||||
];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function generateDetailPageUrl(Articles $article, $partner_id = null)
|
||||
{
|
||||
$lg = \Yii::$app->language;
|
||||
$table = $lg == 'en' ? $article->art_table : self::$pages[$article->art_table];
|
||||
|
||||
if ($article->article) {
|
||||
$slug = Formatter::cyrillicTrans($article->article->title());
|
||||
return "/$lg/" . ViewReg::partnerSite() . "$table/" . Formatter::encodeId($article->id) . "-$slug/";
|
||||
}
|
||||
}
|
||||
|
||||
public static function generateDefaultDetailUrl($pagePart, $id, $title)
|
||||
{
|
||||
$part = self::$pages[$pagePart];
|
||||
$lg = \Yii::$app->language;
|
||||
if ($lg == 'en') {
|
||||
$part = $pagePart;
|
||||
}
|
||||
$slug = Formatter::cyrillicTrans($title);
|
||||
return "/$lg/" . ViewReg::partnerSite() . "$part/" . Formatter::encodeId($id) . "-$slug/";
|
||||
}
|
||||
|
||||
public static function generateDirectDetailPageUrl(ArticleParsedInterface $article, $table)
|
||||
{
|
||||
$lg = \Yii::$app->language;
|
||||
$table = $lg == 'en' ? $table : self::$pages[$table];
|
||||
$slug = Formatter::cyrillicTrans($article->title());
|
||||
return "/$lg/$table/" . Formatter::encodeId($article->id) . "-$slug/";
|
||||
}
|
||||
|
||||
public static function goToPage($id, $query = '')
|
||||
{
|
||||
|
||||
$lg = \Yii::$app->language;
|
||||
|
||||
$page = Pages::findOne($id);
|
||||
|
||||
|
||||
if ($page) {
|
||||
switch ($lg) {
|
||||
default:
|
||||
return '/' . $lg . '/' . self::partnerSite() . $page->slug . '/' . $query;
|
||||
case 'en':
|
||||
return '/' . $lg . '/' . self::partnerSite() . $page->slug_en . '/' . $query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function generateListUrl($page)
|
||||
{
|
||||
$lg = \Yii::$app->language;
|
||||
$table = $lg == 'en' ? $page : self::$pages[$page];
|
||||
return "/$lg/" . ViewReg::partnerSite() . "$table/";
|
||||
}
|
||||
|
||||
public static function generateArticleListUrl($partnerSlug = null)
|
||||
{
|
||||
$lg = \Yii::$app->language;
|
||||
$partnerPage = $lg == 'en' ? 'partner' : 'partnjor';
|
||||
$articlesPage = $lg == 'en' ? 'what-s-new' : 'kakvo-novo';
|
||||
if ($partnerSlug)
|
||||
return "/$lg/$partnerPage/$partnerSlug/$articlesPage/";
|
||||
return "/$lg/$articlesPage/";
|
||||
}
|
||||
|
||||
|
||||
public static function partnerSite($p = null)
|
||||
{
|
||||
$partnerSite = '';
|
||||
if (\Yii::$app->controller->partnerSlug) {
|
||||
$partner = \Yii::$app->language == 'en' ? 'partner' : 'partnjor';
|
||||
if ($p)
|
||||
$partner = $p;
|
||||
$partnerSite = $partner . '/' . \Yii::$app->controller->partnerSlug . '/';
|
||||
}
|
||||
return $partnerSite;
|
||||
}
|
||||
|
||||
public static function appendQueryStr()
|
||||
{
|
||||
$q = $_SERVER['QUERY_STRING'] ?? '';
|
||||
return $q ? '?' . $q : '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user