Project

General

Profile

1
<?php
2

    
3
/**
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
/**
20
 * PageTree generator
21
 *
22
 * @category     WbACP
23
 * @package      WbACP_Pages
24
 * @author       Werner v.d.Decken <wkl@isteam.de>
25
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
26
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
27
 * @version      1.0.0
28
 * @revision     $Revision: 1846 $
29
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/PageTree.php $
30
 * @lastmodified $Date: 2013-01-05 03:04:08 +0100 (Sat, 05 Jan 2013) $
31
 * @since        file added on 2012-12-21
32
 */
33
	
34
class a_pages_PageTree
35
{
36
/** @var array language definitions */
37
	private $_TEXT     = null;
38
/** @var array language definitions */
39
	private $_MESSAGE  = null;
40
/** @var array language definitions */
41
	private $_HEADING  = null;
42
/** @var object instance of the application object */
43
	private $_oApp     = null;
44
/** @var object instance of the database object */
45
	private $_oDb      = null;
46
/** @var array holds several values from the application global scope */	
47
	private $_aReg     = array();
48
/** @var string full HTML formattet list of pages */
49
	private $_sOutput         = '';
50
/** @var integer number of all reachable pages */	
51
	private $_iPagesTotal     = 0;
52
/** @var integer number of all writeable pages */	
53
	private $_iPagesWriteable = 0;
54
/** @var integer index for toggle background color of the list */	
55
	private $_iLineColor      = 0;
56
/** @var array entries to build a select list for parents */	
57
	private $_aParentList     = array();
58
/** @var integer count all executed database requests passing all iterations. */
59
	private $_queries = 0;
60
/**
61
 * constructor used to import some application constants and objects
62
 */	
63
	public function __construct() 
64
	{
65
		// import global vars and objects
66
		$this->_wbAdaptor();
67
	}
68
/**
69
 * parse the page tree and return
70
 * @param int use page-ID as root of the generated page tree. (default: 0)
71
 * @return type
72
 */	
73
	public function parseTree($iTreeRoot = 0) {
74
		return $this->_createTree($iTreeRoot);
75
	}
76
/**
77
 *  parse the page tree and print it out
78
 * @param int use page-ID as root of the generated page tree. (default: 0)
79
 */	
80
	public function displayTree($iTreeRoot = 0) {
81
		echo $this->parseTree($iTreeRoot);
82
	}
83
/**
84
 * total number of found pages which are visible for actual user
85
 * @return integer
86
 */	
87
	public function getTotalPages() {
88
		return $this->_iPagesTotal;
89
	}
90
/**
91
 * number of found pages which are writable for actual user
92
 * @return integer
93
 */	
94
	public function getWriteablePages() {
95
		return $this->_iPagesWriteable;
96
	}
97
/**
98
 * a list with all possible parent pages
99
 * @return array
100
 */	
101
	public function getParentList() {
102
		if(!$this->_sOutput) {
103
			$this->parseTree();
104
		}
105
		return $this->_aParentList;
106
	}
107
/**
108
 * used to import some WB-constants and objects
109
 */	
110
	private function _wbAdaptor()
111
	{
112
		$this->_TEXT        = $GLOBALS['TEXT'];
113
		$this->_MESSAGE     = $GLOBALS['MESSAGE'];
114
		$this->_HEADING     = $GLOBALS['HEADING'];
115
		$this->_oApp        = $GLOBALS['admin'];
116
		$this->_oDb         = WbDatabase::getInstance();
117
		$this->_aReg['PAGE_TRASH']       = PAGE_TRASH;
118
		$this->_aReg['PAGE_LEVEL_LIMIT'] = PAGE_LEVEL_LIMIT;
119
		$this->_aReg['MANAGE_SECTIONS']  = MANAGE_SECTIONS;
120
		$this->_aReg['WB_URL']           = WB_URL;
121
		$this->_aReg['WB_REL']           = WB_REL;
122
		$this->_aReg['ACP_REL']          = ADMIN_REL;
123
		$this->_aReg['THEME_REL']        = THEME_REL;
124
		$this->_aReg['TABLE_PREFIX']     = TABLE_PREFIX;
125
	}
126
/**
127
 * create a page tree as a well formatted, unordered list
128
 * @param int use page-ID as root of the generated page tree. (default: 0)
129
 * @return string the whoole list
130
 */
131
	private function _createTree($iTreeRoot = 0)
132
	{
133
		// compose the complete list
134
		$sOutput = ''
135
		// build the head
136
		      . $this->_Tabs(0 , true).'<div class="jsadmin pages_list">'.PHP_EOL
137
		      . $this->_Tabs(1).'<table>'.PHP_EOL
138
		      . $this->_Tabs(1).'<tbody>'.PHP_EOL
139
		      . $this->_Tabs(1).'<tr class="pages_list_header">'.PHP_EOL
140
		      . $this->_Tabs(1).'<th class="header_list_menu_title">'.$this->_TEXT['VISIBILITY'].
141
		                        ' / '.$this->_TEXT['MENU_TITLE'].':</th>'.PHP_EOL
142
		      . $this->_Tabs(0).'<th class="header_list_page_title">'.$this->_TEXT['PAGE_TITLE'].
143
		                        '</th>'.PHP_EOL
144
		      . $this->_Tabs(0).'<th class="header_list_page_id">PID</th>'.PHP_EOL
145
		      . $this->_Tabs(0).'<th class="header_list_actions">'.$this->_TEXT['ACTIONS'].
146
		                        ':</th>'.PHP_EOL
147
		      . $this->_Tabs(0).'<th class="list_page_id">&nbsp;</th>'.PHP_EOL
148
		      . $this->_Tabs(-1).'</tr>'.PHP_EOL
149
		      . $this->_Tabs(-1).'</tbody>'.PHP_EOL
150
		      . $this->_Tabs(-1).'</table>'.PHP_EOL
151
		// generate the page lines
152
		      . $this->_IterateTree($iTreeRoot)
153
		// build the footer
154
		      . $this->_Tabs(-1).'</div>'.PHP_EOL;
155
		;
156
		$this->_sOutput = $sOutput;
157
		return $sOutput;
158
	}
159
/**
160
 * Create a string of multiple TABs to prettify the HTML-output
161
 * ingrease the number of TABs with a positive and degrease with an negative Value.
162
 * '0' means: do not change the value. Or set an absolute number using $bRelative=false
163
 * @staticvar int $iTabLevel
164
 * @param integer $iTabsDiv number of TABs to add/sub
165
 * @param bool $bRelative false if should be set to absolute value
166
 * @return string
167
 */
168
	private function _Tabs($iTabsDiv = 0, $bRelative = true)
169
	{
170
		static $iTabLevel = 0;
171
		$iTabLevel = ($bRelative ? $iTabLevel + $iTabsDiv : $iTabsDiv);
172
		$iTabLevel += ($iTabLevel < 0 ? 0 - $iTabLevel : $iTabsDiv);
173
		return str_repeat("\t", $iTabLevel);
174
	}
175
/**
176
 * compose the needed SQL statement
177
 * @param integer $iParentKey
178
 * @return string SQL statement
179
 */			
180
	private function _makeSql($iParentKey = 0)
181
	{
182
		if($this->_aReg['PAGE_TRASH'] != 'inline') {
183
			$sUseTrash = ' AND `visibility`!=\'deleted\'';
184
		}else { $sUseTrash = ''; }
185
		$sql = 'SELECT ( SELECT COUNT(*) '
186
		     .          'FROM `'.$this->_aReg['TABLE_PREFIX'].'pages` `x` '
187
		     .          'WHERE x.`parent`=p.`page_id`'
188
		     .        ') `children`, '
189
		     .        's.`module`, MAX(s.`publ_start` + s.`publ_end`) published, p.`link`, '
190
		     .        '(SELECT MAX(`position`) FROM `'.$this->_aReg['TABLE_PREFIX'].'pages` '
191
		     .        'WHERE `parent`='.$iParentKey.') max_position, '
192
		     .        '0 min_position, '
193
		     .        'p.`position`, '
194
		     .        'p.`page_id`, p.`parent`, p.`level`, p.`language`, p.`admin_groups`, '
195
		     .        'p.`admin_users`, p.`viewing_groups`, p.`viewing_users`, p.`visibility`, '
196
		     .        'p.`menu_title`, p.`page_title`, p.`page_trail` '
197
		     . 'FROM `'.$this->_aReg['TABLE_PREFIX'].'pages` p '
198
		     .    'INNER JOIN `'.$this->_aReg['TABLE_PREFIX'].'sections` s '
199
		     .    'ON p.`page_id`=s.`page_id` '
200
		     . 'WHERE `parent`='.$iParentKey.$sUseTrash.' '
201
		     . 'GROUP BY p.`page_id` '
202
		     . 'ORDER BY p.`position` ASC';
203
		return $sql;
204
	}
205
/**
206
 * iterate through all nodes which having subnodes 
207
 * @param integer start iteration from this parent page ( 0 = root)
208
 * @return string all of the item lines 
209
 */	
210
	private function _IterateTree($iParent = 0)
211
	{
212
		$sOutput = '';
213
		// Get page list from database
214
		if(($oPages = $this->_oDb->query($this->_makeSql($iParent)))) 
215
		{
216
			$this->_queries++;
217
			// output block-header
218
			$sOutput .= $this->_Tabs(0).'<ul id="p'.$iParent.'" class="page_list"';
219
			if(!$iParent) {
220
				$sOutput .= ' style="display: block;"';
221
			}else {
222
			// show block depending from Cookies
223
				if (isset ($_COOKIE['p'.$iParent]) && $_COOKIE['p'.$iParent] == '1') {
224
					$sOutput .= ' style="display: block;"';
225
				}
226
			}
227
			$sOutput .= '>'.PHP_EOL;
228
			$iMinPosition = 1;
229
			while($aPage = $oPages->fetchRow(MYSQL_ASSOC))
230
			{ // iterate through the current branch
231
				if($this->_aReg['PAGE_LEVEL_LIMIT'] && ($aPage['level'] > $this->_aReg['PAGE_LEVEL_LIMIT'])) {
232
					return '';
233
				}
234
				$aPage['min_position'] = ($aPage['position'] < $iMinPosition ? $aPage['position'] : $iMinPosition);
235
				$this->_iLineColor = $this->_iPagesTotal++ % 2;
236
				$aPage['iswriteable'] = false;
237
				if( $this->_oApp->ami_group_member($aPage['admin_users']) ||
238
					$this->_oApp->is_group_match($this->_oApp->get_groups_id(), $aPage['admin_groups']))
239
				{
240
					if(($aPage['visibility'] == 'deleted' && $this->_aReg['PAGE_TRASH'] == 'inline') ||
241
					   ($aPage['visibility'] != 'deleted'))
242
					{
243
						$aPage['iswriteable'] = true;
244
						$this->_iPagesWriteable++;
245
					}
246
				} else {
247
					if($aPage['visibility'] == 'private') { continue; }
248
				}
249
				// add this item to the secondary list of parents
250
				$this->_addToParentList($aPage);
251
				$sOutput .= $this->_createListItem($aPage);
252
			}
253
			$sOutput .= $this->_Tabs(-1).'</ul>'.PHP_EOL;
254
		}
255
		return $sOutput;
256
	}
257
/**
258
 * formating the given page object for output
259
 * @param type $aPage
260
 * @return string
261
 */
262
	private function _createListItem($aPage)
263
	{
264
	// output the current item
265
	// --- HEADER ------------------------------------------------------------------------
266
		$sOutput  = $this->_Tabs(0).'<li class="p'.$aPage['parent'].'">'.PHP_EOL
267
		          . $this->_Tabs(1).'<table class="pages_view">'.PHP_EOL
268
		          . $this->_Tabs(1).'<tbody>'.PHP_EOL
269
		          . $this->_Tabs(1).'<tr class="row_'.$this->_iLineColor.'">'.PHP_EOL;
270
	// --- TAB 1 --- (expand/collapse) ---------------------------------------------------
271
		$sOutput .= $this->_Tabs(1).'<td valign="middle" width="20" style="padding-left: '
272
		          . (int)($aPage['level']*20).'px;">';
273
		if((bool)$aPage['children']) {
274
			$sOutput .= '<a href="javascript:toggle_visibility(\'p'.$aPage['page_id'].'\');" '
275
			          . 'title="'.$this->_TEXT['EXPAND'].'/'.$this->_TEXT['COLLAPSE'].'">'
276
			          . '<span><img src="'.$this->_aReg['THEME_REL'].'/images/'
277
			          . ( ((isset($_COOKIE['p'.$aPage['page_id']]) 
278
						  && $_COOKIE['p'.$aPage['page_id']] == '1') ? 'minus' : 'plus')
279
						)
280
			          . '_16.png" onclick="toggle_plus_minus(\''.$aPage['page_id'].'\');" '
281
			          . 'name="plus_minus_'.$aPage['page_id'].'" alt="+" /></span></a>';
282
		}else {
283
			$sOutput .= '&nbsp;';
284
		}
285
		$sOutput .= '</td>'.PHP_EOL;
286
	// --- TAB 2 --- (menu title) --------------------------------------------------------
287
		$sOutput .= $this->_Tabs(0).'<td class="list_menu_title">';
288
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
289
			$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/modify.php?page_id='
290
			          . $aPage['page_id'].'" title="'.$this->_TEXT['MODIFY'].'">';
291
		}
292
		$sIcon = $this->_aReg['THEME_REL'].'/images/'.$aPage['visibility'].'_16.png';
293
		if(!is_readable($sIcon)) {
294
			$sIcon = $this->_aReg['THEME_REL'].'/images/public_16.png';
295
		}
296
		$sOutput .= '<img src="'.$sIcon.'" alt="'.$this->_TEXT['VISIBILITY'].': '
297
		          . $sText.'" class="page_list_rights" />';
298
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
299
			$sOutput .= '<span class="modify_link">'.$aPage['menu_title'].'</span></a>';
300
		}else {
301
			$sOutput .=  '<span class="bold grey">'.$aPage['menu_title'].'</span>';
302
		}
