<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
require_once MODEPATH.'./'.$adminpath.'/makehtml.func.php';
function slist($lists = array(),$id = 0,$step=0){
	@include lang('themes','themes/admin');
	$data = '';
	if(count($lists)){	
		foreach($lists[$id] AS $key => $sort){		
					$trcolor=!$sort['upid'] ? "#F1FAFE" : "#FFFFFF";
					$data.='<tr bgcolor="'.$trcolor.'" align="center" ><td class="smalltxt" width="8%" style="border-bottom:1px solid #DAEEFA;">'.$sort['id'].'</td><td style="border-bottom:1px solid #DAEEFA;" width="15%"><select name="upidnew['.$sort['id'].']"><option value="0">------</option>'.sortselect($lists,$sort['upid']).'</select></td><td style="border-bottom:1px solid #DAEEFA;" align="left">'.($sort['upid']>0?'├':'').str_repeat('----',$step).'<input type="text" name="sortnamenew['.$sort['id'].']" value="'.$sort['sortname'].'"size="15"></td><td style="border-bottom:1px solid #DAEEFA;" width="10%"><input stype="text" name="foldersnew['.$sort['id'].']" value="'.$sort['folders'].'" size="5"></td><td style="border-bottom:1px solid #DAEEFA;" width="10%"><input type="text" name="ordermodenew['.$sort['id'].']" Value="'.$sort['ordermode'].'" size="5"></td style="border-bottom:1px solid #DAEEFA;" width="10%"><td style="border-bottom:1px solid #DAEEFA;" width="10%"><a href="index.php?action=pic_sort&delsort='.$sort['id'].'" onclick="return check(\''.$lang['pic_ask'].'\', \'index.php?action=pic_sort&delsort='.$sort['id'].'\');">'.$lang['deleting'].'</a></td></tr>'."\n";
			
			if(count($lists[$sort['id']])){
				$data.=slist($lists,$sort['id'],$step+1);				
			}
		}
	}
	return $data;
}

