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 2000 darkviper
		$sql = 'UPDATE `'.$this->_oDb->TablePrefix.$sTable.'` '
82 1939 darkviper
		     . '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 2000 darkviper
		     .                 '`visibility`,`viewing_groups`,`viewing_users`,`position`,'
98
		     .                 '`page_title`,`tooltip` '
99
		     . 'FROM `'.$this->_oDb->TablePrefix.'pages` '
100 1939 darkviper
		     . 'WHERE `level`= \'0\' '
101
		     .   'AND `root_parent`=`page_id` '
102 1972 Luisehahne
		     .   'AND (`visibility`!=\'none\' '
103
		     .   'AND `visibility`!=\'hidden\') '
104 1939 darkviper
		     .   ( ($sLangKey!='') ? ' AND `language` = \''.$sLangKey.'\'' : '')
105
		     .   'GROUP BY `language` '
106
		     .   'ORDER BY `position`';
107
		return $this->_oDb->query($sql);
108
	}
109
110
	/**
111
	*
112
	* search for pages with given page code and create a DB result object
113
	* @param integer Pagecode to search for
114
	* @return object result object or null on error
115
	*/
116
	private function _getPageCodeDbResult( $iPageCode )
117
	{
118
		$sql = 'SELECT `language`,'
119
		     .        '`page_id`,`level`,`parent`,`root_parent`,`page_code`,`link`,'
120 2000 darkviper
		     .        '`visibility`,`viewing_groups`,`viewing_users`,`position`,'
121
		     .        '`page_title`,`tooltip` '
122
		     .  'FROM `'.$this->_oDb->TablePrefix.'pages`'
123 1939 darkviper
		     .  'WHERE `page_code` = '.$iPageCode.' '
124
		     .  'ORDER BY `position`';
125
		return $this->_oDb->query($sql);
126
	}
127
128
	/**
129
	 * compose the needed SQL statement
130
	 * @param integer $sLangKey
131
	 * @return database object with given SQL statementt
132
	 */
133
	private function _getLangAddonsDbResult ( $sLangKey='' )
134
	{
135 2000 darkviper
		$sql = 'SELECT `directory`,`name`  FROM `'.$this->_oDb->TablePrefix.'addons` '
136 1939 darkviper
		     . 'WHERE `type` = \'language\' '
137
		     . ( ($sLangKey!='') ? ' AND `directory` = \''.$langKey.'\' ' : '')
138
		     . 'ORDER BY `directory`';
139
		return $this->_oDb->query($sql);
140
	}
141
142
	/**
143
	 *
144
	 * @param integer $parent
145
	 * @return database object with given SQL statement
146
	 */
147
	private function _getPageListDbResult ( $parent )
148
	{
149 2000 darkviper
	    $sql = 'SELECT `language`,`tooltip`,'
150 1939 darkviper
		     .        '`page_id`,`page_title`,`menu_title`, `page_code`, `parent` '
151 2000 darkviper
		     . 'FROM `'.$this->_oDb->TablePrefix.'pages` '
152 1939 darkviper
		     . 'WHERE `parent` = '.$parent. ' '
153
		     . 'ORDER BY `position`';
154
		return $this->_oDb->query($sql);
155
	}
156
157
	private function _getPageCodeValues(  $iPageCode=0 )
158
	{
159
		$aRetval = array();
160
		if( ($oRes = $this->_getPageCodeDbResult($iPageCode)) )
161
		{
162
			while($page = $oRes->fetchRow(MYSQL_ASSOC))
163
			{
164 2000 darkviper
				if(!$this->_oApp->page_is_visible($page)) {continue;}
165
				$aRetval[$page['language']] = $page;
166 1939 darkviper
			}
167
		}
168
		return $aRetval;
169
	}
170
171
	private function _getPageList($parent, $this_page=0 )
172
	{
173
		static $entries = array();
174
		if( ($oLang = $this->_getPageListDbResult($parent)) )
175
		{
176
			while($value = $oLang->fetchRow(MYSQL_ASSOC))
177
			{
178
				if (( $value['page_id'] != $this_page ) )
179
				{
180 2000 darkviper
					$entries [$value['page_id']]['language'] = $value['language'];
181
					$entries [$value['page_id']]['menu_title'] = $value['menu_title'];
182
					$this->_getPageList($value['page_id'], $this_page );
183 1939 darkviper
				}
184
			}
185
		}
186
		return $entries;
187
	}
188
189
190 1970 Luisehahne
    protected function getConfig($sFilename)
191
    {
192
        if(is_readable($sFilename)){
193
            return parse_ini_file($sFilename, true);
194
        }else {
195
            return null;
196
        }
197
    }
198 1939 darkviper
199 1970 Luisehahne
200 1939 darkviper
	private function _getAllowedLanguagesFromAddons($sLangKey='')
