Commit 7f6a9301d6a5f24190ed0e0f1871415e27dd20d2

Authored by Mihail
1 parent f68e7edd

add mails classes

backend/controllers/RgGrupController.php
@@ -195,8 +195,8 @@ class RgGrupController extends BaseController @@ -195,8 +195,8 @@ class RgGrupController extends BaseController
195 195
196 public function actionMail() 196 public function actionMail()
197 { 197 {
198 - //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox', 'mt@soft-terra.com.ua', 'Wehrfyjd8');  
199 - $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562'); 198 + $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'tsurkanovm@gmail.com', 'Wtvr@2000');
  199 + //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562');
200 $mail_saver->saveAttachmentsTo(); 200 $mail_saver->saveAttachmentsTo();
201 } 201 }
202 202
backend/views/cart/index.php
@@ -10,6 +10,7 @@ use kartik\date\DatePicker; @@ -10,6 +10,7 @@ use kartik\date\DatePicker;
10 10
11 $this->title = Yii::t('app', 'Заказы'); 11 $this->title = Yii::t('app', 'Заказы');
12 $this->params['breadcrumbs'][] = $this->title; 12 $this->params['breadcrumbs'][] = $this->title;
  13 +
13 ?> 14 ?>
14 <div class="cart-bills-index"> 15 <div class="cart-bills-index">
15 16
@@ -25,6 +26,13 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title; @@ -25,6 +26,13 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
25 26
26 'id', 27 'id',
27 'account_id', 28 'account_id',
  29 + ['label' =>'Информация',
  30 + 'value' =>function ($data) {
  31 + $info = $data->scode . ' /n';
  32 + $info .= $data->name;
  33 + return $info;
  34 + },
  35 + ],