303
		$sOutput .= '</td>'.PHP_EOL;
304
	// --- TAB 3 --- (page title) --------------------------------------------------------
305
		$sOutput .= $this->_Tabs(0).'<td class="list_page_title">'.$aPage['page_title'].'</td>'.PHP_EOL;
306
	// --- TAB 4 --- (page ID) -----------------------------------------------------------
307
		$sOutput .= $this->_Tabs(0).'<td class="list_page_id right">'.$aPage['page_id'].'</td>'.PHP_EOL;
308
	// --- TAB 5 --- (show this page in new window) --------------------------------------
309
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
310
		if($aPage['visibility'] != 'deleted' && $aPage['visibility'] != 'none') {
311
			$sPageLink = $this->_aReg['WB_REL'].preg_replace(
312
			                                 '/^'.preg_quote($this->_aReg['WB_URL'], '/').'/siU', 
313
			                                 '', 
314
			                                 $this->_oApp->page_link($aPage['link'])
315
			                                );
316
			$sOutput .= '<a href="'.$sPageLink.'" target="_blank" title="'.$this->_TEXT['VIEW']
317
			          . '"><img src="'.$this->_aReg['THEME_REL'].'/images/view_16.png" alt="'
318
			          . $this->_TEXT['VIEW'].'" /></a>';
319
		}else { 
320
			$sOutput .= '<img src="'.$this->_aReg['THEME_REL'].'/images/blank_16.gif" alt=" " />'; 
321
		}
