function map_glossary_entries_to_translations_originals($translations, $glossary) { $glossary_entries = GP::$glossary_entry->by_glossary_id($glossary->id); if (empty($glossary_entries)) { return $translations; } $glossary_entries_terms = array(); //Create array of glossary terms, longest first foreach ($glossary_entries as $key => $value) { $glossary_entries_terms[$key] = $value->term; } uasort($glossary_entries_terms, function ($a, $b) { return gp_strlen($a) < gp_strlen($b); }); foreach ($translations as $key => $t) { //Save our current singular/plural strings before attempting any markup change. Also escape now, since we're going to add some html. $translations[$key]->singular_glossary_markup = esc_translation($t->singular); $translations[$key]->plural_glossary_markup = esc_translation($t->plural); //Search for glossary terms in our strings $matching_entries = array(); foreach ($glossary_entries_terms as $i => $term) { $glossary_entry = $glossary_entries[$i]; if (gp_stripos($t->singular . ' ' . $t->plural, $term) !== false) { $matching_entries[$term][] = array('translation' => $glossary_entry->translation, 'pos' => $glossary_entry->part_of_speech, 'comment' => $glossary_entry->comment); } } //Replace terms in strings with markup foreach ($matching_entries as $term => $glossary_data) { $replacement = '<span class="glossary-word" data-translations="' . htmlspecialchars(gp_json_encode($glossary_data), ENT_QUOTES, 'UTF-8') . '">$1</span>'; $translations[$key]->singular_glossary_markup = preg_replace('/\\b(' . preg_quote($term, '/') . '[es|s]?)(?![^<]*<\\/span>)\\b/iu', $replacement, $translations[$key]->singular_glossary_markup); if ($t->plural) { $translations[$key]->plural_glossary_markup = preg_replace('/\\b(' . preg_quote($term, '/') . '[es|s]?)(?![^<]*<\\/span>)\\b/iu', $replacement, $translations[$key]->plural_glossary_markup); } } } return $translations; }
function gp_array_of_array_of_things_to_json($array) { $map_to_fields = function ($array) { return array_map(function ($thing) { return $thing->fields(); }, $array); }; return gp_json_encode(array_map($map_to_fields, $array)); }
<?php echo gp_json_encode($translations);
<?php echo gp_json_encode(things_to_fields($projects_data));
<?php echo gp_json_encode($locales);
public function glossary_entries_post($project_path, $locale_slug, $translation_set_slug) { $ge = array_shift(gp_post('glossary_entry')); $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'), 200); } $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'), 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 = GP::$user->current()->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 { $ge = $glossary_entry->reload(); $output = gp_tmpl_get_output('glossary-entry-row', get_defined_vars()); echo gp_json_encode($output); } exit; }
<?php echo gp_json_encode(array('success' => false, 'error' => __('Not Found')));
public function translations_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); $this->can_or_forbidden('edit', 'translation-set', $translation_set->id); if (!$translation_set) { return $this->die_with_404(); } $output = array(); foreach (gp_post('translation', array()) as $original_id => $translations) { $data = compact('original_id'); $data['user_id'] = GP::$user->current()->id; $data['translation_set_id'] = $translation_set->id; foreach (range(0, GP::$translation->get_static('number_of_plural_translations')) as $i) { if (isset($translations[$i])) { $data["translation_{$i}"] = $translations[$i]; } } if ($this->can('approve', 'translation-set', $translation_set->id) || $this->can('write', 'project', $project->id)) { $data['status'] = 'current'; } else { $data['status'] = 'waiting'; } $original = GP::$original->get($original_id); $data['warnings'] = GP::$translation_warnings->check($original->singular, $original->plural, $translations, $locale); $existing_translations = GP::$translation->for_translation($project, $translation_set, 'no-limit', array('original_id' => $original_id, 'status' => 'current_or_waiting'), array()); foreach ($existing_translations as $e) { if (array_pad($translations, $locale->nplurals, null) == $e->translations) { return $this->die_with_error(__('Identical current or waiting translation already exists.'), 200); } } $translation = GP::$translation->create($data); if (!$translation->validate()) { $error_output = '<ul>'; foreach ($translation->errors as $error) { $error_output .= '<li>' . $error . '</li>'; } $error_output .= '</ul>'; $translation->delete(); return $this->die_with_error($error_output, 200); } else { if ('current' == $data['status']) { $translation->set_status('current'); } $translations = GP::$translation->for_translation($project, $translation_set, 'no-limit', array('translation_id' => $translation->id), array()); if ($translations) { $t = $translations[0]; $can_edit = $this->can('edit', 'translation-set', $translation_set->id); $can_write = $this->can('write', 'project', $project->id); $can_approve = $this->can('approve', 'translation-set', $translation_set->id); $output[$original_id] = gp_tmpl_get_output('translation-row', get_defined_vars()); } else { $output[$original_id] = false; } } } echo gp_json_encode($output); }
<?php $meta = array('meta' => array('user_display_name' => $user->display_name, 'user_registered' => $user->user_registered)); foreach ($recent_projects as $project) { $project->set_name = html_entity_decode($project->set_name); unset($project->project_id); unset($project->set_id); } $arr = array_merge($meta, compact('locales', 'recent_projects')); switch (gp_get('filter')) { case 'meta': echo gp_json_encode($meta); break; case 'recent_projects': echo gp_json_encode(compact('recent_projects')); break; default: echo gp_json_encode($arr); break; }
function mass_create_sets_preview_post($project_path) { $project = GP::$project->by_path($project_path); if (!$project) { return $this->die_with_404(); } if ($this->cannot_and_redirect('write', 'project', $project->id)) { return; } $other_project = GP::$project->get(gp_post('project_id')); if (!$other_project) { return $this->die_with_error(__('Project wasn’found')); } header('Content-Type: application/json'); echo gp_json_encode($project->set_difference_from($other_project)); }