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: 1973 $
29
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/admin/pages/PageTree.php $
30
 * @lastmodified $Date: 2013-10-03 23:58:14 +0200 (Thu, 03 Oct 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
		      . 'FROM `'.$this->_oDb->TablePrefix.'pages` p '
177
		      .    'INNER JOIN `'.$this->_oDb->TablePrefix.'sections` s '
178
		      .    'ON p.`page_id`=s.`page_id` ';
179
//		if($iParentKey) {
180
//
181
//			$sql .= 'WHERE `root_parent`='.$iParentKey.' ';
182
//		} else {
183
//	// if tree based on root is requested (parent=0)
184
			$sql .= 'WHERE `parent`='.$iParentKey.' ';
185
//		}
186
	// do not get pages with 'deleted' flag set on activated trashcan
187
		if($this->_oReg->PageTrash != 'inline') {
188
			$sql .= 'AND `visibility`!=\'deleted\' ';
189
		}
190
		$sql .= 'GROUP BY p.`page_id` '
191
		      . 'ORDER BY p.`position` ASC';
192
		return $sql;
193
	}
194
/**
195
 * iterate through all nodes which having subnodes 
196
 * @param integer start iteration from this parent page ( 0 = root)
197
 * @return string all of the item lines 
198
 */	
199
	protected function _IterateTree($iParent = 0)
200
	{
201
		$sOutput = '';
202
		// Get page list from database
203
		if(($oPages = $this->_oDb->query($this->_makeSql($iParent)))) 
204
		{
205
			$this->_queries++;
206
			// output block-header
207
			$sOutput .= $this->_Tabs(0).'<ul id="p'.$iParent.'" class="page_list"';
208
			if(!$iParent) {
209
				$sOutput .= ' style="display: block;"';
210
			}else {
211
			// show block depending from Cookies
212
				if (isset ($_COOKIE['p'.$iParent]) && $_COOKIE['p'.$iParent] == '1') {
213
					$sOutput .= ' style="display: block;"';
214
				}
215
			}
216
			$sOutput .= '>'.PHP_EOL;
217
			$iMinPosition = 1;
218
			while($aPage = $oPages->fetchRow(MYSQL_ASSOC))
219
			{ // iterate through the current branch
220
				if($this->_oReg->PageLevelLimit && ($aPage['level'] > $this->_oReg->PageLevelLimit)) {
221
					return '';
222
				}
223
				$aPage['min_position'] = ($aPage['position'] < $iMinPosition ? $aPage['position'] : $iMinPosition);
224
				$this->_iLineColor = $this->_iPagesTotal++ % 2;
225
				$aPage['iswriteable'] = false;
226
				if( $this->_oApp->ami_group_member($aPage['admin_users']) ||
227
					$this->_oApp->is_group_match($this->_oApp->get_groups_id(), $aPage['admin_groups']))
228
				{
229
					if(($aPage['visibility'] == 'deleted' && $this->_oReg->PageTrash == 'inline') ||
230
					   ($aPage['visibility'] != 'deleted'))
231
					{
232
						$aPage['iswriteable'] = true;
233
						$this->_iPagesWriteable++;
234
					}
235
				} else {
236
					if($aPage['visibility'] == 'private') { continue; }
237
				}
238
				// add this item to the secondary list of parents
239
				$this->_addToParentList($aPage);
240
				$sOutput .= $this->_createListItem($aPage);
241
			}
242
			$sOutput .= $this->_Tabs(-1).'</ul>'.PHP_EOL;
243
		}
244
		return $sOutput;
245
	}
246
/**
247
 * formating the given page object for output
248
 * @param type $aPage
249
 * @return string
250
 */
251
	protected function _createListItem($aPage)
252
	{
253
	// output the current item
254
	// --- HEADER ------------------------------------------------------------------------
255
		$sOutput  = $this->_Tabs(0).'<li class="p'.$aPage['parent'].'">'.PHP_EOL
256
		          . $this->_Tabs(1).'<table class="pages_view">'.PHP_EOL
257
		          . $this->_Tabs(1).'<tbody>'.PHP_EOL
258
		          . $this->_Tabs(1).'<tr class="row_'.$this->_iLineColor.'">'.PHP_EOL;
259
	// --- TAB 1 --- (expand/collapse) ---------------------------------------------------
260
		$sOutput .= $this->_Tabs(1).'<td valign="middle" width="20" style="padding-left: '
261
		          . (int)($aPage['level']*20).'px;">';
262
		if((bool)$aPage['children']) {
263
			$sOutput .= '<a href="javascript:toggle_visibility(\'p'.$aPage['page_id'].'\');" '
264
			          . 'title="'.$this->_oTrans->TEXT_EXPAND.'/'.$this->_oTrans->TEXT_COLLAPSE.'">'
265
			          . '<span><img src="'.$this->_oReg->ThemeRel.'/images/'
266
			          . ( ((isset($_COOKIE['p'.$aPage['page_id']]) 
267
						  && $_COOKIE['p'.$aPage['page_id']] == '1') ? 'minus' : 'plus')
268
						)
269
			          . '_16.png" onclick="toggle_plus_minus(\''.$aPage['page_id'].'\');" '
270
			          . 'name="plus_minus_'.$aPage['page_id'].'" alt="+" /></span></a>';
271
		}else {
272
			$sOutput .= '&nbsp;';
273
		}
274
		$sOutput .= '</td>'.PHP_EOL;
275
	// --- TAB 2 --- (menu title) --------------------------------------------------------
276
		$sOutput .= $this->_Tabs(0).'<td class="list_menu_title">';
277
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
278
			$sOutput .= '<a href="'.$this->_oReg->AcpRel.'/pages/modify.php?page_id='
279
			          . $aPage['page_id'].'" title="'.$this->_oTrans->TEXT_MODIFY.'">';
280
		}
