1 <?php
2
3 class ProductImportLog extends DataObject {
4
5 static $db = array(
6 'StartTime' => 'SS_Datetime',
7 'EndTime' => 'SS_Datetime',
8 'ProductCatalogDate' => 'SS_Datetime',
9 'Status' => "Enum('process,ok,error')",
10 'StatusMessage' => 'Text',
11 'Log' => 'Text',
12
13 );
14
15 static $has_many = array(
16 'LogItems' => 'ProductImportLog_Item'
17 );
18
19
20 static $searchable_fields = array(
21
22 'Status',
23 );
24
25 static $summary_fields = array(
26 'StartTime',
27 'EndTime',
28 'Duration',
29 'TextStatus',
30 'StatusMessage',
31 'ProductCatalogDate',
32 );
33
34 static $default_sort = 'ID DESC';
35
36 static function get_last_log($successOnly = false) {
37 $where = ($successOnly) ? '`Status` = "ok"' : '';
38 return DataObject::get_one('ProductImportLog', $where, true, 'ID DESC');
39 }
40
41 static function optimize_log_tables($max_log_count) {
42 $max_log_count -= 1;
43 $oldImportLogs = DataObject::get('ProductImportLog', '', 'ID DESC', '', "$max_log_count,100");
44 if ($oldImportLogs) {
45 foreach($oldImportLogs as $oldImportLog) {
46 $oldImportLog->delete();
47 }
48 }
49 DB::Query("OPTIMIZE TABLE `ProductImportLog_Item`");
50 DB::Query("OPTIMIZE TABLE `ProductImportLog`");
51 }
52
53 function fieldLabels($includerelations = true) {
54 $labels = parent::fieldLabels($includerelations);
55 $labels['LastEdited'] = 'Время обновления';
56 $labels['Duration'] = 'Продолжительность';
57 $labels['TextStatus'] = 'Статус';
58 return $labels;
59 }
60
61 function populateDefaults() {
62 parent::populateDefaults();
63 $this->StartTime = date('Y-m-d H:i:s');
64 $this->Status = 'process';
65 $this->StatusMessage = 'Начало импорта';
66 }
67
68 function getCMSFields() {
69 $fields = new FieldSet();
70 $fields->push(new LiteralField('LogTextResult', $this->renderWith('ProductImportLog')));
71 return $fields;
72 }
73
74 function RealLogItems() {
75 return DataObject::get('ProductImportLogItem', "LogID = {$this->ID}", 'ID', '', 50);
76 }
77
78 function addLog($message, $status='message') {
79
80 if ($this->LogItems()->Count() < 1000) {
81 $logItem = new ProductImportLog_Item();
82 $logItem->LogID = $this->ID;
83 $logItem->Message = $message;
84 $logItem->Status = $status;
85 $logItem->write();
86 }
87
88 89 90 91
92 }
93
94 function addStatus($type, $message) {
95 if(ImportSiteConfig::$sendEmail){
96 $data = array(
97 'type' => $type,
98 'message' => $message,
99 'StartTime' => $this->StartTime,
100 'ID' => $this->ID,
101 'Link' => "admin/import/ProductImportLog/{$this->ID}/edit"
102 );
103 $sc = SiteConfig::current_site_config();
104 foreach($sc->Notifications() as $item){
105
106 $data['Email'] = $item->Email;
107 if($type == 'ok' && $item->Success){
108 $this->sendLogEmail($data);
109 }
110
111 if($type == 'error' && $item->Failure){
112 $this->sendLogEmail($data);
113 }
114 }
115 }
116 $this->Status = $type;
117 $this->StatusMessage = $message;
118
119 $this->write();
120 }
121
122 function sendLogEmail($data){
123 $statuses = array(
124 'ok' => 'Успешно',
125 'error' => 'Не завершен'
126 );
127 $statuses2 = array(
128 'ok' => 'успешно',
129 'error' => 'с ошибкой'
130 );
131
132
133
134
135
136
137
138
139 $body = <<<EOF
140 Импорт товаров от {$data['StartTime']} завершен {$statuses2[$data['type']]}</br>
141 Лог импорта - <a href="{$data['Link']}">перейти</a>
142 EOF;
143
144 $sc = SiteConfig::current_site_config();
145 $mail = new Email();
146 $mail->setFrom($sc->AdminEmail);
147 $mail->setSubject("Импорт товаров от {$data['StartTime']}: {$statuses[$data['type']]}");
148 $mail->setTo($data['Email']);
149 $mail->setBody($body);
150 $mail->send();
151 }
152
153 function Duration() {
154 $end = $this->EndTime ? $this->EndTime : $this->LastEdited;
155
156 return gmdate('H:i:s', strtotime($end) - strtotime($this->StartTime));
157 }
158
159 function TextStatus() {
160 return _t('ImportLog.Status_' . $this->Status, $this->Status);
161 }
162
163 function onBeforeWrite() {
164 parent::onBeforeWrite();
165 if (!$this->EndTime && $this->Status != 'process') {
166 $this->EndTime = date('Y-m-d H:i:s');
167 }
168 }
169
170 function onBeforeDelete() {
171 parent::onBeforeDelete();
172
173 if ($this->LogItems()->Count()) {
174 foreach($this->LogItems() as $item) {
175 $item->delete();
176 }
177 }
178 }
179
180 function canEdit() {
181 return false;
182 }
183 }
184
185 class ProductImportLog_Item extends DataObject {
186
187 static $db = array(
188 'Status' => "Enum('message,warning,error')",
189 'Message' => 'Varchar(255)',
190 );
191
192 static $has_one = array(
193 'Log' => 'ProductImportLog'
194 );
195
196 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-