if(cpchecksubmit('addnew')){
			$sorts=$seletedsort=array();
			$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode");
			while($sort = $db -> fetch_array($query)){
				$sorts[$sort['id']] = $sort;
				$seletedsort[$sort['upid']][$sort['id']] = $sort;
			}			
			$newfolders = trim($newfolders);
			if(!$newfolders) {
				cpmsg('sort_folder_must');
			}
			if(!((ereg("^[a-zA-Z0-9]+$", $newfolders) || ereg("^[a-zA-Z]+$", $newfolders)) && !ereg("^[0-9]+$", $newfolders))){
				cpmsg('sort_folder_unaccepted');
			}
			$folder = $db -> queryarray("SELECT * FROM {$tablepre}picsort WHERE folders='$newfolders'");
			if($folder){
				cpmsg('sort_folder_exit');
			}
			//$path = '/p/'.$newfolders;
			$path = '/'.$newfolders;
			if($newupid) {
				$path = getupfloders($sorts,$newupid).$path;
			}
			if(is_dir(MODEPATH.'.'.$path)){
				cpmsg('sort_folder_exit');
			}
			mkpaths($path);	
			$db -> query("INSERT INTO {$tablepre}picsort  (upid,sortname,folders, folderdir) VALUES ('$newupid', '$newsortname','$newfolders','$path') ");
			$newid = $db->insert_id();
			unset($sorts);
			makelist($newid,1);
			makeindex();
			cpmsg('succeeded', 'index.php?action=pic_sort');
		}elseif(cpchecksubmit('updatelist')){
			$sorts=$seletedsort=array();
			$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode");
			while($sort = $db -> fetch_array($query)){
				$sorts[$sort['id']] = $sort;
				$seletedsort[$sort['upid']][$sort['id']] = $sort;
			}
	
			if(is_array($sortnamenew)){
				foreach($sortnamenew AS $key => $thesort){	
					$path='';
					if($upidnew[$key] !=$sorts[$key]["upid"] && $upidnew[$key] && checktype($sorts,$key,$upidnew[$key])) {
						cpmsg('sort_move_unaccepted');
					}
					$foldersnew[$key] = trim($foldersnew[$key]);
					if(!$foldersnew[$key]) {
						cpmsg('sort_folder_must');
					}
					if(!((ereg("^[a-zA-Z0-9]+$", $foldersnew[$key]) || ereg("^[a-zA-Z]+$", $foldersnew[$key])) && !ereg("^[0-9]+$", $foldersnew[$key]))){
						cpmsg('sort_folder_unaccepted');
					}
					if($foldersnew[$key] != $sorts[$key]["folders"]) {
						$folder = $db -> queryarray("SELECT * FROM {$tablepre}picsort WHERE folders='".$foldersnew[$key]."'");
						if($folder){
							cpmsg('sort_folder_exit2');
						}						
					}
					//$path = '/p/'.$foldersnew[$key];
					$path = '/'.$foldersnew[$key];
					if($upidnew[$key]) {
						$path = getupfloders($sorts,$upidnew[$key]).$path;
					}				
					if($foldersnew[$key] != $sorts[$key]["folders"] && is_dir(MODEPATH.'.'.$path)){
						cpmsg('sort_folder_exit2');
					}
					mkpaths($path);
					$db -> query("UPDATE {$tablepre}picsort SET upid='$upidnew[$key]', sortname='$sortnamenew[$key]', folders='$foldersnew[$key]', folderdir='$path', ordermode='$ordermodenew[$key]' WHERE id='$key'");
				}
			}	
			cpmsg('succeeded', 'index.php?action=pic_sort');
		}elseif($delsort){
			$sorts=$seletedsort=array();
			$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode");
			while($sort = $db -> fetch_array($query)){
				$sorts[$sort['id']] = $sort;
				$seletedsort[$sort['upid']][$sort['id']] = $sort;
			}
			if(empty($sorts[$delsort])) {
				cpmsg('error');
			}
			if(count($seletedsort[$delsort])) {
				cpmsg('sort_del_unaccepted');
			}
			$folders = $sorts[$delsort]['folderdir'];		
			if(!$folders) {
				$folders=getupfloders($sorts,$delsort);
			}
			clearfolder($folders);	
			@rmdir(MODEPATH.'.'.$folders);
			$db -> query("DELETE FROM {$tablepre}picsort WHERE id='$delsort'");
			$query = $db -> query("SELECT * FROM {$tablepre}pic WHERE id='$delsort'");
			$ids = array();
			while($pic = $db -> fetch_array($query)) {	
				@unlink(MODEPATH.$attachdir.'/'.$pic['face']);
				$ids[$pic['pid']]=$pic['pid'];
			}
			if($ids) {

				$upids = idarray($ids);
				$query = $db -> query("SELECT * FROM {$tablepre}uploads WHERE pid IN($upids)");
				while($up = $db -> fetch_array($query)) {	
					@unlink(MODEPATH.$attachdir.'/'.$up['attachment']);
					@unlink(MODEPATH.$attachdir.'/'.$up['face']);
				}
				$db -> query("DELETE FROM {$tablepre}uploads WHERE pid IN($upids)");	
			}			
			$db -> query("DELETE FROM {$tablepre}pic WHERE id='$delsort'");			
			cpmsg('succeeded', 'index.php?action=pic_sort');
		}else{
			$sorts=$seletedsort=array();
			$query = $db -> query("SELECT * FROM {$tablepre}picsort ORDER BY ordermode");
			while($sort = $db -> fetch_array($query)){
				$sorts[$sort['id']] = $sort;
				$seletedsort[$sort['upid']][$sort['id']] = $sort;
			}			
			$select = sortselect($seletedsort);
			$listsort= slist($seletedsort);
			include theme('pic_sort', 'themes/admin', 'admin');
		}

?>