<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// links manager admin
	// ========================================================================================
	
	
	$query = "select * from `".$Settings['DBPrefix']."lm-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$lmSettings[$row['Name']] = $row['Value'];
	}
	
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_links.gif" width="20" height="21"> '.$MsgText[8][2];
	$BrowserTitle = $MsgText[8][2];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[8][2];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[801] == 'y') {
		
			$PageContents .= '<br><a href="#newlink">'.$MsgText[8][7].'</a> | <a href="#newcat">'.$MsgText[8][8].'</a><br><br>';
	
	
			$query = "select * from `".$Settings['DBPrefix']."lm-Categories` ORDER BY Title ASC";
			$result = mysql_query($query) or die (mysql_error());
			
			while ($row = mysql_fetch_array($result)) { 
	            
				$CategoryIDsArray[$row['Parent']][] = $row['RecordNumber'];
				$CategoryNamesArray[$row['Parent']][] = $row['Title'];
				$CategoryExists[$row['RecordNumber']] = 'y';
				
				if ($row['Description'] != '') { 
					$CategoryDescriptionsArray[$row['Parent']][] = '<br>'.$row['Description'];
				}
				else {
					$CategoryDescriptionsArray[$row['Parent']][] = '<!--  -->';
				}
			
				
			}
			
			
			$query = "select * from `".$Settings['DBPrefix']."lm-Links` order by Title asc";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
			
				// if the page has been orphaned, put it back in top level
				if ($CategoryExists[$row['Category']] != 'y' && $CategoryExists[$row['Category2']] != 'y' && $CategoryExists[$row['Category3']] != 'y' && $CategoryExists[$row['Category4']] != 'y' && $CategoryExists[$row['Category5']] != 'y' && $CategoryExists[$row['Category6']] != 'y' && $CategoryExists[$row['Category7']] != 'y' && $CategoryExists[$row['Category8']] != 'y' && $CategoryExists[$row['Category9']] != 'y' && $CategoryExists[$row['Category10']] != 'y') {
					$row['Category'] = 0;
				}
            	
				$LinkIDsArray[$row['Category']][] = $row['RecordNumber'];
				$LinkTitlesArray[$row['Category']][] = $row['Title'];
				$LinkDescriptionsArray[$row['Category']][] = $row['Description'];
				$LinkURLsArray[$row['Category']][] = $row['Link'];
				
				if ($row['Category2'] > 0) {
					$LinkIDsArray[$row['Category2']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category2']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category2']][] = $row['Description'];
					$LinkURLsArray[$row['Category2']][] = $row['Link'];
				}
				if ($row['Category3'] > 0) {
					$LinkIDsArray[$row['Category3']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category3']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category3']][] = $row['Description'];
					$LinkURLsArray[$row['Category3']][] = $row['Link'];
				}
				if ($row['Category4'] > 0) {
					$LinkIDsArray[$row['Category4']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category4']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category4']][] = $row['Description'];
					$LinkURLsArray[$row['Category4']][] = $row['Link'];
				}
				if ($row['Category5'] > 0) {
					$LinkIDsArray[$row['Category5']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category5']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category5']][] = $row['Description'];
					$LinkURLsArray[$row['Category5']][] = $row['Link'];
				}
				if ($row['Category6'] > 0) {
					$LinkIDsArray[$row['Category6']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category6']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category6']][] = $row['Description'];
					$LinkURLsArray[$row['Category6']][] = $row['Link'];
				}
				if ($row['Category7'] > 0) {
					$LinkIDsArray[$row['Category7']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category7']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category7']][] = $row['Description'];
					$LinkURLsArray[$row['Category7']][] = $row['Link'];
				}
				if ($row['Category8'] > 0) {
					$LinkIDsArray[$row['Category8']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category8']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category8']][] = $row['Description'];
					$LinkURLsArray[$row['Category8']][] = $row['Link'];
				}
				if ($row['Category9'] > 0) {
					$LinkIDsArray[$row['Category9']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category9']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category9']][] = $row['Description'];
					$LinkURLsArray[$row['Category9']][] = $row['Link'];
				}
				if ($row['Category10'] > 0) {
					$LinkIDsArray[$row['Category10']][] = $row['RecordNumber'];
					$LinkTitlesArray[$row['Category10']][] = $row['Title'];
					$LinkDescriptionsArray[$row['Category10']][] = $row['Description'];
					$LinkURLsArray[$row['Category10']][] = $row['Link'];
				}
				
			}
			
			
			
			$CatsPrint .= '<br><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="10" alt="" border="0"><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:ExpandCollapse(\''.$ItemsCount.'\');" style="text-decoration: none;"><img id="img'.$ItemsCount.'" src="'.$ImagesURL.'/admin/minus.gif" border="0" width="9" height="9" alt=""><font size="2" face="Arial" color="'.$Colors['Text'].'">&nbsp;<b>'.$MsgText[0][248].'</b></font><br><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"><br><div id="div'.$ItemsCount.'"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="40"><img src="'.$ImagesURL.'/admin/spacer.gif" width="40" height="1" alt="" border="0"></td><td width="100%">';
			
			
			
			
			
			
			function SpiderCategories($CatParent) {
				
				Global $CategoryIDsArray, $CategoryNamesArray, $CategoryDescriptionsArray, $LinkURLsArray, $LinkTitlesArray, $LinkDescriptionsArray, $LinkIDsArray, $CatsPrint, $LastParent, $LastParentAbove, $CatSpacersArray, $CatLevel, $CatsCount, $BaseURL, $ImagesURL, $MainURL, $FontSize, $FontColor, $MsgText, $Permissions, $ItemsCount, $Colors;
				
				for ($i = 0; $i < count($CategoryIDsArray[$CatParent]); $i++) {
					
					$LastParentAboveB = $LastParentAbove;
					$LastParentB = $LastParent;
					
					$CatLevel[$CategoryIDsArray[$CatParent][$i]] = $CatLevel[$CatParent];
					if ($CatParent != $LastParent && $CatParent == $LastParentAbove) {
				
						$CatLevel[$CategoryIDsArray[$CatParent][$i]] = $CatLevel[$CatParent] + 1;
						$CatLevel[$CatParent]++;
						
						$CatsCount[$CatLevel[$CatParent]] = 0;
				
					}
					
					$LastParent = $CatParent;
					$LastParentAbove = $CategoryIDsArray[$CatParent][$i];
					
					$CatsCount[$CatLevel[$CatParent]]++;
					
					
					
					$CatEditLink = '&nbsp;&nbsp;&nbsp;<font size="1"><a href="'.$MainURL.'/admin/links-catedit/'.$CategoryIDsArray[$CatParent][$i].'.html">'.$MsgText[0][661].'</a></a>';
			
					$ItemsCount++;
										
					if ($CategoryDescriptionsArray[$CatParent][$i] != '') {
						$CategoryDescriptionsArray[$CatParent][$i] = $CategoryDescriptionsArray[$CatParent][$i] . '<br>';
					}
					
					$CatsPrint .= '<img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="10" alt="" border="0"><br><table border="0" cellpadding="0" cellspacing="0"><tr><td><font size="2" face="Arial" color="'.$Colors['Text'].'">&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:ExpandCollapse(\''.$ItemsCount.'\');" style="text-decoration: none;"><img id="img'.$ItemsCount.'" src="'.$ImagesURL.'/admin/minus.gif" border="0" width="9" height="9" alt=""></a><font size="2" face="Arial" color="'.$Colors['Text'].'">&nbsp;</font></td><td><a href="javascript:ExpandCollapse(\''.$ItemsCount.'\');" style="text-decoration: none;"><font size="2" face="Arial" color="'.$Colors['Text'].'"><b>'.$CategoryNamesArray[$CatParent][$i].'</b></a>'.$CatEditLink.'<font size="1">'.$CategoryDescriptionsArray[$CatParent][$i].'</font></font></td></tr></table><div id="div'.$ItemsCount.'"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="40"><img src="'.$ImagesURL.'/admin/spacer.gif" width="40" height="1" alt="" border="0"></td><td width="100%">';
					
					
								
					
					
					if (count($CategoryIDsArray[$CatParent][$i]) > 0) {
						SpiderCategories($CategoryIDsArray[$CatParent][$i]);
					}
					
					
					
					
					for ($x = 0; $x < count($LinkIDsArray[$CategoryIDsArray[$CatParent][$i]]); $x++) {
					
						
						if ($LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x] != '') {
							
							if (strlen($LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x]) > 70) {
								$LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x] = substr($LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x],0,70).'...';
							}
						
							$LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x] = '<br><font size="1">'.$LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x].'</font>';
						}

						$CatsPrint .= '<img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"><br><table border="0" cellpadding="0" cellspacing="0"><tr><td width="20"><img src="'.$ImagesURL.'/admin/spacer.gif" width="20" height="1" alt="" border="0"></td><td width="1" bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td><td width="10"><img src="'.$ImagesURL.'/admin/spacer.gif" width="10" height="1" alt="" border="0"></td><td><font size="2" face="Arial" color="'.$Colors['Link'].'"><a href="'.$MainURL.'/admin/links-edit/'.$LinkIDsArray[$CategoryIDsArray[$CatParent][$i]][$x].'.html" style="text-decoration: none;"><b>'.$LinkTitlesArray[$CategoryIDsArray[$CatParent][$i]][$x].'</b></a>'.$LinkDescriptionsArray[$CategoryIDsArray[$CatParent][$i]][$x].'</font></td><td><img src="'.$ImagesURL.'/admin/spacer.gif" width="20" height="1" alt="" border="0"></td><td align="center"><font size="1" face="Arial"><a href="'.$MainURL.'/admin/links-edit/'.$LinkIDsArray[$CategoryIDsArray[$CatParent][$i]][$x].'.html"><img src="'.$ImagesURL.'/admin/pages_edit.gif" border="0" width="16" height="16" alt=""><br>'.$MsgText[0][155].'</a></td></tr></table><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"><br>';
					
					}
					
					
					
					
					$CatsPrint .= '</td></tr></table><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"></div><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="10" alt="" border="0"><br>';
				
				}
				
				
			}
			
			SpiderCategories('0');
			
			
			if (count($LinkIDsArray[0]) > 0) {
								
				for ($x = 0; $x < count($LinkIDsArray[0]); $x++) {
											
										
					if ($LinkDescriptionsArray[0][$x] != '') {
							
						if (strlen($LinkDescriptionsArray[0][$x]) > 70) {
							$LinkDescriptionsArray[0][$x] = substr($LinkDescriptionsArray[0][$x],0,70).'...';
						}
						
						$LinkDescriptionsArray[0][$x] = '<br><font size="1">'.$LinkDescriptionsArray[0][$x].'</font>';
					}

			
					$CatsPrint .= '<img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"><br><table border="0" cellpadding="0" cellspacing="0"><tr><td width="20"><img src="'.$ImagesURL.'/admin/spacer.gif" width="20" height="1" alt="" border="0"></td><td width="1" bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td><td width="10"><img src="'.$ImagesURL.'/admin/spacer.gif" width="10" height="1" alt="" border="0"></td><td><font size="2" face="Arial" color="'.$Colors['Link'].'"><a href="'.$MainURL.'/admin/links-edit/'.$LinkIDsArray[0][$x].'.html" style="text-decoration: none;"><b>'.$LinkTitlesArray[0][$x].'</b></a>'.$LinkDescriptionsArray[0][$x].'</font></td><td><img src="'.$ImagesURL.'/admin/spacer.gif" width="20" height="1" alt="" border="0"></td><td align="center"><font size="1" face="Arial"><a href="'.$MainURL.'/admin/links-edit/'.$LinkIDsArray[0][$x].'.html"><img src="'.$ImagesURL.'/admin/pages_edit.gif" border="0" width="16" height="16" alt=""><br>'.$MsgText[0][155].'</a></td></tr></table><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="4" alt="" border="0"><br>';
					
				
				}
					
			}
			
			
			
			$CatsPrint .= '</td></tr></table></div>';
				
				
					
			
			$PageContents .= $CatsPrint;
			
			
			$PageContents .= '<script>

