1 <?php
2
3 class RealtyImportLog extends DataObject {
4
5 static $db = array(
6 'StartTime' => 'SS_Datetime',
7 'EndTime' => 'SS_Datetime',
8
9 'Status' => "Enum('process,ok,error')",
10 'StatusMessage' => 'Text',
11 'Log' => 'Text',
12
13 );
14
15 static $has_many = array(
16 'LogItems' => 'RealtyImportLog_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
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('RealtyImportLog', $where, true, 'ID DESC');
39 }
40
41 static function optimize_log_tables($max_log_count) {
42 $max_log_count -= 1;
43 $oldImportLogs = DataObject::get('RealtyImportLog', '', 'ID DESC', '', "$max_log_count,100");
44 if ($oldImportLogs) {
45 foreach($oldImportLogs as $oldImportLog) {
46 $oldImportLog->delete();
47 }
48 }
49 DB::Query("OPTIMIZE TABLE `RealtyImportLog_Item`");
50 DB::Query("OPTIMIZE TABLE `RealtyImportLog`");
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('RealtyImportLog')));
71 return $fields;
72 }
73
74 function RealLogItems() {
75 return DataObject::get('RealtyImportLog_Item', "LogID = {$this->ID}", 'ID', '', 50);
76 }
77
78 function addLog($message, $status='message') {
79
80 if ($this->LogItems()->Count() < 1000) {
81 $logItem = new RealtyImportLog_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 $this->Status = $type;
96 $this->StatusMessage = $message;
97
98 $this->write();
99 }
100
101 function Duration() {
102 $end = $this->EndTime ? $this->EndTime : $this->LastEdited;
103
104 return gmdate('H:i:s', strtotime($end) - strtotime($this->StartTime));
105 }
106
107 function TextStatus() {
108 return _t('ImportLog.Status_' . $this->Status, $this->Status);
109 }
110
111 function onBeforeWrite() {
112 parent::onBeforeWrite();
113 if (!$this->EndTime && $this->Status != 'process') {
114 $this->EndTime = date('Y-m-d H:i:s');
115 }
116 }
117
118 function onBeforeDelete() {
119 parent::onBeforeDelete();
120
121 if ($this->LogItems()->Count()) {
122 foreach($this->LogItems() as $item) {
123 $item->delete();
124 }
125 }
126 }
127
128 function canEdit() {
129 return false;
130 }
131 }
132
133 class RealtyImportLog_Item extends DataObject {
134
135 static $db = array(
136 'Status' => "Enum('message,warning,error')",
137 'Message' => 'Varchar(255)',
138 );
139
140 static $has_one = array(
141 'Log' => 'RealtyImportLog'
142 );
143
144 }
[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.
-