1 <?php
2
3 class Import1CLog extends DataObject {
4
5 static $db = array(
6 'StartTime' => 'SS_Datetime',
7 'EndTime' => 'SS_Datetime',
8 'ProductCatalogDate' => 'SS_Datetime',
9 'Stage' => "Enum('checkauth,init,file,unzip,import,offers,query,success')",
10 'Status' => "Enum('process,ok,error')",
11 'StatusMessage' => 'Text',
12 'Log' => 'Text',
13 'Type' => "Enum('catalog,sale')",
14
15 );
16
17 static $has_many = array(
18 'LogItems' => 'Import1CLog_Item',
19 'Files' => 'Import1CLog_File',
20 'Tasks' => 'Import1CLog_Task',
21 );
22
23 static $searchable_fields = array(
24
25 'Status',
26 'Type',
27 );
28
29 static $summary_fields = array(
30 'TextType',
31 'StartTime',
32 'EndTime',
33 'Duration',
34 'TextStatus',
35 'TextStage',
36 'StatusMessage',
37
38 'PendingTaskCount',
39 );
40
41 static $default_sort = 'ID DESC';
42
43 static function get_last_log($successOnly = false) {
44 $where = ($successOnly) ? '`Status` = "ok"' : '';
45 return DataObject::get_one('Import1CLog', $where, true, 'ID DESC');
46 }
47
48 function fieldLabels($includerelations = true) {
49 $labels = parent::fieldLabels($includerelations);
50 $labels['LastEdited'] = 'Время обновления';
51 $labels['Duration'] = 'Продолжительность';
52 $labels['TextStatus'] = 'Статус';
53 $labels['TextStage'] = $labels['Stage'];
54 $labels['PendingTaskCount'] = 'Очередь задач';
55 $labels['TextType'] = $labels['Type'];
56 return $labels;
57 }
58
59 function populateDefaults() {
60 parent::populateDefaults();
61 $this->StartTime = date('Y-m-d H:i:s');
62 $this->Status = 'process';
63 $this->StatusMessage = 'Начало импорта';
64 }
65
66 function getCMSFields() {
67 $fields = new FieldSet();
68 $fields->push(new LiteralField('LogTextResult', $this->renderWith('ProductImport1CLog')));
69 return $fields;
70 }
71
72 function RealLogItems() {
73 return DataObject::get('Import1CLogItem', "LogID = {$this->ID}", 'ID', '', 50);
74 }
75
76 function addUploadFile($filename) {
77 $file = new Import1CLog_File();
78 $file->Filename = $filename;
79 $file->LogID = $this->ID;
80 $file->write();
81
82 return $file;
83 }
84
85 function addImportTask($filename, $operation) {
86 $task = new Import1CLog_Task();
87 $task->Filename = $filename;
88 $task->Operation = $operation;
89 $task->LogID = $this->ID;
90 $task->write();
91 $this->addLog("Queue task: $operation ($filename)");
92 return $task;
93 }
94
95 function clearTaskQueue() {
96 if ($taskQueue = DataObject::get('Import1CLog_Task')) {
97 foreach($taskQueue as $entry) {
98 $entry->delete();
99 }
100 }
101 }
102
103 function addLog($message, $status='message') {
104
105 if ($this->LogItems()->Count() < 1000) {
106 $logItem = new Import1CLog_Item();
107 $logItem->LogID = $this->ID;
108 $logItem->Message = $message;
109 $logItem->Status = $status;
110 $logItem->write();
111 }
112
113 114 115 116
117 }
118
119 function addStatus($type, $message) {
120 $this->Status = $type;
121 $this->StatusMessage = $message;
122
123
124
125 if ($type == 'error') {
126 if ($this->Tasks()) {
127 foreach ($this->Tasks() as $item) {
128 if ($item->Status == 'new') {
129 $item->Status = 'error';
130 $item->write();
131 }
132 }
133 }
134 }
135
136 $this->write();
137 }
138
139 function addStage($stage, $message = '') {
140 $this->Stage = $stage;
141 if ($message) {
142 $this->StatusMessage = $message;
143 }
144 $this->write();
145 }
146
147 function Duration() {
148 $end = $this->EndTime ? $this->EndTime : $this->LastEdited;
149
150 return gmdate('H:i:s', strtotime($end) - strtotime($this->StartTime));
151 }
152
153 function TextStatus() {
154 return _t('Import1CLog.Status_' . $this->Status, $this->Status);
155 }
156
157 function TextStage() {
158 return _t('Import1CLog.Stage_' . $this->Stage, $this->Stage);
159 }
160
161 function TextType() {
162 return _t('Import1CLog.Type_' . $this->Type, $this->Type);
163 }
164
165 function PendingTaskCount() {
166 return $this->Tasks("Status = 'new'")->Count();
167 }
168
169 function onBeforeWrite() {
170 parent::onBeforeWrite();
171 if (!$this->EndTime && $this->Status != 'process') {
172 $this->EndTime = date('Y-m-d H:i:s');
173 }
174 }
175
176 function onAfterDelete() {
177 if ($this->LogItems()) {
178 foreach ($this->LogItems() as $item) {
179 $item->delete();
180 }
181 }
182 if ($this->Files()) {
183 foreach ($this->Files() as $item) {
184 $item->delete();
185 }
186 }
187 if ($this->Tasks()) {
188 foreach ($this->Tasks() as $item) {
189 $item->delete();
190 }
191 }
192
193 }
194 }
195
196 class Import1CLog_Item extends DataObject {
197 static $db = array(
198 'Status' => "Enum('message,warning,error')",
199 'Message' => 'Varchar(255)',
200 );
201
202 static $has_one = array(
203 'Log' => 'Import1CLog'
204 );
205
206 static $default_sort = 'ID';
207 }
208
209 class Import1CLog_File extends DataObject {
210 static $db = array(
211 'Filename' => 'Varchar(200)',
212 );
213
214 static $has_one = array(
215 'Log' => 'Import1CLog'
216 );
217
218 static $default_sort = 'ID';
219 }
220
221 class Import1CLog_Task extends DataObject {
222 static $db = array(
223 'Filename' => 'Varchar(200)',
224 'Operation' => "Enum('catalog,offers,orders')",
225 'Status' => "Enum('new,process,finish,error')",
226 );
227 static $has_one = array(
228 'Log' => 'Import1CLog'
229 );
230
231 static $default_sort = 'ID';
232
233
234 }
235
[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.
-