Exemple #1
0
 public function save()
 {
     access::verify_csrf();
     if (!identity::active_user()->admin) {
         access::forbidden();
     }
     $locale = Gallery_I18n::instance()->locale();
     $input = Input::instance();
     $key = $input->post("l10n-message-key");
     $root_message = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", "root")->find();
     if (!$root_message->loaded()) {
         throw new Exception("@todo bad request data / illegal state");
     }
     $is_plural = Gallery_I18n::is_plural_message(unserialize($root_message->message));
     $is_empty = true;
     if ($is_plural) {
         $plural_forms = l10n_client::plural_forms($locale);
         $translation = array();
         foreach ($plural_forms as $plural_form) {
             $value = $input->post("l10n-edit-plural-translation-{$plural_form}");
             if (null === $value || !is_string($value)) {
                 throw new Exception("@todo bad request data");
             }
             $translation[$plural_form] = $value;
             $is_empty = $is_empty && empty($value);
         }
     } else {
         $translation = $input->post("l10n-edit-translation");
         $is_empty = empty($translation);
         if (null === $translation || !is_string($translation)) {
             throw new Exception("@todo bad request data");
         }
     }
     $entry = ORM::factory("outgoing_translation")->where("key", "=", $key)->where("locale", "=", $locale)->find();
     if ($is_empty) {
         if ($entry->loaded()) {
             $entry->delete();
         }
     } else {
         if (!$entry->loaded()) {
             $entry->key = $key;
             $entry->locale = $locale;
             $entry->message = $root_message->message;
             $entry->base_revision = null;
         }
         $entry->translation = serialize($translation);
         $entry_from_incoming = ORM::factory("incoming_translation")->where("key", "=", $key)->where("locale", "=", $locale)->find();
         if (!$entry_from_incoming->loaded()) {
             $entry->base_revision = $entry_from_incoming->revision;
         }
         $entry->save();
     }
     Gallery_I18n::clear_cache($locale);
     print json_encode(new stdClass());
 }