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: 2021 $
29
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/PageTree.php $
30
 * @lastmodified $Date: 2013-12-06 11:34:25 +0100 (Fri, 06 Dec 2013) $
31
 * @since        file added on 2012-12-21
32
 */
33
	
34
class a_pages_PageTree
35
{
36
/** @var object instance of the application object */
37
	protected $_oApp     = null;
38
/** @var object instance of the database object */
39
	protected $_oDb      = null;
40
/** @var object instance holds several values from the application global scope */
41
	protected $_oReg     = null;
42
/** @var object instance holds all of the translations */
43
	protected $_oTrans   = null;
44
/** @var string full HTML formattet list of pages */
45
	protected $_sOutput         = '';
46
/** @var integer number of all reachable pages */	
47
	protected $_iPagesTotal     = 0;
48
/** @var integer number of all writeable pages */	
49
	protected $_iPagesWriteable = 0;
50
/** @var integer index for toggle background color of the list */	
51
	protected $_iLineColor      = 0;
52
/** @var array entries to build a select list for parents */	
53
	protected $_aParentList     = array();
54
/** @var integer count all executed database requests passing all iterations. */
55
	protected $_queries = 0;
56
/**
57
 * constructor used to import some application constants and objects
58
 */	
59
	public function __construct() 
60
	{
61
		// import global vars and objects
62
		$this->_oApp   = $GLOBALS['admin'];
63
		$this->_oDb    = WbDatabase::getInstance();
64
		$this->_oReg   = WbAdaptor::getInstance();
65
		$this->_oTrans = Translate::getInstance();
66
	}
67
/**
68
 * parse the page tree and return
69
 * @param int use page-ID as root of the generated page tree. (default: 0)
70
 * @return type
71
 */	
72
	public function parseTree($iTreeRoot = 0) {
73
		return $this->_createTree($iTreeRoot);
74
	}
75
/**
76
 *  parse the page tree and print it out
77
 * @param int use page-ID as root of the generated page tree. (default: 0)
78
 */	
79
	public function displayTree($iTreeRoot = 0) {
80
		echo $this->parseTree($iTreeRoot);
81
	}
82
/**
83
 * total number of found pages which are visible for actual user
84
 * @return integer
85
 */	
86
	public function getTotalPages() {
87
		return $this->_iPagesTotal;
88
	}
89
/**
90
 * number of found pages which are writable for actual user
91
 * @return integer
92
 */	
93
	public function getWriteablePages() {
94
		return $this->_iPagesWriteable;
95
	}
96
/**
97
 * a list with all possible parent pages
98
 * @return array
99
 */	
100
	public function getParentList($iTreeRoot = 0) {
101
		if(!$this->_sOutput) {
102
			$this->parseTree($iTreeRoot);
103
		}
104
		return $this->_aParentList;
105
	}
106
/**
107
 * create a page tree as a well formatted, unordered list
108
 * @param int use page-ID as root of the generated page tree. (default: 0)
109
 * @return string the whoole list
110
 */
111
	protected function _createTree($iTreeRoot = 0)
112
	{
113
		// compose the complete list
114
		$sOutput = ''
115
		// build the head
116
		      . $this->_Tabs(0 , true).'<div class="jsadmin pages_list">'.PHP_EOL
117
		      . $this->_Tabs(1).'<table>'.PHP_EOL
118
		      . $this->_Tabs(1).'<tbody>'.PHP_EOL
119
		      . $this->_Tabs(1).'<tr class="pages_list_header">'.PHP_EOL
120
		      . $this->_Tabs(1).'<th style="width:20px;">'.PHP_EOL.'</th>'.PHP_EOL
121
		      . $this->_Tabs(1).'<th class="list_menu_title">'.$this->_oTrans->TEXT_VISIBILITY.
122
		                        ' / '.$this->_oTrans->TEXT_MENU_TITLE.':</th>'.PHP_EOL
123
		      . $this->_Tabs(0).'<th class="list_page_title">'.$this->_oTrans->TEXT_PAGE_TITLE.
124
		                        '</th>'.PHP_EOL
125
		      . $this->_Tabs(0).'<th class="list_page_id">PID</th>'.PHP_EOL
126
		      . $this->_Tabs(0).'<th class="header_list_actions">'.$this->_oTrans->TEXT_ACTIONS.
127
		                        ':</th>'.PHP_EOL
128
		      . $this->_Tabs(0).'<th class="list_page_id">&nbsp;</th>'.PHP_EOL
129
		      . $this->_Tabs(-1).'</tr>'.PHP_EOL
130
		      . $this->_Tabs(-1).'</tbody>'.PHP_EOL
131
		      . $this->_Tabs(-1).'</table>'.PHP_EOL
132
		// generate the page lines
133
		      . $this->_IterateTree($iTreeRoot)
134
		// build the footer
135
		      . $this->_Tabs(-1).'</div>'.PHP_EOL;
136
		;
137
		$this->_sOutput = $sOutput;
138
		return $sOutput;
139
	}
140
/**
141
 * Create a string of multiple TABs to prettify the HTML-output
142
 * ingrease the number of TABs with a positive and degrease with an negative Value.
143
 * '0' means: do not change the value. Or set an absolute number using $bRelative=false
144
 * @staticvar int $iTabLevel
145
 * @param integer $iTabsDiv number of TABs to add/sub
146
 * @param bool $bRelative false if should be set to absolute value
147
 * @return string
148
 */
149
	protected function _Tabs($iTabsDiv = 0, $bRelative = true)
150
	{
151
		static $iTabLevel = 0;
152
		$iTabLevel = ($bRelative ? $iTabLevel + $iTabsDiv : $iTabsDiv);
153
		$iTabLevel += ($iTabLevel < 0 ? 0 - $iTabLevel : $iTabsDiv);
154
		return str_repeat("\t", $iTabLevel);
155
	}
156
/**
157
 * compose the needed SQL statement
158
 * @param  integer $iParentKey
159
 * @return string SQL statement
160
 */			
161
	protected function _makeSql($iParentKey = 0)
162
	{
163
		$iParentKey = intval($iParentKey);
164
		$sql  = 'SELECT ( SELECT COUNT(*) '
165
		      .          'FROM `'.$this->_oDb->TablePrefix.'pages` `x` '
166
		      .          'WHERE x.`parent`=p.`page_id`'
167
		      .        ') `children`, '
168
		      .        's.`module`, MAX(s.`publ_start` + s.`publ_end`) published, p.`link`, '
169
		      .        '(SELECT MAX(`position`) FROM `'.$this->_oDb->TablePrefix.'pages` '
170
		      .        'WHERE `parent`='.$iParentKey.') max_position, '
171
		      .        '0 min_position, '
172
		      .        'p.`position`, '
173
		      .        'p.`page_id`, p.`parent`, p.`level`, p.`language`, p.`admin_groups`, '
174
		      .        'p.`admin_users`, p.`viewing_groups`, p.`viewing_users`, p.`visibility`, '
175
		      .        'p.`menu_title`, p.`page_title`, p.`page_trail`, '
176
		      .        'GROUP_CONCAT(CAST(CONCAT(s.`section_id`, \' - \', s.`module`) AS CHAR) ORDER BY s.`position` SEPARATOR \'\n\') `section_list` '
177
		      . 'FROM `'.$this->_oDb->TablePrefix.'pages` p '
178
		      .    'INNER JOIN `'.$this->_oDb->TablePrefix.'sections` s '
179
		      .    'ON p.`page_id`=s.`page_id` '
180
		      . 'WHERE `parent`='.$iParentKey.' '
181
		      .    (($this->_oReg->PageTrash != 'inline') ? 'AND `visibility`!=\'deleted\' ' : '')
182
		      . 'GROUP BY p.`page_id` '
183
		      . 'ORDER BY p.`position` ASC';
184
		return $sql;
185
	}
186
/**
187
 * iterate through all nodes which having subnodes 
188
 * @param integer start iteration from this parent page ( 0 = root)
189
 * @return string all of the item lines 
190
 */	
191
	protected function _IterateTree($iParent = 0)
192
	{
193
		$sOutput = '';
194
		// Get page list from database
195
		if(($oPages = $this->_oDb->query($this->_makeSql($iParent)))) 
196
		{
197
			$this->_queries++;
198
			// output block-header
199
			$sOutput .= $this->_Tabs(0).'<ul id="p'.$iParent.'" class="page_list"';
200
			if(!$iParent) {
201
				$sOutput .= ' style="display: block;"';
202
			}else {
203
			// show block depending from Cookies
204
				if (isset ($_COOKIE['p'.$iParent]) && $_COOKIE['p'.$iParent] == '1') {
205
					$sOutput .= ' style="display: block;"';
206
				}
207
			}
208
			$sOutput .= '>'.PHP_EOL;
209
			$iMinPosition = 1;
210
			while($aPage = $oPages->fetchRow(MYSQL_ASSOC))
211
			{ // iterate through the current branch
212
				if($this->_oReg->PageLevelLimit && ($aPage['level'] > $this->_oReg->PageLevelLimit)) {
213
					return '';
214
				}
215
				$aPage['min_position'] = ($aPage['position'] < $iMinPosition ? $aPage['position'] : $iMinPosition);
216
				$this->_iLineColor = $this->_iPagesTotal++ % 2;
217
				$aPage['iswriteable'] = false;
218
				if( $this->_oApp->ami_group_member($aPage['admin_users']) ||
219
					$this->_oApp->is_group_match($this->_oApp->get_groups_id(), $aPage['admin_groups']))
220
				{
221
					if(($aPage['visibility'] == 'deleted' && $this->_oReg->PageTrash == 'inline') ||
222
					   ($aPage['visibility'] != 'deleted'))
223
					{
224
						$aPage['iswriteable'] = true;
225
						$this->_iPagesWriteable++;
226
					}
227
				} else {
228
					if($aPage['visibility'] == 'private') { continue; }
229
				}
230
				// add this item to the secondary list of parents
231
				$this->_addToParentList($aPage);
232
				$sOutput .= $this->_createListItem($aPage);
233
			}
234
			$sOutput .= $this->_Tabs(-1).'</ul>'.PHP_EOL;
235
		}
236
		return $sOutput;
237
	}
238
/**
239
 * formating the given page object for output
240
 * @param type $aPage
241
 * @return string
242
 */
243
	protected function _createListItem($aPage)
244
	{
245
	// output the current item
246
	// --- HEADER ------------------------------------------------------------------------
247
		$sOutput  = $this->_Tabs(0).'<li class="p'.$aPage['parent'].'">'.PHP_EOL
248
		          . $this->_Tabs(1).'<table class="pages_view">'.PHP_EOL
249
		          . $this->_Tabs(1).'<tbody>'.PHP_EOL
250
		          . $this->_Tabs(1).'<tr class="row_'.$this->_iLineColor.'">'.PHP_EOL;
251
	// --- TAB 1 --- (expand/collapse) ---------------------------------------------------
252
		$sOutput .= $this->_Tabs(1).'<td valign="middle" width="20" style="padding-left: '
253
		          . (int)($aPage['level']*20).'px;">';
254
		if((bool)$aPage['children']) {
255
			$sOutput .= '<a href="javascript:toggle_visibility(\'p'.$aPage['page_id'].'\');" '
256
			          . 'title="'.$this->_oTrans->TEXT_EXPAND.'/'.$this->_oTrans->TEXT_COLLAPSE.'">'
257
			          . '<span><img src="'.$this->_oReg->ThemeRel.'images/'
258
			          . ( ((isset($_COOKIE['p'.$aPage['page_id']]) 
259
						  && $_COOKIE['p'.$aPage['page_id']] == '1') ? 'minus' : 'plus')
260
						)
261
			          . '_16.png" onclick="toggle_plus_minus(\''.$aPage['page_id'].'\');" '
262
			          . 'name="plus_minus_'.$aPage['page_id'].'" alt="+" /></span></a>';
263
		}else {
264
			$sOutput .= '&nbsp;';
265
		}
266
		$sOutput .= '</td>'.PHP_EOL;
267
	// --- TAB 2 --- (menu title) --------------------------------------------------------
268
		$sOutput .= $this->_Tabs(0).'<td class="list_menu_title">';
269
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
270
			$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/modify.php?page_id='
271
			          . $aPage['page_id'].'" title="'.$this->_oTrans->TEXT_MODIFY.'">';
272
		}
273
		$sIcon = $this->_oReg->ThemeRel.'images/'.$aPage['visibility'].'_16.png';
274
		if(!is_readable(rtrim($this->_oReg->DocumentRoot, '/').$sIcon)) {
275
			$sIcon = $this->_oReg->ThemeRel.'images/public_16.png';
276
		}
277
		$sOutput .= '<img src="'.$sIcon.'" alt="'.$this->_oTrans->TEXT_VISIBILITY.': '
278
				  . $this->_oTrans->{'TEXT_'.strtoupper($aPage['visibility'])}  .'" class="page_list_rights" />';
279
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
280
			$sOutput .= '<span class="modify_link">'.$aPage['menu_title'].'</span></a>';
281
		}else {
282
			$sOutput .=  '<span class="bold grey">'.$aPage['menu_title'].'</span>';
283
		}