281
		$sIcon = $this->_oReg->ThemeRel.'/images/'.$aPage['visibility'].'_16.png';
282
		if(!is_readable($this->_oReg->DocumentRoot.$sIcon)) {
283
			$sIcon = $this->_oReg->ThemeRel.'/images/public_16.png';
284
		}
285
		$sOutput .= '<img src="'.$sIcon.'" alt="'.$this->_oTrans->TEXT_VISIBILITY.': '
286
				  . $this->_oTrans->{'TEXT_'.strtoupper($aPage['visibility'])}  .'" class="page_list_rights" />';
287
		if($this->_oApp->get_permission('pages_modify') && $aPage['iswriteable']) {
288
			$sOutput .= '<span class="modify_link">'.$aPage['menu_title'].'</span></a>';
289
		}else {
290
			$sOutput .=  '<span class="bold grey">'.$aPage['menu_title'].'</span>';
291
		}
292
		$sOutput .= '</td>'.PHP_EOL;
293
	// --- TAB 3 --- (page title) --------------------------------------------------------
294
		$sOutput .= $this->_Tabs(0).'<td class="list_page_title">'.$aPage['page_title'].'</td>'.PHP_EOL;
295
	// --- TAB 4 --- (page ID) -----------------------------------------------------------
296
		$sOutput .= $this->_Tabs(0).'<td class="list_page_id right">'.$aPage['page_id'].'</td>'.PHP_EOL;
297
	// --- TAB 5 --- (show this page in new window) --------------------------------------
298
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
299
		if($aPage['visibility'] != 'deleted' && $aPage['visibility'] != 'none') {
300
			$sPageLink = $this->_oReg->AppRel.preg_replace(
301
			                                 '/^'.preg_quote($this->_oReg->AppUrl, '/').'/siU',
302
			                                 '', 
303
			                                 $this->_oApp->page_link($aPage['link'])
304
			                                );
305
			$sOutput .= '<a href="'.$sPageLink.'" target="_blank" title="'.$this->_oTrans->TEXT_VIEW
306
			          . '"><img src="'.$this->_oReg->ThemeRel.'/images/view_16.png" alt="'
307
			          . $this->_oTrans->TEXT_VIEW.'" /></a>';
308
		}else { 
309
			$sOutput .= '<img src="'.$this->_oReg->ThemeRel.'/images/blank_16.gif" alt=" " />';
310
		}
311
		$sOutput .= '</td>'.PHP_EOL;
312

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

    
329
	// --- TAB 7 --- (edit sections) -----------------------------------------------------
330
		$sOutput .= $this->_Tabs(0).'<td class="list_actions">';
331
		if( $this->_oReg->ManageSections && $this->_oApp->get_permission('pages_add') && $aPage['iswriteable'] ) {
332
			$file = $this->_oApp->page_is_active($aPage) ? "clock_16.png" : "clock_red_16.png";
333
			$file = ($aPage['published'] && $aPage['module'] != 'menu_link') ? $file : 'noclock_16.png';
334
			$sOutput .= '<a href="'.$this->_oReg->AcpRel.'/pages/sections.php?page_id='
335
			          . $aPage['page_id'].'" title="'.$this->_oTrans->HEADING_MANAGE_SECTIONS.'">'
336
			          . '<img src="'.$this->_oReg->ThemeRel.'/images/'.$file.'" alt="'
337
			          . $this->_oTrans->HEADING_MANAGE_SECTIONS.'" /></a>';
338
		}else { 
339
			$sOutput .= '<img src="'.$this->_oReg->ThemeRel.'/images/blank_16.gif" alt=" " />';
340
		}
341
		$sOutput .= '</td>'.PHP_EOL;
342

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

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

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

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