/
glossary-entry.php
371 lines (287 loc) · 12.5 KB
/
glossary-entry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* Routes: GP_Route_Glossary_Entry class
*
* @package GlotPress
* @subpackage Routes
* @since 1.0.0
*/
/**
* Core class used to implement the glossary entry route.
*
* @since 1.0.0
*/
class GP_Route_Glossary_Entry extends GP_Route_Main {
public function glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
$project = GP::$project->by_path( $project_path );
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $project || ! $locale ) {
return $this->die_with_404();
}
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
if ( ! $translation_set ){
return $this->die_with_404();
}
$glossary = GP::$glossary->by_set_or_parent_project( $translation_set, $project );
if ( ! $glossary ){
return $this->die_with_404();
}
$glossary_entries = GP::$glossary_entry->by_glossary_id( $glossary->id );
foreach ( $glossary_entries as $key => $entry ) {
$user = get_userdata( $entry->last_edited_by );
if ( $user ) {
$glossary_entries[$key]->user_login = $user->user_login;
$glossary_entries[$key]->user_display_name = $user->display_name;
}
}
$can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
$url = gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) );
$this->tmpl( 'glossary-view', get_defined_vars() );
}
public function glossary_entry_add_post( $project_path, $locale_slug, $translation_set_slug ) {
$project = GP::$project->by_path( $project_path );
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $project || ! $locale ) {
return $this->die_with_404();
}
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
if ( ! $translation_set ){
return $this->die_with_404();
}
if ( $this->invalid_nonce_and_redirect( 'add-glossary-entry_' . $project->path . $locale_slug . $translation_set_slug ) ) {
return;
}
if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
return;
}
$new_glossary_entry = new GP_Glossary_Entry( gp_post('new_glossary_entry') );
$new_glossary_entry->last_edited_by = get_current_user_id();
$glossary = GP::$glossary->get( $new_glossary_entry->glossary_id );
if ( ! $new_glossary_entry->validate() ) {
$this->errors = $new_glossary_entry->errors;
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
}
else {
preg_match( '/\b' . preg_quote( $new_glossary_entry->term, '/' ) . '\b/i', $new_glossary_entry->term, $m );
if ( $m[0] !== $new_glossary_entry->term ) {
$this->errors[] = __( 'Glossary terms cannot contain word break or multi-byte characters!', 'glotpress' );
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
} else {
$find_parms = array(
'glossary_id' => $glossary->id,
'term' => $new_glossary_entry->term,
'translation' => $new_glossary_entry->translation,
);
if ( GP::$glossary_entry->find_one( $find_parms ) ) {
// Translators: 1: the glossary term that was attempted to be added, 2: the part of speach that was attempted to be added.
$this->errors[] = sprintf( __( 'Identical glossary entry already exists for "%1$s" as "%2$s"!', 'glotpress' ), esc_html( $new_glossary_entry->term ), esc_html( $new_glossary_entry->part_of_speech ) );
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
} else {
$created_glossary_entry = GP::$glossary_entry->create_and_select( $new_glossary_entry );
if ( ! $created_glossary_entry ) {
$this->errors[] = __( 'Error in creating glossary entry!', 'glotpress' );
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
} else {
$this->notices[] = __( 'The glossary entry was created!', 'glotpress' );
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
}
}
}
}
}
public function glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
$ge_post = gp_post('glossary_entry');
$ge = array_shift( $ge_post );
$glossary_entry = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
if ( ! $glossary_entry ){
return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
}
if ( ! $this->verify_nonce( 'edit-glossary-entry_' . $glossary_entry->id ) ) {
return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
}
$glossary = GP::$glossary->get( $glossary_entry->glossary_id );
$translation_set = GP::$translation_set->get( $glossary->translation_set_id );
$can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
if ( ! $can_edit ) {
return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
}
$project = GP::$project->get( $translation_set->project_id );
$locale = GP_Locales::by_slug( $translation_set->locale );
$new_glossary_entry = new GP_Glossary_Entry( $ge );
$new_glossary_entry->last_edited_by = get_current_user_id();
if ( ! $new_glossary_entry->validate() ) {
$this->errors = $new_glossary_entry->errors;
}
else {
if ( ! $glossary_entry->update( $new_glossary_entry ) ) {
$this->errors = $glossary_entry->errors;
}
}
if ( $this->errors ) {
$error_output = '<ul>';
foreach ( $this->errors as $error ) {
$error_output .= '<li>' . $error . '</li>';
}
$error_output .= '</ul>';
return $this->die_with_error( $error_output, 200 );
}
else {
$entry = $glossary_entry->reload();
$user = get_userdata( $entry->last_edited_by );
$entry->user_login = $user ? $user->user_login : '';
$entry->user_display_name = $user ? $user->display_name : '';
$output = gp_tmpl_get_output( 'glossary-entry-row', get_defined_vars() );
echo wp_json_encode( $output );
}
exit();
}
public function glossary_entry_delete_post() {
$ge_post = gp_post('glossary_entry');
$ge = array_shift( $ge_post );
$glossary_entry = GP::$glossary_entry->get( absint( $ge['glossary_entry_id'] ) );
if ( ! $glossary_entry ) {
return $this->die_with_error( __( 'The glossary entry cannot be found', 'glotpress' ), 200 );
}
if ( ! $this->verify_nonce( 'delete-glossary-entry_' . $glossary_entry->id ) ) {
return $this->die_with_error( __( 'An error has occurred. Please try again.', 'glotpress' ), 403 );
}
$glossary = GP::$glossary->get( $glossary_entry->glossary_id );
$translation_set = GP::$translation_set->get( $glossary->translation_set_id );
$can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
if ( ! $can_edit ) {
return $this->die_with_error( __( 'Forbidden', 'glotpress' ), 403 );
}
if ( ! $glossary_entry->delete() ) {
$this->errors[] = __( 'Error in deleting glossary entry!', 'glotpress' );
}
if ( ! empty( $this->errors ) ) {
$error_output = '<ul>';
foreach ( $this->errors as $error ) {
$error_output .= '<li>' . $error . '</li>';
}
$error_output .= '</ul>';
return $this->die_with_error( $error_output, 200 );
}
exit();
}
public function export_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
$project = GP::$project->by_path( $project_path );
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $project || ! $locale ) {
return $this->die_with_404();
}
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
if ( ! $translation_set ) {
return $this->die_with_404();
}
$glossary = GP::$glossary->by_set_id( $translation_set->id );
if ( ! $glossary ) {
return $this->die_with_404();
}
$glossary_entries = GP::$glossary_entry->by_glossary_id( $glossary->id );
$filename = sprintf( '%s-%s-glossary.csv', str_replace( '/', '-', $project->path ), $locale->slug );
$last_modified = gmdate( 'D, d M Y H:i:s', gp_gmt_strtotime( GP::$glossary_entry->last_modified( $glossary ) ) ) . ' GMT';
$this->headers_for_download( $filename, $last_modified );
$this->print_export_file( $locale->slug, $glossary_entries );
}
public function import_glossary_entries_get( $project_path, $locale_slug, $translation_set_slug ) {
$project = GP::$project->by_path( $project_path );
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $project || ! $locale ) {
return $this->die_with_404();
}
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
if ( ! $translation_set ) {
return $this->die_with_404();
}
if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
return;
}
$can_edit = $this->can( 'approve', 'translation-set', $translation_set->id );
$this->tmpl( 'glossary-import', get_defined_vars() );
}
public function import_glossary_entries_post( $project_path, $locale_slug, $translation_set_slug ) {
$project = GP::$project->by_path( $project_path );
$locale = GP_Locales::by_slug( $locale_slug );
if ( ! $project || ! $locale ) {
return $this->die_with_404();
}
$translation_set = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $translation_set_slug, $locale_slug );
if ( ! $translation_set ) {
return $this->die_with_404();
}
$glossary = GP::$glossary->by_set_id( $translation_set->id );
if ( ! $glossary ) {
return $this->die_with_404();
}
if ( $this->invalid_nonce_and_redirect( 'import-glossary-entries_' . $project->path . $locale_slug . $translation_set_slug ) ) {
return;
}
if ( $this->cannot_and_redirect( 'approve', 'translation-set', $translation_set->id ) ) {
return;
}
if ( ! is_uploaded_file( $_FILES['import-file']['tmp_name'] ) ) {
$this->redirect_with_error( __( 'Error uploading the file.', 'glotpress' ) );
return;
}
$replace = gp_post( 'import-flush' );
if ( 'on' === $replace && $this->can( 'approve', 'translation-set', $translation_set->id ) ) {
GP::$glossary_entry->delete_many( array( 'glossary_id' => $glossary->id ) );
}
$glossary_entries_added = $this->read_glossary_entries_from_file( $_FILES['import-file']['tmp_name'], $glossary->id, $locale->slug );
if ( empty( $this->errors ) && is_int( $glossary_entries_added ) ) {
$this->notices[] = sprintf( __( '%s glossary entries were added', 'glotpress' ), $glossary_entries_added );
}
$this->redirect( gp_url_join( gp_url_project_locale( $project->path, $locale_slug, $translation_set_slug ), array( 'glossary' ) ) );
}
private function print_export_file( $locale_slug, $entries ) {
$outstream = fopen("php://output", 'w');
fputcsv( $outstream, array( 'en', $locale_slug, 'pos', 'description' ) );
foreach ( $entries as $entry ) {
$values = array( $entry->term, $entry->translation, $entry->part_of_speech, $entry->comment );
fputcsv( $outstream, $values );
}
fclose( $outstream );
}
private function read_glossary_entries_from_file( $file, $glossary_id, $locale_slug ) {
$f = fopen( $file, 'r' );
$glossary_entries = 0;
$data = fgetcsv( $f, 0, ',');
if ( ! is_array( $data ) ) {
return;
}
else if ( $data[1] !== $locale_slug ) {
$this->redirect_with_error( __( 'Unexpected values in the CSV file header row.', 'glotpress' ) );
return;
}
while ( ( $data = fgetcsv( $f, 0, ',') ) !== FALSE ) {
// We're only parsing one locale per file right now
if ( count ($data) > 4 ) {
$data = array_splice( $data, 2, -2 );
}
$entry_data = array(
'glossary_id' => $glossary_id,
'term' => $data[0],
'translation' => $data[1],
'part_of_speech' => $data[2],
'comment' => $data[3],
'last_edited_by' => get_current_user_id()
);
$new_glossary_entry = new GP_Glossary_Entry( $entry_data );
if ( ! $new_glossary_entry->validate() ) {
continue;
} else {
$entry_exists = GP::$glossary_entry->find_one( $entry_data );
if ( $entry_exists ) {
continue;
}
$created_glossary_entry = GP::$glossary_entry->create_and_select( $new_glossary_entry );
if ( $created_glossary_entry ) {
$glossary_entries++;
}
}
}
fclose($f);
return $glossary_entries;
}
}