EventViewBootstrap.php 5.29 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
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
<?php
/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 *
 * @package    elitedivision\amos\translation
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */

namespace elitedivision\amos\translation\bootstrap;

use Yii;
use yii\base\Application;
use yii\helpers\FileHelper;
use elitedivision\amos\translation\models\TranslationConf;
use elitedivision\amos\translation\AmosTranslation;

/**
 * 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([
                    'elitedivision\amos\translation\bootstrap\EventViewBootstrap',
                    ], $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()]);
                        }
                    }
                }
            }
        }
    }
}