284
		$sOutput .= '</td>'.PHP_EOL;
285
	// --- TAB 3 --- (page title) --------------------------------------------------------
286
		$sOutput .= $this->_Tabs(0).'<td class="list_page_title">'.$aPage['page_title'].'</td>'.PHP_EOL;
287
	// --- TAB 4 --- (page ID) -----------------------------------------------------------
288
		$sOutput .= $this->_Tabs(0).'<td class="list_page_id right">'.$aPage['page_id'].'</td>'.PHP_EOL;
289
	// --- TAB 5 --- (show this page in new window) --------------------------------------
290
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
291
		if($aPage['visibility'] != 'deleted' && $aPage['visibility'] != 'none') {
292
			$sPageLink = $this->_oReg->AppRel.preg_replace(
293
			                                 '/^'.preg_quote($this->_oReg->AppUrl, '/').'/siU',
294
			                                 '', 
295
			                                 $this->_oApp->page_link($aPage['link'])
296
			                                );
297
			$sOutput .= '<a href="'.$sPageLink.'" target="_blank" title="'.$this->_oTrans->TEXT_VIEW
298
			          . '"><img src="'.$this->_oReg->ThemeRel.'images/view_16.png" alt="'
299
			          . $this->_oTrans->TEXT_VIEW.'" /></a>';
300
		}else { 
301
			$sOutput .= '<img src="'.$this->_oReg->ThemeRel.'images/blank_16.gif" alt=" " />';
302
		}
