1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel tickets module |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-ticket |
6
|
|
|
* @package hipanel-module-ticket |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\ticket\widgets; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\ticket\models\Template; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\Widget; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
use yii\helpers\Json; |
18
|
|
|
use yii\helpers\Url; |
19
|
|
|
use yii\web\JsExpression; |
20
|
|
|
|
21
|
|
|
class TemplatesWidget extends Widget |
22
|
|
|
{ |
23
|
|
|
public $textareaSelector = '#thread-message'; |
24
|
|
|
|
25
|
|
|
protected $formId; |
26
|
|
|
|
27
|
|
|
public function run() |
28
|
|
|
{ |
29
|
|
|
if (!Yii::$app->user->can('support')) { |
30
|
|
|
return null; |
31
|
|
|
} |
32
|
|
|
$this->formId = mt_rand(); |
33
|
|
|
|
34
|
|
|
$this->registerClientScript(); |
35
|
|
|
|
36
|
|
|
return $this->render('templatesWidget', [ |
37
|
|
|
'languages' => $this->getLanguages(), |
38
|
|
|
'defaultLanguage' => $this->getDefaultLanguage(), |
39
|
|
|
'formId' => $this->formId, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getDefaultLanguage() |
44
|
|
|
{ |
45
|
|
|
$languages = $this->getLanguages(); |
46
|
|
|
$applicationLanguage = Yii::$app->language; |
47
|
|
|
|
48
|
|
|
if (isset($languages[$applicationLanguage])) { |
49
|
|
|
return $languages[$applicationLanguage]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$fallbackLanguage = substr($applicationLanguage, 0, 2); |
53
|
|
|
if (isset($languages[$fallbackLanguage])) { |
54
|
|
|
return $languages[$fallbackLanguage]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return reset($languages); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getLanguages() |
61
|
|
|
{ |
62
|
|
|
return Yii::$app->cache->getOrSet([__METHOD__, Yii::$app->user->getId()], function () { |
63
|
|
|
$result = []; |
64
|
|
|
|
65
|
|
|
$templates = Template::find()->joinWith('texts')->all(); |
66
|
|
|
|
67
|
|
|
foreach (ArrayHelper::getColumn($templates, 'texts') as $texts) { |
68
|
|
|
foreach ($texts as $text) { |
69
|
|
|
$result[$text->lang] = [ |
70
|
|
|
'name' => Yii::t('hipanel', $text->lang), |
71
|
|
|
'code' => $text->lang, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
}, 86400); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function registerClientScript() |
81
|
|
|
{ |
82
|
|
|
$options = Json::encode([ |
83
|
|
|
'url' => Url::to('@ticket/template/text'), |
84
|
|
|
'data' => [ |
85
|
|
|
'id' => new JsExpression('id'), |
86
|
|
|
'lang' => new JsExpression('language'), |
87
|
|
|
], |
88
|
|
|
'success' => new JsExpression("function (data) { |
89
|
|
|
if (data.text) { |
90
|
|
|
var messageText = $('#thread-message').val(); |
91
|
|
|
if (messageText.length > 0) { |
92
|
|
|
messageText = messageText + ' '; |
93
|
|
|
} |
94
|
|
|
$('$this->textareaSelector').val(messageText + data.text).trigger('blur').focus(); |
95
|
|
|
} |
96
|
|
|
}"), |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$this->view->registerJs(" |
100
|
|
|
$('#{$this->formId}').on('select2:select', function (e) { |
101
|
|
|
var id = $(e.target).val(), |
102
|
|
|
language = $('.selected-language').attr('data-language'); |
103
|
|
|
|
104
|
|
|
$.ajax($options); |
105
|
|
|
}); |
106
|
|
|
$('.template-selector ul li>a').on('click', function (event) { |
107
|
|
|
var language = { |
108
|
|
|
code: $(this).attr('data-language'), |
109
|
|
|
name: $(this).text() |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
$('.template-selector .selected-language').text(language.name).attr('data-language', language.code); |
113
|
|
|
event.preventDefault(); |
114
|
|
|
}); |
115
|
|
|
"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function getFormId() |
122
|
|
|
{ |
123
|
|
|
return $this->formId; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|