Project

General

Profile

1
<?php
2
/**
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

    
19
/**
20
 * Lib.php
21
 *
22
 * @category     Modules
23
 * @package      Modules_MultiLingual
24
 * @author       Werner v.d.Decken <wkl@isteam.de>
25
 * @author       Dietmar Wöllbrink <dietmar.woellbrink@websiteBaker.org>
26
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28
 * @version      1.6.8
29
 * @revision     $Revision: 1972 $
30
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/MultiLingual/Lib.php $
31
 * @lastmodified $Date: 2013-10-03 13:59:14 +0200 (Thu, 03 Oct 2013) $
32
 * @since        File available since 09.01.2013
33
 * @description  provides a flexible posibility for changeing to a translated page
34
 */
35

    
36
class m_MultiLingual_Lib {
37
/** @var object instance of the WbAdaptor object */
38
	protected $_oReg = null;
39
/** @var object instance of the application object */
40
	private $_oApp     = null;
41
/** @var object instance of the database object */
42
	private $_oDb      = null;
43

    
44
	private $_defaultPageId = 0;
45

    
46

    
47
/** @var array holds several values from the default.ini */	
48
	private $_config      = array();
49
/** @var array set several values for Twig_Environment */	
50
	private $_aTwigEnv    = array();
51
/** @var array set several values for Twig_Loader */	
52
	private $_aTwigLoader = array();
53
/** @var string set icon extension */	
54
	private $_sExtension  = array();
55
/**
56
 * constructor used to import some application constants and objects
57
 */	
58
	public function __construct() 
59
	{
60
		// import global vars and objects
61
		if(!defined('ADMIN_REL')) { define('ADMIN_REL', WB_REL.'/'.ADMIN_DIRECTORY); }
62
		$this->_oApp        = (isset($GLOBALS['admin']) ? $GLOBALS['admin'] : $GLOBALS['wb']);
63
		$this->_oDb         = WbDatabase::getInstance();
64
		$this->_oReg        = WbAdaptor::getInstance();
65
		$this->_config      = $this->_aConfig = $this->getConfig((dirname(__FILE__)).'/default.ini');
66
		$this->_aTwigEnv    = $this->_config['twig-environment'];
67
		$this->_aTwigLoader = $this->_config['twig-loader-file'];
68
	}
69

    
70
	/**
71
	 * methode to update a var/value-pair into table
72
	 * @param integer $iPageId which page shall be updated
73
	 * @param string $sTable the pages table
74
	 * @param integer $iEntry 
75
	 * @return bool
76
	 */ 
77
	private function _updatePageCode($iPageId, $sTable, $iNewPageCode = null)
78
	{
79
		// if new Pagecode is missing then set the own page ID
80
		$entry = ( !isset($iNewPageCode) ? $iPageId : $iNewPageCode);
81
		$sql = 'UPDATE `'.$this->_oReg->TablePrefix.$sTable.'` '
82
		     . 'SET `page_code`='.$entry.', '
83
		     .     '`modified_when` = '.time().' '
84
		     . 'WHERE `page_id` = '.$iPageId;
85
		return (bool)$this->_oDb->query($sql);
86
	}
87

    
88
	/**
89
	 * compose the needed SQL statement
90
	 * @param integer $sLangKey
91
	 * @return database object with given SQL statement
92
	 */			
93
	private function _getLangInUsedDbResult ( $sLangKey='' ) 
94
	{
95
		$sql = 'SELECT DISTINCT `language`,'
96
		     .                 '`page_id`,`level`,`parent`,`root_parent`,`page_code`,`link`,'
97
		     .                 '`visibility`,`viewing_groups`,`viewing_users`,`position`,`page_title` '
98
		     . 'FROM `'.$this->_oReg->TablePrefix.'pages` '
99
		     . 'WHERE `level`= \'0\' '
100
		     .   'AND `root_parent`=`page_id` '
101
		     .   'AND (`visibility`!=\'none\' '
102
		     .   'AND `visibility`!=\'hidden\') '
103
		     .   ( ($sLangKey!='') ? ' AND `language` = \''.$sLangKey.'\'' : '')
104
		     .   'GROUP BY `language` '
105
		     .   'ORDER BY `position`';
106
		return $this->_oDb->query($sql);
107
	}
108

    
109
	/**
110
	* 
111
	* search for pages with given page code and create a DB result object
112
	* @param integer Pagecode to search for
113
	* @return object result object or null on error
114
	*/
115
	private function _getPageCodeDbResult( $iPageCode )
116
	{
117
		$sql = 'SELECT `language`,'
118
		     .        '`page_id`,`level`,`parent`,`root_parent`,`page_code`,`link`,'
119
		     .        '`visibility`,`viewing_groups`,`viewing_users`,`position`,`page_title` '
120
		     .  'FROM `'.$this->_oReg->TablePrefix.'pages`'
121
		     .  'WHERE `page_code` = '.$iPageCode.' '
122
		     .  'ORDER BY `position`';
123
		return $this->_oDb->query($sql);
124
	}
125

    
126
	/**
127
	 * compose the needed SQL statement
128
	 * @param integer $sLangKey
129
	 * @return database object with given SQL statementt
130
	 */
131
	private function _getLangAddonsDbResult ( $sLangKey='' ) 
132
	{
133
		$sql = 'SELECT `directory`,`name`  FROM `'.$this->_oReg->TablePrefix.'addons` '
134
		     . 'WHERE `type` = \'language\' '
135
		     . ( ($sLangKey!='') ? ' AND `directory` = \''.$langKey.'\' ' : '')
136
		     . 'ORDER BY `directory`';
137
		return $this->_oDb->query($sql);
138
	}
139

    
140
	/**
141
	 * 
142
	 * @param integer $parent
143
	 * @return database object with given SQL statement
144
	 */
145
	private function _getPageListDbResult ( $parent ) 
146
	{
147
	    $sql = 'SELECT `language`,'
148
		     .        '`page_id`,`page_title`,`menu_title`, `page_code`, `parent` '
149
		     . 'FROM `'.$this->_oReg->TablePrefix.'pages` '
150
		     . 'WHERE `parent` = '.$parent. ' '
151
		     . 'ORDER BY `position`';
152
		return $this->_oDb->query($sql);
153
	}
154

    
155
	private function _getPageCodeValues(  $iPageCode=0 )
156
	{
157
		$aRetval = array();
158
		if( ($oRes = $this->_getPageCodeDbResult($iPageCode)) )
159
		{
160
			while($page = $oRes->fetchRow(MYSQL_ASSOC))
161
			{
162
			if(!$this->_oApp->page_is_visible($page)) {continue;}
163
			$aRetval[$page['language']] = $page;
164
			}
165
		}
166
		return $aRetval;
167
	}
168

    
169
	private function _getPageList($parent, $this_page=0 )
170
	{
171
		static $entries = array();
172
		if( ($oLang = $this->_getPageListDbResult($parent)) )
173
		{
174
			while($value = $oLang->fetchRow(MYSQL_ASSOC))
175
			{
176
				if (( $value['page_id'] != $this_page ) )
177
				{
178
				$entries [$value['page_id']]['language'] = $value['language'];
179
				$entries [$value['page_id']]['menu_title'] = $value['menu_title'];
180
				$this->_getPageList($value['page_id'], $this_page );
181
				}
182
			}
183
		}
184
		return $entries;
185
	}
186

    
187

    
188
    protected function getConfig($sFilename)
189
    {
190
        if(is_readable($sFilename)){
191
            return parse_ini_file($sFilename, true);
192
        }else {
193
            return null;
194
        }
195
    }
196

    
197

    
198
	private function _getAllowedLanguagesFromAddons($sLangKey='')
199
	{
200
		$aLangAddons = array();
201
		if( ($oLang = $this->_getLangAddonsDbResult($sLangKey)) )
202
		{
203
			while( $aLang = $oLang->fetchRow(MYSQL_ASSOC) )
204
			{
205
				$aLangAddons[$aLang['directory']] = $aLang['name'];
206
			}
207
		}
208
		return $aLangAddons;
209
	}
210

    
211
	/**
212
	 * 
213
	 * 
214
	 * @param 
215
	 * @return array of first visible language pages with defined fields
216
	 */
217
	private function _getLanguagesDetailsInUsed ( $sLangKey='' ) 
218
	{
219
		$aRetval = array();
220
		if( ($oRes = $this->_getLangInUsedDbResult($sLangKey)) ) 
221
		{
222
			while($page = $oRes->fetchRow(MYSQL_ASSOC))
223
			{
224
				if(!$this->_oApp->page_is_visible($page)) {continue;}
225
				$aRetval[$page['language']] = $page;
226
			}
227
		}
228
		return $aRetval;
229
	}
230
	
231
	/**
232
	* m_MultiLingual_Lib::getLangMenuData()
233
	* 
234
	* @param mixed $config
235
	* @param mixed $oApp
236
	* @return
237
	*/
238
	private function _getLangMenuData ( ) 
239
	{
240
		$data = array();
241
		$SetLanguageUrl = array();
242
		$SetLanguageIcons = array();
243
		$SetLanguageIcons = $this->_getLanguagesDetailsInUsed( );
244
		if(sizeof($SetLanguageIcons)>1) 
245
		{
246
			$pages = $this->_getPageCodeValues( $this->_oApp->page_code );
247
			$tmppage = array_intersect_key($pages,$SetLanguageIcons);
248
			$pages = array_merge($SetLanguageIcons,$tmppage);
249
			foreach ( $SetLanguageIcons AS $value) 
250
			{
251
				$data[] = array(
252
				      'sIconUrl' => $this->_oReg->AppRel . 'modules/' . basename(dirname(__FILE__)) . '/',
253
				      'bCurrent' => ( ($value['language'] == $this->_oReg->Language ) ? true : false),
254
				      'sUrl' => $this->_oReg->AppRel.$this->_oReg->PagesDir.trim($pages[$value['language']]['link'],'/').$this->_oReg->PageExtension,
255
				      'sTitle' => $pages[$value['language']]['page_title'],
256
				      'FilePrefix' => strtolower($pages[$value['language']]['language']),
257
				      'sExtension' => $this->_sExtension,
258
				);
259
			}
260
		}
261
		return $data;
262
	}
263

    
264
	/**
265
	* m_MultiLingual_Lib::getLangMenu()
266
	* 
267
	* @param mixed $config
268
	* @param mixed $oApp
269
	* @return
270
	*/
271
	private function _getLangMenuTwig ( ) 
272
	{
273
		$loader = new Twig_Loader_Filesystem( dirname(__FILE__).$this->_aTwigLoader['templates_dir'] );
274
		$twig   = new Twig_Environment( $loader );
275
		$data['aTargetList']   = $this->_getLangMenuData( );
276
		return $twig->render($this->_aTwigLoader['default_template'], $data);
277
	}
278

    
279
    private function _detectIE()
280
    {
281
        preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $aMatches);
282

    
283
        if (count($aMatches)>1){
284
          return true;
285
        }
286
        return false;
287
    }
288
    
289
	public function setExtension($sExtension = 'auto') 
290
	{   
291
		if($sExtension == 'auto' || $sExtension == 'svg') {
292
			$this->_sExtension = ($this->_detectIE() == true) ? 'png' : 'svg';
293
		} else {
294
			$this->_sExtension = 'png';
295
		}
296
		return;
297
	}
298
    
299
	public function getLangMenu() 
300
	{
301
		return $this->_getLangMenuTwig ( );
302
	}
303

    
304
	public function updateDefaultPagesCode (  ) 
305
	{
306
		$retVal  = false;
307
		$aLangs  = $this->_getLanguagesDetailsInUsed(  );
308
		$entries = $this->_getPageList( 0 );
309
// fill page_code with page_id for default_language
310
		while( list( $page_id, $val ) = each ( $entries ) )
311
		{
312
			if( $val['language'] == $this->_oReg->DefaultLanguage ) {
313
				if( ($retVal = $this->_updatePageCode((int)$page_id, 'pages', (int)$page_id ))==false ){ break;  }
314
			}
315
		}
316
		return $retVal;
317
	}
318
	
319
}
(1-1/11)