본문 바로가기

Web/PHP

[self] 클래스 카테고리

<?
class Category
{
    var $objDB;
    var $objCommon;
    var $sTable = 'category';

    var $nCategoryID;
    var $sItem;
    var $sName;
    var $sHeader;
    var $sFooter;
    var $nParent;
    var $sCatePath;
    var $nRelation;
    var $nSort;


    function Category( )
    {
        $this->objDB     = $GLOBALS[objDB];
        $this->objCommon = $GLOBALS[objCommon];
    }
    

    # 데이터 값 넣기
    function setData( &$aPost )
    {
        $aDenyType = array(
            'submit',
        );

        foreach( $aPost as $index => $value )
        {
            if( !in_array( $index, $aDenyType ) )
            {
                $this->$index = $value;
            }
        }
    }


    # 현재 카테고리가 존재하는지 검사
    function categoryChk( $nCategoryID, $sName, $sItem )
    {
        $sTable = 'category';
        $sWhere = "nParent = $nCategoryID AND sName = '$sName' AND sItem = '$sItem'";

        # 반환값이 있으면 사용 못헌다.
        if( $this->objDB->getCount( $sTable, $sWhere ) )
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }        
    }


    # 정렬을 위해서 젤로큰 번호를 넘겨준다.
    function SortChk( )
    {
        $this->nSort = $this->objDB->getMax( $this->sTable, 'nCategoryID' );
    }


    # 하위 카테고리시 카테고리 패스 값 주기
    function catePathFind( )
    {
        if( $this->nCategoryID )
        {
            $sWhere = "nCategoryID = '$this->nCategoryID'";
            $data    = $this->objDB->slt( $this->sTable, 'sCatePath', $sWhere );
            $this->sCatePath = $data[sCatePath].'>>'.$this->sName;
        }
        else
        {
            $this->sCatePath = '>>'.$this->sName;
        }        
    }


    # 부모값이 있는지 검사.
    function parentFind( )
    {
        if( $this->nCategoryID )
        {
            $this->nParent = $this->nCategoryID;
        }
        else
        {
            $this->nParent = 0;
        }
    }


    # 카테고리를 써보자.
    function categoryWrite()
    {
        # 있는 카테고린지 검사.
        if( $this->categoryChk( $this->nCategoryID, $this->sName, $this->sItem ) )
        {
            $this->objCommon->msg( '사용할수없는 카테고립니다.', NULL );
        }

        $this->SortChk();
        $this->catePathFind();
        $this->parentFind();
    
        $aPost = array(
            'sItem'            => $this->sItem,
            'sName'            => $this->sName,
            'sHeader'        => $this->sHeader,
            'sFooter'        => $this->sFooter,
            'nParent'        => $this->nParent,
            'sCatePath'        => $this->sCatePath,
            'nSort'            => $this->nSort,
            'wdate'            => date(Ymd),
        );

        return $aPost;
    }


    # 받은 no로 카테고리 패스값을 넘겨준다.
    function noCatePathFind( $nCategoryID )
    {
        $sWhere = "nCategoryID=$nCategoryID";
        $data = $this->objDB->slt( $this->sTable, 'sCatePath', $sWhere );
        $this->sCatePath = $data[sCatePath];
    }


    # 카테고리 삭제해블자
    function removeSub( $nCategoryID )
    {
        $this->objDB->delete( $this->sTable, "nCategoryID = $nCategoryID" );
    }


    # 카테고리 삭제하기위해 찾자. 받았던 카테고리 패스를 기준으로 하위를 모두 찾는다.
    function categoryRemove( $nCategoryID )
    {
        $this->noCatePathFind( $nCategoryID );

        $sOrder = 'ORDER BY nCategoryID DESC';
        $sWhere = "sCatePath LIKE '$this->sCatePath%' AND sCatePath != '$this->sCatePath'";
        $sField = 'nCategoryID, sCatePath';
        
        # 리턴할내용
        $rtn = "삭제한 카테고리는 \\n$this->sCatePath";

        $res = $this->objDB->select( $this->sTable, $sField, $sWhere );        
        for( $i=0; $data = $this->objDB->fetch($res); $i++ )
        {
            $rtn .= "\\n$data[sCatePath]";
            $this->removeSub( $data[nCategoryID] );
        }
        $this->removeSub( $nCategoryID );
        
        $rtn .= "입니다.";
        return $rtn;
    }
    

    function modifyCatePath( $nCategoryID, $sCatePath )
    {
        $objDB->query( "UPDATE $this->sTable SET sCatePath = '$sCatePath' WHERE nCategoryID = $nCategoryID" );
    }


    function modifySub( &$sBfCatePath, &$sAfCatePath )
    {
        $sOrder = 'ORDER BY nCategoryID DESC';
        $sWhere = "sCatePath LIKE '$sBfCatePath%' AND sCatePath != $sBfCatePath";
        $sField = 'sCatePath';
        

        $rtn = "함계수정된 카테고리는\\n";
        $res = $objDB->select( $this->sTable, $sField, $sWhere );
        for( $i=0; $data = $objDB->fetch($res); $i++ )
        {
            $rtn .= "$data[sCatePath] \\n ";
            $sChangeCatePath = str_replace( $sBfCatePath, $sAfCatePath, $data[sCatePath] );
            $this->modifyCatePath( $data[nCategoryID], $sChangeCatePath );
        }
        $rtn .= "입니다.";

        return $rtn;
    }


    function modify( )
    {
        $this->catePathFind();
        $this->parentFind();

        $aPost = array(
            'sItem'            => $this->sItem,
            'sName'            => $this->sName,
            'sHeader'        => $this->sHeader,
            'sFooter'        => $this->sFooter,
            'nParent'        => $this->nParent,
            'sCatePath'        => $this->sCatePath,
        );

        return $aPost;
    }

    
    function selfnCategoryIDFind( $sCatePath )
    {
        $sWhere = "nCategoryID = $nCategoryID";
        $data = $objDB->slt( $this->sTable, 'nCategoryID', $sWhere );

        return $data[nCategoryID];
    }


    # 현재 자기 위치값을 내준다.
    function selfFind( $nCategoryID, $sItemName=NULL )
    {
        $sWhere = "nCategoryID = $nCategoryID";
        $data = $objDB->slt( $this->sTable, 'sCatePath', $sWhere );

        $aCatePath = explode( '>>', $data[sCatePath] );
        foreach( $aCatePath as $index => $value )
        {
            if( !$index )
            {
                echo "<a href='/'>HOME</a> >> ";
            }
            else
            {
                $nCategoyID = selfnCategoryIDFind( $value );
                echo "<a href='$PHP_SELF?nCategoryID=$nCategoryID'>$value</a> >> ";
                
                if( $sItemName )
                {
                    echo " <b>$sItemName 상품자세히보기</b>";
                }
            }
        }
    }


    # 최상위 카테고리 값을 넘겨준다.
    function topCategoryFind( $nSort=NULL )
    {
        $nSort = ( !$nSort ) ? 'nCategoryID' : $nSort;

        $sOrder = "ORDER BY $nSort DESC";
        $sWhere = "nParent=0";
        $sField = 'sName, nCategoryID';
    
        $this->objDB->select( $this->sTable, $sField, $sWhere );
        for( $i=0; $data = $this->objDB->fetch(); $i++ )
        {
            $nCategoryID = $data[nCategoryID];
            $topCategoryFind[$nCategoryID] = $data[sName];
        }
        return $topCategoryFind;
    }


    # 최상위 카테고리 갯수를 넘겨준다.
    function topCategoryTotal( )
    {
        $sWhere = 'nParent = 0';
        $rtn = $this->objDB->getCount( $this->sTable, $sWhere );

        return $rtn;
    }


    # 해당 카테고리의 갯수를 넘겨준다.(하위도 포함);
    function subCateogryTotal( $nCategoryID )
    {
        $this->noCatePathFind( $nCategoryID );
        
        $sWhere = "sCatePath LIKE '$this->sCatePath%'";
        $rtn = $this->objDB->getCount( $this->sTable, $sWhere );
        
        return $rtn;
    }
}

if( !$objCategory )
{
    $objCategory = & new Category;
}
?>

'Web > PHP' 카테고리의 다른 글

[self] 클래스 데이터체크  (0) 2013.09.26
[self] 클래스 공통사용  (0) 2013.09.26
[문법][팁] 변수이름 자체를 변수로 가공하기  (0) 2013.09.26
[self] 클래스 업로드  (0) 2013.09.26
GD를 이용한 원형 백분율 예제  (0) 2013.09.26