-
Notifications
You must be signed in to change notification settings - Fork 25
/
Module.php
227 lines (194 loc) · 6.92 KB
/
Module.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @author akiraz@bk.ru
* @link https://github.com/akiraz2/yii2-ticket-support
* @copyright 2018 akiraz2
* @license MIT
*/
namespace akiraz2\support;
use akiraz2\support\models\Content;
use akiraz2\support\models\Ticket;
use PhpImap\Mailbox;
use Yii;
use yii\queue\Queue;
/**
* support module definition class
*/
class Module extends \yii\base\Module
{
/** @var string DB type `sql` or `mongodb` */
public $dbType = 'sql';
/**
* @inheritdoc
*/
public $controllerNamespace = 'akiraz2\support\controllers';
/** @var linked user (for example, 'common\models\User::class' */
public $userModel;
/** @var string Primary Key for user table, by default 'id' */
public $userPK = 'id';
/** @var string username uses in view (may be field `username` or `email` or `login`) */
public $userName = 'username';
/** @var string email field in user table */
public $userEmail = 'email';
/** @var string url for viewing user model, use in views/ticket/manage */
public $urlViewUser;
/** @var array Mailer configuration */
public $mailer = [];
/** @var string The Administrator permission name. for future possible usage. */
public $adminPermission;
/** @var string need to create correct Url from backend when notify by email */
public $urlManagerFrontend = 'urlManager';
public $notifyByEmail = true;
/** @var string config.php: `'id' => 'app-backend',` */
public $appBackendId = 'app-backend';
/** @var bool|function for accessRule matchCallback
* for example,
* 'adminMatchCallback' => function () {
* return \Yii::$app->user->identity->getIsAdmin();
* }
*/
public $adminMatchCallback = true;
/** @var string wysiwyg component for creating content of ticket */
public $redactorModule = 'redactor';
/** @var string|Queue component for queue */
public $queueComponent = 'queue';
public $countDaysToClose = 7;
/** @var array Imap config for fetch mailbox
* 'imap' => [
'host' => 'imap.site.com',
'username' => 'support@site.com',
'password' => '123456789879',
] */
public $imap = [];
public $showUsernameSupport = true;
public $userNameSupport = 'Support';
/** @var boolean If true it makes extension compatible to yii2 basic template */
public $yii2basictemplate = false;
/**
* @var null|false|function Function to generate a hash
* for example,
* 'hashGenerator' => function ($data) {
* return date('Ymd-His');
* }
*
*/
public $hashGenerator = null;
/**
* Translate message
* @param $message
* @param array $params
* @param null $language
* @return mixed
*
* public static function t($message, $params = [], $language = null)
* {
* return Yii::$app->getModule('support')->translate($message, $params, $language);
* }*/
/**
* Translate message
* @param $message
* @param array $params
* @param null $language
* @return mixed
*/
public static function translate($message, $params = [], $language = null)
{
return self::t('support', $message, $params, $language);
}
/**
* Translates a message to the specified language.
*
* This is a shortcut method of [[\yii\i18n\I18N::translate()]].
*
* The translation will be conducted according to the message category and the target language will be used.
*
* You can add parameters to a translation message that will be substituted with the corresponding value after
* translation. The format for this is to use curly brackets around the parameter name as you can see in the following example:
*
* ```php
* $username = 'Alexander';
* echo \Yii::t('app', 'Hello, {username}!', ['username' => $username]);
* ```
*
* Further formatting of message parameters is supported using the [PHP intl extensions](http://www.php.net/manual/en/intro.intl.php)
* message formatter. See [[\yii\i18n\I18N::translate()]] for more details.
*
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en-US`, `en`). If this is null, the current
* [[\yii\base\Application::language|application language]] will be used.
*
* @return string the translated message.
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t('akiraz2/' . $category, $message, $params, $language);
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
}
public function isMongoDb()
{
return $this->dbType === 'mongodb';
}
public function getIsBackend()
{
return Yii::$app->id === $this->appBackendId;
}
public function fetchMail()
{
$mailbox = $this->getImapMailBox();
if ($mailbox == null) {
return false;
}
$mailsIds = $mailbox->searchMailbox('ALL');
if (!$mailsIds) {
return false;
}
$name = Yii::$app->name;
for ($i = 0; $i < count($mailsIds); $i++) {
$mail = $mailbox->getMail($mailsIds[$i], false);
preg_match("/\[$name.*#(.{10,})\]/", $mail->subject, $output_array);
if (isset($output_array[1]) && ($ticket = Ticket::findOne(['hash_id' => $output_array[1], 'user_contact' => $mail->fromAddress])) !== null) {
// reply
$reply = new Content();
$reply->id_ticket = $ticket->id;
$reply->mail_id = $mail->id;
$reply->fetch_date = $mail->date;
$reply->user_id = $ticket->user_id;
$reply->content = strip_tags($mail->textHtml ?? $mail->textPlain);
$reply->info = $mail->headersRaw;
$reply->save();
$ticket->status = Ticket::STATUS_OPEN;
$ticket->save();
} else {
$ticket = new Ticket();
$ticket->setScenario('create');
$ticket->loadFromEmail($mail);
$ticket->save();
}
//$mailbox->deleteMail($mailsIds[$i]);
}
return true;
}
public function getImapMailBox()
{
if (empty($this->imap)) {
return null;
}
$host = $this->imap['host'];
$username = $this->imap['username'];
$password = $this->imap['password'];
$port = $this->imap['port'] ?? 993;
if (!($host && $username && $password)) {
return null;
}
$mailbox = new Mailbox("{" . $host . ":" . $port . "/imap/ssl/novalidate-cert}INBOX", $username, $password);
return $mailbox;
}
}