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
/**
54
 * constructor used to import some application constants and objects
55
 */	
56
	public function __construct() 
57
	{
58
		// import global vars and objects
59
		$this->_wbAdaptor();
60
	}
61

    
62
	/**
63
	 * used to import some WB-constants and objects
64
	 */	
65
	private function _wbAdaptor()
66
	{
67
		if(!defined('ADMIN_REL')) { define('ADMIN_REL', WB_REL.'/'.ADMIN_DIRECTORY); }
68

    
69
		$this->_oApp   = (isset($GLOBALS['admin']) ? $GLOBALS['admin'] : $GLOBALS['wb']);
70
		$this->_oDb    = WbDatabase::getInstance();
71
		$this->_oReg = WbAdaptor::getInstance();
72

    
73
		$this->_config = parse_ini_file(dirname(__FILE__).'/default.ini',true);
74
		$this->_aTwigEnv = $this->_config['twig-environment'];
75
		$this->_aTwigLoader = $this->_config['twig-loader-file'];
76

    
77
	}
78

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

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

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

    
135
	/**
136
	 * compose the needed SQL statement
137
	 * @param integer $sLangKey
138
	 * @return database object with given SQL statementt
139
	 */
140
	private function _getLangAddonsDbResult ( $sLangKey='' ) 
141
	{
142
		$sql = 'SELECT `directory`,`name`  FROM `'.$this->_oReg->TablePrefix.'addons` '
143
		     . 'WHERE `type` = \'language\' '
144
		     . ( ($sLangKey!='') ? ' AND `directory` = \''.$langKey.'\' ' : '')
145
		     . 'ORDER BY `directory`';
146
		return $this->_oDb->query($sql);
147
	}
148

    
149
	/**
150
	 * 
151
	 * @param integer $parent
152
	 * @return database object with given SQL statement
153
	 */
154
	private function _getPageListDbResult ( $parent ) 
155
	{
156
	    $sql = 'SELECT `language`,'
157
		     .        '`page_id`,`page_title`,`menu_title`, `page_code`, `parent` '
158
		     . 'FROM `'.$this->_oReg->TablePrefix.'pages` '
159
		     . 'WHERE `parent` = '.$parent. ' '
160
		     . 'ORDER BY `position`';
161
		return $this->_oDb->query($sql);
162
	}
163

    
164
	private function _getPageCodeValues(  $iPageCode=0 )
165
	{
166
		$aRetval = array();
167
		if( ($oRes = $this->_getPageCodeDbResult($iPageCode)) )
168
		{
169
			while($page = $oRes->fetchRow(MYSQL_ASSOC))
170
			{
171
			if(!$this->_oApp->page_is_visible($page)) {continue;}
172
			$aRetval[$page['language']] = $page;
173
			}
174
		}
175
		return $aRetval;
176
	}
177

    
178
	private function _getPageList($parent, $this_page=0 )
179
	{
180
		static $entries = array();
181
		if( ($oLang = $this->_getPageListDbResult($parent)) )
182
		{
183
			while($value = $oLang->fetchRow(MYSQL_ASSOC))
184
			{
185
				if (( $value['page_id'] != $this_page ) )
186
				{
187
				$entries [$value['page_id']]['language'] = $value['language'];
188
				$entries [$value['page_id']]['menu_title'] = $value['menu_title'];
189
				$this->_getPageList($value['page_id'], $this_page );
190
				}
191
			}
192
		}
193
		return $entries;
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
				);
258
			}
259
		}
260
		return $data;
261
	}
262

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

    
278

    
279
	public function getLangMenu() 
280
	{
281
		return $this->_getLangMenuTwig ( );
282
	}
283

    
284
	public function updateDefaultPagesCode (  ) 
285
	{
286
		$retVal  = false;
287
		$aLangs  = $this->_getLanguagesDetailsInUsed(  );
288
		$entries = $this->_getPageList( 0 );
289
// fill page_code with page_id for default_language
290
		while( list( $page_id, $val ) = each ( $entries ) )
291
		{
292
			if( $val['language'] == $this->_oReg->DefaultLangauage ) {
293
				if( ($retVal = $this->_updatePageCode((int)$page_id, 'pages', (int)$page_id ))==false ){ break;  }
294
			}
295
		}
296
		return $retVal;
297
	}
298
	
299
}
(1-1/11)