303
		$sOutput .= '</td>'.PHP_EOL;
304

    
305
	// --- TAB 6 --- (edit settings) -----------------------------------------------------
306
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
307
		if($aPage['visibility'] != 'deleted') { 
308
			if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
309
				$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/settings.php?page_id='
310
				          . $aPage['page_id'].'" title="'.$this->_oTrans->TEXT_SETTINGS.'">'
311
				          . '<img src="'.$this->_oReg->ThemeRel.'images/modify_16.png" alt="'
312
				          . $this->_oTrans->TEXT_SETTINGS.'" /></a>';
313
			}
314
		}else {
315
			$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/restore.php?page_id='.$aPage['page_id'].'" '
316
			          . 'title="'.$this->_oTrans->TEXT_RESTORE.'"><img src="'.$this->_oReg->ThemeRel
317
			          . 'images/restore_16.png" alt="'.$this->_oTrans->TEXT_RESTORE.'" /></a>';
318
		}
319
		$sOutput .= '</td>'.PHP_EOL;
320

    
321
	// --- TAB 7 --- (edit sections) -----------------------------------------------------
322
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
323
		if( $this->_oReg->ManageSections && $this->_oApp->get_permission('pages_add') && $aPage['iswriteable'] ) {
324
			$file = $this->_oApp->page_is_active($aPage) ? "clock_16.png" : "clock_red_16.png";
325
			$file = ($aPage['published'] && $aPage['module'] != 'menu_link') ? $file : 'noclock_16.png';
326
			$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/sections.php?page_id='
327
			          . $aPage['page_id'].'" title="'.$this->_oTrans->HEADING_MANAGE_SECTIONS."\n".$aPage['section_list'].'">'
328
			          . '<img src="'.$this->_oReg->ThemeRel.'images/'.$file.'" alt="'
329
			          . $this->_oTrans->HEADING_MANAGE_SECTIONS."\n".$aPage['section_list'].'" /></a>';
330
		}else { 
331
			$sOutput .= '<img src="'.$this->_oReg->ThemeRel.'images/blank_16.gif" alt=" " />';
332
		}
