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'])); $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 json_encode($output); } exit; }
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'] = get_current_user_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.', 'glotpress'), 200); } } $translation = GP::$translation->create($data); if (!$translation) { return $this->die_with_error(__('Error in saving the translation!', 'glotpress')); } 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 wp_json_encode($output); }
/** * Loads a template. * * @param string $template template name to load * @param array $args Associative array with arguements, which will be exported in the template PHP file * @param bool|string $honor_api If this is true or 'api' and the route is processing an API request * the template name will be suffixed with .api. The actual file loaded will be template.api.php */ function tmpl($template, $args = array(), $honor_api = true) { if ($this->fake_request) { $this->rendered_template = true; $this->loaded_template = $template; } $this->set_notices_and_errors(); if ($this->api && $honor_api !== false && 'no-api' !== $honor_api) { $template = $template . '.api'; $this->header('Content-Type: application/json'); } else { $this->header('Content-Type: text/html; charset=utf-8'); } if ($this->fake_request) { $this->template_output = gp_tmpl_get_output($template, $args, $this->template_path); return true; } return gp_tmpl_load($template, $args, $this->template_path); }
function translations_post($project_path, $locale_slug, $translation_set_slug) { $this->logged_in_or_forbidden(); $project = GP::$project->by_path($project_path); $locale = GP_Locales::by_slug($locale_slug); $translation_set = GP::$translation_set->by_project_id_slug_and_locale($project->id, $translation_set_slug, $locale_slug); if (!$project || !$locale || !$translation_set) { gp_tmpl_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, 3) 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); // TODO: validate $translation = GP::$translation->create($data); if ('current' == $data['status']) { $translation->set_as_current(); } wp_cache_delete($translation_set->id, 'translation_set_status_breakdown'); $translations = GP::$translation->for_translation($project, $translation_set, 'no-limit', array('translation_id' => $translation->id), array()); if ($translations) { $t = $translations[0]; $parity = returner('even'); $can_edit = GP::$user->logged_in(); $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 json_encode($output); }
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; }