<?php 
require_once('class.pagination.php');
/****************************************************************
 * All plugin function will define here
 *****************************************************************/ 

class clsUsers
 {

    var $db = null;
	var $paging = null;
	var $data = null;
	
	function __construct()
	 {
	  
	  $dbconn = new wpdb(get_option('cake_dbuser'),get_option('cake_dbpwd'), get_option('cake_dbname'), get_option('cake_dbhost'));
      $this->db = $dbconn;
	 
	  //make object for pagination
	  $pagination = new Pagination();
      $this->paging = $pagination;  
     }
 
   //call get data to get users information
   
   function getData()
     {
	   $where = $this->_searchAdminWhere();
       
	   $q = "SELECT u.* FROM ".USER_MASTERS." AS u ".$where.$this->setPagination();
       $this->data = $this->db->get_results($q);
     
	   return $this->data;	 

     }
 
   function setPagination()
	 {
   
       $page = 1;
       $size = 25;
 
         // we get the current page from $_GET
       if (isset($_GET['limit'])){
         $page = (int) $_GET['limit'];
       }
	  
	   $where = $this->_searchAdminWhere();
	   $total_records = $this->db->get_var($this->db->prepare( "SELECT COUNT(*) FROM ".USER_MASTERS." as u".$where) );
        // create the pagination class
		$this->paging->setLink("options-general.php?page=userm&limit=%s");
		$this->paging->setPage($page);
		$this->paging->setSize($size);
		$this->paging->setTotalRecords($total_records);
        return $this->paging->getLimitSql();
     
 
 }
   
   function create_paging_link()
	 {
        $this->setPagination();
        return $this->paging->create_links();
     }
   
   private function _searchAdminWhere()
     {

    $where = array();
    $filter_title		= $_SESSION['Search.title'];
    $filter_type		= $_SESSION['Search.type'];
    
    switch($filter_type){

     case '2':

   	  $where[] = '(u.user_type_id = 2) ';

   	  break;

     case '3':

   	  $where[] = '(u.user_type_id = 3) ';

   	  break;
	  
	  }

   if ($filter_title) {
			$where[] = ' ((u.name LIKE \''.$filter_title.'%\') OR (u.email LIKE \'%'.$filter_title.'%\')) ';
		}

    $where 		= ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );

    return $where;

  }

 function blockUser($uid)
 {
 
	   $q = "UPDATE ".USER_MASTERS." SET block = '1' WHERE id=".$uid;
	   
	   if($this->db->query($q)){
	   return true;
	   }else{
	   return false;
	   }
 
 }

 function unblockUser($uid)
 {
     $q = "UPDATE ".USER_MASTERS." SET block = '0' WHERE id=".$uid;
	   
	   if($this->db->query($q)){
	   return true;
	   }else{
	   return false;
	   }
 
 }  function getSalesReport($id,$type){  
		$r="select * from tbl_configuration where id='1'"; 
		$this->revenu=$this->db->get_results($r);
		if($type==3){
			$qdata = "SELECT * from tbl_user_masters WHERE user_type_id=3 and id=$id";
			$this->qdata = $this->db->get_results($qdata);
			$q = "SELECT u.id,u.name,u.email,u.username,u.user_type_id,u.last_visit_date,u.register_date,a.id as aid,a.user_id,a.title as atitle,a.price as aprice,a.image,a.hits,at.id as tid ,at.album_id,at.title as attitle,at.price as atprice,at.track_art,at.is_deleted FROM ((`tbl_user_masters` as u left join tbl_albums as a on u.id=a.user_id) left join tbl_album_tracks as at on at.album_id=a.id) WHERE u.user_type_id=3 and u.id=$id and a.is_deleted='0'";       
			$this->data = $this->db->get_results($q);	   
			$this->image =$this->getCustomFieldValue($id,5);	   
			$this->band =$this->getCustomFieldValue($id,2);	   
			$this->tag =$this->getCustomFieldValue($id,3);	   
			$q1= "SELECT mo.id as moid, mo.payment_status as payment_status,mo.subtotal,od.order_id,od.price,a.user_id FROM ((`tbl_master_orders` as mo right join tbl_order_details as od on mo.id=od.order_id) left join tbl_albums as a on a.id=od.buy_id) WHERE mo.payment_status='Completed' and a.user_id=$id";	   
			$this->sales = $this->db->get_results($q1);	   
			include('salesdataforartist.php');	
			}else{	   
			$qdata = "SELECT * from tbl_user_masters WHERE user_type_id=2 and id=$id";       
			$this->data = $this->db->get_results($qdata);	   
			$q= "SELECT mo.id as moid, mo.payment_status as payment_status,mo.subtotal,mo.customer_id,mo.created_date, od.order_id,od.price,a.user_id FROM ((`tbl_master_orders` as mo right join tbl_order_details as od on mo.id=od.order_id) left join tbl_albums as a on a.id=od.buy_id) WHERE mo.payment_status='Completed' and mo.customer_id=$id group by od.order_id";       
			$this->sales = $this->db->get_results($q);	   
			include('salesdataforcustomer.php');	
			}	    
			}  
			
			function getCustomFieldValue($id,$field_id){     
			$q="SELECT v.* FROM tbl_user_masters AS u right join tbl_user_field_values as v on u.id=v.user_id where v.user_id=".$id." and v.field_id=".$field_id;	 $personalData=$this->db->get_results($q);	 
			if($field_id==3){		 
				foreach($personalData as $data){			
					$data1.=ucfirst($data->field_value)." ";		 
					}	 
			}else{		 
				foreach($personalData as $data){
					$data1=$data->field_value;
					}	
					}	 return $data1; }
}
?>