|
18 | 18 | class ContactLog extends BaseContactLog |
19 | 19 | { |
20 | 20 |
|
| 21 | + public static $moduleId = 'contact'; |
| 22 | + |
21 | 23 | public function behaviors() |
22 | 24 | { |
23 | 25 | $behaviors = parent::behaviors(); |
@@ -64,6 +66,9 @@ public function sendMessage() |
64 | 66 | $message->setTo($to); |
65 | 67 | $message->setSubject($this->emailSubject); |
66 | 68 | $message->setTextBody($this->dataValue2txt(Json::decode($this->json))); |
| 69 | + if (Yii::$app->getModule(self::$moduleId)->sendHtmlMails) { |
| 70 | + $message->setHtmlBody($this->dataValue2Html(Json::decode($this->json))); |
| 71 | + } |
67 | 72 |
|
68 | 73 | return $message->send(); |
69 | 74 | } |
@@ -94,11 +99,41 @@ private function dataValue2txt($data, $level = 0) |
94 | 99 | } else { |
95 | 100 | $valueText = trim($value); |
96 | 101 | } |
97 | | - $text .= $prefix . trim($key) . ': ' . $valueText . "\n"; |
| 102 | + $text .= $prefix . trim($this->labelFromAttribute($key)) . ': ' . $valueText . "\n"; |
98 | 103 |
|
99 | 104 | } |
100 | 105 |
|
101 | 106 | return $text; |
102 | 107 |
|
103 | 108 | } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param array $data |
| 112 | + * @return void |
| 113 | + */ |
| 114 | + private function dataValue2Html(array $data = []): string |
| 115 | + { |
| 116 | + if (!is_array($data)) { |
| 117 | + return ''; |
| 118 | + } |
| 119 | + $rows = ''; |
| 120 | + foreach ($data as $attribute => $value) { |
| 121 | + if (is_array($value)) { |
| 122 | + $row = $this->dataValue2Html($value); |
| 123 | + } else { |
| 124 | + $row = '<tr><td><b>' . $this->labelFromAttribute($attribute) . '</b></td><td>' . $value . '</td></tr>'; |
| 125 | + } |
| 126 | + $rows .= $row; |
| 127 | + } |
| 128 | + return '<table>' . $rows . '</table>'; |
| 129 | + } |
| 130 | + |
| 131 | + private static $_schemaCache = []; |
| 132 | + private function labelFromAttribute(string $attribute) |
| 133 | + { |
| 134 | + if (empty(static::$_schemaCache)) { |
| 135 | + static::$_schemaCache = Json::decode($this->contactTemplate->form_schema); |
| 136 | + } |
| 137 | + return static::$_schemaCache['properties'][$attribute]['title'] ?? $attribute; |
| 138 | + } |
104 | 139 | } |
0 commit comments