<?php
/**
 * @package		Ad Platform
 * @author		Uta Marian - Alexandru ( -uMarian- )
 * @license		http://codecanyon.net/licenses
 * @link		http://www.uta-marian.com
 * @version		Version 1.0
 */

require_once(dirname(__FILE__).'/../../config.php');
require_once(DIR_SYSTEM.'/users/session.user.php');

//** CHECK IF PUBLISHER IS ON or OFF **//
if ( PUBLISHER != 'ON' ) { header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard' ); exit; }
//** END CHECK **//

$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];
$ID			=	$prepare->sanitize($_GET['id'],'input');


$channelsList	=	$db->Query("SELECT `pc_id`, `pc_title`, `pc_details`, `pc_domain` FROM publisher_channels where pc_user_id=? order by pc_id DESC","i",$USER_ID);
$numbers = count ($channelsList);

//PAGINATION
$page		=	$prepare->sanitize($_GET['p'], 'input');
$on_page	=	"10";
$page=!empty($page)?$page:"1";
$limitfrom=($page-1)*$on_page;
$limitto=($page*$on_page)-($page-1)*$on_page;
$itemsPerPage=$on_page;
$upsy = $paginator['rows']=$numbers;	 // rows
	  $paginator['pager']=$prepare->page($page,$upsy,$itemsPerPage);
	  $paginator['limitfrom']=$limitfrom+1;
	  $paginator['limitto']=$limitfrom+$upsy;
	  $paginator['itemsnum']=$upsy;
	  $paginator['page']=$page;
$view->paginator	=	$paginator;
//END PAGINATION
$view->channels = array_slice($channelsList, $limitfrom, $limitto);
//***********************************************************************//

if ( !empty ( $ID ) ) {

$view->channel	=	$channel = $db->Query("SELECT `pc_id`, `pc_title`, `pc_details`, `pc_domain` FROM publisher_channels where pc_user_id=? and pc_id=?","ii",$USER_ID,$ID);

}

$m	=	$prepare->sanitize($_GET['m'],'input');

if ( $m == 'yes-delete' ) {
	$id	=	$prepare->sanitize($_GET['id'],'input');


	//** VALIDATE CHANNEL **//
	$validate	=	$db->Query("SELECT `pc_id` FROM publisher_channels where pc_user_id=? and pc_id=?","ii",$USER_ID,$id);

	if ( !empty ( $validate ) ) {

		//** REMOVE DATA **//
		$db->NoQuery("DELETE FROM publisher_channels WHERE pc_id=?","i",$id);
		$db->NoQuery("DELETE FROM publisher_channels_statistics WHERE pcs_channel_id=?","i",$id);

		$ads		= $db->Query("SELECT `pau_id` FROM publishers_ad_units where pau_channel_id=?","i",$id);
		$countAds	=	count($ads);


		$db->NoQuery("update publishers_profiles set pub_profile_ads=pub_profile_ads-".$countAds." where pub_profile_user_id=?","i",$USER_ID);

		foreach ( $ads as $ad ) {
			$db->NoQuery("DELETE FROM publishers_ad_units WHERE pau_id=?","i",$ad['pau_id']);
			$db->NoQuery("DELETE FROM publisher_ad_units_clicks WHERE pauc_ad_id=?","i",$ad['pau_id']);
			$db->NoQuery("DELETE FROM publishers_ad_units_statistics WHERE paus_ad_id=?","i",$ad['pau_id']);
		}
	}

	$db->NoQuery("update publishers_profiles set pub_profile_channels=pub_profile_channels-1 where pub_profile_user_id=?","i",$USER_ID);

	header ( 'Location: '.HTTP_SERVER_WEBSITE.'dashboard/publisher/channels' ); exit;
}







/* set page title */
$view->title = $LANG["WBS_PUB_MANAGE_CHANNELS"];
$view->description = $LANG["WBS_PUB_MANAGE_CHANNELS"];
$view->keywords = $LANG["WBS_PUB_MANAGE_CHANNELS"];

//**DEFINE CUSTOM CSS/JS FILLES**//
$view->customCode = '<link href="'.HTTP_SERVER_WEBSITE.'templates/'.WEBSITE_TEMPLATE.'/assets/css/dashboard.css" rel="stylesheet">';

$view->publisher_menu = 'on';
$view->header = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/header.php');
$view->footer = $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/footer.php');

echo $view->render(DIR_TEMPLATE.WEBSITE_TEMPLATE.'/plugins/publisher/channels.php');
?>