333
		$sOutput .= '</td>'.PHP_EOL;
334

    
335
	// --- TAB 8 --- (move up) -----------------------------------------------------------
336
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
337
		if($aPage['position'] > $aPage['min_position']) {
338
			if($aPage['visibility'] != 'deleted') {
339
				if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
340
					$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/move_up.php?page_id='
341
					          . $aPage['page_id'].'" title="'.$this->_oTrans->TEXT_MOVE_UP
342
					          . '"><img src="'.$this->_oReg->ThemeRel.'images/up_16.png" alt="'
343
					          . $this->_oTrans->TEXT_MOVE_UP.'" /></a>';
344
				}
345
			}
346
		}
347
		$sOutput .= '</td>'.PHP_EOL;
348

    
349
	// --- TAB 9 --- (move down) ---------------------------------------------------------
350
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
351
		if($aPage['position'] < $aPage['max_position']) {
352
			if($aPage['visibility'] != 'deleted') {
353
				if($this->_oApp->get_permission('pages_settings') && $aPage['iswriteable']) {
354
					$sOutput .= '<a href="'.$this->_oReg->AcpRel.'pages/move_down.php?page_id='
355
					          . $aPage['page_id'].'" title="'.$this->_oTrans->TEXT_MOVE_DOWN
356
					          . '"><img src="'.$this->_oReg->ThemeRel.'images/down_16.png" alt="'
357
					          . $this->_oTrans->TEXT_MOVE_DOWN.'" /></a>';
358
				}
359
			}
360
		}
