AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / classes / DAO / LanguagesDAO.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 * DAO for "config" table
15 * @access       public
16 * @author       Cindy Qi Li
17 * @package      DAO
18 */
19
20 if (!defined('TR_INCLUDE_PATH')) exit;
21
22 require_once(TR_INCLUDE_PATH. 'classes/DAO/DAO.class.php');
23
24 class LanguagesDAO extends DAO {
25
26         /**
27         * Insert table languages
28         * @access  public
29         * @param   $langCode, $charset, $regExp, $nativeName, $englishName, $status
30         * @return  true / false
31         * @author  Cindy Qi Li
32         */
33         function Create($langCode, $charset, $regExp, $nativeName, $englishName, $status)
34         {
35                 global $languageManager, $msg;
36                 
37                 // check if the required fields are filled
38                 if (!$this->ValidateFields($langCode, $charset, $nativeName, $englishName)) return false;
39                 
40                 // check if the language already exists
41                 if ($languageManager->exists($langCode)) $msg->addError('LANG_EXISTS');
42                 
43                 if (!$msg->containsErrors())
44                 {
45                         $sql = "INSERT INTO ".TABLE_PREFIX."languages (language_code, charset, reg_exp, native_name, english_name, status) 
46                                 VALUES ('".$langCode."', '".$charset."', '".$regExp."', '".$nativeName."', '".$englishName."', ".$status.")";
47                         return $this->execute($sql);
48                 }
49         }
50
51         /**
52         * Update a row
53         * @access  public
54         * @param   $langCode: required
55         *          $charset: required
56         * @return  true / false
57         * @author  Cindy Qi Li
58         */
59         function Update($langCode, $charset, $regExp, $nativeName, $englishName, $status)
60         {
61                 // check if the required fields are filled
62                 if (!$this->ValidateFields($langCode, $charset, $nativeName, $englishName)) return false;
63                 
64                 $sql = "UPDATE ".TABLE_PREFIX."languages 
65                            SET reg_exp='".$regExp."',
66                                native_name = '".$nativeName."',
67                                english_name = '".$englishName."',
68                                status = ".$status."
69                          WHERE language_code = '".$langCode."'
70                            AND charset = '".$charset."'";
71                 return $this->execute($sql);
72         }
73
74         /**
75         * Update the given field with the given value by language_code and charset
76         * @access  public
77         * @param   $langCode
78         *          $fieldName
79         *          $fieldValue
80         * @return  true / false
81         * @author  Cindy Qi Li
82         */
83         function UpdateField($langCode, $fieldName, $fieldValue)
84         {
85                 global $addslashes;
86                 
87                 // check if the required fields are filled
88                 if ($fieldValue == '') return false;
89                 
90                 $sql = "UPDATE ".TABLE_PREFIX."languages 
91                            SET ".$addslashes($fieldName)."='".$addslashes($fieldValue)."'
92                          WHERE language_code = '".$addslashes($langCode)."'";
93                 return $this->execute($sql);
94         }
95
96         /**
97         * Delete a row
98         * @access  public
99         * @param   $langCode
100         *          $charset
101         * @return  true / false
102         * @author  Cindy Qi Li
103         */
104         function Delete($langCode)
105         {
106                 $sql = "DELETE FROM ".TABLE_PREFIX."languages 
107                          WHERE language_code = '".$langCode."'";
108                 if (!$this->execute($sql)) return false;
109
110                 $sql = "DELETE FROM ".TABLE_PREFIX."language_text 
111                      WHERE language_code = '".$langCode."'";
112                 
113                 return $this->execute($sql);
114         }
115
116         /**
117         * Return all languages
118         * @access  public
119         * @param   none
120         * @return  table rows
121         * @author  Cindy Qi Li
122         */
123         function getAll()
124         {
125             $sql = "SELECT * FROM ".TABLE_PREFIX."languages l
126                      ORDER BY l.native_name";
127             return $this->execute($sql);
128         }
129
130         /**
131         * Return all enabled languages
132         * @access  public
133         * @param   none
134         * @return  table rows
135         * @author  Cindy Qi Li
136         */
137         function getAllEnabled()
138         {
139             $sql = "SELECT * FROM ".TABLE_PREFIX."languages l
140                      WHERE status = ".TR_STATUS_ENABLED."
141                      ORDER BY l.native_name";
142             return $this->execute($sql);
143         }
144
145         /**
146         * Return language with given language code
147         * @access  public
148         * @param   $langCode
149         *          $charset
150         * @return  table rows
151         * @author  Cindy Qi Li
152         */
153         function getByLangCodeAndCharset($langCode, $charset)
154         {
155             $sql = "SELECT * FROM ".TABLE_PREFIX."languages l
156                      WHERE l.language_code = '".$langCode."'
157                        AND l.charset='".$charset."'
158                      ORDER BY l.native_name";
159
160                 if ($rows = $this->execute($sql))
161                 {
162                         return $rows[0];
163                 }
164         }
165
166         /**
167         * Return all languages except the ones with language code in the given string 
168         * @access  public
169         * @param   $langCode : one language codes, for example: en
170         * @return  table rows
171         * @author  Cindy Qi Li
172         */
173         function getAllExceptLangCode($langCode)
174         {
175                 if (trim($langCode) == '')
176                         return $this->getAll();
177                 else
178                 {
179                 $sql = "SELECT * FROM ".TABLE_PREFIX."languages
180                                          WHERE language_code <> '".$langCode."'
181                                  ORDER BY native_name";
182                     return $this->execute($sql);
183                 }
184         }
185         
186         /**
187         * Return all languages except the ones with language code in the given string 
188         * @access  public
189         * @param   $langCode : one language codes, for example: en
190         * @return  table rows
191         * @author  Cindy Qi Li
192         */
193         function ValidateFields($langCode, $charset, $nativeName, $englishName)
194         {
195                 global $msg;
196                 
197                 $missing_fields = array();
198
199                 if ($langCode == '') {
200                         $missing_fields[] = _AT('lang_code');
201                 }
202                 if ($charset == '') {
203                         $missing_fields[] = _AT('charset');
204                 }
205                 if ($nativeName == '') {
206                         $missing_fields[] = _AT('name_in_language');
207                 }
208                 if ($englishName == '') {
209                         $missing_fields[] = _AT('name_in_english');
210                 }
211
212                 if ($missing_fields) {
213                         $missing_fields = implode(', ', $missing_fields);
214                         $msg->addError(array('EMPTY_FIELDS', $missing_fields));
215                         return false;
216                 }
217                 return true;
218         }
219 }
220 ?>