322
		$sOutput .= '</td>'.PHP_EOL;
323

    
324
	// --- TAB 6 --- (edit settings) -----------------------------------------------------
325
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
326
		if($aPage['visibility'] != 'deleted') { 
327
			if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
328
				$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/settings.php?page_id='
329
				          . $aPage['page_id'].'" title="'.$this->_TEXT['SETTINGS'].'">'
330
				          . '<img src="'.$this->_aReg['THEME_REL'].'/images/modify_16.png" alt="'
331
				          . $this->_TEXT['SETTINGS'].'" /></a>';
332
			}
333
		}else {
334
			$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/restore.php?page_id='.$aPage['page_id'].'" '
335
			          . 'title="'.$this->_TEXT['RESTORE'].'"><img src="'.$this->_aReg['THEME_REL']
336
			          . '/images/restore_16.png" alt="'.$this->_TEXT['RESTORE'].'" /></a>';
337
		}
338
		$sOutput .= '</td>'.PHP_EOL;
339

    
340
	// --- TAB 7 --- (edit sections) -----------------------------------------------------
341
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
342
		if( $this->_aReg['MANAGE_SECTIONS'] && $this->_oApp->get_permission('pages_add') && $aPage['iswriteable'] ) {
343
			$file = $this->_oApp->page_is_active($aPage) ? "clock_16.png" : "clock_red_16.png";
344
			$file = ($aPage['published'] && $aPage['module'] != 'menu_link') ? $file : 'noclock_16.png';
345
			$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/sections.php?page_id='
346
			          . $aPage['page_id'].'" title="'.$this->_HEADING['MANAGE_SECTIONS'].'">'
347
			          . '<img src="'.$this->_aReg['THEME_REL'].'/images/'.$file.'" alt="'
348
			          . $this->_HEADING['MANAGE_SECTIONS'].'" /></a>';
349
		}else { 
350
			$sOutput .= '<img src="'.$this->_aReg['THEME_REL'].'/images/blank_16.gif" alt=" " />'; 
351
		}