361
		$sOutput .= '</td>'.PHP_EOL;
362

    
363
	// --- TAB 10 --- (delete page) ------------------------------------------------------
364
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
365
		if($this->_oApp->get_permission('pages_delete') && $aPage['iswriteable']) {
366
			$sOutput .= '<a href="javascript:confirm_link(pages_delete_confirm+\'?\',\''
367
			          . $this->_oReg->AcpRel.'pages/delete.php?page_id='
368
			          . $this->_oApp->getIDKEY($aPage['page_id']).'\');" title="'
369
			          . $this->_oTrans->TEXT_DELETE.'"><img src="'.$this->_oReg->ThemeRel
370
			          . 'images/delete_16.png" alt="'.$this->_oTrans->TEXT_DELETE.'" /></a>';
371
		}else { 
372
			$sOutput .= '<img src="'.$this->_oReg->ThemeRel.'images/blank_16.gif" alt=" " />';
373
		}
374
		$sOutput .= '</td>'.PHP_EOL;
375

    
376
	// --- TAB 11 --- (add child page) ---------------------------------------------------
377
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
378
		if( 
379
			$this->_oApp->get_permission('pages_add')
380
			&& $aPage['iswriteable'] 
381
			&& ($aPage['visibility'] != 'deleted') 
382
			&& $aPage['level'] < ($this->_oReg->PageLevelLimit - 1)
383
		  )
384
		{
385
			$sOutput .= '<a href="javascript:add_child_page(\''.$aPage['page_id'].'\');" '
386
			          . 'title="'.$this->_oTrans->HEADING_ADD_CHILD_PAGE.'"><img src="'
387
			          . $this->_oReg->ThemeRel.'images/siteadd.png" name="addpage_'.$aPage['page_id']
388
			          . '" alt="'.$this->_oTrans->HEADING_ADD_CHILD_PAGE.'" /></a>';
389
		}else { 
390
			$sOutput .= '&nbsp;'; 
391
		}
392
		$sOutput .= '</td>'.PHP_EOL;
393
	// --- TAB 12 --- (show language) ----------------------------------------------------
394
		$sOutput .= $this->_Tabs(0).'<td class="list_page_id center">'.$aPage['language'].'</td>'.PHP_EOL;
395
	// --- FOOTER ------------------------------------------------------------------------
396
		$sOutput .= $this->_Tabs(-1).'</tr>'.PHP_EOL
397
		          . $this->_Tabs(-1).'</tbody>'.PHP_EOL
398
		          . $this->_Tabs(-1).'</table>'.PHP_EOL;
399
	// if there children, iterate through this children now
400
		if((bool)$aPage['children']) {
401
			$sOutput .= $this->_IterateTree($aPage['page_id']);
402
		}
403
		$sOutput .= $this->_Tabs(-1).'</li>'.PHP_EOL;
404
		return $sOutput;
405
	} // end of method _createListItem
406
/**
407
 * build a list of possible parent pages
408
 * @param array $aPage 
409
 */	
410
	protected function _addToParentList(array $aPage)
411
	{
412
		if( ($aPage['level'] < ($this->_oReg->PageLevelLimit - 1))
413
			&& $aPage['iswriteable'] 
414
			&& ($aPage['visibility'] != 'deleted')
415
			&& $this->_oApp->get_permission('pages_add') ) 
416
		{
417
			$aPage['disabled'] = ($aPage['iswriteable'] ? 0 : 1);
418
			$this->_aParentList[] = $aPage;
419
		}
420
	}
421
	
422
} // end of class PageTree
(1-1/25)