|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace execut\import\models; |
|
4
|
|
|
|
|
5
|
|
|
use execut\import\components\source\adapter\Email; |
|
6
|
|
|
use execut\import\components\source\adapter\email\Filter; |
|
7
|
|
|
use execut\import\components\source\adapter\email\Receiver; |
|
8
|
|
|
use execut\import\components\source\adapter\Ftp; |
|
9
|
|
|
use execut\import\components\source\adapter\Site; |
|
10
|
|
|
use execut\yii\base\Exception; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\helpers\ArrayHelper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is the model class for table "import_files_sources". |
|
16
|
|
|
* |
|
17
|
|
|
* @property integer $id |
|
18
|
|
|
* @property string $created |
|
19
|
|
|
* @property string $updated |
|
20
|
|
|
* @property string $name |
|
21
|
|
|
* @property string $key |
|
22
|
|
|
* |
|
23
|
|
|
* @property \execut\import\models\File[] $importFiles |
|
24
|
|
|
*/ |
|
25
|
|
|
class FilesSource extends base\FilesSource |
|
26
|
|
|
{ |
|
27
|
|
|
public static $emailAdapter = null; |
|
28
|
|
|
// const ALL_TYPES = [ |
|
29
|
|
|
// self::TYPE_EMAIL, |
|
30
|
|
|
// self::TYPE_FTP, |
|
31
|
|
|
// self::TYPE_SITE, |
|
32
|
|
|
// ]; |
|
33
|
|
|
const TYPE_EMAIL = 'email'; |
|
34
|
|
|
const TYPE_FTP = 'ftp'; |
|
35
|
|
|
const TYPE_SITE = 'site'; |
|
36
|
|
|
public function getAdapterForSetting(Setting $setting) { |
|
37
|
|
|
switch ($this->key) { |
|
38
|
|
|
case self::TYPE_EMAIL: |
|
39
|
|
|
if (self::$emailAdapter === null) { |
|
40
|
|
|
self::$emailAdapter = new Email([ |
|
41
|
|
|
'receiver' => new Receiver([ |
|
42
|
|
|
'imap' => \yii::$app->imap->connection, |
|
43
|
|
|
'filter' => new Filter(), |
|
44
|
|
|
]), |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$adapter = self::$emailAdapter; |
|
49
|
|
|
/** |
|
50
|
|
|
* @var Filter $filter |
|
51
|
|
|
*/ |
|
52
|
|
|
$filter = $adapter->receiver->filter; |
|
53
|
|
|
$filter->subject = $setting->email_title_match; |
|
54
|
|
|
$filter->attachmentName = $setting->email_attachment_template; |
|
|
|
|
|
|
55
|
|
|
$filter->sender = $setting->email; |
|
56
|
|
|
break; |
|
57
|
|
|
case self::TYPE_FTP: |
|
58
|
|
|
$adapter = new Ftp([ |
|
59
|
|
|
'host' => $setting->ftp_host, |
|
|
|
|
|
|
60
|
|
|
'ssl' => $setting->ftp_ssl, |
|
|
|
|
|
|
61
|
|
|
'port' => $setting->ftp_port, |
|
|
|
|
|
|
62
|
|
|
'timeout' => $setting->ftp_timeout, |
|
|
|
|
|
|
63
|
|
|
'login' => $setting->ftp_login, |
|
|
|
|
|
|
64
|
|
|
'password' => $setting->ftp_password, |
|
|
|
|
|
|
65
|
|
|
'dir' => $setting->ftp_dir, |
|
|
|
|
|
|
66
|
|
|
'fileName' => $setting->ftp_file_name, |
|
|
|
|
|
|
67
|
|
|
]); |
|
68
|
|
|
break; |
|
69
|
|
|
case self::TYPE_SITE: |
|
70
|
|
|
$adapter = new Site([ |
|
71
|
|
|
'site' => $setting->site_host, |
|
|
|
|
|
|
72
|
|
|
'authUrl' => $setting->site_auth_url, |
|
|
|
|
|
|
73
|
|
|
'method' => $setting->site_auth_method, |
|
|
|
|
|
|
74
|
|
|
'loginField' => $setting->site_login_field, |
|
|
|
|
|
|
75
|
|
|
'passwordField' => $setting->site_password_field, |
|
|
|
|
|
|
76
|
|
|
'otherFields' => $setting->siteOtherFields, |
|
|
|
|
|
|
77
|
|
|
'login' => $setting->site_login, |
|
|
|
|
|
|
78
|
|
|
'password' => $setting->site_password, |
|
|
|
|
|
|
79
|
|
|
'fileUrl' => $setting->site_file_url, |
|
|
|
|
|
|
80
|
|
|
]); |
|
81
|
|
|
break; |
|
82
|
|
|
default: |
|
83
|
|
|
throw new Exception('Adapter for source type ' . $this->key . ' is not supported'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $adapter; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public static function getIdByKey($key) { |
|
90
|
|
|
return self::find()->andWhere(['key' => $key])->select('id')->scalar(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|