201
	{
202
		$aLangAddons = array();
203
		if( ($oLang = $this->_getLangAddonsDbResult($sLangKey)) )
204
		{
205
			while( $aLang = $oLang->fetchRow(MYSQL_ASSOC) )
206
			{
207
				$aLangAddons[$aLang['directory']] = $aLang['name'];
208
			}
209
		}
210
		return $aLangAddons;
211
	}
212
213
	/**
214
	 *
215
	 *
216
	 * @param
217
	 * @return array of first visible language pages with defined fields
218
	 */
219
	private function _getLanguagesDetailsInUsed ( $sLangKey='' )
220
	{
221
		$aRetval = array();
222
		if( ($oRes = $this->_getLangInUsedDbResult($sLangKey)) )
223
		{
224
			while($page = $oRes->fetchRow(MYSQL_ASSOC))
225
			{
226
				if(!$this->_oApp->page_is_visible($page)) {continue;}
227
				$aRetval[$page['language']] = $page;
228
			}
229
		}
230
		return $aRetval;
231
	}
232
233
	/**
234
	* m_MultiLingual_Lib::getLangMenuData()
235
	*
236
	* @param mixed $config
237
	* @param mixed $oApp
238
	* @return
239
	*/
240
	private function _getLangMenuData ( )
241
	{
242 2000 darkviper
		$aTplData = array();
243
	// get root pages for all used languages
244
		$aAllowedRootLanguages = $this->_getLanguagesDetailsInUsed( );
245
		if(sizeof($aAllowedRootLanguages)>1)
246 1939 darkviper
		{
247 2000 darkviper
		// get all pages witch the same page_code
248
			$aMatchingPages = $this->_getPageCodeValues( $this->_oApp->page['page_code'] );
249
		// remove all pages from list with not avaliable languages
250
			$aPossibleMatchingPages = array_intersect_key($aMatchingPages, $aAllowedRootLanguages);
251
		// add Allowed root pages to possible matches
252
			$aAvailablePages = array_merge($aPossibleMatchingPages, $aAllowedRootLanguages);
253
			foreach ( $aAvailablePages AS $aPage)
254 1939 darkviper
			{
255 2000 darkviper
				$aTplData[] = array(
256
				    'sIconUrl'         => $this->_oReg->AppRel . 'modules/'
257
					                    . basename(dirname(__FILE__)) . '/',
258
				    'bCurrentLanguage' => (($aPage['language'] == $this->_oReg->Language) ? true : false),
259
				    'sTargetPageUrl'   => $this->_oReg->AppRel . $this->_oReg->PagesDir
260
				                        . trim($aAvailablePages[$aPage['language']]['link'],'/')
261
					                    . $this->_oReg->PageExtension,
262
				    'sPageTitle'       => $aAvailablePages[$aPage['language']]['page_title'],
263
				    'sFilename'        => strtolower($aAvailablePages[$aPage['language']]['language']),
264
				    'sImageType'       => $this->_sExtension,
265
					'sToolTip'         => $aAvailablePages[$aPage['language']]['tooltip']
266 1939 darkviper
				);
267
			}
268
		}
269 2000 darkviper
		return $aTplData;
270 1939 darkviper
	}
271
272
	/**
273
	* m_MultiLingual_Lib::getLangMenu()
274
	*
275
	* @param mixed $config
276
	* @param mixed $oApp
277
	* @return
278
	*/
279
	private function _getLangMenuTwig ( )
280
	{
281
		$loader = new Twig_Loader_Filesystem( dirname(__FILE__).$this->_aTwigLoader['templates_dir'] );
282
		$twig   = new Twig_Environment( $loader );
283
		$data['aTargetList']   = $this->_getLangMenuData( );
284
		return $twig->render($this->_aTwigLoader['default_template'], $data);
285
	}
286
287 1945 darkviper
    private function _detectIE()
288
    {
289
        preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $aMatches);
290
        if (count($aMatches)>1){
291
          return true;
292
        }
293
        return false;
294
    }
295
296
	public function setExtension($sExtension = 'auto')
297
	{
298
		if($sExtension == 'auto' || $sExtension == 'svg') {
299
			$this->_sExtension = ($this->_detectIE() == true) ? 'png' : 'svg';
300
		} else {
301
			$this->_sExtension = 'png';
302
		}
303
		return;
304
	}
305
306 1939 darkviper
	public function getLangMenu()
307
	{
308
		return $this->_getLangMenuTwig ( );
309
	}
310
311
	public function updateDefaultPagesCode (  )
312
	{
313
		$retVal  = false;
314
		$aLangs  = $this->_getLanguagesDetailsInUsed(  );
315
		$entries = $this->_getPageList( 0 );
316
// fill page_code with page_id for default_language
317
		while( list( $page_id, $val ) = each ( $entries ) )
318
		{
319 1970 Luisehahne
			if( $val['language'] == $this->_oReg->DefaultLanguage ) {
320 1939 darkviper
				if( ($retVal = $this->_updatePageCode((int)$page_id, 'pages', (int)$page_id ))==false ){ break;  }
321
			}
322
		}
323
		return $retVal;
324
	}
325
326
}