<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( '../init.php' );
}

include ( 'sessioninc.php' );

define( 'PAGE_ID', 'mship_mgt' );

if ( !checkAdminPermission( PAGE_ID ) ) {

	header( 'location: not_authorize.php' );
	exit;
}

//default membership level
$mid = 1;

if ( $_POST['modify'] ) {

	$vchat 					= $_POST['chat'] == 'on' ? 1 : 0;

	$vforum 				= $_POST['forum'] == 'on' ? 1 : 0;

	$vblog 				= $_POST['blog'] == 'on' ? 1 : 0;

	$vpoll 				= $_POST['poll'] == 'on' ? 1 : 0;

	$vincludeinsearch 		= $_POST['includeinsearch'] == 'on' ? 1 : 0;

	$vmessage 				= $_POST['message'] == 'on' ? 1 : 0;

	$vuploadpicture 		= $_POST['uploadpicture'] == 'on' ? 1 : 0;

	$vseepictureprofile 	= $_POST['seepictureprofile'] == 'on' ? 1 : 0;

	$vfavouritelist 	= $_POST['favouritelist'] == 'on' ? 1 : 0;

	$vsendwinks 	= $_POST['sendwinks'] == 'on' ? 1 : 0;

	$vextsearch 	= $_POST['extsearch'] == 'on' ? 1 : 0;

	$vfullsignup 			= $_POST['fullsignup'] == 'on' ? 1 : 0;

	$vuploadpicturecnt = $_POST['uploadpicturecnt'] == '' ? 1: $_POST['uploadpicturecnt'];

	$vallowalbum = 	$_POST['allowalbum'] == 'on' ? 1: 0;

	$vallowim = 	$_POST['allowim'] == 'on' ? 1: 0;

	$vallow_mysettings =  $_POST['allow_mysettings'] == 'on' ? 1: 0;

	$vprice					= $_POST['txtprice'];

	$vcurrency				= $_POST['txtcurrency'];

	$vname					= $_POST['txtname'];

	$activedays			= $_POST['activedays'];

	$event_mgt 			= $_POST['event_mgt'] == 'on' ? 1: 0;

	$vallow_php121 			= $_POST['allow_php121'] == 'on' ? 1: 0;

	$saveprofiles 		= $_POST['saveprofiles'] == 'on' ? 1: 0;

	$saveprofilescnt = isset($_POST['saveprofilescnt'])?$_POST['saveprofilescnt']:0;

	$profilepicscnt = isset($_POST['profilepicscnt'])?$_POST['profilepicscnt']:0;

	if ($saveprofilescnt == '') $saveprofilescnt = 0;

	if ($profilepicscnt == '') $profilepicscnt = 0;
	$allow_videos = $_POST['allow_videos']=='on'?1:0;

	$videoscnt = isset($_POST['videoscnt'])?$_POST['videoscnt']:0;

	$message_keep_cnt = isset($_POST['message_keep_cnt'])?$_POST['message_keep_cnt']:0;

	$message_keep_days = isset($_POST['message_keep_days'])?$_POST['message_keep_days']:0;

	$messages_per_day = $_POST['messages_per_day'];

	if ($messages_per_day == '') $messages_per_day = 0;

	$winks_per_day = $_POST['winks_per_day'];

	if ($winks_per_day == '') $winks_per_day = 0;

	if ($message_keep_cnt == '') $message_keep_cnt = 0;

	if ($message_keep_days == '') $message_keep_days = 0;

	$mid = $_POST['mshipid'];


	$osDB->query( 'UPDATE ! ' .
	" SET	name				= '$vname',
			chat 				= '$vchat',
			forum				= '$vforum',
			blog				= '$vblog',
			poll				= '$vpoll',
			includeinsearch		= '$vincludeinsearch',
			message				= '$vmessage',
			message_keep_cnt    = '$message_keep_cnt',
			message_keep_days   = '$message_keep_days',
			messages_per_day 	= '$messages_per_day',
			uploadpicture		= '$vuploadpicture',
			uploadpicturecnt 	= '$vuploadpicturecnt',
			allowalbum 			= '$vallowalbum',
			allowim 			= '$vallowim',
			seepictureprofile	= '$vseepictureprofile',
			favouritelist		= '$vfavouritelist',
			sendwinks			= '$vsendwinks',
			winks_per_day 		= '$winks_per_day',
			extsearch			= '$vextsearch',
			fullsignup			= '$vfullsignup',
			saveprofiles		= '$saveprofiles',
			saveprofilescnt		= '$saveprofilescnt',
			allow_videos		= '$allow_videos',
			profilepicscnt		= '$profilepicscnt',
			videoscnt			= '$videoscnt',
			price				= '$vprice',
			currency			= '$vcurrency',
			activedays			= '$activedays',
			event_mgt			= '$event_mgt',
			allow_mysettings	= '$vallow_mysettings',
			allow_php121	= '$vallow_php121'
			WHERE roleid = ? AND id = ?", array( MEMBERSHIP_TABLE, $_POST['mshipid'], $_POST['id']) );

} elseif( $_POST['enable'] ) {

	 $osDB->query( 'UPDATE ! SET enabled = ? WHERE id = ?', array( MEMBERSHIP_TABLE, 'Y', $_POST['id']) );

} elseif( $_POST['disable'] ) {

	 $osDB->query( 'UPDATE ! SET enabled = ? WHERE id = ?', array( MEMBERSHIP_TABLE, 'N', $_POST['id']) );

} elseif( $_POST['delete'] ) {

	 $osDB->query( 'DELETE FROM !  WHERE id =?', array( MEMBERSHIP_TABLE ,$_POST['id'] ) );

	 $mid = $osDB->getOne('select id from !', array( MEMBERSHIP_TABLE) );
}

if ( $_POST['membership'] ) {

	$mid = $_POST['membership'];

}

$t->assign( 'data',$osDB->getRow( 'SELECT * FROM ! WHERE roleid = ?', array(MEMBERSHIP_TABLE, $mid) ) );

$roles = array();

$rs =& $osDB->getAll( 'SELECT roleid, name FROM !',array( MEMBERSHIP_TABLE ) );

foreach ( $rs as $row ) {

	$roles[$row['roleid']] = $row['name'];
}

$t->assign( 'memberships', $roles );

unset($roles, $rs);

$lang['privileges'] = get_lang_values('privileges');

$lang['activedays_array'] = get_lang_values('activedays_array');

$t->assign('lang',$lang);

$t->assign('rendered_page', $t->fetch('admin/membership.tpl'));

$t->display( 'admin/index.tpl' );
?>