Просмотр файла flashcat/admin/categories.php

Размер файла: 10.94Kb
<?php
	require_once "../conf.php";
	require_once 'auth.php';
	include 'menu.php';

	if(!$_REQUEST[os]>0){
    	$_REQUEST[os]=0;
	}

    if(!mysql_connect($_conf[mysql_host],$_conf[mysql_user],$_conf[mysql_passwd])){
    	die('Не удалось установить соединение с MySQL!');
    }

    if(!mysql_select_db($_conf[mysql_dbname])){
        die('База данных не найдена!');
    }

    if(isset($_REQUEST['act'])){
    	switch($_REQUEST['act']){
            case 's_add':
                show_form('add');
                echo "<a href=\"categories.php?parent_id={$_REQUEST[parent_id]}\">Назад</a>";
            	break;
            case 's_edit':
                show_form('edit',$_REQUEST['id']);
                echo "<a href=\"categories.php?parent_id={$_REQUEST[parent_id]}\">Назад</a>";
            	break;
        	case 'add':
	            mysql_query('insert into '.$_conf[table_prefix].'categories (name, parent_id) values (\''.$_REQUEST[name].'\', \''.$_REQUEST[parent_id].'\')');
	            show();
        		break;
        	case 'edit':
	            mysql_query('update '.$_conf[table_prefix].'categories set name = \''.$_REQUEST['name'].'\' where id='.$_REQUEST[id]);
	            show();
        		break;
        	case 'del':
                mysql_query('delete from '.$_conf[table_prefix].'categories where id='.$_REQUEST[id]);
                mysql_query('delete from '.$_conf[table_prefix].'sites where category_id='.$_REQUEST[id]);
                $res=mysql_query('select * from '.$_conf[table_prefix].'categories where parent_id='.$_REQUEST[id]);
                while($row=mysql_fetch_array($res)){
	                mysql_query('delete from '.$_conf[table_prefix].'categories where id='.$row[id]);
	                mysql_query('delete from '.$_conf[table_prefix].'sites where category_id='.$row[id]);
                }
                show();
                break;
        	default:
            	show();
    	}
    }else{
    	show();
    }

	function show(){
		GLOBAL $_conf;
        if($_REQUEST[parent_id]>0){
        	$cat=mysql_fetch_array(mysql_query('select * from '.$_conf[table_prefix].'categories where id='.$_REQUEST[parent_id]));
            $name = $cat[name].': подразделы';
        }else{
        	$name = 'Разделы каталога';
        }
        $query='select
	            c.id,
	            c.name,
	            count(sc.id) sc_n
        	from
        		'.$_conf[table_prefix].'categories c
        		left join '.$_conf[table_prefix].'categories sc on (c.id = sc.parent_id)
        	where
        		c.parent_id=\''.$_REQUEST[parent_id].'\'
        	group by c.id order by c.name';
        $res=mysql_query($query);

        $res1=mysql_query('select c.id, count(s.id) s_n from '.$_conf[table_prefix].'categories c left join '.$_conf[table_prefix].'sites s on (s.category_id=c.id) where c.parent_id=\''.$_REQUEST[parent_id].'\' group by c.id');
        $total_sites=array();
        while($row1=mysql_fetch_array($res1)){
        	$total_sites[$row1[id]]=$row1[s_n];
        }

        $res1=mysql_query('select c.id, count(s.id) s_n from '.$_conf[table_prefix].'categories c left join '.$_conf[table_prefix].'sites s on (s.category_id=c.id and s.allowed=0) where c.parent_id=\''.$_REQUEST[parent_id].'\' group by c.id');
        $new_sites=array();
        while($row1=mysql_fetch_array($res1)){
        	$new_sites[$row1[id]]=$row1[s_n];
        }
        ?>
		<table bgcolor="#d1d1d1" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<th style="padding: 5px;">
	                <table border="0" cellspacing="0" cellpadding="0" width="100%">
	                    <tr>
	                        <td><b><?=$name?>:</b> <a href="categories.php?act=s_add&parent_id=<?=$_REQUEST[parent_id]?>">добавить</a></td>
	                        <td align="right" valign="bottom"><strong>Всего: <?=mysql_num_rows($res)?></strong></td>
	                    </tr>
	                </table>
				</th>
			</tr>
			<tr>
				<td>
	                <table cellspacing="1" border="0" cellpadding="2" width="100%">
	                    <tr>
	                        <td bgColor="#f5f5f5" align="left" width=100%>&nbsp;Название</td>
	                        <td bgColor="#f5f5f5" align="center">&nbsp;Сайтов</td>
	                        <td bgColor="#f5f5f5" align="center">&nbsp;На&nbsp;модерации</td>
	                        <?
                            if($_REQUEST[parent_id]==0){
	                            //учет сайтов в подразделах
                                $query='
                                    select
                                        c.id,
                                        count(s.id) s_n
                                    from
                                        '.$_conf[table_prefix].'categories c
                                        left join '.$_conf[table_prefix].'categories sc on (sc.parent_id=c.id)
                                        left join '.$_conf[table_prefix].'sites s on (s.category_id=sc.id)
                                    where
                                        c.parent_id=\''.$_REQUEST[parent_id].'\'
                                    group by c.id';
	                            $res1=mysql_query($query);
	                            $cn_sites=array();
	                            while($row1=mysql_fetch_array($res1)){
	                                $cn_sites[$row1[id]]=$row1[s_n];
	                            }
                                $query='
                                    select
                                        c.id,
                                        count(s.id) s_n
                                    from
                                        '.$_conf[table_prefix].'categories c
                                        left join '.$_conf[table_prefix].'categories sc on (sc.parent_id=c.id)
                                        left join '.$_conf[table_prefix].'sites s on (s.category_id=sc.id and s.allowed=0)
                                    where
                                        c.parent_id=\''.$_REQUEST[parent_id].'\'
                                    group by c.id';
	                            $res1=mysql_query($query);
	                            $cn_sites_n=array();
	                            while($row1=mysql_fetch_array($res1)){
	                                $cn_sites_n[$row1[id]]=$row1[s_n];
	                            }

                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;Подразделов</td>';
                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;Сайтов&nbsp;в&nbsp;подразделах</td>';
                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;На&nbsp;модерации</td>';
                            }
	                        ?>
	                        <td bgColor="#f5f5f5">&nbsp;</td>
	                    </tr>
	                    <?
	                    while ($row=mysql_fetch_array($res)) {
	                    ?>
	                    <tr>
	                        <td bgColor="#f5f5f5" align="left">&nbsp;<?=$row[name]?></td>
	                        <td bgColor="#f5f5f5" align="center">&nbsp;<?=(int)$total_sites[$row[id]]?></td>
	                        <td bgColor="#f5f5f5" align="center">&nbsp;<?=($new_sites[$row[id]]>0?'<b style="color: red;">'.(int)$new_sites[$row[id]].'</b>':'0')?></td>
                            <?
                            if($_REQUEST[parent_id]==0){
                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;'.$row[sc_n].'</td>';
                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;'.(int)$cn_sites[$row[id]].'</td>';
                            	echo '<td bgColor="#f5f5f5" align="center">&nbsp;'.($cn_sites_n[$row[id]]>0?'<b style="color: red;">'.(int)$cn_sites_n[$row[id]].'</b>':'0').'</td>';
                            }
	                        ?>
	                        <td bgColor="#f5f5f5" align="right"><nobr>
	                        <?
								echo '[<a href="categories.php?act=s_edit&id='.$row[id].'&parent_id='.$_REQUEST[parent_id].'">Изменить</a>]';
								if($_REQUEST[parent_id]==0){
                                	echo '[<a href="categories.php?parent_id='.$row[id].'">Подразделы</a>]';
								}
								echo '[<a href="sites.php?category_id='.$row[id].'">Сайты</a>]';
								echo '[<a href="categories.php?act=del&id='.$row[id].'&parent_id='.$_REQUEST[parent_id].'" onclick="return confirm(\'Действительно удалить раздел?\');">Удалить</a>]';
                           	?> </nobr>
	                        </td>
	                    </tr>
	                    <?
	                    }
	                    ?>
	                </table>
	        	</td>
	    	</tr>
		</table>
<?
		if($_REQUEST[parent_id]>0){
        	echo '<a href="categories.php">Назад</a>';
		}
	}

	function show_form($act,$id=null){
        GLOBAL $_conf;
        if($act=='edit'){
	        $row=mysql_fetch_array(mysql_query('select * from '.$_conf[table_prefix].'categories where id='.$id));
	        $name = '<b>Редактирование раздела</b>';
	        $submit='Сохранить';
	    }else{
           $d->priority=0;
           $name = '<b>Создание нового раздела</b>';
        	 $submit='Добавить';
	    }
?>
	    <script Language="JavaScript">
	    function checkform(theForm)
	    {
	        if (theForm.name.value == "")
	        {
	            alert("Введите название раздела!");
	            theForm.name.focus();
	            return (false);
	        }
	        return (true);
	    }
	    </script>
		<table bgcolor="#d1d1d1" cellpadding="1" cellspacing="0" border="0" width="100%">
			<form action="categories.php" method="post" onsubmit="return checkform(this);">
			<tr>
				<th style="padding: 5px;">
	                <table border="0" cellspacing="0" cellpadding="0" width="100%">
	                    <tr>
	                        <td><b><?=$name?></b></td>
	                    </tr>
	                </table>
				</th>
			</tr>
            <tr>
                <td>
                	<input type="hidden" name="act" value="<?=$act?>">
	                <input type="hidden" name="id" value="<?=$id?>">
	                <input type="hidden" name="parent_id" value="<?=$_REQUEST[parent_id]?>">
	                <table cellpadding="2" bgcolor="#f5f5f5" width="100%" border="0">
	                	<tr>
	                    	<td valign="top">Название&nbsp;</td>
	                        <td bgColor=#ffffff width="100%">
	                        	<input style="width:100%" type="text" name="name" value="<?=$row[name]?>">
                        	</td>
                        </tr>
	                    <tr>
	                    	<td valign="top">&nbsp;</td>
	                        <td><input bgColor=#f5f5f5 type="submit" value="<?=$submit?>" style="width:100;"></td>
	                    </tr>
	                </table>
                </td>
            </tr>
            </form>
        </table>
<?
	}