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: $
30
 * @link         $HeadURL: $
31
 * @lastmodified $Date: $
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      = parse_ini_file(dirname(__FILE__).'/default.ini',true);
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

    
189
	private function _getAllowedLanguagesFromAddons($sLangKey='')
190
	{
191
		$aLangAddons = array();
192
		if( ($oLang = $this->_getLangAddonsDbResult($sLangKey)) )
193
		{
194
			while( $aLang = $oLang->fetchRow(MYSQL_ASSOC) )
195
			{
196
				$aLangAddons[$aLang['directory']] = $aLang['name'];
197
			}
198
		}
199
		return $aLangAddons;
200
	}
201

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

    
255
	/**
256
	* m_MultiLingual_Lib::getLangMenu()
257
	* 
258
	* @param mixed $config
259
	* @param mixed $oApp
260
	* @return
261
	*/
262
	private function _getLangMenuTwig ( ) 
263
	{
264
		$loader = new Twig_Loader_Filesystem( dirname(__FILE__).$this->_aTwigLoader['templates_dir'] );
265
		$twig   = new Twig_Environment( $loader );
266
		$data['aTargetList']   = $this->_getLangMenuData( );
267
		return $twig->render($this->_aTwigLoader['default_template'], $data);
268
	}
269

    
270
    private function _detectIE()
271
    {
272
        preg_match('/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $aMatches);
273

    
274
        if (count($aMatches)>1){
275
          return true;
276
        }
277
        return false;
278
    }
279
    
280
	public function setExtension($sExtension = 'auto') 
281
	{   
282
		if($sExtension == 'auto' || $sExtension == 'svg') {
283
			$this->_sExtension = ($this->_detectIE() == true) ? 'png' : 'svg';
284
		} else {
285
			$this->_sExtension = 'png';
286
		}
287
		return;
288
	}
289
    
290
	public function getLangMenu() 
291
	{
292
		return $this->_getLangMenuTwig ( );
293
	}
294

    
295
	public function updateDefaultPagesCode (  ) 
296
	{
297
		$retVal  = false;
298
		$aLangs  = $this->_getLanguagesDetailsInUsed(  );
299
		$entries = $this->_getPageList( 0 );
300
// fill page_code with page_id for default_language
301
		while( list( $page_id, $val ) = each ( $entries ) )
302
		{
303
			if( $val['language'] == $this->_oReg->DefaultLangauage ) {
304
				if( ($retVal = $this->_updatePageCode((int)$page_id, 'pages', (int)$page_id ))==false ){ break;  }
305
			}
306
		}
307
		return $retVal;
308
	}
309
	
310
}
(1-1/11)