var ItemsCount = '.number_format($ItemsCount,0,'','').';

function CollapseAll() {

	for (x=1; x<=ItemsCount; x++) {
	
		var ElementID = document.getElementById("div"+x);
		var ImgElementID = document.getElementById("img"+x);
		
		ElementID.style.display = "none"
		ImgElementID.src = "'.$ImagesURL.'/admin/plus.gif";
	
	}

}

function ExpandCollapse(IDNumber) {

	var ElementID = document.getElementById("div"+IDNumber);
	var ImgElementID = document.getElementById("img"+IDNumber);
	
	if (ElementID.style.display != "none") {
		ElementID.style.display = "none"
		ImgElementID.src = "'.$ImagesURL.'/admin/plus.gif";
	}
	else {
		ElementID.style.display = ""
		ImgElementID.src = "'.$ImagesURL.'/admin/minus.gif";
	}

}

CollapseAll();

</script>';
			
			
			
			$query69 = "select * from `".$Settings['DBPrefix']."lm-Links` order by RecordNumber desc limit 1";
			$result69 = mysql_query($query69) or die (mysql_error());
			$row69 = mysql_fetch_array($result69);
			
			$LastCatUsed = $row69['Category'];
			
			
			
					
			$CategoryDropdown = '<option value="0"> '.$MsgText[0][248].'</option>';
			$CategoryDropdown2 = '<option value="0"> '.$MsgText[0][248].'</option>';
			
			
			$CatParent5 = 0;
			$LastParentAbove5 = '0';
			$CatLevel5['0'] = 0;
						
			function SpiderCategories5($CatParent5) {
				
				Global $CategoryIDsArray, $CategoryNamesArray, $CategoryDropdown, $CategoryDropdown2, $LastParent5, $LastParentAbove5, $CatSpacersArray5, $CatLevel5, $CatsCount5, $LastCatUsed;
				
				for ($i = 0; $i < count($CategoryIDsArray[$CatParent5]); $i++) {
					
					$LastParentAboveB5 = $LastParentAbove5;
					$LastParentB5 = $LastParent5;
					
					$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5];
					if ($CatParent5 != $LastParent5 && $CatParent5 == $LastParentAbove5) {
				
						$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5] + 1;
						$CatLevel5[$CatParent5]++;
												
						$CatsCount5[$CatLevel5[$CatParent5]] = 0;
				
					}
					
					$LastParent5 = $CatParent5;
					$LastParentAbove5 = $CategoryIDsArray[$CatParent5][$i];
					
					$CatsCount5[$CatLevel5[$CatParent5]]++;
					
										
					$CatSpacer5 = '';
					for ($x = 0; $x < $CatLevel5[$CatParent5]; $x++) {
						
						if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5]) && $x == $CatLevel5[$CatParent5]-1) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'y';
						}
						else if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5])) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						else {
							if ($x < $CatLevel5[$CatParent5]) {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
								$ResetAfter5 = 'y2';
							}
							else {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							}
						}
						
							
						$CatSpacer5 .= $CatSpacersArray5[$x+1];
						
						if ($ResetAfter5 == 'y') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
						else if ($ResetAfter5 == 'y2') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
										
					}
					
					
					if ($LastCatUsed == $CategoryIDsArray[$CatParent5][$i]) {
						$Selected = ' selected';
					}
					else {
						$Selected = '';
					}
					
					$CategoryDropdown .= '<option value="'.$CategoryIDsArray[$CatParent5][$i].'"'.$Selected.'> '.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i].'</option>';

					$CategoryDropdown2 .= '<option value="'.$CategoryIDsArray[$CatParent5][$i].'"> '.$CatSpacer5.' '.$CategoryNamesArray[$CatParent5][$i].'</option>';
					
										
					if (count($CategoryIDsArray[$CatParent5][$i]) > 0) {
						SpiderCategories5($CategoryIDsArray[$CatParent5][$i]);
					}
				
				}
				
				
			}
			
			SpiderCategories5('0');	
								
			
			
			$PageContents .= '<br><br><a name="newlink"></a><br><b>'.$MsgText[8][7].':</b><br><form action="'.$MainURL.'/admin/links-new" method="post">
			
			<b>'.$MsgText[8][13].':</b><br>
			<input type="text" name="Title" size="50" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][14].':</b><br>
			<input type="text" name="Link" size="50" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][15].':</b><br>
			<input type="text" name="Description" size="50" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][16].':</b><br>
			<input type="text" name="StartingCount" size="10" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][17].':</b><br>
			<select name="Category" style="'.$FormStyle.'">'.$CategoryDropdown.'</select><br>
			<select name="Category2" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category3" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category4" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category5" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category6" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category7" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category8" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category9" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select><br>
			<select name="Category10" style="'.$FormStyle.'"><option value="0"></option>'.$CategoryDropdown2.'</select>
			<br><br><br>
			<input type="submit" value="'.$MsgText[8][18].'" style="'.$FormStyle.'">
			
			</form>
			
			
			<br><br><br><br><a name="newcat"></a><br><b>'.$MsgText[8][8].':</b><br><form action="'.$MainURL.'/admin/links-newcat" method="post">
			
			<b>'.$MsgText[8][13].':</b><br>
			<input type="text" name="Title" size="50" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][15].':</b><br>
			<input type="text" name="Description" size="50" style="'.$FormStyle.'">
			<br><br>
			<b>'.$MsgText[8][17].':</b><br>
			<select name="Parent" style="'.$FormStyle.'">'.$CategoryDropdown2.'</select>
			<br><br><br>
			<input type="submit" value="'.$MsgText[8][19].'" style="'.$FormStyle.'">
			
			</form>';
			
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
				
	
	// ========================================================================================




?>
