AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / docs / include / classes / Language / Language.class.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14 * Language
15 * Class for accessing information about a single language.
16 * @access       public
17 * @author       Joel Kronenberg
18 * @see          LanguageManager::getLanguage()
19 * @see          LanguageManager::getMyLanguage()
20 * @package      Language
21 */
22 include_once(TR_INCLUDE_PATH.'classes/DAO/LangCodesDAO.class.php');
23
24 class Language {
25         // all private
26         var $code;
27         var $characterSet;
28         var $direction;
29         var $regularExpression;
30         var $nativeName;
31         var $englishName;
32         var $status;
33         var $transformable_version;
34
35         // constructor
36         function Language($language_row) {
37
38                 if (is_array($language_row)) {
39                         $this->code              = $language_row['language_code'];
40                         $this->characterSet      = $language_row['charset'];
41                         $this->regularExpression = $language_row['reg_exp'];
42                         $this->nativeName        = $language_row['native_name'];
43                         $this->englishName       = $language_row['english_name'];
44                         $this->status            = $language_row['status'];
45                         $this->transformable_version    = isset($language_row['version']) ? $language_row['version'] : VERSION;
46
47                         $langCodesDAO = new LangCodesDAO();
48                         $row_langCodes = $langCodesDAO->GetLangCodeBy3LetterCode($this->getParentCode($language_row['language_code']));
49
50                         $this->direction = $row_langCodes['direction'];
51                         
52                 } else if (is_object($language_row)) {
53                         $this->cloneThis($language_row);
54                 }
55         }
56
57         // private
58         // copies the properties from $from to $this Object
59         function cloneThis($from) {
60                 $vars = get_object_vars($from);
61                 foreach ($vars as $key => $value) {
62                         $this->$key = $value;
63                 }
64         }
65
66         // returns whether or not the $search_string matches the regular expression
67         function isMatchHttpAcceptLanguage($search_string) {
68                 return preg_match('/^(' . $this->regularExpression . ')(;q=[0-9]\\.[0-9])?$/i', $search_string);
69         }
70
71         // returns boolean whether or not $search_string is in HTTP_USER_AGENT
72         function isMatchHttpUserAgent($search_string) {
73                 return preg_match('/(\(|\[|;[\s])(' . $this->regularExpression . ')(;|\]|\))/', $search_string);
74
75         }
76
77         function getCode() {
78                 return $this->code;
79         }
80
81         function getCharacterSet() {
82                 return $this->characterSet;
83         }
84
85         function getDirection() {
86                 return $this->direction;
87         }
88
89         function getRegularExpression() {
90                 return $this->regularExpression;
91         }
92
93         function getTransformableVersion() {
94                 return $this->transformable_version;
95         }
96
97         function getTranslatedName() {
98                 if ($this->code == $_SESSION['lang']) {
99                         return $this->nativeName;
100                 }
101                 // this code has to be translated:
102                 return _AT('lang_' . str_replace('-', '_', $this->code));
103         }
104
105         function getNativeName() {
106                 return $this->nativeName;
107         }
108
109         function getEnglishName() {
110                 return $this->englishName;
111         }
112
113         function getStatus() {
114                 return $this->status;
115         }
116
117
118         // public
119         function sendContentTypeHeader() {
120                 header('Content-Type: text/html; charset=' . $this->characterSet);
121         }
122
123         // public
124         function saveToSession() {
125                 $_SESSION['lang'] = $this->code;
126         }
127
128         /* 
129          * public
130          * @param       member_id or login for members and admin respectively
131          * @param       1 for admin, 0 for members, all other integers are ignored. 
132          */
133         function saveToPreferences($id, $is_admin) {
134                 global $db;
135                 if ($id) {
136                         if ($is_admin === 0) {
137                                 $sql = "UPDATE ".TABLE_PREFIX."members SET language='".$this->code."', creation_date=creation_date, last_login=last_login WHERE member_id=$id";
138                         } elseif ($is_admin === 1) {
139                                 $sql = "UPDATE ".TABLE_PREFIX."admins SET language='".$this->code."', last_login=last_login WHERE login='$id'";
140                         }
141                         mysql_query($sql,$db);
142                 }
143         }
144
145         // public
146         // returns whether or not this language is right-to-left
147         // possible langues are: arabic, farsi, hebrew, urdo
148         function isRTL() {
149                 if ($this->direction == 'rtl') {
150                         return true;
151                 }
152
153                 return false;
154         }
155
156         function getParentCode($code = '') {
157                 if (!$code && isset($this)) {
158                         $code = $this->code;
159                 }
160                 $peices = explode(TR_LANGUAGE_LOCALE_SEP, $code, 2);
161                 return $peices[0];
162         }
163
164         // public
165         // can be called staticly
166         function getLocale($code = '') {
167                 if (!$code && isset($this)) {
168                         $code = $this->code;
169                 }
170                 $peices = explode(TR_LANGUAGE_LOCALE_SEP, $code, 2);
171                 return $peices[1];
172         }
173         
174         function getXML($part=FALSE) {
175                 if (!$part) {
176                         $xml = '<?xml version="1.0" encoding="iso-8859-1"?>
177                         <!-- This is an AContent  language pack -->
178
179                         <!DOCTYPE language [
180                            <!ELEMENT transformable-version (#PCDATA)>
181                            <!ELEMENT charset (#PCDATA)>
182                            <!ELEMENT reg-exp (#PCDATA)>
183                            <!ELEMENT native-name (#PCDATA)>
184                            <!ELEMENT english-name (#PCDATA)>
185                            <!ELEMENT status (#PCDATA)>
186
187                            <!ATTLIST language code ID #REQUIRED>
188                         ]>';
189                 } 
190
191                 $xml .= '<language code="'.$this->code.'">
192                         <transformable-version>'.VERSION.'</transformable-version>
193                         <charset>'.$this->characterSet.'</charset>
194                         <reg-exp>'.$this->regularExpression.'</reg-exp>
195                         <native-name>'.$this->nativeName.'</native-name>
196                         <english-name>'.$this->englishName.'</english-name>
197                         <status>'.$this->status.'</status>
198                 </language>';
199
200                 return $xml;
201         }
202 }
203 ?>