forked from v0lume/yii2-meta-tags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetaTags.php
79 lines (61 loc) · 1.79 KB
/
MetaTags.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
<?php
/**
* Created by PhpStorm.
* User: artemshmanovsky
* Date: 11.03.15
* Time: 15:03
*/
namespace v0lume\yii2\metaTags;
use Yii;
use yii\base\Widget;
use yii\base\Exception;
use v0lume\yii2\metaTags\MetaTagsComponent;
use v0lume\yii2\metaTags\models\MetaTag;
class MetaTags extends Widget
{
public $model;
public $form;
public function init()
{
parent::init();
self::registerTranslations();
if (!$this->model->getBehavior(MetaTagsComponent::$behaviorName))
{
throw new Exception(self::t('messages', 'widget_behavior_exception {behaviorName}', ['behaviorName' => MetaTagsComponent::$behaviorName]), 500);
}
}
public static function registerTranslations()
{
$i18n = Yii::$app->i18n;
$i18n->translations['metaTags/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'sys',
'basePath' => '@vendor/v0lume/yii2-meta-tags/messages',
'fileMap' => [
'metaTags/messages' => 'messages.php',
],
];
}
public function run()
{
$model = new MetaTag;
if(!$this->model->isNewRecord)
{
$meta_tag = MetaTag::findOne([
'model_id' => $this->model->id,
'model' => (new \ReflectionClass($this->model))->getShortName()
]);
if(isset($meta_tag))
$model = $meta_tag;
}
return $this->render('MetaTags', [
'model' => $model,
'form' => $this->form,
]);
}
public static function t($category, $message, $params = [], $language = null)
{
self::registerTranslations();
return Yii::t('metaTags/' . $category, $message, $params, $language);
}
}