352
		$sOutput .= '</td>'.PHP_EOL;
353

    
354
	// --- TAB 8 --- (move up) -----------------------------------------------------------
355
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
356
		if($aPage['position'] > $aPage['min_position']) {
357
			if($aPage['visibility'] != 'deleted') {
358
				if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
359
					$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/move_up.php?page_id='
360
					          . $aPage['page_id'].'" title="'.$this->_TEXT['MOVE_UP']
361
					          . '"><img src="'.$this->_aReg['THEME_REL'].'/images/up_16.png" alt="'
362
					          . $this->_TEXT['MOVE_UP'].'" /></a>';
363
				}
364
			}
365
		}
366
		$sOutput .= '</td>'.PHP_EOL;
367

    
368
	// --- TAB 9 --- (move down) ---------------------------------------------------------
369
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
370
		if($aPage['position'] < $aPage['max_position']) {
371
			if($aPage['visibility'] != 'deleted') {
372
				if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
373
					$sOutput .= '<a href="'.$this->_aReg['ACP_REL'].'/pages/move_down.php?page_id='
374
					          . $aPage['page_id'].'" title="'.$this->_TEXT['MOVE_DOWN']
375
					          . '"><img src="'.$this->_aReg['THEME_REL'].'/images/down_16.png" alt="'
376
					          . $this->_TEXT['MOVE_DOWN'].'" /></a>';
377
				}
378
			}
