<?php 
/*#################################################################*\
|# Licence Number 0H6R-0808-0UVQ-0111
|# -------------------------------------------------------------   #|
|# Copyright (c)2012 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	 
	
/**
 # ################################################################################
 # Project:   PHP Link Directory
 #
 # **********************************************************************
 # Copyright (C) 2004-2012 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 if you purchased any of our "link back" 
 #      versions of the software.
 #
 #
 # 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-2012 NetCreated, Inc. (http://www.netcreated.com/)
 # @projectManager David DuVal <david@david-duval.com>
 # @package        PHPLinkDirectory
 # @version        4.2.1
 # ################################################################################
 */
 
require_once 'init.php';

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

if (isset($_REQUEST['mode'])) {
    $mode = $_REQUEST['mode'];
}
$tpl->assign('mode', $mode);

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

$id = $_REQUEST['ID'];
$link = getLinkByID($id);
foreach ($link as $key=>$value) {
	$link[$key] = stripslashes($value);
}

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

if ($mode == 'del') {
    //deleting link
    echo '<h4 style="color: #666666;">Deleting link, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
        if ($link['rights']['delLink'] == 1) {
             //get category parent first
             $data = $db->GetOne("SELECT `CACHE_URL` FROM `{$tables['category']['name']}`
                                    WHERE `ID`=(SELECT `CATEGORY_ID` FROM `{$tables['link']['name']}`
                                    WHERE `ID`=".$db->qstr($id).")");
             
             $return = DOC_ROOT."/".$data;
             
             //delete links
             $data = $db->Execute("DELETE FROM `{$tables['link']['name']}` WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("DELETE FROM `{$tables['link_review']['name']}` WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
        	if (isset($_REQUEST['detail']) && ($_REQUEST['detail'] == '1')) {
            	echo '<script> window.top.location="' . $return . '"; </script>';
            } else {
             	echo '<script> window.top.location = window.top.location; </script>';
            }
        }
    }
    
} elseif ($mode == 'active') {
    echo '<h4 style="color: #666666;">Activating link, please wait...</h4>';
    if (isset($_REQUEST['ID']) && isset($_SESSION['phpld']['user']['id'])) {
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
        if ($link['rights']['editLink'] == 1) {
             $data = $db->Execute("UPDATE `{$tables['link']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
             $data = $db->Execute("UPDATE `{$tables['link_review']['name']}` SET `STATUS` = '2' WHERE `ID`=".$db->qstr($id)."");
//             http_custom_redirect($return);
				 echo '<script> window.top.location = window.top.location; </script>';
        } else {
//             http_custom_redirect($return);
				 echo '<script> window.top.location = window.top.location; </script>';
        }
    }
    
} else {
    //Disable any caching by the browser
    disable_browser_cache();
    if (isset($_REQUEST['ID']) & isset($_SESSION['phpld']['user']['id'])) {
   
        $link['rights'] =  user_needs_approval($_SESSION['phpld']['user']['id'], $link['CATEGORY_ID']);
    
        if ($link['rights']['editLink'] == 1) {
            http_custom_redirect(DOC_ROOT."/dir_links_edit.php?action=E:".$_REQUEST['ID']);
        } else {
            $error = 1;
            $_SESSION['inplace_linkerror'] = "Sorry, you have no editing rights for this link.";
        }
    } else {
        $error = 1;
        $_SESSION['inplace_linkerror'] = "Sorry, you have no editing rights for this link.";
    }

    $tpl->assign("ID", $id);
    $tpl->assign('error', $error);
    $tpl->assign('error_message', $_SESSION['inplace_linkerror']);
    $tpl->assign("load_Javascript", true);
    
    $useRTE = (defined ('SUBMIT_USE_RTE') ? SUBMIT_USE_RTE : 0);
    $tpl->assign('useRTE', $useRTE);

    echo $tpl->fetch('inplace_editlink.tpl');
}
?>