28 'sum', 36 'sum',
29 [ 37 [
30 'label' =>'Статус', 38 'label' =>'Статус',
@@ -34,17 +42,18 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title; @@ -34,17 +42,18 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
34 'manager_name', 42 'manager_name',
35 [ 43 [
36 'label' =>'Дата', 44 'label' =>'Дата',
  45 + 'value' =>function ($data) {
  46 + return date('Y-m-d', $data->dt);
  47 + },
37 'attribute' => 'dt', 48 'attribute' => 'dt',
38 'filter' => DatePicker::widget([ 49 'filter' => DatePicker::widget([
39 - 'name' => 'data1',  
40 - // 'value' => '01-Feb-2015', 50 + 'model' =>$searchModel,
  51 + 'language' =>'ru',
  52 + 'size' =>'xs',
  53 + 'separator' =>'по',
  54 + 'attribute' => 'dt',
41 'type' => DatePicker::TYPE_RANGE, 55 'type' => DatePicker::TYPE_RANGE,
42 - 'name2' => 'data2',  
43 - // 'value2' => '27-Feb-2015',  
44 - 'pluginOptions' => [  
45 - 'autoclose'=>true,  
46 - 'format' => 'dd-M-yyyy'  
47 - ] 56 + 'attribute2' => 'date_to',
48 ]), 57 ]),
49 ], 58 ],
50 59
common/components/mail/ImapMailReader.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Tsurkanov
  5 + * Date: 02.11.2015
  6 + * Time: 16:56
  7 + */
  8 +
  9 +namespace common\components\mail;
  10 +
  11 +
  12 +class ImapMailReader extends MailReader {
  13 +
  14 + function __construct( $hostname, $username, $password )
  15 + {
  16 + parent::__construct($hostname, $username, $password);
  17 + $this->connection = imap_open($hostname, $username, $password);
  18 +
  19 + if ($this->connection === false)
  20 + throw new \Exception('Cannot connect to Gmail: ' . imap_last_error());
  21 +
  22 + }
  23 +
  24 + public function getEmails( $flag )
  25 + {
  26 + return imap_search( $this->connection, $flag );
  27 + }
  28 +
  29 + /**
  30 + * @return mixed
  31 + */
  32 + public function getListMailboxes()
  33 + {
  34 + return imap_list($this->connection, $this->hostname, "*");
  35 + }
  36 +
  37 + public function getCurrentEmailStructure( $email_number )
  38 + {
  39 + return imap_fetchstructure($this->connection, $email_number);
  40 + }
  41 +
  42 + public function reOpen( $hostname )
  43 + {
  44 + imap_reopen( $this->connection, $hostname );
  45 + }
  46 +
  47 + function __destruct()
  48 + {
  49 + /* close the connection */
  50 + imap_close( $this->connection );
  51 + }
  52 +
  53 +
  54 +}
0 \ No newline at end of file 55 \ No newline at end of file
common/components/parsers/MailAttachmentsSaver.php renamed to common/components/mail/MailAttachmentsSaver.php
@@ -6,31 +6,32 @@ @@ -6,31 +6,32 @@
6 * Time: 10:53 6 * Time: 10:53
7 */ 7 */
8 8
9 -namespace common\components\parsers; 9 +namespace common\components\mail;
10 10
11 11
12 use common\components\CustomVarDamp; 12 use common\components\CustomVarDamp;
13 13
14 class MailAttachmentsSaver 14 class MailAttachmentsSaver
15 { 15 {
16 -//$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox';  
17 - protected $connection; 16 + protected $mail_reader;
  17 + protected $massage_type;
  18 + public $file_name_prefix;
18 19
19 - public function __construct($hostname, $username, $password) 20 + public function __construct( MailReader $mail_reader, $massage_type )
20 { 21 {
21 - $this->connection = imap_open($hostname, $username, $password);  
22 - CustomVarDamp::dumpAndDie( $this->connection);  
23 - if ($this->connection === false)  
24 - throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); 22 + $this->mail_reader = $mail_reader;
  23 + $this->massage_type = $massage_type;
  24 +
25 } 25 }
26 26
27 27
28 - public function saveAttachmentsTo() 28 + public function saveAttachmentsTo( $destination )
29 { 29 {
30 30
31 - $emails = imap_search($this->connection, 'FROM forallthings'); 31 + $emails = $this->mail_reader->getEmails( $this->massage_type );
32 32
33 /* if emails are returned, cycle through each... */ 33 /* if emails are returned, cycle through each... */
  34 +
34 if ($emails) { 35 if ($emails) {
35 36
36 /* begin output var */ 37 /* begin output var */
@@ -38,14 +39,10 @@ class MailAttachmentsSaver @@ -38,14 +39,10 @@ class MailAttachmentsSaver
38 39
39 /* put the newest emails on top */ 40 /* put the newest emails on top */
40 rsort($emails); 41 rsort($emails);
41 - 42 + // CustomVarDamp::dump($emails);
42 foreach ($emails as $email_number) { 43 foreach ($emails as $email_number) {
43 44
44 - /* get information specific to this email */  
45 -// $overview = imap_fetch_overview($inbox, $email_number, 0);  
46 -// $message = imap_fetchbody($inbox, $email_number, 2);  
47 - $structure = imap_fetchstructure($this->connection, $email_number);  
48 - 45 + $structure = $this->mail_reader->getCurrentEmailStructure($email_number);
49 $attachments = array(); 46 $attachments = array();
50 if (isset($structure->parts) && count($structure->parts)) { 47 if (isset($structure->parts) && count($structure->parts)) {
51 for ($i = 0; $i < count($structure->parts); $i++) { 48 for ($i = 0; $i < count($structure->parts); $i++) {
@@ -74,7 +71,7 @@ class MailAttachmentsSaver @@ -74,7 +71,7 @@ class MailAttachmentsSaver
74 } 71 }
75 72
76 if ($attachments[$i]['is_attachment']) { 73 if ($attachments[$i]['is_attachment']) {
77 - $attachments[$i]['attachment'] = imap_fetchbody($this->connection, $email_number, $i + 1); 74 + $attachments[$i]['attachment'] = imap_fetchbody($this->mail_reader->connection, $email_number, $i + 1);
78 if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 75 if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
79 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 76 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
80 } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE 77 } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
@@ -83,16 +80,21 @@ class MailAttachmentsSaver @@ -83,16 +80,21 @@ class MailAttachmentsSaver
83 } 80 }
84 } 81 }
85 82
86 - CustomVarDamp::dumpAndDie($attachments);  
87 -  
88 if (count($attachments) != 0) { 83 if (count($attachments) != 0) {
89 84
  85 + foreach($attachments as $key => &$val){
  86 + if ($val['is_attachment'] == 1) {
90 87
91 - foreach ($attachments as $at) { 88 + if( isset($this->file_name_prefix) ){
  89 + $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']);
  90 + }else{
  91 + $name = $destination . '/' . mb_decode_mimeheader($val['name']);
  92 + }
  93 + mb_internal_encoding("UTF-8");
92 94
93 - if ($at['is_attachment'] == 1) {  
94 - //die(__DIR__);  
95 - file_put_contents('test.csv', $at['attachment']); 95 + //file_put_contents(\Yii::getAlias('@temp_upload') . '/' . $name, $val['attachment']);
  96 + file_put_contents( $name, $val['attachment'] );
  97 + }
96 98
97 } 99 }
98 } 100 }
@@ -101,10 +103,7 @@ class MailAttachmentsSaver @@ -101,10 +103,7 @@ class MailAttachmentsSaver
101 103
102 } 104 }
103 105
104 - // echo $output;  
105 } 106 }
106 - /* close the connection */  
107 - imap_close($this->connection); 107 +
108 } 108 }
109 - }  
110 } 109 }
111 \ No newline at end of file 110 \ No newline at end of file
common/components/mail/MailReader.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Tsurkanov
  5 + * Date: 02.11.2015
  6 + * Time: 16:47
  7 + */
  8 +
  9 +namespace common\components\mail;
  10 +
  11 +
  12 +abstract class MailReader {
  13 +
  14 + public $connection;
  15 + protected $hostname;
  16 + protected $username;
  17 + protected $password;
  18 +
  19 + /* get information specific to this email */
  20 +// $overview = imap_fetch_overview($inbox, $email_number, 0);
  21 +// $message = imap_fetchbody($inbox, $email_number, 2);
  22 +
  23 + function __construct( $hostname, $username, $password )
  24 + {
  25 + $this->hostname = $hostname;
  26 + $this->username = $username;
  27 + $this->password = $password;
  28 + }
  29 +
  30 + /**
  31 + * @return mixed
  32 + */
  33 + public function getHostname()
  34 + {
  35 + return $this->hostname;
  36 + }
  37 +
  38 +
  39 +
  40 + public abstract function getListMailboxes();
  41 +
  42 + public abstract function getEmails( $flag );
  43 +
  44 + public abstract function getCurrentEmailStructure( $email );
  45 +
  46 + public abstract function reOpen( $hostname );
  47 +
  48 +
  49 +
  50 +}
