Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * MessageSource_Database class file.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the BSD License.
7
 *
8
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
9
 *
10
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
11
 * The latest version of PRADO can be obtained from:
12
 * {@link http://prado.sourceforge.net/}
13
 *
14
 * @package System.I18N.core
15
 */
16
 
17
/**
18
 * Get the MessageSource class file.
19
 */
20
require_once(dirname(__FILE__).'/MessageSource.php');
21
 
22
/**
23
 * MessageSource_Database class.
24
 *
25
 * Retrive the message translation from a database.
26
 *
27
 * See the MessageSource::factory() method to instantiate this class.
28
 *
29
 * @package System.I18N.core
30
 */
31
class MessageSource_Database extends MessageSource
32
{
33
	private $_connID='';
34
	private $_conn;
35
 
36
	/**
37
	 * Constructor.
38
	 * Create a new message source using a Database
39
	 * @param string MySQL datasource, in PEAR's DB DSN format.
40
	 * @see MessageSource::factory();
41
	 */
42
	function __construct($source)
43
	{
44
		$this->_connID= (string)$source;
45
	}
46
 
47
	/**
48
	 * @return TDbConnection the database connection that may be used to retrieve messages.
49
	 */
50
	public function getDbConnection()
51
	{
52
		if($this->_conn===null)
53
		{
54
			$this->_conn=$this->createDbConnection($this->_connID);
55
			$this->_conn->setActive(true);
56
		}
57
		return $this->_conn;
58
	}
59
 
60
	/**
61
	 * Creates the DB connection.
62
	 * @param string the module ID for TDataSourceConfig
63
	 * @return TDbConnection the created DB connection
64
	 * @throws TConfigurationException if module ID is invalid or empty
65
	 */
66
	protected function createDbConnection($connectionID)
67
	{
68
		if($connectionID!=='')
69
		{
70
			$conn=Prado::getApplication()->getModule($connectionID);
71
			if($conn instanceof TDataSourceConfig)
72
				return $conn->getDbConnection();
73
			else
74
				throw new TConfigurationException('messagesource_connectionid_invalid',$connectionID);
75
		}
76
		else
77
			throw new TConfigurationException('messagesource_connectionid_required');
78
	}
79
 
80
	/**
81
	 * Get an array of messages for a particular catalogue and cultural
82
	 * variant.
83
	 * @param string the catalogue name + variant
84
	 * @return array translation messages.
85
	 */
86
	protected function &loadData($variant)
87
	{
88
		$command=$this->getDBConnection()->createCommand(
89
			'SELECT t.id, t.source, t.target, t.comments
90
				FROM trans_unit t, catalogue c
91
				WHERE c.cat_id =  t.cat_id
92
					AND c.name = :variant
93
				ORDER BY id ASC');
94
		$command->bindParameter(':variant',$variant,PDO::PARAM_STR);
95
		$dataReader=$command->query();
96
 
97
		$result = array();
98
 
99
		foreach ($dataReader as $row)
100
			$result[$row['source']] = array($row['target'],$row['id'],$row['comments']);
101
 
102
		return $result;
103
	}
104
 
105
	/**
106
	 * Get the last modified unix-time for this particular catalogue+variant.
107
	 * We need to query the database to get the date_modified.
108
	 * @param string catalogue+variant
109
	 * @return int last modified in unix-time format.
110
	 */
111
	protected function getLastModified($source)
112
	{
113
		$command=$this->getDBConnection()->createCommand(
114
			'SELECT date_modified FROM catalogue WHERE name = :source');
115
		$command->bindParameter(':source',$source,PDO::PARAM_STR);
116
		$result=$command->queryScalar();
117
		return $result ? $result : 0;
118
	}
119
 
120
 
121
	/**
122
	 * Check if a particular catalogue+variant exists in the database.
123
	 * @param string catalogue+variant
124
	 * @return boolean true if the catalogue+variant is in the database,
125
	 * false otherwise.
126
	 */
127
	protected function isValidSource($variant)
128
	{
129
		$command=$this->getDBConnection()->createCommand(
130
			'SELECT COUNT(*) FROM catalogue WHERE name = :variant');
131
		$command->bindParameter(':variant',$variant,PDO::PARAM_STR);
132
		return $command->queryScalar()==1;
133
	}
134
 
135
	/**
136
	 * Get all the variants of a particular catalogue.
137
	 * @param string catalogue name
138
	 * @return array list of all variants for this catalogue.
139
	 */
140
	protected function getCatalogueList($catalogue)
141
	{
142
		$variants = explode('_',$this->culture);
143
 
144
		$catalogues = array($catalogue);
145
 
146
		$variant = null;
147
 
148
		for($i = 0, $k = count($variants); $i < $k; ++$i)
149
		{
150
			if(isset($variants[$i]{0}))
151
			{
152
				$variant .= ($variant)?'_'.$variants[$i]:$variants[$i];
153
				$catalogues[] = $catalogue.'.'.$variant;
154
			}
155
		}
156
		return array_reverse($catalogues);
157
	}
158
 
159
	/**
160
	 * Retrive catalogue details, array($cat_id, $variant, $count).
161
	 * @param string catalogue
162
	 * @return array catalogue details, array($cat_id, $variant, $count).
163
	 */
164
	private function getCatalogueDetails($catalogue='messages')
165
	{
166
		if(empty($catalogue))
167
			$catalogue = 'messages';
168
 
169
		$variant = $catalogue.'.'.$this->culture;
170
 
171
		$command=$this->getDBConnection()->createCommand(
172
			'SELECT cat_id FROM catalogue WHERE name = :variant');
173
		$command->bindParameter(':variant',$variant,PDO::PARAM_STR);
174
		$cat_id=$command->queryScalar();
175
 
176
		if ($cat_id===null) return false;
177
 
178
		$command=$this->getDBConnection()->createCommand(
179
			'SELECT COUNT(msg_id) FROM trans_unit WHERE cat_id = :catid ');
180
		$command->bindParameter(':catid',$cat_id,PDO::PARAM_INT);
181
		$count=$command->queryScalar();
182
 
183
		return array($cat_id, $variant, $count);
184
	}
185
 
186
	/**
187
	 * Update the catalogue last modified time.
188
	 * @return boolean true if updated, false otherwise.
189
	 */
190
	private function updateCatalogueTime($cat_id, $variant)
191
	{
192
		$time = time();
193
		$command=$this->getDBConnection()->createCommand(
194
			'UPDATE catalogue SET date_modified = :moddate WHERE cat_id = :catid');
195
		$command->bindParameter(':moddate',$time,PDO::PARAM_INT);
196
		$command->bindParameter(':catid',$cat_id,PDO::PARAM_INT);
197
		$result=$command->execute();
198
 
199
		if(!empty($this->cache))
200
			$this->cache->clean($variant, $this->culture);
201
 
202
		return $result;
203
	}
204
 
205
	/**
206
	 * Save the list of untranslated blocks to the translation source.
207
	 * If the translation was not found, you should add those
208
	 * strings to the translation source via the <b>append()</b> method.
209
	 * @param string the catalogue to add to
210
	 * @return boolean true if saved successfuly, false otherwise.
211
	 */
212
	function save($catalogue='messages')
213
	{
214
		$messages = $this->untranslated;
215
 
216
		if(count($messages) <= 0) return false;
217
 
218
		$details = $this->getCatalogueDetails($catalogue);
219
 
220
		if($details)
221
			list($cat_id, $variant, $count) = $details;
222
		else
223
			return false;
224
 
225
		if($cat_id <= 0) return false;
226
		$inserted = 0;
227
 
228
		$time = time();
229
 
230
		$command=$this->getDBConnection()->createCommand(
231
			'INSERT INTO trans_unit (cat_id,id,source,date_added) VALUES (:catid,:id,:source,:dateadded)');
232
		$command->bindParameter(':catid',$cat_id,PDO::PARAM_INT);
233
		$command->bindParameter(':id',$count,PDO::PARAM_INT);
234
		$command->bindParameter(':source',$message,PDO::PARAM_STR);
235
		$command->bindParameter(':dateadded',$time,PDO::PARAM_INT);
236
		foreach($messages as $message)
237
		{
238
			if (empty($message)) continue;
239
			$count++; $inserted++;
240
			$command->execute();
241
		}
242
		if($inserted > 0)
243
			$this->updateCatalogueTime($cat_id, $variant);
244
 
245
		return $inserted > 0;
246
	}
247
 
248
	/**
249
	 * Delete a particular message from the specified catalogue.
250
	 * @param string the source message to delete.
251
	 * @param string the catalogue to delete from.
252
	 * @return boolean true if deleted, false otherwise.
253
	 */
254
	function delete($message, $catalogue='messages')
255
	{
256
		$details = $this->getCatalogueDetails($catalogue);
257
		if($details)
258
			list($cat_id, $variant, $count) = $details;
259
		else
260
			return false;
261
 
262
		$command=$this->getDBConnection()->createCommand(
263
			'DELETE FROM trans_unit WHERE cat_id = :catid AND source = :message');
264
		$command->bindParameter(':catid',$cat_id,PDO::PARAM_INT);
265
		$command->bindParameter(':message',$message,PDO::PARAM_STR);
266
 
267
		return ($command->execute()==1) ? $this->updateCatalogueTime($cat_id, $variant) : false;
268
 
269
	}
270
 
271
	/**
272
	 * Update the translation.
273
	 * @param string the source string.
274
	 * @param string the new translation string.
275
	 * @param string comments
276
	 * @param string the catalogue of the translation.
277
	 * @return boolean true if translation was updated, false otherwise.
278
	 */
279
	function update($text, $target, $comments, $catalogue='messages')
280
	{
281
		$details = $this->getCatalogueDetails($catalogue);
282
		if($details)
283
			list($cat_id, $variant, $count) = $details;
284
		else
285
			return false;
286
 
287
		$time = time();
288
		$command=$this->getDBConnection()->createCommand(
289
			'UPDATE trans_unit SET target = :target, comments = :comments, date_modified = :datemod
290
					WHERE cat_id = :catid AND source = :source');
291
		$command->bindParameter(':target',$target,PDO::PARAM_STR);
292
		$command->bindParameter(':comments',$comments,PDO::PARAM_STR);
293
		$command->bindParameter(':datemod',$time,PDO::PARAM_INT);
294
		$command->bindParameter(':catid',$cat_id,PDO::PARAM_INT);
295
		$command->bindParameter(':source',$text,PDO::PARAM_STR);
296
 
297
		return ($command->execute()==1) ? $this->updateCatalogueTime($cat_id, $variant) : false;
298
	}
299
 
300
	/**
301
	 * Returns a list of catalogue as key and all it variants as value.
302
	 * @return array list of catalogues
303
	 */
304
	function catalogues()
305
	{
306
		$command=$this->getDBConnection()->createCommand( 'SELECT name FROM catalogue ORDER BY name');
307
		$dataReader=$command->query();
308
 
309
		$result = array();
310
 
311
		foreach ($dataReader as $row)
312
		{
313
			$details = explode('.',$row[0]);
314
			if(!isset($details[1])) $details[1] = null;
315
 
316
			$result[] = $details;
317
		}
318
 
319
		return $result;
320
	}
321
 
322
}
323
?>