379
		}
380
		$sOutput .= '</td>'.PHP_EOL;
381

    
382
	// --- TAB 10 --- (delete page) ------------------------------------------------------
383
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
384
		if($this->_oApp->get_permission('pages_delete') && $aPage['iswriteable']) {
385
			$sOutput .= '<a href="javascript:confirm_link(pages_delete_confirm+\'?\',\''
386
			          . $this->_aReg['ACP_REL'].'/pages/delete.php?page_id='
387
			          . $this->_oApp->getIDKEY($aPage['page_id']).'\');" title="'
388
			          . $this->_TEXT['DELETE'].'"><img src="'.$this->_aReg['THEME_REL']
389
			          . '/images/delete_16.png" alt="'.$this->_TEXT['DELETE'].'" /></a>';
390
		}else { 
391
			$sOutput .= '<img src="'.$this->_aReg['THEME_REL'].'/images/blank_16.gif" alt=" " />'; 
392
		}
393
		$sOutput .= '</td>'.PHP_EOL;
394

    
395
	// --- TAB 11 --- (add child page) ---------------------------------------------------
396
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
397
		if( 
398
			$this->_oApp->get_permission('pages_add')
399
			&& $aPage['iswriteable'] 
400
			&& ($aPage['visibility'] != 'deleted') 
401
			&& $aPage['level'] < ($this->_aReg['PAGE_LEVEL_LIMIT'] - 1)
402
		  )
