Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 250 lines (235 sloc) 10.173 kb
acbb58a @quipo version 1.2.5
quipo authored
1 <?php
2 /**
3 * Additional functions for Translation class
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
4 *
5 * Functions allowing user to create the new language, translation for specific
6 * strings and full management of the languages database.
7 *
8 * @author Wojciech Zieliñski <voyteck@caffe.com.pl>
9 * @author Lorenzo Alberton <l dot alberton at quipo dot it>
10 * @version 1.3
acbb58a @quipo version 1.2.5
quipo authored
11 * @access public
12 * @package Translation
13 */
14
15 require_once 'DB.php';
16
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
17
acbb58a @quipo version 1.2.5
quipo authored
18 /**
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
19 * helper method
20 */
21 function setDefaultTableDefinitions($LangID, $LangName, $CustomTables)
22 {
23 //set defaults
24 $TableDefinitions = array(
25 'langsavail' => array(
26 'name' => 'tr_langsavail',
27 'lang_id' => 'lang_id',
28 'lang_name' => 'name',
29 'metatags' => 'metatags',
30 'errortext' => 'errortext'
31 ),
32 'strings_'.$LangID => array(
33 'name' => 'strings_'.$LangID,
34 'page_id' => 'page_id',
35 'string_id' => 'string_id',
36 'string' => 'string'
37 )
38 );
39 if (is_array($CustomTables['langsavail'])) {
40 $TableDefinitions['langsavail']['name'] = isset($CustomTables['langsavail']['name']) ? $CustomTables['langsavail']['name'] : 'tr_langsavail';
41 $TableDefinitions['langsavail']['lang_id'] = isset($CustomTables['langsavail']['lang_id']) ? $CustomTables['langsavail']['lang_id'] : 'lang_id';
42 $TableDefinitions['langsavail']['lang_name'] = isset($CustomTables['langsavail']['lang_name']) ? $CustomTables['langsavail']['lang_name'] : 'lang_name';
43 $TableDefinitions['langsavail']['metatags'] = isset($CustomTables['langsavail']['metatags']) ? $CustomTables['langsavail']['metatags'] : 'metatags';
44 $TableDefinitions['langsavail']['errortext'] = isset($CustomTables['langsavail']['errortext']) ? $CustomTables['langsavail']['errortext'] : 'errortext';
45 } elseif (!empty($CustomTables['langsavail'])) {
46 $TableDefinitions['langsavail']['name'] = $CustomTables['langsavail'];
47 }
48
49 if (is_array($CustomTables['strings_'.$LangID])) {
50 $TableDefinitions['strings_'.$LangID]['name'] = isset($CustomTables['strings_'.$LangID]['name']) ? $CustomTables['strings_'.$LangID]['name'] : 'strings_'.$LangID;
51 $TableDefinitions['strings_'.$LangID]['page_id'] = isset($CustomTables['strings_'.$LangID]['page_id']) ? $CustomTables['strings_'.$LangID]['page_id'] : 'page_id';
52 $TableDefinitions['strings_'.$LangID]['string_id'] = isset($CustomTables['strings_'.$LangID]['string_id']) ? $CustomTables['strings_'.$LangID]['string_id'] : 'string_id';
53 $TableDefinitions['strings_'.$LangID]['string'] = isset($CustomTables['strings_'.$LangID]['string']) ? $CustomTables['strings_'.$LangID]['string'] : 'string';
54 } elseif (!empty($CustomTables['strings_'.$LangID])) {
55 $TableDefinitions['strings_'.$LangID]['name'] = $CustomTables['strings_'.$LangID];
56 }
57 return $TableDefinitions;
58 }
59
60
61 /**
62 * New language creation
63 *
64 * Creates new language in the system.
65 * Creates lang entry in the languages table and the table for language strings.
66 * If other langs have been created before and their tables were filled with
67 * strings, function addTranslation should be executed for each of the added
68 * strings just after calling this function and before using the Translation class
69 * for any purpose.
70 *
71 * @param string $LangID Language identifier
72 * @param string $LangName Language name - store the language name of the lang,
73 * possibly in the language described. This name can be later
74 * retrieved by calling getLangName and getOtherLangs methods
75 * and used for hyperlinks changing the site language.
76 * @param string $METATags Tags that may describe the language codepage etc.
77 * These tags can be retrieved by calling getMetaTags method.
78 * @param string $pear_DSN PEAR DSN string for database connection
79 * @param array $CustomTables Custom table definitions
80 * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
81 */
82 function createNewLang($LangID, $LangName, $METATags, $pear_DSN, $CustomTables=0)
83 {
84 $TableDefinitions = setDefaultTableDefinitions($LangID, $LangName, $CustomTables);
85
acbb58a @quipo version 1.2.5
quipo authored
86 $db = DB::connect($pear_DSN);
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
87 if (DB::isError($db)) {
acbb58a @quipo version 1.2.5
quipo authored
88 return $db;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
89 }
90 $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)',
91 $TableDefinitions['langsavail']['name'],
92 $TableDefinitions['langsavail']['lang_id'],
93 $TableDefinitions['langsavail']['lang_name'],
94 $TableDefinitions['langsavail']['lang_metatags'],
95 addslashes($LangID),
96 addslashes($LangName),
97 addslashes($METATags)
98 );
99 $result = $db->query($query);
acbb58a @quipo version 1.2.5
quipo authored
100 if (DB::isError($result)) {
101 return $result;
102 }
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
103
104 //check if the table already exists
105 $exists = false;
106 $res = $db->query('SHOW TABLES');
107 if (DB::isError($res)) {
108 return $res;
109 }
110 while ($row = $res->fetchRow()) {
111 if ($row[0] == 'tr_strings_'.$LangID) {
112 $exists = true;
113 break;
114 }
115 }
116 if (!$exists) {
117 $query = 'CREATE TABLE '. $TableDefinitions['strings_'.$LangID]['name'] .'('
118 . $TableDefinitions['strings_'.$LangID]['page_id'] .' VARCHAR(16) default NULL, '
119 . $TableDefinitions['strings_'.$LangID]['string_id'].' VARCHAR(32) NOT NULL, '
120 . $TableDefinitions['strings_'.$LangID]['string'] .' TEXT, '
121 .'UNIQUE KEY page_id ('.$TableDefinitions['strings_'.$LangID]['page_id'].', '
122 .$TableDefinitions['strings_'.$LangID]['string_id'].'))';
123 $result = $db->query($query);
124 if (DB::isError($result)) {
125 $query = sprintf('DELETE FROM %s WHERE %s=%s',
126 $TableDefinitions['langsavail']['name'],
127 $TableDefinitions['langsavail']['lang_id'],
128 addslashes($LangID)
129 );
130 $delresult = $db->query($query);
131 return $result;
132 }
133 }
acbb58a @quipo version 1.2.5
quipo authored
134 return 1;
135 }
136
137 /**
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
138 * Language removal
139 *
140 * Removes language from system.
141 * This function should be used carefully - it will permanently remove all the
142 * strings that has been added to the language table by dropping this table.
143 * If other langs are stored in the table, then only this lang column will be dropped.
144 *
145 * @param string $LangID Language identifier
146 * @param string $pear_DSN PEAR DSN string for database connection
147 * @param array $CustomTables Custom table definitions
148 * @param boolean $force If true, the table is dropped without checks
149 * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
150 */
151 function removeLang($LangID, $pear_DSN, $CustomTables=0, $force=false)
152 {
acbb58a @quipo version 1.2.5
quipo authored
153 $db = DB::connect($pear_DSN);
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
154 if (DB::isError($db)) {
acbb58a @quipo version 1.2.5
quipo authored
155 return $db;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
156 }
157
158 $TableDefinitions = setDefaultTableDefinitions($LangID, $LangName, $CustomTables);
159
160 if (!$force) {
161 //check if other langs are stored in this table
162 //'DESCRIBE' == 'SHOW COLUMNS FROM'
163 $res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']);
164 if (DB::isError($res)) {
165 return $res;
166 }
167 if ($res->numRows() > 3) {
168 $query = 'ALTER TABLE '. $TableDefinitions['strings_'.$LangID]['name']
169 .' DROP COLUMN '. $TableDefinitions['strings_'.$LangID]['string'];
170 $res = $db->query('DESCRIBE '.$TableDefinitions['strings_'.$LangID]['name']);
171 if (DB::isError($res)) {
172 return $res;
173 }
174 return 1;
175 }
176 }
177
178 $result = $db->query('DROP TABLE '.$TableDefinitions['strings_'.$LangID]['name']);
179 if (DB::isError($result)) {
acbb58a @quipo version 1.2.5
quipo authored
180 return $result;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
181 }
182 $query = sprintf('DELETE FROM %s WHERE %s=%s',
183 $TableDefinitions['langsavail']['name'],
184 $TableDefinitions['langsavail']['lang_id'],
185 $LangID);
186 $result = $db->query($query);
187 if (DB::isError($result)) {
acbb58a @quipo version 1.2.5
quipo authored
188 return $result;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
189 }
acbb58a @quipo version 1.2.5
quipo authored
190 return 1;
191 }
192
193 /**
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
194 * Translation adding
195 * Adds string to one or more language tables.
196 *
197 * @param string $PageID page identifier. Might be "" if the string is to be
198 * available from any page, independendly from translation
199 * object creation parameters.
200 * @param string $StringID string identifier. Must be unique for the same PageID
201 * and strings that were created without PageID's.
202 * This rule must be respected to prevent ambiguities.
203 * @param array $String array of strings - the array keys should be languages id's,
204 * the values - the sttrings in these languages - e.g.:
205 * ("en"->"English text", "pl"->"Tekst polski", ...)
206 * @param string $pear_DSN PEAR DSN string for database connection
207 * @return mixed Return 1 if everything went OK, a PEAR::DB_Error object if not.
208 */
209 function addTranslation($PageID, $StringID, $String, $pear_DSN)
210 {
acbb58a @quipo version 1.2.5
quipo authored
211 $db = DB::connect($pear_DSN);
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
212 if (DB::isError($db)) {
acbb58a @quipo version 1.2.5
quipo authored
213 return $db;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
214 }
215 foreach ($String as $LangID => $Text) {
acbb58a @quipo version 1.2.5
quipo authored
216 $data[] = array("tr_strings_$LangID", $Text);
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
217 }
acbb58a @quipo version 1.2.5
quipo authored
218 $result = $db->executeMultiple(($db->prepare("INSERT INTO ! (page_id, string_id, string) VALUES ('$PageID', '$StringID', ?)")), $data);
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
219 if (DB::isError($result)) {
acbb58a @quipo version 1.2.5
quipo authored
220 return $result;
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
221 }
acbb58a @quipo version 1.2.5
quipo authored
222 return 1;
223 }
224
225 /**
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
226 * Translation removal
227 *
228 * Removes string from all of string tables
229 * @param string $PageID page identifier.
230 * @param string $StringID string identifier.
231 * @param string $pear_DSN PEAR DSN string for database connection
232 * @return integer $result 1 if everything went OK or PEAR DB_Error object if something goes wrong
233 */
acbb58a @quipo version 1.2.5
quipo authored
234 function removeTranslation($PageID, $StringID, $pear_DSN) {
235 $db = DB::connect($pear_DSN);
236 if (DB::isError($db))
237 return $db;
238 $result = $db->query("SELECT lang_id FROM tr_langsavail");
239 if (DB::isError($result))
240 return $result;
241 while ($row = $result->fetchRow())
242 $languages[] = "tr_strings_" . $row[0];
243 //print_r($languages);
244 $result = $db->executeMultiple(($db->prepare("DELETE FROM ! WHERE page_id = '$PageID' and string_id = '$StringID'")), $languages);
245 if (DB::isError($result))
246 return $result;
247 return $result;
248 }
8ce8190 @quipo allow custom table definitions in admin class too
quipo authored
249
acbb58a @quipo version 1.2.5
quipo authored
250 ?>
Something went wrong with that request. Please try again.