openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Language.class.php (6453B)


      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 		return $this;
    106 	}
    107 
    108 
    109 	/**
    110      * Speichern der Sprache in der Datenbank
    111      */
    112 	public function save()
    113 	{
    114 		$db = \cms\base\DB::get();
    115 
    116 		// Gruppe speichern		
    117 		$sql = $db->sql( 'UPDATE {{language}} '.
    118 		                'SET name      = {name}, '.
    119 		                '    isocode   = {isocode} '.
    120 		                'WHERE id={languageid}' );
    121 		$sql->setString( 'name'     ,$this->name    );
    122 		$sql->setString( 'isocode'  ,$this->isoCode );
    123 
    124 		$sql->setInt( 'languageid',$this->languageid );
    125 
    126 		// Datenbankabfrage ausfuehren
    127 		$sql->execute();
    128 	}
    129 
    130 
    131 	/**
    132 	 * Ermitteln aller Eigenschaften dieser Sprache
    133 	 * @return array
    134 	 */
    135 	function getProperties()
    136 	{
    137 		return Array( 'name'   =>$this->name,
    138 		              'isocode'=>$this->isoCode );
    139 	}
    140 
    141 
    142 	/**
    143 	 * Neue Sprache hinzuf?gen
    144 	 */
    145 	function add( $isocode='' )
    146 	{
    147 		$db = \cms\base\DB::get();
    148 
    149 		if	( $isocode != '' )
    150 		{
    151 			// Kleiner Trick, damit "no" (Norwegen) in der .ini-Datei stehen kann
    152 			$isocode = str_replace('_','',$isocode);
    153 			
    154 			$this->isocode = $isocode;
    155 			$codes = Configuration::subset('countries')->getConfig();
    156 			$this->name    = $codes[ $isocode ];
    157 		}
    158 
    159 		$sql = $db->sql('SELECT MAX(id) FROM {{language}}');
    160 		$this->languageid = intval($sql->getOne())+1;
    161 
    162 		// Sprache hinzuf?gen
    163 		$sql = $db->sql( 'INSERT INTO {{language}} '.
    164 		                '(id,projectid,name,isocode,is_default) VALUES( {languageid},{projectid},{name},{isocode},0 )');
    165 		$sql->setInt   ('languageid',$this->languageid );
    166 		$sql->setInt   ('projectid' ,$this->projectid  );
    167 		$sql->setString('name'      ,$this->name       );
    168 		$sql->setString('isocode'   ,$this->isoCode    );
    169 
    170 		// Datenbankbefehl ausfuehren
    171 		$sql->execute();
    172 	}
    173 
    174 
    175 	// Diese Sprache als 'default' markieren.
    176 	public function setDefault()
    177 	{
    178 		$db = \cms\base\DB::get();
    179 
    180 		// Zuerst alle auf nicht-Standard setzen
    181 		$sql = $db->sql( 'UPDATE {{language}} '.
    182 		                '  SET is_default = 0 '.
    183 		                '  WHERE projectid={projectid}' );
    184 		$sql->setInt('projectid',$this->projectid );
    185 		$sql->execute();
    186 	
    187 		// Jetzt die gew?nschte Sprachvariante auf Standard setzen
    188 		$sql = $db->sql( 'UPDATE {{language}} '.
    189 		                '  SET is_default = 1 '.
    190 		                '  WHERE id={languageid}' );
    191 		$sql->setInt('languageid',$this->languageid );
    192 		$sql->execute();
    193 	}
    194 
    195 
    196 	/**
    197 	 * Delete language
    198 	 */
    199 	public function delete()
    200 	{
    201 		$db = \cms\base\DB::get();
    202 
    203 		// Inhalte mit dieser Sprache l?schen
    204 		$sql = $db->sql( 'DELETE FROM {{pagecontent}} WHERE languageid={languageid}' );
    205 		$sql->setInt( 'languageid',$this->languageid );
    206 		$sql->execute();
    207 
    208 		// Inhalte mit dieser Sprache l?schen
    209 		$sql = $db->sql( 'DELETE FROM {{name}} WHERE languageid={languageid}' );
    210 		$sql->setInt( 'languageid',$this->languageid );
    211 		$sql->execute();
    212 
    213 		// Andere Sprache auf "Default" setzen
    214 		if   ( $this->isDefault ) {
    215 
    216 			$sql = $db->sql( 'SELECT id FROM {{language}} WHERE projectid={projectid}' );
    217 			$sql->setInt( 'projectid',$this->projectid );
    218 			$new_default_languageid = $sql->getOne();
    219 
    220 			if   ( $new_default_languageid )
    221 			{
    222 				$sql = $db->sql( 'UPDATE {{language}} SET is_default=1 WHERE id={languageid}' );
    223 				$sql->setInt( 'languageid',$new_default_languageid );
    224 				$sql->execute();
    225 			}
    226 		}
    227 
    228 		// Sprache l?schen
    229 		$sql = $db->sql( 'DELETE FROM {{language}} WHERE id={languageid}' );
    230 		$sql->setInt( 'languageid',$this->languageid );
    231 		$sql->execute();
    232 	}
    233 
    234     public function setCurrentLocale()
    235     {
    236         self::setLocale( $this->isoCode );
    237     }
    238 
    239     public function getName()
    240     {
    241         return $this->name;
    242     }
    243 
    244 
    245 
    246 	public function getId()
    247 	{
    248 		return $this->languageid;
    249 	}
    250 
    251 }