1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This does the job of handling attachment related errors |
5
|
|
|
* |
6
|
|
|
* @name ElkArte Forum |
7
|
|
|
* @copyright ElkArte Forum contributors |
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
9
|
|
|
* |
10
|
|
|
* @version 1.1 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace ElkArte\Errors; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Error context for attachments |
18
|
|
|
* |
19
|
|
|
* @todo Can this be simplified and extend ErrorContext? |
20
|
|
|
*/ |
21
|
|
|
class AttachmentErrorContext |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Holds our static instance of the class |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
private static $_context = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Holds all of the attachment ids |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $_attachs = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Holds any errors found |
37
|
|
|
* @var ErrorContext|null |
38
|
|
|
*/ |
39
|
|
|
private $_generic_error = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Holds if the error is generic of specific to an attachment |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $_active_attach = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add attachment |
49
|
|
|
* |
50
|
|
|
* - Automatically activate the attachments added |
51
|
|
|
* |
52
|
|
|
* @param string $id |
53
|
|
|
* @param string $name |
54
|
|
|
*/ |
55
|
|
|
public function addAttach($id, $name) |
56
|
|
|
{ |
57
|
|
|
if (empty($id) || empty($name)) |
58
|
|
|
{ |
59
|
|
|
$this->activate(); |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!isset($this->_attachs[$id])) |
64
|
|
|
$this->_attachs[$id] = array( |
65
|
|
|
'name' => $name, |
66
|
|
|
'error' => ErrorContext::context($id, 1), |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->activate($id); |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Sets the active attach (errors are "attached" to that) |
76
|
|
|
* |
77
|
|
|
* @param string|null $id A valid attachment, if invalid it defaults to 'generic' |
78
|
|
|
*/ |
79
|
|
|
public function activate($id = null) |
80
|
|
|
{ |
81
|
|
|
if (empty($id) || !isset($this->_attachs[$id])) |
82
|
|
|
$this->_active_attach = 'generic'; |
83
|
|
|
else |
84
|
|
|
$this->_active_attach = $id; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add an error |
91
|
|
|
* |
92
|
|
|
* @param string $error error code |
93
|
|
|
* @param string|null $lang_file = null |
94
|
|
|
*/ |
95
|
|
|
public function addError($error, $lang_file = null) |
96
|
|
|
{ |
97
|
|
|
if (empty($error)) |
98
|
|
|
return; |
99
|
|
|
|
100
|
|
|
if ($this->_active_attach == 'generic') |
101
|
|
|
{ |
102
|
|
|
if (!isset($this->_attachs[$this->_active_attach])) |
103
|
|
|
$this->_generic_error = ErrorContext::context('attach_generic_error', 1); |
104
|
|
|
|
105
|
|
|
$this->_generic_error->addError($error, $lang_file); |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->_attachs[$this->_active_attach]['error']->addError($error, $lang_file); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Removes an error |
114
|
|
|
* |
115
|
|
|
* @param string $error error code |
116
|
|
|
*/ |
117
|
|
|
public function removeError($error) |
118
|
|
|
{ |
119
|
|
|
if (empty($error)) |
120
|
|
|
return; |
121
|
|
|
|
122
|
|
|
$this->_attachs[$this->_active_attach]['error']->removeError($error); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* If this error context has errors stored. |
127
|
|
|
* |
128
|
|
|
* @param string|null $attachID |
129
|
|
|
* @param int|null $severity the severity level |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function hasErrors($attachID = null, $severity = null) |
132
|
|
|
{ |
133
|
|
|
if ($this->_generic_error !== null) |
134
|
|
|
if ($this->_generic_error->hasErrors($severity)) |
135
|
|
|
return true; |
136
|
|
|
|
137
|
|
|
if (!empty($this->_attachs)) |
138
|
|
|
{ |
139
|
|
|
if ($attachID !== null) |
140
|
|
|
{ |
141
|
|
|
if (isset($this->_attachs[$attachID])) |
142
|
|
|
return $this->_attachs[$attachID]['error']->hasErrors($severity); |
143
|
|
|
} |
144
|
|
|
else |
145
|
|
|
{ |
146
|
|
|
foreach ($this->_attachs as $attach) |
147
|
|
|
if ($attach['error']->hasErrors($severity)) |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* If this error context has a particular error code. |
156
|
|
|
* |
157
|
|
|
* @param string $error_code the code of the error |
158
|
|
|
* @param string|null $attachID |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function hasError($error_code, $attachID = null) |
161
|
|
|
{ |
162
|
|
|
if ($this->_generic_error !== null) |
163
|
|
|
{ |
164
|
|
|
if ($this->_generic_error->hasError($error_code)) |
165
|
|
|
{ |
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!empty($this->_attachs)) |
171
|
|
|
{ |
172
|
|
|
if ($attachID !== null) |
173
|
|
|
{ |
174
|
|
|
return isset($this->_attachs[$attachID]) && $this->_attachs[$attachID]['error']->hasError($error_code); |
175
|
|
|
} |
176
|
|
|
else |
177
|
|
|
{ |
178
|
|
|
foreach ($this->_attachs as $attach) |
179
|
|
|
{ |
180
|
|
|
if ($attach['error']->hasError($error_code)) |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Prepare the errors for display. |
190
|
|
|
* |
191
|
|
|
* - Return an array containing the error strings |
192
|
|
|
* - If severity is null the function returns all the errors |
193
|
|
|
* |
194
|
|
|
* @param int|null $severity = null the severity level wanted |
195
|
|
|
*/ |
196
|
|
|
public function prepareErrors($severity = null) |
197
|
|
|
{ |
198
|
|
|
global $txt; |
199
|
|
|
|
200
|
|
|
$returns = array(); |
201
|
|
|
|
202
|
|
|
if ($this->_generic_error !== null) |
203
|
|
|
$returns['attach_generic'] = array( |
204
|
|
|
'errors' => $this->_generic_error->prepareErrors($severity), |
205
|
|
|
'type' => $this->getErrorType(), |
206
|
|
|
'title' => $txt['attach_error_title'], |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
if (!empty($this->_attachs)) |
210
|
|
|
{ |
211
|
|
|
foreach ($this->_attachs as $attachID => $error) |
212
|
|
|
{ |
213
|
|
|
$returns[$attachID] = array( |
214
|
|
|
'errors' => $error['error']->prepareErrors($severity), |
215
|
|
|
'type' => $this->getErrorType(), |
216
|
|
|
'title' => sprintf($txt['attach_warning'], $error['name']), |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $returns; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function getName() |
225
|
|
|
{ |
226
|
|
|
return 'attach_error_title'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return the type of the error |
231
|
|
|
*/ |
232
|
|
|
public function getErrorType() |
233
|
|
|
{ |
234
|
|
|
return 1; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Find and return Attachment_ErrorContext instance if it exists, |
239
|
|
|
* or create it if it doesn't exist |
240
|
|
|
*/ |
241
|
|
|
public static function context() |
242
|
|
|
{ |
243
|
|
|
if (self::$_context === null) |
244
|
|
|
self::$_context = new self(); |
245
|
|
|
|
246
|
|
|
return self::$_context; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|