Project

General

Profile

1 1939 darkviper
<?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 1970 Luisehahne
 * @revision     $Revision$
30
 * @link         $HeadURL$
31
 * @lastmodified $Date$
32 1939 darkviper
 * @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 1945 darkviper
	private $_config      = array();
49 1939 darkviper
/** @var array set several values for Twig_Environment */
50 1945 darkviper
	private $_aTwigEnv    = array();
51 1939 darkviper
/** @var array set several values for Twig_Loader */
52
	private $_aTwigLoader = array();
53 1945 darkviper
/** @var string set icon extension */
54
	private $_sExtension  = array();
55 1939 darkviper
/**
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 1945 darkviper
		$this->_oApp        = (isset($GLOBALS['admin']) ? $GLOBALS['admin'] : $GLOBALS['wb']);
63
		$this->_oDb         = WbDatabase::getInstance();
64
		$this->_oReg        = WbAdaptor::getInstance();
65 1970 Luisehahne
		$this->_config      = $this->_aConfig = $this->getConfig((dirname(__FILE__)).'/default.ini');
66 1945 darkviper
		$this->_aTwigEnv    = $this->_config['twig-environment'];
67 1939 darkviper
		$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 1972 Luisehahne
		     .   'AND (`visibility`!=\'none\' '
102
		     .   'AND `visibility`!=\'hidden\') '
103 1939 darkviper
		     .   ( ($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 1970 Luisehahne
    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 1939 darkviper
197 1970 Luisehahne
198 1939 darkviper
	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 1941 darkviper
				      'sIconUrl' => $this->_oReg->AppRel . 'modules/' . basename(dirname(__FILE__)) . '/',
253 1939 darkviper
				      '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 1945 darkviper
				      'sExtension' => $this->_sExtension,
258 1939 darkviper
				);
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 1945 darkviper
    private function _detectIE()
280
    {
281
        preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $aMatches);
282 1939 darkviper
283 1945 darkviper
        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 1939 darkviper
	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 1970 Luisehahne
			if( $val['language'] == $this->_oReg->DefaultLanguage ) {
313 1939 darkviper
				if( ($retVal = $this->_updatePageCode((int)$page_id, 'pages', (int)$page_id ))==false ){ break;  }
314
			}
315
		}
316
		return $retVal;
317
	}
318
319
}