1 | <?php |
||
2 | |||
3 | namespace Firesphere\SearchBackend\Models; |
||
4 | |||
5 | use SilverStripe\Control\Director; |
||
6 | use SilverStripe\ORM\DataObject; |
||
7 | use SilverStripe\ORM\FieldType\DBDatetime; |
||
8 | use SilverStripe\Security\Member; |
||
9 | use SilverStripe\Security\PermissionProvider; |
||
10 | |||
11 | /** |
||
12 | * Class \Firesphere\SearchSearch\Models\SearchLog |
||
13 | * |
||
14 | * @property string $Timestamp |
||
15 | * @property string $Index |
||
16 | * @property string $Type |
||
17 | * @property string $Level |
||
18 | * @property string $Message |
||
19 | */ |
||
20 | class SearchLog extends DataObject implements PermissionProvider |
||
21 | { |
||
22 | /** |
||
23 | * @var array Used to give the Gridfield rows a corresponding colour |
||
24 | */ |
||
25 | protected static $row_color = [ |
||
26 | 'ERROR' => 'alert alert-danger', |
||
27 | 'WARN' => 'alert alert-warning', |
||
28 | 'INFO' => 'alert alert-info', |
||
29 | ]; |
||
30 | /** |
||
31 | * @var string Database table name |
||
32 | */ |
||
33 | private static $table_name = 'Search_SearchLog'; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
34 | /** |
||
35 | * @var array Database columns |
||
36 | */ |
||
37 | private static $db = [ |
||
0 ignored issues
–
show
|
|||
38 | 'Timestamp' => 'Datetime', |
||
39 | 'Index' => 'Varchar(255)', |
||
40 | 'Type' => 'Enum("Config,Index,Query")', |
||
41 | 'Level' => 'Varchar(10)', |
||
42 | 'Message' => 'Text', |
||
43 | ]; |
||
44 | /** |
||
45 | * @var array Summary fields |
||
46 | */ |
||
47 | private static $summary_fields = [ |
||
0 ignored issues
–
show
|
|||
48 | 'Timestamp', |
||
49 | 'Index', |
||
50 | 'Type', |
||
51 | 'Level', |
||
52 | ]; |
||
53 | /** |
||
54 | * @var array Searchable fields |
||
55 | */ |
||
56 | private static $searchable_fields = [ |
||
0 ignored issues
–
show
|
|||
57 | 'Created', |
||
58 | 'Timestamp', |
||
59 | 'Index', |
||
60 | 'Type', |
||
61 | 'Level', |
||
62 | ]; |
||
63 | /** |
||
64 | * @var array Timestamp is indexed |
||
65 | */ |
||
66 | private static $indexes = [ |
||
0 ignored issues
–
show
|
|||
67 | 'Timestamp' => true, |
||
68 | ]; |
||
69 | /** |
||
70 | * @var string Default sort |
||
71 | */ |
||
72 | private static $default_sort = 'Timestamp DESC'; |
||
0 ignored issues
–
show
|
|||
73 | |||
74 | /** |
||
75 | * Convert the Timestamp to a DBDatetime for compatibility |
||
76 | */ |
||
77 | public function onBeforeWrite() |
||
78 | { |
||
79 | parent::onBeforeWrite(); |
||
80 | |||
81 | $this->Timestamp = DBDatetime::create()->setValue(strtotime($this->Timestamp)); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Return the first line of this log item error |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getLastErrorLine() |
||
90 | { |
||
91 | $lines = explode(PHP_EOL, $this->Message); |
||
92 | |||
93 | return $lines[0]; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Not creatable by users |
||
98 | * |
||
99 | * @param null|Member $member |
||
100 | * @param array $context |
||
101 | * @return bool|mixed |
||
102 | */ |
||
103 | public function canCreate($member = null, $context = []) |
||
104 | { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Not editable by users |
||
110 | * |
||
111 | * @param null|Member $member |
||
112 | * @return bool|mixed |
||
113 | */ |
||
114 | public function canEdit($member = null) |
||
115 | { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Member has view access? |
||
121 | * |
||
122 | * @param null|Member $member |
||
123 | * @return bool|mixed |
||
124 | */ |
||
125 | public function canView($member = null) |
||
126 | { |
||
127 | return parent::canView($member); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Only deleteable by admins or when in dev mode to clean up |
||
132 | * |
||
133 | * @param null|Member $member |
||
134 | * @return bool|mixed |
||
135 | */ |
||
136 | public function canDelete($member = null) |
||
137 | { |
||
138 | if ($member) { |
||
139 | return $member->inGroup('administrators') || Director::isDev(); |
||
140 | } |
||
141 | |||
142 | return parent::canDelete($member) || Director::isDev(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get the extra classes to colour the gridfield rows |
||
147 | * |
||
148 | * @return mixed|string |
||
149 | */ |
||
150 | public function getExtraClass() |
||
151 | { |
||
152 | $classMap = static::$row_color; |
||
153 | |||
154 | return $classMap[$this->Level] ?? 'alert alert-info'; |
||
155 | } |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Return a map of permission codes to add to the dropdown shown in the Security section of the CMS. |
||
160 | * array( |
||
161 | * 'VIEW_SITE' => 'View the site', |
||
162 | * ); |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function providePermissions() |
||
167 | { |
||
168 | return [ |
||
169 | 'DELETE_LOG' => [ |
||
170 | 'name' => _t(self::class . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Search logs'), |
||
171 | 'category' => _t('Permissions.LOGS_CATEGORIES', 'Search logs permissions'), |
||
172 | 'help' => _t( |
||
173 | self::class . '.PERMISSION_DELETE_HELP', |
||
174 | 'Permission required to delete existing Search logs.' |
||
175 | ), |
||
176 | ], |
||
177 | 'VIEW_LOG' => [ |
||
178 | 'name' => _t(self::class . '.PERMISSION_VIEW_DESCRIPTION', 'View Search logs'), |
||
179 | 'category' => _t('Permissions.LOGS_CATEGORIES', 'Search logs permissions'), |
||
180 | 'help' => _t( |
||
181 | self::class . '.PERMISSION_VIEW_HELP', |
||
182 | 'Permission required to view existing Search logs.' |
||
183 | ), |
||
184 | ], |
||
185 | ]; |
||
186 | } |
||
187 | } |
||
188 |