403
		{
404
			$sOutput .= '<a href="javascript:add_child_page(\''.$aPage['page_id'].'\');" '
405
			          . 'title="'.$this->_HEADING['ADD_CHILD_PAGE'].'"><img src="'
406
			          . $this->_aReg['THEME_REL'].'/images/siteadd.png" name="addpage_'.$aPage['page_id']
407
			          . '" alt="'.$this->_HEADING['ADD_CHILD_PAGE'].'" /></a>';
408
		}else { 
409
			$sOutput .= '&nbsp;'; 
410
		}
411
		$sOutput .= '</td>'.PHP_EOL;
412
	// --- TAB 12 --- (show language) ----------------------------------------------------
413
		$sOutput .= $this->_Tabs(0).'<td class="list_page_id center">'.$aPage['language'].'</td>'.PHP_EOL;
414
	// --- FOOTER ------------------------------------------------------------------------
415
		$sOutput .= $this->_Tabs(-1).'</tr>'.PHP_EOL
416
		          . $this->_Tabs(-1).'</tbody>'.PHP_EOL
417
		          . $this->_Tabs(-1).'</table>'.PHP_EOL;
418
	// if there children, iterate through this children now
419
		if((bool)$aPage['children']) {
420
			$sOutput .= $this->_IterateTree($aPage['page_id']);
421
		}
422
		$sOutput .= $this->_Tabs(-1).'</li>'.PHP_EOL;
423
		return $sOutput;
424
	} // end of method _createListItem
425
/**
426
 * build a list of possible parent pages
427
 * @param array $aPage 
428
 */	
429
	private function _addToParentList(array $aPage)
430
	{
431
		if( ($aPage['level'] < ($this->_aReg['PAGE_LEVEL_LIMIT'] - 1))
432
			&& $aPage['iswriteable'] 
433
			&& ($aPage['visibility'] != 'deleted')
434
			&& $this->_oApp->get_permission('pages_add') ) 
435
		{
436
			$aNewEntry = array();
437
			$aNewEntry['ID']             = $aPage['page_id'];
438
			$aNewEntry['TITLE']          = $aPage['menu_title'];
439
			$aNewEntry['DISABLED']       = ($aPage['iswriteable'] ? 0 : 1);
440
			$aNewEntry['PARENT']         = $aPage['parent'];
441
			$aNewEntry['FLAG_ROOT_ICON'] = '';
442
			$aNewEntry['LEVEL']          = $aPage['level'];
443
			$aNewEntry['LANGUAGE']       = $aPage['language'];
444
			$this->_aParentList[]        = $aNewEntry;
445
		}
446
	}
447
	
448
} // end of class PageTree
(1-1/23)