0 \ No newline at end of file 51 \ No newline at end of file
common/models/CartBillsSearch.php
@@ -13,7 +13,7 @@ use common\models\CartBillsView; @@ -13,7 +13,7 @@ use common\models\CartBillsView;
13 */ 13 */
14 class CartBillsSearch extends CartBillsView 14 class CartBillsSearch extends CartBillsView
15 { 15 {
16 - 16 + public $date_to;
17 /** 17 /**
18 * @inheritdoc 18 * @inheritdoc
19 */ 19 */
@@ -21,7 +21,7 @@ class CartBillsSearch extends CartBillsView @@ -21,7 +21,7 @@ class CartBillsSearch extends CartBillsView
21 { 21 {
22 return [ 22 return [
23 [['id', 'account_id', 'status'], 'integer'], 23 [['id', 'account_id', 'status'], 'integer'],
24 - [['data1,data2'], 'safe'], 24 + [['dt', 'date_to'], 'string', 'max' => 10],
25 ]; 25 ];
26 } 26 }
27 27
@@ -50,8 +50,7 @@ class CartBillsSearch extends CartBillsView @@ -50,8 +50,7 @@ class CartBillsSearch extends CartBillsView
50 ]); 50 ]);
51 51
52 $this->load($params); 52 $this->load($params);
53 - //$timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);  
54 - CustomVarDamp::dumpAndDie(date_timestamp_get($params['data1'])); 53 +
55 if (!$this->validate()) { 54 if (!$this->validate()) {
56 // uncomment the following line if you do not want to return any records when validation fails 55 // uncomment the following line if you do not want to return any records when validation fails
57 // $query->where('0=1'); 56 // $query->where('0=1');
@@ -61,10 +60,20 @@ class CartBillsSearch extends CartBillsView @@ -61,10 +60,20 @@ class CartBillsSearch extends CartBillsView
61 $query->andFilterWhere([ 60 $query->andFilterWhere([
62 'id' => $this->id, 61 'id' => $this->id,
63 'account_id' => $this->account_id, 62 'account_id' => $this->account_id,
64 - 'timestamp' => $this->dt,  
65 'status_id' => $this->status, 63 'status_id' => $this->status,
66 ]); 64 ]);
67 65
  66 + if($this->dt !== null || $this->date_to !== null){
  67 + $date_from = mktime(0,0,0,(int)substr($this->dt,4,2),(int)substr($this->dt,1,2),(int)substr($this->dt,7,4));
  68 + $date_to = mktime(23,59,59,(int)substr($this->date_to,4,2),(int)substr($this->date_to,1,2),(int)substr($this->date_to,7,4));
  69 +
  70 + $query->andFilterWhere([
  71 + 'between', 'dt', $date_from, $date_to
  72 + ]);
  73 + }
  74 +
  75 +
  76 +
68 // $query->andFilterWhere(['like', 'f1', $this->f1]) 77 // $query->andFilterWhere(['like', 'f1', $this->f1])
69 // ->andFilterWhere(['like', 'f2', $this->f2]) 78 // ->andFilterWhere(['like', 'f2', $this->f2])
70 // ->andFilterWhere(['like', 'f3', $this->f3]) 79 // ->andFilterWhere(['like', 'f3', $this->f3])
@@ -73,4 +82,9 @@ class CartBillsSearch extends CartBillsView @@ -73,4 +82,9 @@ class CartBillsSearch extends CartBillsView
73 82
74 return $dataProvider; 83 return $dataProvider;
75 } 84 }
  85 +
  86 + public static function findById($id){
  87 +
  88 + return CartBillsView::find()->where(['id' => $id])->one();
  89 + }
