<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';

if (!isset($_REQUEST['ID'])) {
	die();
}

if (isset($_REQUEST['mode'])) {
    $mode = $_REQUEST['mode'];
}

if (isset($_REQUEST['detail'])) {
	$detail = 1;	
}

$tpl->assign('mode', $mode);
if ($mode != 'edit' && $mode != 'del') {
    if (isset($_SESSION['inplace_caterror'])) {
        unset($_SESSION['inplace_caterror']);
    }
} else {
    $tpl->assign('error_message', $_SESSION['inplace_caterror']);
}

$id = $_REQUEST['ID'];

$categs = get_categs_tree_frontend(0, $id);
$tpl->assign('categs', $categs);

if ($mode == 'del') {
    //deleting category
    echo '<h4 style="color: #666666;">Deleting category, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $category = getCategoryByID($id);
	    foreach ($category as $key=>$value) {
			$category[$key] = stripslashes($value);
		}
        $category['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $category['ID']);
        if ($category['rights']['delCat'] == 1) {
             //get category parent first
             $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
                                    WHERE `ID`=(SELECT `PARENT_ID` FROM `{$tables['category']['name']}`
                                    WHERE `ID`=".$db->qstr($id).")");
             
             $return = DOC_ROOT."/".$data;
             
             //delete cat
             $data = $db->Execute("DELETE FROM `{$tables['category']['name']}` WHERE `ID`=".$db->qstr($id)."");
             //delete subcats
             $data = $db->Execute("DELETE FROM `{$tables['category']['name']}` WHERE `PARENT_ID`=".$db->qstr($id)."");
             //delete links
             $data = $db->Execute("DELETE FROM `{$tables['link']['name']}` WHERE `CATEGORY_ID`=".$db->qstr($id)."");
             $data = $db->Execute("DELETE FROM `{$tables['link_review']['name']}` WHERE `CATEGORY_ID`=".$db->qstr($id)."");
             //delete articles
             $data = $db->Execute("DELETE FROM `{$tables['article']['name']}` WHERE `CATEGORY_ID`=".$db->qstr($id)."");
             $data = $db->Execute("DELETE FROM `{$tables['article_review']['name']}` WHERE `CATEGORY_ID`=".$db->qstr($id)."");
             
             if ($detail == '1') {
             	echo '<script> window.top.location = "'.$return.'"; </script>';
             } else {
             	echo '<script> window.top.location = window.top.location; </script>';
             }
        }
    }
} elseif ($mode == 'active') {
    //activating category
    echo '<h4 style="color: #666666;">Activating category, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $category = getCategoryByID($id);
        $category['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $category['ID']);
        if ($category['rights']['editCat'] == 1) {
             $data = $db->Execute("UPDATE `{$tables['category']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
             echo '<script> window.top.location = window.top.location; </script>';
        }
        echo '<script> window.top.location = window.top.location; </script>';
    }
} else {
    //Disable any caching by the browser
    disable_browser_cache();
    
//    //Generate unique imagehash for visual confirmation
//    if (VISUAL_CONFIRM == 1)
//    {
//       require_once 'include/functions_imgverif.php';
//       $imagehash = fetch_captcha_hash();
//       $tpl->assign('imagehash', $imagehash);
//       unset ($imagehash);
//    }
    
    
    $category = getCategoryByID($id);
    $tpl->assign('stats', array (0 => _L('Inactive'), 1 => _L('Pending'), 2 => _L('Active'),));
    
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $category['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $category['ID']);
        
        if ($category['rights']['editCat'] == 1) {
            if (isset($_REQUEST['action']) && $_REQUEST['action']=="submit") {
                $title 				= $_REQUEST['TITLE'];
                $description 		= $_REQUEST['DESCRIPTION'];
                $rss_url 			= $_REQUEST['RSS_URL'];
                $status 			= $_REQUEST['STATUS'];
                $tdescription 		= $_REQUEST['TDESCRIPTION'];
                $catcontent 		= $_REQUEST['CATCONTENT'];
                $meta_description 	= $_REQUEST['META_DESCRIPTION'];
                $meta_keywords 		= $_REQUEST['META_KEYWORDS'];
                $parent_id 			= $_REQUEST['PARENT_ID'];
                $id 				= $_REQUEST['ID'];
                
                
                
                //keep edited data somewhere, just in case it's not validated at submit from some reason
                
                $_SESSION['editcat'] = $_POST;
            	foreach ($_SESSION['editcat'] as $key=>$value) {
					$_SESSION['editcat'][$key] = stripslashes($value);
				}
                $data = $db->GetOne("SELECT COUNT(*) AS c FROM `{$tables['category']['name']}`
                                    WHERE `PARENT_ID`=".$db->qstr($parent_id)."
                                        AND `TITLE`=".$db->qstr($title)."
                                        AND `ID`!=".$db->qstr($id)."");
               if ($data['c']>0) {
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "Title is not unique in the parent category. Information not updated.";
               } elseif ($title == '') {
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "Title cannot be empty.";
               } elseif (!((strlen($title)>=TITLE_MIN_LENGTH) && (strlen($title)<=TITLE_MAX_LENGTH))){
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "The title must have ".TITLE_MIN_LENGTH." to ".TITLE_MAX_LENGTH." characters.";
               } elseif (!((strlen($description)>=DESCRIPTION_MIN_LENGTH) && (strlen($description)<=DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "The description must have ".DESCRIPTION_MIN_LENGTH." to ".DESCRIPTION_MAX_LENGTH." characters.";
               } elseif ($meta_description != '' && !((strlen($meta_description)>=META_DESCRIPTION_MIN_LENGTH) && (strlen($meta_description)<=META_DESCRIPTION_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "The meta description must have ".META_DESCRIPTION_MIN_LENGTH." to ".META_DESCRIPTION_MAX_LENGTH." characters.";
               } elseif ($meta_keywords != '' && !((strlen($meta_keywords)>=META_KEYWORDS_MIN_LENGTH) && (strlen($meta_keywords)<=META_KEYWORDS_MAX_LENGTH))) {
                    $error = 1;
                    $_SESSION['inplace_caterror'] = "The meta keywords must have ".META_KEYWORDS_MIN_LENGTH." to ".META_KEYWORDS_MAX_LENGTH." characters.";
//               } elseif (verify_captcha_hash($_POST['IMAGEHASH'],$_POST['CAPTCHA']) == 0) {
//                    $error = 1;
//                    $_SESSION['inplace_caterror'] = "Your security code does not match the image.";
               } else {
                    //Build URL
                    if (!get_magic_quotes_gpc()) {
                    	$title_url = addslashes(preg_replace('`[^\w_-]`', '_', $title));
	                    //Build cache
	                    $cache_title = addslashes(trim (buildCategUrlTitle($id)));
	                    $cache_url   = addslashes(trim (buildCategUrl($id)));
                    } else {
                    	$title_url = addslashes(preg_replace('`[^\w_-]`', '_', $title));
                    	//Build cache
                    	$cache_title = trim (buildCategUrlTitle($id));
                    	$cache_url   = trim (buildCategUrl($id));
                    }
                    if ($db->Execute("UPDATE `{$tables['category']['name']}`
                        SET `TITLE` = ".$db->qstr($title).",
                        `DESCRIPTION` = ".$db->qstr($description).",
                        `TITLE_URL` = ".$db->qstr($title_url).",
                        `CACHE_TITLE` = ".$db->qstr($cache_title).",
                        `CACHE_URL` = ".$db->qstr($cache_url).",
                        `RSS_URL` = ".$db->qstr($rss_url).",
                        `STATUS` = ".$db->qstr($status).",
                        `TDESCRIPTION` = ".$db->qstr($tdescription).",
                        `CATCONTENT` = ".$db->qstr($catcontent).",
                        `META_DESCRIPTION` = ".$db->qstr($meta_description).",
                        `META_KEYWORDS` = ".$db->qstr($meta_keywords).",
                        `PARENT_ID` = ".$db->qstr($parent_id)."
                         WHERE `ID`=".$db->qstr($id)."")) {

                    	//recaching category and subcats
						recacheCategory($id);
                    	
						if ($detail == '1') {
							//get category parent first
                            $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
                                                 WHERE `ID`=".$db->qstr($id).")");
                            $return = DOC_ROOT."/".$data;
                            echo '<script> window.top.location="' . $return . '"; </script>';
						} else {
                        	echo '<script> window.top.location = window.top.location; </script>';
						}
                        
                         } else {
                         	echo mysql_error();
                             $error = 1;
                             $_SESSION['inplace_caterror'] = "Sorry, some database error occured. Please try again."."UPDATE `{$tables['category']['name']}`
                        SET `TITLE` = ".$db->qstr($title).",
                        `DESCRIPTION` = ".$db->qstr($description).",
                        `TITLE_URL` = ".$db->qstr($title_url).",
                        `CACHE_TITLE` = ".$db->qstr($cache_title).",
                        `CACHE_URL` = ".$db->qstr($cache_url).",
                        `RSS_URL` = ".$db->qstr($rss_url).",
                        `STATUS` = ".$db->qstr($status).",
                        `TDESCRIPTION` = ".$db->qstr($tdescription).",
                        `CATCONTENT` = ".$db->qstr($catcontent).",
                        `META_DESCRIPTION` = ".$db->qstr($meta_description).",
                        `META_KEYWORDS` = ".$db->qstr($meta_keywords).",
                        `PARENT_ID` = ".$db->qstr($parent_id)."
                         WHERE `ID`=".$db->qstr($id)."";
                             echo '<script> window.top.location = window.top.location; </script>';
                         }
                }
            } else {
                if ($mode == "edit") {
                    $tpl->assign('category', $_SESSION['editcat']);
                     unset($_SESSION['editcat']);
                     unset($_SESSION['inplace_caterror']);
                } else {
                    $tpl->assign('category', $category);
                }
            }
        } else {
            $error = 1;
            $_SESSION['inplace_caterror'] = "Sorry, you have no editing rights for this category.";
			echo '<script> window.top.location = window.top.location; </script>';
        }
    } else {
        $error = 1;
        $_SESSION['inplace_caterror'] = "Sorry, you have no editing rights for this category.";
       	echo '<script>window.top.location = window.top.location; </script>';
    }
    $tpl->assign('error', $error);
    
    if ($error == 1) {
    	echo '<script>  window.top.location = window.top.location; </script>';
    }
    
    $useRTE = (defined ('LINK_SUBMIT_USE_RTE') && LINK_SUBMIT_USE_RTE == 1 ? 1 : 0);
    $tpl->assign('useRTE', $useRTE);
    echo $tpl->fetch('inplace_editcat.tpl');
}
?>
