openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Language.class.php (6628B)


      1 <?php
      2 namespace cms\model;
      3 // OpenRat Content Management System
      4 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      5 //
      6 // This program is free software; you can redistribute it and/or
      7 // modify it under the terms of the GNU General Public License
      8 // as published by the Free Software Foundation; either version 2
      9 // of the License, or (at your option) any later version.
     10 //
     11 // This program is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 //
     16 // You should have received a copy of the GNU General Public License
     17 // along with this program; if not, write to the Free Software
     18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     19 use cms\base\Configuration;
     20 use cms\base\DB as Db;
     21 use logger\Logger;
     22 
     23 
     24 /**
     25  * Darstellen einer Sprache. Jeder Seiteninhalt wird einer Sprache zugeordnet.
     26  *
     27  * @version $Revision$
     28  * @author $Author$
     29  * @package openrat.objects
     30  */
     31 class Language extends ModelBase
     32 {
     33 	public $languageid;
     34     public $projectid;
     35 
     36     public $name;
     37     public $isoCode;
     38     public $isDefault = false;
     39 
     40 
     41 	// Konstruktor
     42 	function __construct( $languageid='' )
     43 	{
     44 		if   ( is_numeric($languageid) )
     45 			$this->languageid = $languageid;
     46 	}
     47 
     48     public static function setLocale( $isoCode )
     49     {
     50         $localeConf = Configuration::subset('i18n')->subset('locale');
     51 
     52         if	( $localeConf->has(strtolower($isoCode)) )
     53         {
     54             $locale = $localeConf->get(strtolower($isoCode));
     55             $locale_ok = setlocale(LC_ALL,$locale);
     56             if	( !$locale_ok )
     57                 // Hat nicht geklappt. Entweder ist das Mapping falsch oder die locale ist
     58                 // nicht korrekt installiert.
     59                 Logger::warn("Could not set locale '$locale', please check with 'locale -a' if it is installaled correctly");
     60         }
     61         else
     62         {
     63             setlocale(LC_ALL,'');
     64         }
     65     }
     66 
     67 
     68     /**
     69 	 * Stellt fest, ob die angegebene Id existiert.
     70 	 */
     71 	function available( $id )
     72 	{
     73 		$db = \cms\base\DB::get();
     74 
     75 		$sql = $db->sql('SELECT 1 FROM {{language}} '.
     76 		               ' WHERE id={id}');
     77 		$sql->setInt('id' ,$id  );
     78 
     79 		return intval($sql->getOne()) == 1;
     80 	}
     81 
     82 	
     83 
     84 	/**
     85      * Lesen aus der Datenbank.
     86      *
     87      */
     88 	public function load()
     89 	{
     90 		$stmt = Db::sql( 'SELECT * FROM {{language}}'.
     91 		                ' WHERE id={languageid}' );
     92 		$stmt->setInt( 'languageid',$this->languageid );
     93 
     94 		$row = $stmt->getRow();
     95 		
     96 		if	( count($row) > 0 )
     97 		{
     98 			$this->name      =         $row['name'     ];
     99 			$this->isoCode   =         $row['isocode'  ];
    100 			$this->projectid = intval( $row['projectid'] );
    101 			
    102 			$this->isDefault = ( $row['is_default'] == '1' );
    103 		}
    104 	}
    105 
    106 
    107 	/**
    108      * Speichern der Sprache in der Datenbank
    109      */
    110 	public function save()
    111 	{
    112 		$db = \cms\base\DB::get();
    113 
    114 		// Gruppe speichern		
    115 		$sql = $db->sql( 'UPDATE {{language}} '.
    116 		                'SET name      = {name}, '.
    117 		                '    isocode   = {isocode} '.
    118 		                'WHERE id={languageid}' );
    119 		$sql->setString( 'name'     ,$this->name    );
    120 		$sql->setString( 'isocode'  ,$this->isoCode );
    121 
    122 		$sql->setInt( 'languageid',$this->languageid );
    123 
    124 		// Datenbankabfrage ausfuehren
    125 		$sql->execute();
    126 	}
    127 
    128 
    129 	/**
    130 	 * Ermitteln aller Eigenschaften dieser Sprache
    131 	 * @return array
    132 	 */
    133 	function getProperties()
    134 	{
    135 		return Array( 'name'   =>$this->name,
    136 		              'isocode'=>$this->isoCode );
    137 	}
    138 
    139 
    140 	/**
    141 	 * Neue Sprache hinzuf?gen
    142 	 */
    143 	function add( $isocode='' )
    144 	{
    145 		$db = \cms\base\DB::get();
    146 
    147 		if	( $isocode != '' )
    148 		{
    149 			// Kleiner Trick, damit "no" (Norwegen) in der .ini-Datei stehen kann
    150 			$isocode = str_replace('_','',$isocode);
    151 			
    152 			$this->isocode = $isocode;
    153 			$codes = Configuration::subset('countries')->getConfig();
    154 			$this->name    = $codes[ $isocode ];
    155 		}
    156 
    157 		$sql = $db->sql('SELECT MAX(id) FROM {{language}}');
    158 		$this->languageid = intval($sql->getOne())+1;
    159 
    160 		// Sprache hinzuf?gen
    161 		$sql = $db->sql( 'INSERT INTO {{language}} '.
    162 		                '(id,projectid,name,isocode,is_default) VALUES( {languageid},{projectid},{name},{isocode},0 )');
    163 		$sql->setInt   ('languageid',$this->languageid );
    164 		$sql->setInt   ('projectid' ,$this->projectid  );
    165 		$sql->setString('name'      ,$this->name       );
    166 		$sql->setString('isocode'   ,$this->isoCode    );
    167 
    168 		// Datenbankbefehl ausfuehren
    169 		$sql->execute();
    170 	}
    171 
    172 
    173 	// Diese Sprache als 'default' markieren.
    174 	function setDefault()
    175 	{
    176 		$db = \cms\base\DB::get();
    177 
    178 		// Zuerst alle auf nicht-Standard setzen
    179 		$sql = $db->sql( 'UPDATE {{language}} '.
    180 		                '  SET is_default = 0 '.
    181 		                '  WHERE projectid={projectid}' );
    182 		$sql->setInt('projectid',$this->projectid );
    183 		$sql->execute();
    184 	
    185 		// Jetzt die gew?nschte Sprachvariante auf Standard setzen
    186 		$sql = $db->sql( 'UPDATE {{language}} '.
    187 		                '  SET is_default = 1 '.
    188 		                '  WHERE id={languageid}' );
    189 		$sql->setInt('languageid',$this->languageid );
    190 		$sql->execute();
    191 	}
    192 
    193 
    194 	// Sprache entfernen
    195 	function delete()
    196 	{
    197 		$db = \cms\base\DB::get();
    198 
    199 		// Sprache l?schen
    200 //		$sql = $db->sql( 'SELECT COUNT(*) FROM {{language}} WHERE projectid={projectid}' );
    201 //		$sql->setInt( 'projectid',$this->projectid );
    202 //		$count = $sql->getOne( $sql );
    203 //		
    204 //		// Nur l?schen, wenn es mindestens 2 Sprachen gibt
    205 //		if   ( $count >= 2 )
    206 //		{
    207 			// Inhalte mit dieser Sprache l?schen
    208 			$sql = $db->sql( 'DELETE FROM {{value}} WHERE languageid={languageid}' );
    209 			$sql->setInt( 'languageid',$this->languageid );
    210 			$sql->execute();
    211 
    212 			// Inhalte mit dieser Sprache l?schen
    213 			$sql = $db->sql( 'DELETE FROM {{name}} WHERE languageid={languageid}' );
    214 			$sql->setInt( 'languageid',$this->languageid );
    215 			$sql->execute();
    216 
    217 			// Sprache l?schen
    218 			$sql = $db->sql( 'DELETE FROM {{language}} WHERE id={languageid}' );
    219 			$sql->setInt( 'languageid',$this->languageid );
    220 			$sql->execute();
    221 
    222 			// Andere Sprache auf "Default" setzen
    223 			$sql = $db->sql( 'SELECT id FROM {{language}} WHERE projectid={projectid}' );
    224 			$sql->setInt( 'projectid',$this->projectid );
    225 			$new_default_languageid = $sql->getOne();
    226 
    227 			$sql = $db->sql( 'UPDATE {{language}} SET is_default=1 WHERE id={languageid}' );
    228 			$sql->setInt( 'languageid',$new_default_languageid );
    229 			$sql->execute();
    230 //		}
    231 	}
    232 
    233     public function setCurrentLocale()
    234     {
    235         self::setLocale( $this->isoCode );
    236     }
    237 
    238     public function getName()
    239     {
    240         return $this->name;
    241     }
    242 
    243 
    244 
    245 	public function getId()
    246 	{
    247 		return $this->languageid;
    248 	}
    249 
    250 }