76 } 90 }
console/controllers/ParserController.php
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 namespace console\controllers; 2 namespace console\controllers;
3 3
4 use common\components\CustomVarDamp; 4 use common\components\CustomVarDamp;
  5 +use common\components\mail\ImapMailReader;
  6 +use common\components\mail\MailAttachmentsSaver;
5 use yii\console\Controller; 7 use yii\console\Controller;
6 use yii\helpers\Console; 8 use yii\helpers\Console;
7 use common\components\PriceWriter; 9 use common\components\PriceWriter;
@@ -118,4 +120,26 @@ class ParserController extends Controller @@ -118,4 +120,26 @@ class ParserController extends Controller
118 \Yii::info('2', 'parser'); 120 \Yii::info('2', 'parser');
119 121
120 } 122 }
  123 +
  124 + public function actionSaveMailAttachments()
  125 + {
  126 + \Yii::info('Начало сохранения файлов почты', 'mail');
  127 +
  128 + $mail_reader = new ImapMailReader( '{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'tsurkanovm@gmail.com', 'Wtvr@2000' );
  129 + $mailboxes = $mail_reader->getListMailboxes();
  130 + foreach ($mailboxes as $custom_label) {
  131 + $words = explode(" ",str_replace(array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label)));
  132 + $importer_id = (int)preg_replace("/[^A-Z0-9]+/","",strtoupper($words[0]));
  133 +
  134 + $mail_reader->reOpen($custom_label);
  135 + $saver = new MailAttachmentsSaver( $mail_reader, 'UNSEEN' );
  136 + $saver->file_name_parefix = $importer_id . '~!~';
  137 + $saver->saveAttachmentsTo(\Yii::getAlias('@temp_upload'));
  138 +
  139 +// $files_model = new ImportersFiles();
  140 +// $files_model->importer_id = $importer_id;
  141 + }
  142 +
  143 +
  144 + }
121 } 145 }
122 \ No newline at end of file 146 \ No newline at end of file