summaryrefslogtreecommitdiff
path: root/modules/gallery/controllers/admin_languages.php
blob: 894daedb6702d8f227bc908654cffe2ebcf2bd97 (plain)
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
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2009 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class Admin_Languages_Controller extends Admin_Controller {
  public function index($share_translations_form=null) {
    $v = new Admin_View("admin.html");
    $v->content = new View("admin_languages.html");
                $v->content->available_locales = locales::available();
    $v->content->installed_locales = locales::installed();
    $v->content->default_locale = module::get_var("gallery", "default_locale");
                
    if (empty($share_translations_form)) {
      $share_translations_form = $this->_share_translations_form();
    }
    $v->content->share_translations_form = $share_translations_form;
    $this->_outgoing_translations_count();
    print $v;
  }

  public function save() {
    access::verify_csrf();
                
                locales::update_installed($this->input->post("installed_locales"));
                
                $installed_locales = array_keys(locales::installed());
    $new_default_locale = $this->input->post("default_locale");
                if (!in_array($new_default_locale, $installed_locales)) {
                        if (!empty($installed_locales)) {
                                $new_default_locale = $installed_locales[0];
                        } else {
                                $new_default_locale = "en_US";
                        }
                }
                module::set_var("gallery", "default_locale", $new_default_locale);
                
                print json_encode(array("result" => "success"));
  }

  public function share() {
    access::verify_csrf();

    $form = $this->_share_translations_form();
    if (!$form->validate()) {
      // Show the page with form errors
      return $this->index($form);
    }

    if ($form->sharing->share) {
      l10n_client::submit_translations();
      message::success(t("Translations submitted"));
    } else {
      return $this->_save_api_key($form);
    }
    url::redirect("admin/languages");
  }

  private function _save_api_key($form) {
    $new_key = $form->sharing->api_key->value;
    if ($new_key && !l10n_client::validate_api_key($new_key)) {
      $form->sharing->api_key->add_error("invalid", 1);
      $valid = false;
    } else {
      $valid = true;
    }

    if ($valid) {
        $old_key = l10n_client::api_key();
        l10n_client::api_key($new_key);
        if ($old_key && !$new_key) {
          message::success(t("Your API key has been cleared."));
        } else if ($old_key && $new_key && $old_key != $new_key) {
          message::success(t("Your API key has been changed."));
        } else if (!$old_key && $new_key) {
          message::success(t("Your API key has been saved."));
        }

        log::success(t("gallery"), t("l10n_client API key changed."));
        url::redirect("admin/languages");
    } else {
      // Show the page with form errors
      $this->index($form);
    }
  }

  private function _outgoing_translations_count() {
    return ORM::factory("outgoing_translation")->count_all();
  }

  private function _share_translations_form() {
    $form = new Forge("admin/languages/share", "", "post", array("id" => "gShareTranslationsForm"));
    $group = $form->group("sharing")
      ->label(t("Sharing your own translations with the Gallery community is easy. Please do!"));
    $api_key = l10n_client::api_key();
    $server_link = l10n_client::server_api_key_url();
    $group->input("api_key")
      ->label(empty($api_key)
              ? t("This is a unique key that will allow you to send translations to the remote server. To get your API key go to %server-link.",
                  array("server-link" => html::mark_clean(html::anchor($server_link))))
              : t("API Key"))
      ->value($api_key)
      ->error_messages("invalid", t("The API key you provided is invalid."));
    $group->submit("save")->value(t("Save settings"));
    if ($api_key && $this->_outgoing_translations_count()) {
      // TODO: UI improvement: hide API key / save button when API key is set.
      $group->submit("share")->value(t("Submit translations"));
    }
    return $form;
  }
}