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