| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /********************************************************************\ |
|---|
| 4 | * This program is free software; you can redistribute it and/or * |
|---|
| 5 | * modify it under the terms of the GNU General Public License as * |
|---|
| 6 | * published by the Free Software Foundation; either version 2 of * |
|---|
| 7 | * the License, or (at your option) any later version. * |
|---|
| 8 | * * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, * |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 12 | * GNU General Public License for more details. * |
|---|
| 13 | * * |
|---|
| 14 | * You should have received a copy of the GNU General Public License* |
|---|
| 15 | * along with this program; if not, contact: * |
|---|
| 16 | * * |
|---|
| 17 | * Free Software Foundation Voice: +1-617-542-5942 * |
|---|
| 18 | * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
|---|
| 19 | * Boston, MA 02111-1307, USA gnu@gnu.org * |
|---|
| 20 | * * |
|---|
| 21 | \********************************************************************/ |
|---|
| 22 | /**@file Langstring.php |
|---|
| 23 | * @author Copyright (C) 2004-2005 Benoit Grégoire, Technologies Coeus inc. |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | require_once BASEPATH.'classes/FormSelectGenerator.php'; |
|---|
| 27 | require_once BASEPATH.'classes/Content.php'; |
|---|
| 28 | require_once BASEPATH.'classes/LocaleList.php'; |
|---|
| 29 | require_once BASEPATH.'classes/Locale.php'; |
|---|
| 30 | |
|---|
| 31 | error_reporting(E_ALL); |
|---|
| 32 | |
|---|
| 33 | /** Représente un Langstring en particulier, ne créez pas un objet langstrings si vous n'en avez pas spécifiquement besoin |
|---|
| 34 | */ |
|---|
| 35 | class Langstring extends Content |
|---|
| 36 | { |
|---|
| 37 | const ALLOWED_HTML_TAGS = "<a><br><b><h1><h2><h3><h4><i><img><li><ol><p><strong><u><ul><li>"; |
|---|
| 38 | |
|---|
| 39 | /**Constructeur |
|---|
| 40 | @param $content_id Content id |
|---|
| 41 | */ |
|---|
| 42 | function __construct($content_id) |
|---|
| 43 | { |
|---|
| 44 | parent :: __construct($content_id); |
|---|
| 45 | global $db; |
|---|
| 46 | $this->mBd = & $db; |
|---|
| 47 | |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /**Retourne la première chaîne disponible dans la langue par défaut de l'usager (si disponible), sinon dans la même langue majeure, sinon la première chaîne disponible |
|---|
| 51 | * @return string Chaîne UTF-8 retournée |
|---|
| 52 | */ |
|---|
| 53 | function getString() |
|---|
| 54 | { |
|---|
| 55 | $retval = null; |
|---|
| 56 | //Get user's prefered language |
|---|
| 57 | |
|---|
| 58 | $sql = "SELECT value, locales_id, \n"; |
|---|
| 59 | $sql .= Locale :: getSqlCaseStringSelect(Locale::getCurrentLocale()->getId()); |
|---|
| 60 | $sql .= " as score FROM langstring_entries WHERE langstring_entries.langstrings_id = '{$this->id}' AND value!='' ORDER BY score LIMIT 1"; |
|---|
| 61 | $this->mBd->ExecSqlUniqueRes($sql, $row, false); |
|---|
| 62 | if ($row == null) |
|---|
| 63 | { |
|---|
| 64 | $retval = "(Langstring vide)"; |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | $retval = $row['value']; |
|---|
| 69 | } |
|---|
| 70 | return $retval; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /**Ajoute une chaîne de caractère au Langstring |
|---|
| 74 | * @param $string La chaîne de caractère à ajouter. Si la chaîne est vide ('') ou null, la fonction retourne sans toucher à la base de donnée |
|---|
| 75 | * @param $locale La langue régionale de la chaîne ajoutée, exemple: 'fr_CA', peut être NULL |
|---|
| 76 | * @return bollean, true si une chaîne a été ajoutée à la base de donnée, false autrement. |
|---|
| 77 | */ |
|---|
| 78 | function addString($string, $locale, $allow_empty_string = false) |
|---|
| 79 | { |
|---|
| 80 | $retval = false; |
|---|
| 81 | $id = 'NULL'; |
|---|
| 82 | if ($locale) |
|---|
| 83 | { |
|---|
| 84 | $language = new Locale($locale); |
|---|
| 85 | $id = "'".$language->GetId()."'"; |
|---|
| 86 | } |
|---|
| 87 | if ($allow_empty_string || ($string != null && $string != '')) |
|---|
| 88 | { |
|---|
| 89 | $string = $this->mBd->EscapeString($string); |
|---|
| 90 | $this->mBd->ExecSqlUpdate("INSERT INTO langstring_entries (langstring_entries_id, langstrings_id, locales_id, value) VALUES ('".get_guid()."', '$this->id', $id , '$string')", FALSE); |
|---|
| 91 | $retval = true; |
|---|
| 92 | } |
|---|
| 93 | return $retval; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** Updates the string associated with the locale |
|---|
| 97 | * @param $string La chaîne de caractère à ajouter. Si la chaîne est vide ('') ou null, la fonction retourne sans toucher à la base de donnée |
|---|
| 98 | * @param $locale La langue régionale de la chaîne ajoutée, exemple: 'fr_CA', peut être NULL |
|---|
| 99 | * @return bollean, true si une chaîne a été ajoutée à la base de donnée, false autrement. |
|---|
| 100 | */ |
|---|
| 101 | function UpdateString($string, $locale) |
|---|
| 102 | { |
|---|
| 103 | $retval = false; |
|---|
| 104 | $id = 'NULL'; |
|---|
| 105 | if ($locale) |
|---|
| 106 | { |
|---|
| 107 | $language = new Locale($locale); |
|---|
| 108 | $id = "'".$language->GetId()."'"; |
|---|
| 109 | } |
|---|
| 110 | if ($string != null && $string != '') |
|---|
| 111 | { |
|---|
| 112 | $string = $this->mBd->EscapeString($string); |
|---|
| 113 | // If the update returns 0 ( no update ), try inserting the record |
|---|
| 114 | $this->mBd->ExecSqlUniqueRes("SELECT * FROM langstring_entries WHERE locales_id = $id AND langstrings_id = '$this->id'", $row, false); |
|---|
| 115 | if ($row != null) |
|---|
| 116 | $this->mBd->ExecSqlUpdate("UPDATE langstring_entries SET value = '$string' WHERE langstrings_id = '$this->id' AND locales_id = $id", false); |
|---|
| 117 | else |
|---|
| 118 | $this->addString($string, $locale); |
|---|
| 119 | $retval = true; |
|---|
| 120 | } |
|---|
| 121 | return $retval; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /**Affiche l'interface d'administration de l'objet |
|---|
| 125 | @param type_interface SIMPLE pour éditer un seul champ, COMPLETE pour voir toutes les chaînes, LARGE pour avoir un textarea. |
|---|
| 126 | @param num_nouveau Nombre de champ à afficher pour entrer de nouvelles chaîne en une seule opération |
|---|
| 127 | */ |
|---|
| 128 | function getAdminUI($type_interface = 'LARGE', $num_nouveau = 1) |
|---|
| 129 | { |
|---|
| 130 | $html = ''; |
|---|
| 131 | $html .= "<div class='admin_class'>Langstring (".get_class($this)." instance)</div>\n"; |
|---|
| 132 | $html .= "<div class='admin_section_container'>\n"; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | $html .= _("Only these HTML tags are allowed : ").htmlentities(self::ALLOWED_HTML_TAGS); |
|---|
| 137 | $liste_languages = new LocaleList(); |
|---|
| 138 | $sql = "SELECT * FROM langstring_entries WHERE langstring_entries.langstrings_id = '$this->id' ORDER BY locales_id"; |
|---|
| 139 | $this->mBd->ExecSql($sql, $result, FALSE); //echo "type_interface: $type_interface\n"; |
|---|
| 140 | |
|---|
| 141 | /* if ($type_interface == 'COMPLETE') |
|---|
| 142 | { |
|---|
| 143 | $html .= "<TR>\n"; |
|---|
| 144 | $html .= "<TH>Language</TH>\n"; |
|---|
| 145 | $html .= "<TH>Chaîne</TH>\n"; |
|---|
| 146 | $html .= "<TH>Effacer sous-chaîne #</TH>\n"; |
|---|
| 147 | $html .= "</TR>\n"; |
|---|
| 148 | }*/ |
|---|
| 149 | $html .= "<ul class='admin_section_list'>\n"; |
|---|
| 150 | if ($result != null) |
|---|
| 151 | { |
|---|
| 152 | while (list ($key, $value) = each($result)) |
|---|
| 153 | { |
|---|
| 154 | $html .= "<li class='admin_section_list_item'>\n"; |
|---|
| 155 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 156 | $html .= $liste_languages->GenererFormSelect("$value[locales_id]", "langstrings_".$this->id."_substring_$value[langstring_entries_id]_language", 'Langstring::AfficherInterfaceAdmin', TRUE); |
|---|
| 157 | if ($type_interface == 'LARGE') |
|---|
| 158 | { |
|---|
| 159 | $html .= "<textarea name='langstrings_".$this->id."_substring_$value[langstring_entries_id]_string' cols='60' rows='3'>".htmlspecialchars($value['value'], ENT_QUOTES, 'UTF-8')."</textarea>\n"; |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | $html .= "<input type='text' name='langstrings_".$this->id."_substring_$value[langstring_entries_id]_string' size='44' value='".htmlspecialchars($value['value'], ENT_QUOTES, 'UTF-8')."'>\n"; |
|---|
| 164 | } |
|---|
| 165 | $html .= "</div>\n"; |
|---|
| 166 | $html .= "<div class='admin_section_tools'>\n"; |
|---|
| 167 | $name = "langstrings_".$this->id."_substring_$value[langstring_entries_id]_erase"; |
|---|
| 168 | $html .= "<input type='submit' name='$name' value='"._("Delete string")."'>"; |
|---|
| 169 | $html .= "</div>\n"; |
|---|
| 170 | $html .= "</li>\n"; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | //Nouvelles chaîne |
|---|
| 175 | $locale = LocaleList::GetDefault(); |
|---|
| 176 | $html .= "<li class='admin_section_list_item'>\n"; |
|---|
| 177 | $html .= "<div class='admin_section_data'>\n"; |
|---|
| 178 | |
|---|
| 179 | $html .= $liste_languages->GenererFormSelect($locale, "langstrings_".$this->id."_substring_new_language", 'Langstring::AfficherInterfaceAdmin', TRUE); |
|---|
| 180 | $new_substring_name = "langstrings_".$this->id."_substring_new_string"; |
|---|
| 181 | if ($type_interface == 'LARGE') |
|---|
| 182 | { |
|---|
| 183 | $html .= "<textarea name='$new_substring_name' cols='60' rows='3'></textarea>\n"; |
|---|
| 184 | } |
|---|
| 185 | else |
|---|
| 186 | { |
|---|
| 187 | $html .= "<input type='text' name='$new_substring_name' size='44' value=''>\n"; |
|---|
| 188 | } |
|---|
| 189 | $html .= "</div>\n"; |
|---|
| 190 | $html .= "<div class='admin_section_tools'>\n"; |
|---|
| 191 | |
|---|
| 192 | $new_substring_submit_name = "langstrings_".$this->id."_add_new_entry"; |
|---|
| 193 | $html .= "<input type='submit' name='$new_substring_submit_name' value='"._("Add new string")."'>"; |
|---|
| 194 | $html .= "</div>\n"; |
|---|
| 195 | $html .= "</li>\n"; |
|---|
| 196 | |
|---|
| 197 | $html .= "</ul>\n"; |
|---|
| 198 | $html .= "</div>\n"; |
|---|
| 199 | return parent :: getAdminUI($html); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | function processAdminUI() |
|---|
| 203 | { |
|---|
| 204 | if ($this->isOwner(User :: getCurrentUser()) || User :: getCurrentUser()->isSuperAdmin()) |
|---|
| 205 | { |
|---|
| 206 | parent :: processAdminUI(); |
|---|
| 207 | $generateur_form_select = new FormSelectGenerator(); |
|---|
| 208 | $sql = "SELECT * FROM langstring_entries WHERE langstring_entries.langstrings_id = '$this->id'"; |
|---|
| 209 | $this->mBd->ExecSql($sql, $result, FALSE); |
|---|
| 210 | if ($result != null) |
|---|
| 211 | { |
|---|
| 212 | while (list ($key, $value) = each($result)) |
|---|
| 213 | { // print_r($value); |
|---|
| 214 | if (!empty ($_REQUEST["langstrings_".$this->id."_substring_$value[langstring_entries_id]_erase"]) && $_REQUEST["langstrings_".$this->id."_substring_$value[langstring_entries_id]_erase"] == true) |
|---|
| 215 | { |
|---|
| 216 | $this->mBd->ExecSqlUpdate("DELETE FROM langstring_entries WHERE langstrings_id = '$this->id' AND langstring_entries_id='$value[langstring_entries_id]'", FALSE); |
|---|
| 217 | } |
|---|
| 218 | else |
|---|
| 219 | { |
|---|
| 220 | $language = $generateur_form_select->getResult("langstrings_".$this->id."_substring_$value[langstring_entries_id]_language", 'Langstring::AfficherInterfaceAdmin'); |
|---|
| 221 | if (empty ($language)) |
|---|
| 222 | { |
|---|
| 223 | $language = 'NULL'; |
|---|
| 224 | } |
|---|
| 225 | else |
|---|
| 226 | { |
|---|
| 227 | $language = "'".$language."'"; |
|---|
| 228 | } |
|---|
| 229 | // Strip HTML tags ! |
|---|
| 230 | $string = $_REQUEST["langstrings_".$this->id."_substring_$value[langstring_entries_id]_string"]; |
|---|
| 231 | $string = $this->mBd->EscapeString(strip_tags($string, self::ALLOWED_HTML_TAGS)); |
|---|
| 232 | $this->mBd->ExecSqlUpdate("UPDATE langstring_entries SET locales_id = $language , value = '$string' WHERE langstrings_id = '$this->id' AND langstring_entries_id='$value[langstring_entries_id]'", FALSE); |
|---|
| 233 | //$this->UpdateString($string, $language); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | //Ajouter nouvelles chaîne(s) si champ non vide ou si l'usager a appuyé sur le bouton ajouter |
|---|
| 238 | $new_substring_name = "langstrings_".$this->id."_substring_new_string"; |
|---|
| 239 | $new_substring_submit_name = "langstrings_".$this->id."_add_new_entry"; |
|---|
| 240 | if ((isset ($_REQUEST[$new_substring_submit_name]) && $_REQUEST[$new_substring_submit_name] == true) || !empty ($_REQUEST[$new_substring_name])) |
|---|
| 241 | { |
|---|
| 242 | |
|---|
| 243 | $language = $generateur_form_select->getResult("langstrings_".$this->id."_substring_new_language", 'Langstring::AfficherInterfaceAdmin'); |
|---|
| 244 | if (empty ($language)) |
|---|
| 245 | { |
|---|
| 246 | $language = null; |
|---|
| 247 | } |
|---|
| 248 | $this->addString($_REQUEST[$new_substring_name], $language, true); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | /**Affiche l'interface usager de l'objet |
|---|
| 254 | */ |
|---|
| 255 | |
|---|
| 256 | /** Retreives the user interface of this object. Anything that overrides this method should call the parent method with it's output at the END of processing. |
|---|
| 257 | * @param $subclass_admin_interface Html content of the interface element of a children |
|---|
| 258 | * @return The HTML fragment for this interface */ |
|---|
| 259 | public function getUserUI($subclass_user_interface = null) |
|---|
| 260 | { |
|---|
| 261 | $html = ''; |
|---|
| 262 | $html .= "<div class='user_ui_container'>\n"; |
|---|
| 263 | $html .= "<div class='user_ui_object_class'>Langstring (".get_class($this)." instance)</div>\n"; |
|---|
| 264 | $html .= "<div class='langstring'>\n"; |
|---|
| 265 | $html .= $this->getString(); |
|---|
| 266 | $html .= $subclass_user_interface; |
|---|
| 267 | $html .= "</div>\n"; |
|---|
| 268 | $html .= "</div>\n"; |
|---|
| 269 | return parent :: getUserUI($html); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /**Retourne le nombre de sous-chaînes du langstring |
|---|
| 273 | @return Le nombre de sous-chaine. 0 signifie que la chaîne est vide |
|---|
| 274 | */ |
|---|
| 275 | function GetNumStrings() |
|---|
| 276 | { |
|---|
| 277 | $sql = "SELECT count(langstring_entries_id) FROM langstring_entries WHERE langstring_entries.langstrings_id = '$this->id'"; |
|---|
| 278 | $this->mBd->ExecSqlResUnique($sql, $row, false); |
|---|
| 279 | return $row['count']; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | } /* end class Langstring */ |
|---|
| 283 | ?> |
|---|