<?php
/* Include Files ***************************/
require_once('../db-config.php');
	if(  defined('LTDVERSION')  ) 
		die('DEMO VERSION!|0');
require_once('../inc/lib-compat-class.php');
require_once('../inc/lib-db-class.php');
require_once('../inc/lib-login-class.php');
/*******************************************/
global $rSdb;

$user	= new userManagement();
if(  !$user->_logged_in  )
	die(__('Access denied.','recipeshelf'));

if( ! check_ajax_referer('remove-element', false) )
	die('0|'.__('Security Alert: Direct access not permitted!','recipeshelf'));

### ONE element removal
if(  isset($_POST['id'])  ){

	### read only users
	if( $_POST['t']=='6' ){
		$u			= esc_sql( (string) _rem_magic_quote($_POST['u']) );
		$sql 		= "SELECT id FROM {$rSdb->DBobjects} WHERE object='{$u}' LIMIT 1";
		
		$ro_user	= esc_sql( $rSdb->fetchVal( $sql ) );
		
		$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE type IN ('4','5') AND uid ='{$ro_user}'";
		$del	 	= $rSdb->fetch( $sql );

		$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE type='9' AND object= '{$u}_options'";
		$del	 	= $rSdb->fetch( $sql );
	}

	### thoughts
    $UID = '1';
	if( $_POST['t']=='5' )
 		$UID = "uid='".esc_sql($user->_user_id)."'";
	$id			= esc_sql( (int)$_POST['id'] );
    $sql		= "DELETE FROM {$rSdb->DBobjects} WHERE id = '{$id}' AND {$UID}";
	$del	 	= $rSdb->fetch( $sql );

	echo ( $rSdb->affected_rows()>0 ) ? 1 : 0;

}
### SEARCH based removal
else if(  isset($_POST['q']) && isset($_POST['t'])  ){

		### Trim and clean
		$q = esc_sql( trim( str_replace('*','', (string) _rem_magic_quote($_POST['q']) ) ) );

		if( $_POST['t']=='6' ){
		
			$sql 		= "SELECT id FROM {$rSdb->DBobjects} WHERE setting='readonly' AND object LIKE '%{$q}%'";
			$roArr		= $rSdb->fetchArr( $sql );

			if( is_array($roArr) && count($roArr)>0 ){
			
	            $ro_user = '';
				foreach( $roArr as $ro )
					$ro_user .= "'" . $ro->id . "',";
					
	            $ro_user = esc_sql( rtrim($ro_user, ',') );

				$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE type IN ('4','5') AND uid IN ({$ro_user})";
				$del	 	= $rSdb->fetch( $sql );

				$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE setting='readonly' AND object LIKE '%{$q}%'";
				$del	 	= $rSdb->fetch( $sql );
				$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE type='9' AND object LIKE '%{$q}%_options'";
				$del	 	= $rSdb->fetch( $sql );
			}
		}
		else{
			$t			= esc_sql( (int)$_POST['t'] );
			$sql		= "DELETE FROM {$rSdb->DBobjects} WHERE type='{$t}' AND object LIKE '%{$q}%'";
			$del	 	= $rSdb->fetch( $sql );

		}


	$no = $rSdb->affected_rows();


	if ( $no>0 )
		echo '1|'.__('Records succesfully removed:','recipeshelf').' '.$no;
	else
		echo '0|'.__('No records deleted.','recipeshelf');

}
?>