| [ PHPXref.com ] | [ Generated: Sun Jul 20 20:30:18 2008 ] | [ symfony 0.6.2 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
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: sfMessageSource_Creole.class.php 835 2006-02-15 20:57:57Z fabien $ 17 * @package symfony 18 * @subpackage i18n 19 */ 20 21 /** 22 * sfMessageSource_Creole class. 23 * 24 * Retrive the message translation from a Creole supported database. 25 * 26 * See the MessageSource::factory() method to instantiate this class. 27 * 28 * @author RoVeRT <symfony[at]rovert[dot]net> 29 */ 30 class sfMessageSource_Creole extends sfMessageSource 31 { 32 /** 33 * A resource link to the database 34 * @var db 35 */ 36 protected $db; 37 38 /** 39 * Constructor. 40 * Create a new message source using Creole. 41 * @param string Creole datasource. 42 * @see MessageSource::factory(); 43 */ 44 public function __construct($source) 45 { 46 $this->db = sfContext::getInstance()->getDatabaseConnection($source); 47 if ($this->db == null || !$this->db instanceof Connection) 48 { 49 $error = 'Creole dabatase connection doesn\'t exist. Unable to open session.'; 50 throw new sfDatabaseException($error); 51 } 52 } 53 54 /** 55 * Destructor, close the database connection. 56 */ 57 public function __destruct() 58 { 59 } 60 61 /** 62 * Get the database connection. 63 * @return db database connection. 64 */ 65 public function connection() 66 { 67 return $this->db; 68 } 69 70 /** 71 * Get an array of messages for a particular catalogue and cultural 72 * variant. 73 * @param string the catalogue name + variant 74 * @return array translation messages. 75 */ 76 protected function &loadData($variant) 77 { 78 $sql = 'SELECT t.id, t.source, t.target, t.comments ' . 79 'FROM trans_unit t, catalogue c ' . 80 'WHERE c.cat_id = t.cat_id AND c.name = ? ' . 81 'ORDER BY id ASC'; 82 83 $stmt = $this->db->prepareStatement($sql); 84 85 $rs = $stmt->executeQuery(array($variant), ResultSet::FETCHMODE_NUM); 86 87 $result = array(); 88 89 while ($rs->next()) 90 { 91 $source = $rs->getString(2); 92 $result[$source][] = $rs->getString(3); //target 93 $result[$source][] = $rs->getInt(1); //id 94 $result[$source][] = $rs->getString(4); //comments 95 } 96 97 return $result; 98 } 99 100 /** 101 * Get the last modified unix-time for this particular catalogue+variant. 102 * We need to query the database to get the date_modified. 103 * @param string catalogue+variant 104 * @return int last modified in unix-time format. 105 */ 106 protected function getLastModified($source) 107 { 108 $sql = 'SELECT date_modified FROM catalogue WHERE name = ?'; 109 110 $stmt = $this->db->prepareStatement($sql); 111 112 $rs = $stmt->executeQuery(array($source), ResultSet::FETCHMODE_NUM); 113 114 $result = $rs->next()? $rs->getInt(1): 0; 115 116 return $result; 117 } 118 119 /** 120 * Check if a particular catalogue+variant exists in the database. 121 * @param string catalogue+variant 122 * @return boolean true if the catalogue+variant is in the database, 123 * false otherwise. 124 */ 125 protected function isValidSource($variant) 126 { 127 $sql = 'SELECT COUNT(*) FROM catalogue WHERE name = ?'; 128 129 $stmt = $this->db->prepareStatement($sql); 130 131 $rs = $stmt->executeQuery(array($variant), ResultSet::FETCHMODE_NUM); 132 133 $result = $rs->next()? $rs->getInt(1) == 1: false; 134 135 return $result; 136 } 137 138 /** 139 * Get all the variants of a particular catalogue. 140 * @param string catalogue name 141 * @return array list of all variants for this catalogue. 142 */ 143 protected function getCatalogueList($catalogue) 144 { 145 $variants = explode('_', $this->culture); 146 147 $catalogues = array($catalogue); 148 149 $variant = null; 150 151 for($i = 0; $i < count($variants); $i++) 152 { 153 if (strlen($variants[$i])>0) 154 { 155 $variant .= ($variant)?'_'.$variants[$i]:$variants[$i]; 156 $catalogues[] = $catalogue.'.'.$variant; 157 } 158 } 159 return array_reverse($catalogues); 160 } 161 162 /** 163 * Retrive catalogue details, array($cat_id, $variant, $count). 164 * @param string catalogue 165 * @return array catalogue details, array($cat_id, $variant, $count). 166 */ 167 private function getCatalogueDetails($catalogue='messages') 168 { 169 if (empty($catalogue)) 170 $catalogue = 'messages'; 171 172 $variant = $catalogue.'.'.$this->culture; 173 174 $name = $this->getSource($variant); 175 176 $aql = 'SELECT cat_id FROM catalogue WHERE name = ?'; 177 178 $stmt = $this->db->prepareStatement($sql); 179 180 $rs = $stmt->executeQuery(array($name), ResultSet::FETCHMODE_NUM); 181 182 if ($rs->getRecordCount() != 1) 183 { 184 return false; 185 } 186 187 $rs->next(); 188 189 $cat_id = $rs->getInt(1); 190 191 //first get the catalogue ID 192 $sql = 'SELECT count(msg_id) FROM trans_unit WHERE cat_id = ?'; 193 194 $stmt = $this->db->prepareStatement($sql); 195 196 $rs = $stmt->executeQuery(array($cat_id), ResultSet::FETCHMODE_NUM); 197 198 $rs->next(); 199 $count = $rs->getInt(1); 200 201 return array($cat_id, $variant, $count); 202 } 203 204 /** 205 * Update the catalogue last modified time. 206 * @return boolean true if updated, false otherwise. 207 */ 208 private function updateCatalogueTime($cat_id, $variant) 209 { 210 $time = time(); 211 212 $sql = 'UPDATE catalogue SET date_modified = ? WHERE cat_id = ?'; 213 214 $stmt = $this->db->prepareStatement($sql); 215 216 $stmt->executeUpdate(array($time, $cat_id)); 217 218 if (!empty($this->cache)) 219 { 220 $this->cache->clean($variant, $this->culture); 221 } 222 223 return $result; 224 } 225 226 /** 227 * Save the list of untranslated blocks to the translation source. 228 * If the translation was not found, you should add those 229 * strings to the translation source via the <b>append()</b> method. 230 * @param string the catalogue to add to 231 * @return boolean true if saved successfuly, false otherwise. 232 */ 233 function save($catalogue='messages') 234 { 235 $messages = $this->untranslated; 236 237 if (count($messages) <= 0) return false; 238 239 $details = $this->getCatalogueDetails($catalogue); 240 241 if ($details) 242 list($cat_id, $variant, $count) = $details; 243 else 244 return false; 245 246 if ($cat_id <= 0) return false; 247 $inserted = 0; 248 249 $time = time(); 250 251 try 252 { 253 $this->db->begin(); 254 255 $sql = 'INSERT INTO trans_unit ' . 256 '(cat_id, id, source, date_added, date_modified) VALUES ' . 257 '(?, ?, ?, ?, ?)'; 258 259 $stmt = $this->db->prepareStatement($sql); 260 261 foreach($messages as $message) 262 { 263 $stmt->executeUpdate(array($cat_id, $count, $message, $time, $time)); 264 265 ++$count; ++$inserted; 266 } 267 268 $this->db->commit(); 269 } 270 catch (Exception $e) 271 { 272 $this->db->rollback(); 273 } 274 275 if ($inserted > 0) 276 $this->updateCatalogueTime($cat_id, $variant); 277 278 return $inserted > 0; 279 } 280 281 /** 282 * Delete a particular message from the specified catalogue. 283 * @param string the source message to delete. 284 * @param string the catalogue to delete from. 285 * @return boolean true if deleted, false otherwise. 286 */ 287 function delete($message, $catalogue='messages') 288 { 289 $details = $this->getCatalogueDetails($catalogue); 290 if ($details) 291 list($cat_id, $variant, $count) = $details; 292 else 293 return false; 294 295 $deleted = false; 296 297 $sql = 'DELETE FROM trans_unit WHERE cat_id = ? AND source = ?'; 298 299 $stmt = $this->db->prepareStatement($sql); 300 301 $rows = $stmt->executeUpdate(array($cat_id, $message)); 302 303 if ($rows == 1) 304 { 305 $deleted = $this->updateCatalogueTime($cat_id, $variant); 306 } 307 308 return $deleted; 309 } 310 311 /** 312 * Update the translation. 313 * @param string the source string. 314 * @param string the new translation string. 315 * @param string comments 316 * @param string the catalogue of the translation. 317 * @return boolean true if translation was updated, false otherwise. 318 */ 319 function update($text, $target, $comments, $catalogue='messages') 320 { 321 $details = $this->getCatalogueDetails($catalogue); 322 if ($details) 323 { 324 list($cat_id, $variant, $count) = $details; 325 } 326 else 327 { 328 return false; 329 } 330 331 $time = time(); 332 333 $sql = 'UPDATE trans_unit SET target = ?, comments = ?, date_modified = ? ' . 334 'WHERE cat_id = ? AND source = ?'; 335 336 $updated = false; 337 338 $stmt = $this->db->prepareStatement($sql); 339 340 $rows = $stmt->executeUpdate(array($target, $comments, $time, $cat_id, $text)); 341 342 if ($rows == 1) 343 { 344 $updated = $this->updateCatalogueTime($cat_id, $variant); 345 } 346 347 return $updated; 348 } 349 350 /** 351 * Returns a list of catalogue as key and all it variants as value. 352 * @return array list of catalogues 353 */ 354 function catalogues() 355 { 356 $sql = 'SELECT name FROM catalogue ORDER BY name'; 357 358 $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM); 359 360 $result = array(); 361 while ($rs->next()) 362 { 363 $details = explode('.', $rs->getString(1)); 364 if (!isset($details[1])) 365 { 366 $details[1] = null; 367 } 368 369 $result[] = $details; 370 } 371 372 return $result; 373 } 374 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |