From: fomichev Date: Fri, 21 Mar 2025 14:52:47 +0000 (+0300) Subject: Таблица с письмами X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=c8183e54b73da4529cd4fb5cff7b99d2c3fdde2a;p=erp24_rep%2Fyii-erp24%2F.git Таблица с письмами --- diff --git a/erp24/controllers/MarketplaceFlowwowEmailsController.php b/erp24/controllers/MarketplaceFlowwowEmailsController.php new file mode 100644 index 00000000..c40d9c8b --- /dev/null +++ b/erp24/controllers/MarketplaceFlowwowEmailsController.php @@ -0,0 +1,134 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ] + ); + } + + /** + * Lists all MarketplaceFlowwowEmails models. + * + * @return string + */ + public function actionIndex() + { + $searchModel = new MarketplaceFlowwowEmailsSearch(); + $dataProvider = $searchModel->search($this->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single MarketplaceFlowwowEmails model. + * @param int $id ID + * @return string + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new MarketplaceFlowwowEmails model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return string|\yii\web\Response + */ + public function actionCreate() + { + $model = new MarketplaceFlowwowEmails(); + + if ($this->request->isPost) { + if ($model->load($this->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + } else { + $model->loadDefaultValues(); + } + + return $this->render('create', [ + 'model' => $model, + ]); + } + + /** + * Updates an existing MarketplaceFlowwowEmails model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param int $id ID + * @return string|\yii\web\Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionUpdate($id) + { + $model = $this->findModel($id); + + if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } + + return $this->render('update', [ + 'model' => $model, + ]); + } + + /** + * Deletes an existing MarketplaceFlowwowEmails model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param int $id ID + * @return \yii\web\Response + * @throws NotFoundHttpException if the model cannot be found + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the MarketplaceFlowwowEmails model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param int $id ID + * @return MarketplaceFlowwowEmails the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = MarketplaceFlowwowEmails::findOne(['id' => $id])) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } +} diff --git a/erp24/migrations/m250321_134458_create_marketplace_flowwow_emails_table.php b/erp24/migrations/m250321_134458_create_marketplace_flowwow_emails_table.php new file mode 100644 index 00000000..15deea5e --- /dev/null +++ b/erp24/migrations/m250321_134458_create_marketplace_flowwow_emails_table.php @@ -0,0 +1,44 @@ +db->getTableSchema(self::TABLE_NAME); + + if (!isset($tableSchema)) { + $this->createTable(self::TABLE_NAME, [ + 'id' => $this->bigPrimaryKey()->comment('ID'), + 'subject' => $this->string()->notNull()->comment('Тема письма'), + 'subject_pattern' => $this->string()->null()->comment('Шаблон темы письма'), + 'from' => $this->string(255)->notNull()->comment('Отправитель письма'), + 'to' => $this->string()->notNull()->comment('Получатель письма'), + 'date' => $this->dateTime()->notNull()->comment('Дата письма'), + 'body' => $this->text()->notNull()->comment('Тело письма'), + 'created_at' => $this->timestamp() + ->defaultExpression('CURRENT_TIMESTAMP') + ->comment('Дата создания записи'), + ]); + } + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $tableSchema = $this->db->getTableSchema(self::TABLE_NAME); + if (isset($tableSchema)) { + $this->dropTable(self::TABLE_NAME); + } + } +} diff --git a/erp24/records/MarketplaceFlowwowEmails.php b/erp24/records/MarketplaceFlowwowEmails.php new file mode 100644 index 00000000..e148ff52 --- /dev/null +++ b/erp24/records/MarketplaceFlowwowEmails.php @@ -0,0 +1,62 @@ + null], + [['subject', 'from', 'to', 'date', 'body'], 'required'], + [['date', 'created_at'], 'safe'], + [['body'], 'string'], + [['subject', 'subject_pattern', 'from', 'to'], 'string', 'max' => 255], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'subject' => 'Тема письма', + 'subject_pattern' => 'Шаблон темы письма', + 'from' => 'Отправитель письма', + 'to' => 'Получатель письма', + 'date' => 'Дата письма', + 'body' => 'Тело письма', + 'created_at' => 'Дата создания записи', + ]; + } + +} diff --git a/erp24/records/MarketplaceFlowwowEmailsSearch.php b/erp24/records/MarketplaceFlowwowEmailsSearch.php new file mode 100644 index 00000000..a0426ab5 --- /dev/null +++ b/erp24/records/MarketplaceFlowwowEmailsSearch.php @@ -0,0 +1,75 @@ +load()` method. + * + * @return ActiveDataProvider + */ + public function search($params, $formName = null) + { + $query = MarketplaceFlowwowEmails::find(); + + // add conditions that should always apply here + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + + $this->load($params, $formName); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'date' => $this->date, + 'created_at' => $this->created_at, + ]); + + $query->andFilterWhere(['ilike', 'subject', $this->subject]) + ->andFilterWhere(['ilike', 'subject_pattern', $this->subject_pattern]) + ->andFilterWhere(['ilike', 'from', $this->from]) + ->andFilterWhere(['ilike', 'to', $this->to]) + ->andFilterWhere(['ilike', 'body', $this->body]); + + return $dataProvider; + } +} diff --git a/erp24/services/MarketplaceService.php b/erp24/services/MarketplaceService.php index 5af8ebaa..a2d48528 100644 --- a/erp24/services/MarketplaceService.php +++ b/erp24/services/MarketplaceService.php @@ -17,6 +17,7 @@ use yii_app\helpers\DataHelper; use yii_app\helpers\File; use yii_app\records\Balances; use yii_app\records\Images; +use yii_app\records\MarketplaceFlowwowEmails; use yii_app\records\MarketplaceOrderDelivery; use yii_app\records\MarketplaceOrderItems; use yii_app\records\MarketplaceOrders; @@ -1444,18 +1445,23 @@ class MarketplaceService $from = mb_decode_mimeheader($overview[0]->from); $to = mb_decode_mimeheader($overview[0]->to); $date = date('Y-m-d H:i:s', strtotime(mb_decode_mimeheader($overview[0]->date))); + if (isset($structure->parts) && count($structure->parts)) { + foreach ($structure->parts as $partNum => $part) { + if ($part->subtype == 'HTML') { + $htmlMessage = imap_fetchbody($inbox, $email_number, $partNum + 1); + break; + } + } + } + $savedEmail = self::saveEmailIfNotExists($subject, null, $from, $to, $date, $htmlMessage); + foreach ($subjectPatterns as $pattern) { if (preg_match($pattern, $subject)) { $subjectIndex = self::SUBJECT_INDEX[$pattern]; - if (isset($structure->parts) && count($structure->parts)) { - foreach ($structure->parts as $partNum => $part) { - if ($part->subtype == 'HTML') { - $htmlMessage = imap_fetchbody($inbox, $email_number, $partNum + 1); - break; - } - } + if ($savedEmail !== null) { + $savedEmail->subject_pattern = $pattern; + $savedEmail->save(); } - $messages[] = [ 'subject' => $subject, 'subject_index' => $subjectIndex, @@ -1464,27 +1470,27 @@ class MarketplaceService 'date' => $date, 'body' => $htmlMessage, ]; - } - } - self::imap_debug_log("Установка флага SEEN для сообшения #" . $email_number, $debugMode, $progressCallback); - $result = imap_setflag_full($inbox, $email_number, "\\Seen"); - if (!$result) { - self::imap_debug_log("Не удалось установить SEEN flag: " . imap_last_error(), $debugMode, $progressCallback); - } else { - self::imap_debug_log("SEEN установлен успешно", $debugMode, $progressCallback); - } - self::check_imap_errors($debugMode, $progressCallback); + self::imap_debug_log("Установка флага SEEN для сообшения #" . $email_number, $debugMode, $progressCallback); + $result = imap_setflag_full($inbox, $email_number, "\\Seen"); + if (!$result) { + self::imap_debug_log("Не удалось установить SEEN flag: " . imap_last_error(), $debugMode, $progressCallback); + } else { + self::imap_debug_log("SEEN установлен успешно", $debugMode, $progressCallback); + } + self::check_imap_errors($debugMode, $progressCallback); - $overview_after = imap_fetch_overview($inbox, $email_number, 0); - if (isset($overview_after[0]->seen) && $overview_after[0]->seen) { - self::imap_debug_log("Сообщение #" . $email_number . " помечено SEEN", $debugMode, $progressCallback); - } else { - self::imap_debug_log("WARNING: Сообщение #" . $email_number . " не удалось пометить как SEEN", $debugMode, $progressCallback); - } + $overview_after = imap_fetch_overview($inbox, $email_number, 0); + if (isset($overview_after[0]->seen) && $overview_after[0]->seen) { + self::imap_debug_log("Сообщение #" . $email_number . " помечено SEEN", $debugMode, $progressCallback); + } else { + self::imap_debug_log("WARNING: Сообщение #" . $email_number . " не удалось пометить как SEEN", $debugMode, $progressCallback); + } - if ($progressCallback) { - call_user_func($progressCallback, "От: " . $from . " тема " . $subject . " от " . $date); - call_user_func($progressCallback, "Обработано писем: " . ($index + 1) . " из " . count($emails)); + if ($progressCallback) { + call_user_func($progressCallback, "От: " . $from . " тема " . $subject . " от " . $date); + call_user_func($progressCallback, "Обработано писем: " . ($index + 1) . " из " . count($emails)); + } + } } } } else { @@ -1506,6 +1512,36 @@ class MarketplaceService return $messages; } + public static function saveEmailIfNotExists($subject, $subjectPattern, $from, $to, $date, $body) + { + if (strpos($from, 'info@flowwow.com') === false) { + return; + } + + $exists = MarketplaceFlowwowEmails::find() + ->where(['subject' => $subject, 'from' => $from, 'date' => $date]) + ->exists(); + + if (!$exists) { + $email = new MarketplaceFlowwowEmails(); + $email->subject = $subject; + $email->subject_pattern = $subjectPattern; + $email->from = $from; + $email->to = $to; + $email->date = $date; + $email->body = $body; + $email->created_at = date('Y-m-d H:i:s'); + + if ($email->save()) { + return $email; + } else { + Yii::error('Письмо не сохранено' . json_encode($email->errors), __METHOD__); + return null; + } + + } + } + public static function imap_debug_log($message, $debugMode, $progressCallback) { diff --git a/erp24/views/marketplace-flowwow-emails/_form.php b/erp24/views/marketplace-flowwow-emails/_form.php new file mode 100644 index 00000000..9bc491a2 --- /dev/null +++ b/erp24/views/marketplace-flowwow-emails/_form.php @@ -0,0 +1,35 @@ + + +
+ + + + field($model, 'subject')->textInput(['maxlength' => true]) ?> + + field($model, 'subject_pattern')->textInput(['maxlength' => true]) ?> + + field($model, 'from')->textInput(['maxlength' => true]) ?> + + field($model, 'to')->textInput(['maxlength' => true]) ?> + + field($model, 'date')->textInput() ?> + + field($model, 'body')->textarea(['rows' => 6]) ?> + + field($model, 'created_at')->textInput() ?> + +
+ 'btn btn-success']) ?> +
+ + + +
diff --git a/erp24/views/marketplace-flowwow-emails/_search.php b/erp24/views/marketplace-flowwow-emails/_search.php new file mode 100644 index 00000000..dbae3951 --- /dev/null +++ b/erp24/views/marketplace-flowwow-emails/_search.php @@ -0,0 +1,41 @@ + + + diff --git a/erp24/views/marketplace-flowwow-emails/index.php b/erp24/views/marketplace-flowwow-emails/index.php new file mode 100644 index 00000000..07861ce3 --- /dev/null +++ b/erp24/views/marketplace-flowwow-emails/index.php @@ -0,0 +1,50 @@ +title = 'Marketplace Flowwow Emails'; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ 'btn btn-success']) ?> +

+ + render('_search', ['model' => $searchModel]); ?> + + $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'subject', + 'subject_pattern', + 'from', + 'to', + //'date', + //'body:ntext', + //'created_at', + [ + 'class' => ActionColumn::className(), + 'urlCreator' => function ($action, MarketplaceFlowwowEmails $model, $key, $index, $column) { + return Url::toRoute([$action, 'id' => $model->id]); + } + ], + ], + ]); ?> + + +
diff --git a/erp24/views/marketplace-flowwow-emails/update.php b/erp24/views/marketplace-flowwow-emails/update.php new file mode 100644 index 00000000..5a5e5f42 --- /dev/null +++ b/erp24/views/marketplace-flowwow-emails/update.php @@ -0,0 +1,21 @@ +title = 'Update Marketplace Flowwow Emails: ' . $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Marketplace Flowwow Emails', 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]]; +$this->params['breadcrumbs'][] = 'Update'; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/erp24/views/marketplace-flowwow-emails/view.php b/erp24/views/marketplace-flowwow-emails/view.php new file mode 100644 index 00000000..2e94d5d4 --- /dev/null +++ b/erp24/views/marketplace-flowwow-emails/view.php @@ -0,0 +1,43 @@ +title = $model->id; +$this->params['breadcrumbs'][] = ['label' => 'Marketplace Flowwow Emails', 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +\yii\web\YiiAsset::register($this); +?> +
+ +

title) ?>

+ +

+ $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +

+ + $model, + 'attributes' => [ + 'id', + 'subject', + 'subject_pattern', + 'from', + 'to', + 'date', + 'body:ntext', + 'created_at', + ], + ]) ?> + +