PageRenderTime 59ms CodeModel.GetById 17ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 1ms

/e107_admin/frontpage.php

https://github.com/CasperGemini/e107
PHP | 685 lines | 464 code | 95 blank | 126 comment | 53 complexity | 009ba2a627804d1c41d3a227716da77b MD5 | raw file
  1<?php
  2/*
  3 * e107 website system
  4 *
  5 * Copyright (C) 2008-2009 e107 Inc (e107.org)
  6 * Released under the terms and conditions of the
  7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8 *
  9 * Administration Area - Front page
 10 *
 11 * $URL$
 12 * $Id$
 13 *
 14*/
 15
 16/**
 17 *	e107 Front page administration
 18 *
 19 *	@package	e107
 20 *	@subpackage	admin
 21 *	@version 	$Id$;
 22 */
 23
 24require_once ('../class2.php');
 25if(! getperms('G'))
 26{
 27	header('location:'.e_BASE.'index.php');
 28	exit();
 29}
 30
 31include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
 32
 33$e_sub_cat = 'frontpage';
 34require_once ('auth.php');
 35
 36$mes = e107::getMessage();
 37
 38$frontPref = e107::pref('core');              		 	// Get prefs
 39
 40// Get list of possible options for front page
 41$front_page['news'] = array('page' => 'news.php', 'title' => ADLAN_0);
 42//$front_page['download'] = array('page' => 'download.php', 'title' => ADLAN_24);		// Its a plugin now
 43$front_page['wmessage'] = array('page' => 'index.php', 'title' => ADLAN_28);
 44
 45if($sql->db_Select('page', 'page_id, page_title', "menu_name=''"))
 46{
 47	$front_page['custom']['title'] = FRTLAN_30;
 48	while($row = $sql->db_Fetch())
 49	{
 50		$front_page['custom']['page'][] = array('page' => 'page.php?'.$row['page_id'], 'title' => $row['page_title']);
 51	}
 52}
 53
 54// Now let any plugins add to the options - must append to the $front_page array as above
 55if(varset($frontPref['e_frontpage_list']))
 56{
 57	foreach($frontPref['e_frontpage_list'] as $val)
 58	{
 59		if(is_readable(e_PLUGIN.$val.'/e_frontpage.php'))
 60		{
 61			require_once (e_PLUGIN.$val.'/e_frontpage.php');
 62		}
 63	}
 64}
 65
 66
 67
 68// Make sure links relative to SITEURL
 69foreach($front_page as &$front_value)
 70{
 71	if(is_array($front_value['page']))
 72	{ // Its a URL with multiple options
 73		foreach($front_value['page'] as &$multipage)
 74		{
 75			$multipage = str_replace(e_HTTP, '', $multipage);
 76			//if (substr($multipage, 0, 1) != '/') $multipage = '/'.$multipage;
 77		}
 78	}
 79	else
 80	{
 81		$front_value = str_replace(e_HTTP, '', $front_value);
 82		//if (substr($front_value, 0, 1) != '/') $front_value = '/'.$front_value;
 83	}
 84}
 85
 86
 87
 88
 89// Now sort out list of rules for display (based on $pref data to start with)
 90$gotpub = FALSE;
 91if(is_array($frontPref['frontpage']))
 92{
 93	$i = 1;
 94	foreach($frontPref['frontpage'] as $class => $val)
 95	{
 96		if($class == 'all')
 97		{
 98			$class = e_UC_PUBLIC;
 99			$gotpub = TRUE;
100		}
101		if($val)
102		{ // Only add non-null pages
103			$fp_settings[$i] = array('order' => $i, 'class' => $class, 'page' => $val, 'force' => varset($frontPref['frontpage_force'][$class], ''));
104			$i ++;
105		}
106	}
107}
108else
109{ // Legacy stuff to convert
110	$fp_settings = array();
111	$fp_settings[] = array('order' => 0, 'class' => e_UC_PUBLIC, 'page' => varset($frontPref['frontpage'], 'news.php'), 'force' => '');
112}
113
114if(!$gotpub)
115{ // Need a 'default' setting - usually 'all'
116	$fp_settings[] = array('order' => $i, 'class' => e_UC_PUBLIC, 'page' => (isset($frontPref['frontpage']['all']) ? $frontPref['frontpage']['all'] : 'news.php'), 'force' => '');
117}
118
119$fp_update_prefs = FALSE;
120
121
122/*
123Following code replaced - values not passed on image clicks with Firefox
124if(isset($_POST['fp_inc']))
125{
126	$mv = intval($_POST['fp_inc']);
127	echo "Increment: {$mv}<br />";
128	if(($mv > 1) && ($mv <= count($fp_settings)))
129	{
130		$temp = $fp_settings[$mv - 1];
131		$fp_settings[$mv - 1] = $fp_settings[$mv];
132		$fp_settings[$mv] = $temp;
133		$fp_update_prefs = TRUE;
134		frontpage_adminlog('01', 'Inc '.$mv);
135	}
136}
137elseif(isset($_POST['fp_dec']))
138{
139	$mv = intval($_POST['fp_dec']);
140	echo "Decrement: {$mv}<br />";
141	if(($mv > 0) && ($mv < count($fp_settings)))
142	{
143		$temp = $fp_settings[$mv + 1];
144		$fp_settings[$mv + 1] = $fp_settings[$mv];
145		$fp_settings[$mv] = $temp;
146		$fp_update_prefs = TRUE;
147		frontpage_adminlog('01', 'Dec '.$mv);
148	}
149}
150*/
151
152if (isset($_POST))
153{
154	foreach ($_POST as $k => $v)
155	{
156		$incDec = substr($k, 0, 6);
157		$idNum = substr($k, 6);
158		if ($incDec == 'fp_inc')
159		{
160			$mv = intval($idNum);
161			if(($mv > 1) && ($mv <= count($fp_settings)))
162			{
163				$temp = $fp_settings[$mv - 1];
164				$fp_settings[$mv - 1] = $fp_settings[$mv];
165				$fp_settings[$mv] = $temp;
166				$fp_update_prefs = TRUE;
167				frontpage_adminlog('01', 'Inc '.$mv);
168			}
169			break;
170		}
171		elseif ($incDec == 'fp_dec')
172		{
173			$mv = intval($idNum);
174			if(($mv > 0) && ($mv < count($fp_settings)))
175			{
176				$temp = $fp_settings[$mv + 1];
177				$fp_settings[$mv + 1] = $fp_settings[$mv];
178				$fp_settings[$mv] = $temp;
179				$fp_update_prefs = TRUE;
180				frontpage_adminlog('01', 'Dec '.$mv);
181			}
182			break;
183		}
184	}
185}
186
187
188
189// Edit an existing rule
190if(isset($_POST['fp_edit_rule']))
191{
192	$_POST['type'] = (isset($_POST['edit']['all'])) ? 'all_users' : 'user_class';
193	$_POST['class'] = key($_POST['edit']);
194}
195
196// Cancel Edit
197
198
199if(isset($_POST['fp_save_new']))
200{ // Add or edit an existing rule here.
201	// fp_order - zero for a new rule, non-zero if editing an existing rule
202	// class - user class for rule
203	// frontpage - radio button option indicating type of page (for home page)
204	// frontpage_multipage[] - the other information for custom pages and similar - array index matches value of 'frontpage' when selected
205	// frontpage_other - URL for 'other' home page
206	// fp_force_page - radio button option indicating type of page (for post-login page)
207	// fp_force_page_multipage[] - the other information for custom pages and similar - array index matches value of 'frontpage' when selected
208	// fp_force_page_other - URL for forced post-login 'other' page
209
210
211	if($_POST['frontpage'] == 'other')
212	{
213		$_POST['frontpage_other'] = trim($tp->toForm($_POST['frontpage_other']));
214		$frontpage_value = $_POST['frontpage_other'] ? $_POST['frontpage_other'] : 'news.php';
215	}
216	else
217	{
218		if(is_array($front_page[$_POST['frontpage']]['page']))
219		{
220			$frontpage_value = $front_page[$_POST['frontpage']]['page'][$_POST['frontpage_multipage'][$_POST['frontpage']]]['page'];
221		}
222		else
223		{
224			$frontpage_value = $front_page[$_POST['frontpage']]['page'];
225		}
226	}
227
228	if($_POST['fp_force_page'] == 'other')
229	{
230		$_POST['fp_force_page_other'] = trim($tp->toForm($_POST['fp_force_page_other']));
231		$forcepage_value = $_POST['fp_force_page_other']; // A null value is allowable here
232	}
233	else
234	{
235		if(is_array($front_page[$_POST['fp_force_page']]['page']))
236		{
237			$forcepage_value = $front_page[$_POST['fp_force_page']]['page'][$_POST['fp_force_page_multipage'][$_POST['fp_force_page']]]['page'];
238		}
239		else
240		{
241			$forcepage_value = $front_page[$_POST['fp_force_page']]['page'];
242		}
243	}
244
245	$temp = array('order' => intval($_POST['fp_order']), 'class' => $_POST['class'], 'page' => $frontpage_value, 'force' => trim($forcepage_value));
246	if($temp['order'] == 0)
247	{ // New index to add
248		$ind = 0;
249		for($i = 1; $i <= count($fp_settings); $i ++)
250		{
251			if($fp_settings[$i]['class'] == $temp['class'])
252				$ind = $i;
253		}
254		if($ind)
255		{
256			unset($fp_settings[$ind]); // Knock out duplicate definition for class
257			echo "duplicate definition for class: ".$ind."<br />";
258		}
259		array_unshift($fp_settings, $temp); // Deliberately add twice
260		array_unshift($fp_settings, $temp); // ....because re-indexed from zero
261		unset($fp_settings[0]); // Then knock out index zero
262		$fp_update_prefs = TRUE;
263		frontpage_adminlog('02', "class => {$_POST['class']},[!br!]page => {$frontpage_value},[!br!]force => {$forcepage_value}");
264	}
265	elseif(array_key_exists($temp['order'], $fp_settings))
266	{
267		$fp_settings[$temp['order']] = $temp;
268		$fp_update_prefs = TRUE;
269		frontpage_adminlog('03', "posn => {$temp},[!br!]class => {$_POST['class']},[!br!]page => {$frontpage_value},[!br!]force => {$forcepage_value}");
270	}
271	else
272	{ // Someone playing games
273		$mes->addError('Software error'); // TODO LAN
274	}
275}
276
277if(isset($_POST['fp_delete_rule']))
278{
279	if(isset($fp_settings[key($_POST['fp_delete_rule'])]))
280	{
281		$rule_no = key($_POST['fp_delete_rule']);
282		$array_size = count($fp_settings);
283		frontpage_adminlog('04', "Rule {$rule_no},[!br!]class => {$fp_settings[$rule_no]['class']},[!br!]page => {$fp_settings[$rule_no]['page']},[!br!]force => {$fp_settings[$rule_no]['force']}");
284		unset($fp_settings[$rule_no]);
285		while($rule_no < $array_size)
286		{ // Move up and renumber any entries after the deleted rule
287			$fp_settings[$rule_no] = $fp_settings[$rule_no + 1];
288			$rule_no ++;
289			unset($fp_settings[$rule_no]);
290		}
291		$fp_update_prefs = TRUE;
292	}
293}
294
295if($fp_update_prefs)
296{ // Save the two arrays
297	$fp_list = array();
298	$fp_force = array();
299	for($i = 1; $i <= count($fp_settings); $i ++)
300	{
301		$fp_list[$fp_settings[$i]['class']] = $fp_settings[$i]['page'];
302		$fp_force[$fp_settings[$i]['class']] = $fp_settings[$i]['force'];
303	}
304
305	$corePrefs = e107::getConfig('core');				// Core Prefs Object.
306	$corePrefs->set('frontpage', $fp_list);
307	$corePrefs->set('frontpage_force', $fp_force);
308	$result = $corePrefs->save(FALSE, TRUE);
309}
310
311
312
313
314// All updates complete now - latest data is in the $fp_settings, $fp_list and $fp_force arrays
315$fp = new frontpage($front_page);
316
317
318
319
320
321class frontpage
322{
323	protected	$frm;
324	protected	$frontPage = array();		// List of options for front page
325
326	public function __construct($fp)
327	{
328		$this->frm = e107::getForm();
329		$this->frontPage = $fp;
330		
331		$ns = e107::getRender();
332		$mes = e107::getMessage();
333		
334		global $fp_settings;
335		
336		
337		if(vartrue($_GET['mode']) == 'create')
338		{
339			$text = $this->edit_rule(array('order' => 0, 'class' => e_UC_PUBLIC, 'page' => 'news.php', 'force' => FALSE)); // Display edit form as well
340		//	$text .= $this->select_class($fp_settings, FALSE);
341			$ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_42, $text);
342		}
343		elseif(vartrue($_GET['id']))
344		{
345			$key = intval($_GET['id']);
346			$text = $this->edit_rule($fp_settings[$key]); // Display edit form as well
347		//	$text .= $this->select_class($fp_settings, FALSE);
348			$ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_46, $text);
349		}
350		else
351		{ // Just show existing rules
352			$ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_13, $mes->render().$this->select_class($fp_settings, TRUE));
353		}
354		
355	}
356
357
358
359	/**
360	 *	Show a list of existing rules, with edit/delete/move buttons, and optional button to add a new rule
361	 *
362	 *	@param boolean $show_button - show the 'Add new rule' button if true
363	 *
364	 *	@return string text for display
365	 */
366	function select_class(&$fp_settings, $show_button = TRUE)
367	{
368		$frm = e107::getForm();
369		// List of current settings
370		$show_legend = $show_button ? " class='e-hideme'" : '';
371		$text = "
372		<form method='post' action='".e_SELF."'>
373			<fieldset id='frontpage-settings'>
374				<legend{$show_legend}>".FRTLAN_13."</legend>
375
376				<table class='table adminlist'>
377					<colgroup>
378						<col style='width:  5%' />
379						<col style='width: 25%' />
380						<col style='width: 30%' />
381						<col style='width: 25%' />
382						<col style='width: 15%' />
383					</colgroup>
384					<thead>
385						<tr>
386							<th class='first'>".LAN_ORDER."</th>
387							<th>".FRTLAN_53."</th>
388							<th>".FRTLAN_49."</th>
389							<th>".FRTLAN_35."</th>
390							<th class='center last'>".LAN_OPTIONS."</th>
391						</tr>
392					</thead>
393					<tbody>";
394
395		foreach($fp_settings as $order => $current_value)
396		{
397			$title = e107::getUserClass()->uc_get_classname($current_value['class']);
398			$text .= "
399					<tr>
400						<td class='right'>".$order."</td>
401						<td>".$title."</td>
402						<td>".$this->lookup_path($current_value['page'])."</td>
403						<td>".$this->lookup_path($current_value['force'])."</td>
404						<td class='center options last'>
405						<div class='btn-group'>
406							".$frm->admin_button('fp_inc',$order,'up',ADMIN_UP_ICON)."
407							".$frm->admin_button('fp_dec',$order,'down',ADMIN_DOWN_ICON)."
408							<a class='btn' title='".LAN_EDIT."' href='".e_SELF."?id=".$order."' >".ADMIN_EDIT_ICON."</a>
409							".$frm->admin_button('fp_delete_rule['.$order.']',$order,'',ADMIN_DELETE_ICON)."					
410						</div>
411						</td>
412					</tr>";
413					
414					
415		}
416		$text .= "
417		 		</tbody>
418		 	</table>";
419
420		if($show_button)
421		{
422			$text .= "
423				<div class='buttons-bar center'>
424					 <a href='".e_SELF."?mode=create' class='btn btn-success'>".FRTLAN_42."</a>
425				</div>";
426		}
427
428		$text .= "
429			</fieldset>
430			</form>";
431
432		return $text;
433	}
434
435
436
437	/**
438	 *	Display form to add/edit rules
439	 *
440	 *	@param array $rule_info - initial data (must be preset if new rule)
441	 *
442	 *	@return string - text for display
443	 */
444	function edit_rule($rule_info)
445	{
446		$is_other_home = TRUE;
447		$is_other_force = TRUE;
448		//$force_checked = $rule_info['force'] ? " checked='checked'" : '';
449		$text_tmp_1 = '';
450		$text_tmp_2 = '';
451		foreach($this->frontPage as $front_key => $front_value)
452		{
453			//$type_selected = FALSE;
454
455			$text_tmp_1 .= "
456			<tr>
457				".$this->show_front_val('frontpage', $front_key, $front_value, $is_other_home, $rule_info['page'])."
458			</tr>
459		  	";
460
461			$text_tmp_2 .= "
462			<tr>
463				".$this->show_front_val('fp_force_page', $front_key, $front_value, $is_other_force, $rule_info['force'])."
464			</tr>
465		  	";
466
467		}
468
469// <legend class='e-hideme'>".($rule_info['order'] ? FRTLAN_46 : FRTLAN_42)."</legend>
470
471		$text = "
472		<form method='post' action='".e_SELF."'>";
473		
474		$text .= '<ul class="nav nav-tabs" id="myTabs">
475			<li class="active"><a data-toggle="tab" href="#home">'.FRTLAN_49.'</a></li>
476			<li><a data-toggle="tab" href="#postlogin">'.FRTLAN_35.'</a></li>
477			</ul>
478			 ';
479			
480			$text .= "
481			<div class='tab-content'>	
482				<div class='tab-pane active' id='home'>
483					<table class='table adminform'>
484						<colgroup>
485							<col style='width: 30%' />
486							<col style='width: 70%' />
487						</colgroup>
488						<tbody>
489							".$text_tmp_1."
490							<tr>
491								".$this->add_other('frontpage', $is_other_home, $rule_info['page'])."
492							</tr>
493						</tbody>
494					</table>
495				</div>
496				
497				<div class='tab-pane' id='postlogin'>
498					<table class='table adminform'>
499						<colgroup>
500							<col style='width: 30%' />
501							<col style='width: 70%' />
502						</colgroup>
503						<tbody>
504							".$text_tmp_2."
505							<tr>
506								".$this->add_other('fp_force_page', $is_other_force, $rule_info['force'])."
507							</tr>
508						</tbody>
509					</table>
510				</div>
511			</div>
512			
513				<div class='buttons-bar center'>
514					".$this->frm->hidden('fp_order', $rule_info['order'])."
515					".FRTLAN_43.": ".e107::getUserClass()->uc_dropdown('class', $rule_info['class'], 'public,guest,member,admin,main,classes')."
516					".$this->frm->admin_button('fp_save_new', LAN_UPDATE, 'update')."
517					".$this->frm->admin_button('fp_cancel', LAN_CANCEL, 'cancel')."
518				</div>
519			
520		</form>
521		";
522		return $text;
523	}
524
525
526
527	/**
528	 *	Given a path string related to a choice, returns the 'type' (title) for it
529	 *
530	 *	@param string $path
531	 *
532	 *	@return string - title of option
533	 */
534	function lookup_path($path)
535	{
536		foreach($this->frontPage as $front_value)
537		{
538			if(is_array($front_value['page']))
539			{ // Its a URL with multiple options
540				foreach($front_value['page'] as $multipage)
541				{
542					if($path == $multipage['page'])
543					{
544						//			  return $front_value['title'].":".$path;
545						return $front_value['title'].":".$multipage['title'];
546					}
547				}
548			}
549			else
550			{
551				if($path == $front_value['page'])
552				{
553					return $front_value['title'];
554				}
555			}
556		}
557		if(strlen($path))
558			return FRTLAN_51.":".$path; // 'Other'
559		else
560			return LAN_NONE; // 'None'
561	}
562
563
564
565	/**
566	 *	Show the selection options for a possible target of a rule
567	 *
568	 *	@param string $ob_name - name of the radio button which selects this element 
569	 *	@param string $front_key 
570	 *	@param array|string $front_value - array of choices, or a single value
571	 *	@param boolean $is_other - passed by reference - set if some other option is selected
572	 *	@param string $current_setting - current value
573	 *
574	 *	@return string - text for display
575	 */
576	function show_front_val($ob_name, $front_key, $front_value, &$is_other, $current_setting)
577	{
578		$type_selected = FALSE;
579		$text = '';
580
581		// First, work out if the selection os one of these options
582		if (is_array($front_value['page']))
583		{ // Its a URL with multiple options
584			foreach($front_value['page'] as $multipage)
585			{
586				if($current_setting == $multipage['page'])
587				{
588					$type_selected = TRUE;
589					$is_other = FALSE;
590				}
591			}
592		}
593		else
594		{
595			if($current_setting == $front_value['page'])
596			{
597				$type_selected = TRUE;
598				$is_other = FALSE;
599			}
600		}
601
602		// Now generate the display text - two table cells worth
603		if (is_array($front_value['page']))
604		{ // Multiple options for same page name
605			$text .= "
606				<td>
607					".$this->frm->radio($ob_name, $front_key, $type_selected, array('label'=>$front_value['title']))."
608				</td>
609				<td>
610			";
611			$text .= $this->frm->select_open($ob_name.'_multipage['.$front_key.']');
612			foreach($front_value['page'] as $multipage_key => $multipage_value)
613			{
614				$text .= "\n".$this->frm->option($multipage_value['title'], $multipage_key, ($current_setting == $multipage_value['page']))."\n";
615			}
616			$text .= $this->frm->select_close();
617			$text .= "</td>";
618		}
619		else
620		{ // Single option for URL
621			$text .= "
622				<td>
623					".$this->frm->radio($ob_name, $front_key, $type_selected, array('label'=>$front_value['title']))."
624
625				</td>
626				<td>&nbsp;</td>";
627		}
628		return $text;
629	}
630
631
632
633	/**
634	 *	Provide the text for an 'other' option - a text box for URL entry
635	 *
636	 *	@param string $ob_name - name of the radio button which selects this element 
637	 *	@param string $front_key 
638	 *	@param string $curval - current 'selected' value
639	 *	@param string $cur_page - probably the secondary (e.g. custom page) value for any option that has one
640	 *
641	 *	@return string - text for display
642	 */
643	function add_other($ob_name, $cur_val, $cur_page)
644	{
645		$label = ($cur_val) ? "Disabled or Enter Custom URL:" : "Custom URL: ";
646		
647	  	return  "
648			<td>".$this->frm->radio($ob_name, 'other', $cur_val, array('label'=> $label))."</td>
649			<td>".$this->frm->text($ob_name.'_other', ($cur_val ? $cur_page : ''), 150, "size=xxlarge&id={$ob_name}-other-txt")."</td>
650		";
651	}
652}
653
654require_once(e_ADMIN.'footer.php');
655
656/**
657 *	Log event to admin log
658 *
659 *	@param string $msg_num - exactly two numeric characters corresponding to a log message
660 *	@param string $woffle - information for the body of the log entre
661 *
662 *	@return none
663 */
664function frontpage_adminlog($msg_num = '00', $woffle = '')
665{
666	e107::getAdminLog()->log_event('FRONTPG_'.$msg_num, $woffle, E_LOG_INFORMATIVE, '');
667}
668
669
670function frontpage_adminmenu() 
671{
672
673	$action = vartrue($_GET['mode'],'main');
674	
675	$var['main']['text'] = LAN_MANAGE;
676	$var['main']['link'] = e_SELF;
677	$var['create']['text'] = LAN_CREATE;
678	$var['create']['link'] = e_SELF."?mode=create";
679
680
681	show_admin_menu(FRTLAN_PAGE_TITLE, $action, $var);
682}
683
684
685?>