Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/creole/i18n/sfMessageSource_Creole.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/creole/i18n/sfMessageSource_Creole.class.php

Revision 9132, 9.7 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: fixed @param phpdoc in sfPropelPlugin (refs #2991). many methods still need a phpdoc block though!

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /**
4  * sfMessageSource_Creole class file.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the BSD License.
8  *
9  * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10  *
11  * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12  * The latest version of PRADO can be obtained from:
13  * {@link http://prado.sourceforge.net/}
14  *
15  * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
16  * @version    $Id$
17  * @package    symfony
18  * @subpackage i18n
19  */
20
21 /*
22 CREATE TABLE `catalogue` (
23   `cat_id` int(11) NOT NULL auto_increment,
24   `name` varchar(100) NOT NULL default '',
25   `source_lang` varchar(100) NOT NULL default '',
26   `target_lang` varchar(100) NOT NULL default '',
27   `date_created` int(11) NOT NULL default '0',
28   `date_modified` int(11) NOT NULL default '0',
29   `author` varchar(255) NOT NULL default '',
30   PRIMARY KEY  (`cat_id`)
31 );
32
33 CREATE TABLE `trans_unit` (
34   `msg_id` int(11) NOT NULL auto_increment,
35   `cat_id` int(11) NOT NULL default '1',
36   `source` text NOT NULL,
37   `target` text NOT NULL,
38   `comments` text NOT NULL,
39   `date_added` int(11) NOT NULL default '0',
40   `date_modified` int(11) NOT NULL default '0',
41   `author` varchar(255) NOT NULL default '',
42   `translated` tinyint(1) NOT NULL default '0',
43   PRIMARY KEY  (`msg_id`)
44 );
45
46 */
47
48 /**
49  * sfMessageSource_Creole class.
50  *
51  * Retrieve the message translation from a Creole supported database.
52  *
53  * See the MessageSource::factory() method to instantiate this class.
54  *
55  * @author RoVeRT <symfony[at]rovert[dot]net>
56  */
57 class sfMessageSource_Creole extends sfMessageSource_Database
58 {
59   /**
60    * A resource link to the database
61    * @var db
62    */
63   protected $db;
64
65   /**
66    * Constructor.
67    * Create a new message source using Creole.
68    * @param string $source Creole datasource.
69    * @see MessageSource::factory();
70    */
71   public function __construct($source)
72   {
73     $this->db = sfContext::getInstance()->getDatabaseConnection($source);
74     if ($this->db == null || !$this->db instanceof Connection)
75     {
76       throw new sfDatabaseException('Creole dabatase connection doesn\'t exist. Unable to open session.');
77     }
78   }
79
80   /**
81    * Destructor, close the database connection.
82    */
83   public function __destruct()
84   {
85   }
86
87   /**
88    * Get the database connection.
89    * @return db database connection.
90    */
91   public function connection()
92   {
93     return $this->db;
94   }
95
96   /**
97    * Get an array of messages for a particular catalogue and cultural
98    * variant.
99    * @param string $variant the catalogue name + variant
100    * @return array translation messages.
101    */
102   public function &loadData($variant)
103   {
104     $sql = 'SELECT t.source, t.target, t.comments '.
105            'FROM trans_unit t, catalogue c '.
106            'WHERE c.cat_id =  t.cat_id AND c.name = ? '.
107            'ORDER BY msg_id ASC';
108
109     $stmt = $this->db->prepareStatement($sql);
110
111     $rs = $stmt->executeQuery(array($variant), ResultSet::FETCHMODE_NUM);
112
113     $result = array();
114
115     $count = 0;
116     while ($rs->next())
117     {
118       $source = $rs->getString(1);
119       $result[$source][] = $rs->getString(2); //target
120       $result[$source][] = $count++;          //id
121       $result[$source][] = $rs->getString(3); //comments
122     }
123
124     return $result;
125   }
126
127   /**
128    * Get the last modified unix-time for this particular catalogue+variant.
129    * We need to query the database to get the date_modified.
130    *
131    * @param string $source catalogue+variant
132    * @return int last modified in unix-time format.
133    */
134   protected function getLastModified($source)
135   {
136     $sql = 'SELECT date_modified FROM catalogue WHERE name = ?';
137
138     $stmt = $this->db->prepareStatement($sql);
139
140     $rs = $stmt->executeQuery(array($source), ResultSet::FETCHMODE_NUM);
141
142     $result = $rs->next() ? $rs->getInt(1) : 0;
143
144     return $result;
145   }
146
147   /**
148    * Check if a particular catalogue+variant exists in the database.
149    *
150    * @param string $variant catalogue+variant
151    * @return boolean true if the catalogue+variant is in the database, false otherwise.
152    */
153   public function isValidSource($variant)
154   {
155     $sql = 'SELECT COUNT(*) FROM catalogue WHERE name = ?';
156
157     $stmt = $this->db->prepareStatement($sql);
158
159     $rs = $stmt->executeQuery(array($variant), ResultSet::FETCHMODE_NUM);
160
161     $result = $rs->next() ? $rs->getInt(1) == 1 : false;
162
163     return $result;
164   }
165
166   /**
167    * Retrieve catalogue details, array($catId, $variant, $count).
168    *
169    * @param string $catalogue catalogue
170    * @return array catalogue details, array($catId, $variant, $count).
171    */
172   protected function getCatalogueDetails($catalogue = 'messages')
173   {
174     if (empty($catalogue))
175     {
176       $catalogue = 'messages';
177     }
178
179     $variant = $catalogue.'.'.$this->culture;
180
181     $name = $this->getSource($variant);
182
183     $sql = 'SELECT cat_id FROM catalogue WHERE name = ?';
184
185     $stmt = $this->db->prepareStatement($sql);
186
187     $rs = $stmt->executeQuery(array($name), ResultSet::FETCHMODE_NUM);
188
189     if ($rs->getRecordCount() != 1)
190     {
191       return false;
192     }
193
194     $rs->next();
195
196     $catId = $rs->getInt(1);
197
198     //first get the catalogue ID
199     $sql = 'SELECT count(msg_id) FROM trans_unit WHERE cat_id = ?';
200
201     $stmt = $this->db->prepareStatement($sql);
202
203     $rs = $stmt->executeQuery(array($catId), ResultSet::FETCHMODE_NUM);
204
205     $rs->next();
206     $count = $rs->getInt(1);
207
208     return array($catId, $variant, $count);
209   }
210
211   /**
212    * Update the catalogue last modified time.
213    *
214    * @return boolean true if updated, false otherwise.
215    */
216   protected function updateCatalogueTime($catId, $variant)
217   {
218     $time = time();
219
220     $sql = 'UPDATE catalogue SET date_modified = ? WHERE cat_id = ?';
221
222     $stmt = $this->db->prepareStatement($sql);
223
224     $result = $stmt->executeUpdate(array($time, $catId));
225
226     if (!empty($this->cache))
227     {
228       $this->cache->clean($variant, $this->culture);
229     }
230
231     return true;
232   }
233
234   /**
235    * Save the list of untranslated blocks to the translation source.
236    * If the translation was not found, you should add those
237    * strings to the translation source via the <b>append()</b> method.
238    *
239    * @param string $catalogue the catalogue to add to
240    * @return boolean true if saved successfuly, false otherwise.
241    */
242   function save($catalogue='messages')
243   {
244     $messages = $this->untranslated;
245
246     if (count($messages) <= 0)
247     {
248       return false;
249     }
250
251     $details = $this->getCatalogueDetails($catalogue);
252
253     if ($details)
254     {
255       list($catId, $variant, $count) = $details;
256     }
257     else
258     {
259       return false;
260     }
261
262     if ($catId <= 0)
263     {
264       return false;
265     }
266     $inserted = 0;
267
268     $time = time();
269
270     try
271     {
272       $sql = 'SELECT msg_id FROM trans_unit WHERE source = ?';
273
274       $stmt = $this->db->prepareStatement($sql);
275
276       foreach($messages as $key => $message)
277       {
278         $rs = $stmt->executeQuery(array($message), ResultSet::FETCHMODE_NUM);
279         if ($rs->next())
280         {
281            unset($messages[$key]);
282         }
283       }
284     }
285     catch (Exception $e)
286     {
287     }
288
289     try
290     {
291       $this->db->begin();
292
293       $sql = 'INSERT INTO trans_unit (cat_id, source, target, comments, date_added, date_modified) VALUES (?, ?, ?, ?, ?, ?)';
294
295       $stmt = $this->db->prepareStatement($sql);
296
297       foreach ($messages as $message)
298       {
299         $stmt->executeUpdate(array($catId, $message, '', '', $time, $time));
300         ++$inserted;
301       }
302
303       $this->db->commit();
304     }
305     catch (Exception $e)
306     {
307       $this->db->rollback();
308     }
309
310     if ($inserted > 0)
311     {
312       $this->updateCatalogueTime($catId, $variant);
313     }
314
315     return $inserted > 0;
316   }
317
318   /**
319    * Delete a particular message from the specified catalogue.
320    *
321    * @param string $message   the source message to delete.
322    * @param string $catalogue the catalogue to delete from.
323    * @return boolean true if deleted, false otherwise.
324    */
325   function delete($message, $catalogue='messages')
326   {
327     $details = $this->getCatalogueDetails($catalogue);
328
329     if ($details)
330     {
331       list($catId, $variant, $count) = $details;
332     }
333     else
334     {
335       return false;
336     }
337
338     $deleted = false;
339
340     $sql = 'DELETE FROM trans_unit WHERE cat_id = ? AND source = ?';
341
342     $stmt = $this->db->prepareStatement($sql);
343
344     $rows = $stmt->executeUpdate(array($catId, $message));
345
346     if ($rows == 1)
347     {
348       $deleted = $this->updateCatalogueTime($catId, $variant);
349     }
350
351     return $deleted;
352   }
353
354   /**
355    * Update the translation.
356    *
357    * @param string $text      the source string.
358    * @param string $target    the new translation string.
359    * @param string $comments  comments
360    * @param string $catalogue the catalogue of the translation.
361    * @return boolean true if translation was updated, false otherwise.
362    */
363   function update($text, $target, $comments, $catalogue = 'messages')
364   {
365     $details = $this->getCatalogueDetails($catalogue);
366     if ($details)
367     {
368       list($catId, $variant, $count) = $details;
369     }
370     else
371     {
372       return false;
373     }
374
375     $time = time();
376
377     $sql = 'UPDATE trans_unit SET target = ?, comments = ?, date_modified = ? WHERE cat_id = ? AND source = ?';
378
379     $updated = false;
380
381     $stmt = $this->db->prepareStatement($sql);
382
383     $rows = $stmt->executeUpdate(array($target, $comments, $time, $catId, $text));
384
385     if ($rows == 1)
386     {
387       $updated = $this->updateCatalogueTime($catId, $variant);
388     }
389
390     return $updated;
391   }
392
393   /**
394    * Returns a list of catalogue as key and all it variants as value.
395    *
396    * @return array list of catalogues
397    */
398   function catalogues()
399   {
400     $sql = 'SELECT name FROM catalogue ORDER BY name';
401
402     $rs = $this->db->executeQuery($sql, ResultSet::FETCHMODE_NUM);
403
404     $result = array();
405     while ($rs->next())
406     {
407       $details = explode('.', $rs->getString(1));
408       if (!isset($details[1]))
409       {
410         $details[1] = null;
411       }
412
413       $result[] = $details;
414     }
415
416     return $result;
417   }
418 }
419
Note: See TracBrowser for help on using the browser.