EventViewBootstrap.php 5.47 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
1
2
<?php
/**
3
4
5
 * Copyright 2020 Art-ER S. Cons. P.A.
 * EROI - Emilia Romagna Open Innovation is based on:
 * https://www.open2.0.regione.lombardia.it
Marcello Pivanti's avatar
Marcello Pivanti committed
6
 *
7
 * @see https://repo.art-er.it Developers' community
8
9
 * @license GPLv3
 * @license https://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3
Marcello Pivanti's avatar
Marcello Pivanti committed
10
 *
11
 * @package    arter\amos\translation\bootstrap
Marcello Pivanti's avatar
Marcello Pivanti committed
12
13
14
15
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

waltermazza's avatar
waltermazza committed
16
namespace arter\amos\translation\bootstrap;
Marcello Pivanti's avatar
Marcello Pivanti committed
17
18
19
20

use Yii;
use yii\base\Application;
use yii\helpers\FileHelper;
waltermazza's avatar
waltermazza committed
21
22
use arter\amos\translation\models\TranslationConf;
use arter\amos\translation\AmosTranslation;
Marcello Pivanti's avatar
Marcello Pivanti committed
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

/**
 * Class TranslationBootstrap: <br />
 * Add behaviours for the translation of the model <br />
 * ELIGIBLE FOR DEPRECATION (DEPRECATED)
 * @package backend\bootstrap
 */
class EventViewBootstrap extends \yii\web\View
{

    /**
     * Application-specific roles initialization
     * @uses onApplicationAction
     */
    public function init()
    {
        \yii\base\Event::on(\yii\web\View::className(), self::EVENT_AFTER_RENDER, [$this, 'onEventAfterRender']);
        //\yii\base\Event::on(\yii\web\View::className(), self::EVENT_END_BODY, [$this, 'onEventAfterRender']);
        parent::init();
    }

    /**
     * 
     * @param yii\base\Event $event
     */
    public function onEventAfterRender($event)
    {
        $moduleTranslation = \Yii::$app->getModule('translation');
        if (!empty($moduleTranslation)) {
            if ($moduleTranslation->enableWidgetView && \Yii::$app instanceof Application) {
                $viewSender    = FileHelper::normalizePath(str_replace(['.php', '.PHP'], '', $event->sender->viewFile));
                $modelSource   = null;
                $modelsEnabled = [];

                $blackListModels = array_merge([
waltermazza's avatar
waltermazza committed
58
                    'arter\amos\translation\bootstrap\EventViewBootstrap',
Marcello Pivanti's avatar
Marcello Pivanti committed
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
                    ], $moduleTranslation->eventViewBlackListModels);

                $blackListViews = array_merge([
                    'views/layouts/',
                    'yii2-debug\views',
                    ], $moduleTranslation->eventViewBlackListViews);


                $checkView = false;
                $checkPart = false;

                foreach ($blackListViews as $val) {
                    if (strpos($event->sender->viewFile, $val) === false) {
                        $checkView = true;
                    }
                }

                foreach ($moduleTranslation->eventViewWhiteListParts as $part) {
                    if (in_array($part, explode(DIRECTORY_SEPARATOR, str_replace('.php', '', $event->sender->viewFile)))) {
                        $checkPart = true;
                    }
                }
                $arrayModels = [];
                if ($checkView && $checkPart) {
                    if (isset($moduleTranslation->translationBootstrap['configuration']['translationContents']['models'])) {
                        foreach ($moduleTranslation->translationBootstrap['configuration']['translationContents']['models'] as $value) {
                            if (!empty($value['namespace'])) {
                                $arrayModels[$value['namespace']] = (isset($value['workflow']) ? $value['workflow'] : 'AmosTranslationWorkflow');
                            }
                            if (!empty($value['view']) && !empty($value['namespace']) && FileHelper::normalizePath(str_replace([
                                    '.php', '.PHP'], '', \Yii::getAlias($value['view']))) == $viewSender) {
                                $modelSource = $value['namespace'];
                            }
                        }
                    }
                    if (empty($modelSource)) {
                        if (!empty($event->sender->context->model) && !in_array(get_class($event->sender->context->model),
                                $blackListModels) && array_key_exists(get_class($event->sender->context->model),
                                $arrayModels)) {
                            $modelSource = $event->sender->context->model;
                        }
                    }
                    if (!empty($modelSource)) {
                        $conf      = new TranslationConf();
                        $statuses  = [];
                        $languages = TranslationConf::getStaticAllActiveLanguages(true);
                        $namespace = get_class($modelSource);
                        $workflow  = $arrayModels[$namespace];
                        if ($languages->count() > 1) {
                            foreach ($languages->all() as $lang) {
                                $statusLang = $conf->getStatus($namespace, $lang, $modelSource->id);
                                $label      = AmosTranslation::t('amostranslation', 'Not translated');
                                if (!empty($statusLang->status)) {
                                    $label = (!empty($statusLang->status) ? $conf->getLabelStatus($statusLang->status,
                                            $workflow) : AmosTranslation::t('amostranslation', 'Not translated'));
                                }
                                $statuses[$lang->language_id] = $label;
                            }
                            echo $event->sender->renderFile($moduleTranslation->widgetViewFile,
                                ['statusLangs' => $statuses, 'namespace' => $namespace, 'getParams' => \Yii::$app->request->get()]);
                        }
                    }
                }
            }
        }
    }
}