<?php
// If it's going to need the database, then it's 
// probably smart to require it before we start.
require_once('initializer.php');

class Business extends DatabaseObject {
	
  protected static $table_name="BVIS_Business";
  
  //we will populate this array of attribure dynamically
	//protected static $db_fields = array('id','Username','Privilege','First_Name','Last_Name','Password');
  
  protected static $db_fields;
  protected static $exclu_fields = array ("Password","Token");

	// public  $id;
	// public $Username;
	// public $Privilege;
	// public $Last_Name;
  // public $First_Name;
  // public $Password;

  public function __construct($options=""){
    if ($options=="Blank") {
      $myFields = static::Get_Object_Fields ();
      foreach($myFields as $field) {
        $this->{$field} = "";include 'Business.php';
        
      }
    }

   // unset ($this->id);
    
  }


  public static function testGetField () {
    return self::$db_fields;
  }

  public static function createProperty( $name, $value ){
     $this->{$name} = $value;
}

public static function block_Staff( $staffid="" ){
  global $database;
  
  if ($staffid == 1) {
    return false ;
  } else {
    $sql = "UPDATE `APEX_Staff` SET `Blocked`='Yes' WHERE `id`='{$staffid}' ";

    error_log(print_r( $sql , true));

  $result =  $database->query($sql);
 

  if ( $result) {
    //we get all staff and uppdate client list
    return self::find_all("Staff") ;
} else {
  return false ;
}

  }
  


 

}

public static function checkPopular ($id=""){
  global $database;

  $sql = "SELECT `id`, ( SUM(`Likes`)+SUM(`Shares`) ) AS `Total` FROM `BVIS_Business` WHERE `Block` ='false'  GROUP BY `id` ORDER BY `Total`  DESC LIMIT 3";

  $result =  $database->query($sql);
  while($row = $result->fetch_assoc()) {
   // var_dump($row);
    if ($row["id"] == $id) {
      if ($row["Total"] >0 && $row["id"] == $id) {
        return true;
      } else {
        return false;
      }
       

    }
    
}

  
}



public static function RandonCTA ($numOfPack=""){
	$CTA;
	if ($numOfPack > 1){
		  $CTA = array ("Deals", "Offers");
	}else {
		$CTA = array ("Deal", "Offer");
	}

  
  
  return  $CTA[array_rand($CTA)];
  
  
}


public static function UserCTA ($category=""){
  $CTA;
  
  switch ($category) {
    case "Villas":
      $CTA = array ( "Get Away","Escape");
      break;
    case  "Spas":
       $CTA = array ("Rejuvenate", "Recharge", "Revitalize",  "Refresh");
      break;
    case  "Hotels":
       $CTA = array ("Get Away", "Escape");
      break;
   case  "Hiking":
         $CTA = array ("Explore", "Uncover", "Discover", );
      break;
    case  "Sailing":
	     $CTA = array ("Indulge", "Enjoy",  "Discover", );
    break;
    case  "Diving":
	     $CTA = array ("Indulge", "Enjoy",  "Discover");
	  break;
   case  "Watersports":
        $CTA = array ("Indulge", "Enjoy", "Bliss",  "Discover");
     break;
	  
      case  "Fishing":
           $CTA = array ("Explore", "Enjoy",   "Discover"  );
         break;
	  
   case  "Dining":
           $CTA = array ( "Enjoy", "eat up!", "Bon Appetit", "Dig In", "chow down");
         break;
		 
	 case  "Rental":
	      $CTA = array ( "Enjoy", "Bliss",  "Discover", "Rejoicing");
	    break;
		
		 
   	 case  "Exursions":
   	      $CTA = array ( "Enjoy", "Bliss",  "Discover", "Rejoicing");
   	    break;
		
     default:
      $CTA = array ("Indulge", "Enjoy", "Bliss",  "Discover", "Rejoicing");
  }
  
  return  $CTA[array_rand($CTA)];
}

public static function   stdToArray($obj){
  $reaged = (array)$obj;
  foreach($reaged as $key => &$field){
    if(is_object($field))$field = stdToArray($field);
  }
  return $reaged;
}

public static function FindActiveBusinesses (){

  $sql = "SELECT * FROM `BVIS_Business` WHERE `Block` = 'false' ORDER BY RAND () ";

  $Bus_packages =   self::find_by_sql($sql, 'FormatPackages')  ;

  $temp = array();
  foreach ($Bus_packages as $key => $value) {
      if ($value != null) {
          //unset($Bus_packages[$i]);
 	  array_push($temp,$value );
      }
    
  }

return   $temp ; 



}




  public static function setGetField ($idf) {
    return self::$db_fields['id'] = $idf;
  }

  public static function  Get_DAILY_FLOAT ($staffid=""){
    $staff =self::find_by_id($staffid );
    return  $staff->Daily_Float;
  }

  public static function GetStaffName ($staffid) {
    $sql = 'SELECT * FROM `Orders` WHERE `id` ="4"';
     $user =self::find_by_id($staffid );


     if ($staffid == 0) {
       return "Online Sys.";
     } else {
      return $user->First_Name." ". substr($user->Last_Name, 0, 1);
     }
     

  }


  public static function getFullName ($staffId){
    $staff = self::find_by_id ($staffId);

    return  $staff->full_nameFull();

  }


  public static function getBUS_Name ($bus){
    $bus = self::find_by_id ($bus);

    return  $bus->Name;

  }
  
  public static function Get_Logo ($bus){
    $bus = self::find_by_id ($bus);

    return  $bus->Logo;

  }
  



  public static function ShowBUS_Name ($bus){
    $bus = self::find_by_id ($bus);

    echo ($bus->Name) ;

  }

  public static function getBUS_Logo ($bus){
    $bus = self::find_by_id ($bus);

    return  $bus->Logo;

  }


  public static function  getLocation ($bus) {
    $bus = self::find_by_id ($bus);
 
    return  $bus->Island;
  }



  public static function  find_IDs ($island=""){

    $sql = "SELECT `id` FROM `BVIS_Business` WHERE `Block`='false' AND `Island`='{$island}'";

    $result =  self::find_by_sql($sql,"");

    return    $result;

  }



	
  public function full_name() {
    if(isset($this->First_Name) && isset($this->Last_Name)) {
      return $this->First_Name . " " . $this->Last_Name;
    } else {
      return "";
    }
  }

  public function full_nameFull() {
    if(isset($this->First_Name) && isset($this->Last_Name)) {
      return $this->First_Name . " " . $this->Last_Name. " (" .$this->Username . ")" ;
    } else {
      return "";
    }
  }


  public static function authenticateStaff($Password="", $StaffIDp="", $Permission="")  {

    global $database;
     $passwordP = SHA1($database->escape_value($Password));

    $sql = "SELECT `id` FROM `APEX_Staff` WHERE `id`='{$StaffIDp}' AND `Password`='{$passwordP}' AND `Permission` LIKE '%{$Permission}%'";
   // $sql = "SELECT `id` FROM `APEX_Staff` WHERE `Password`='{$passwordP}' AND `Privilege`='Admin'";

    $result_array = self::find_by_sql($sql,"");
 

     return !empty($result_array) ? array_shift($result_array) : false; 

  }

  



	public static function authenticate($Username="", $Password="") {
    global $database;
    $usernameP = $database->escape_value($Username);
    $passwordP = SHA1($database->escape_value($Password));

   // error_log(print_r("inside authenticate auth auth_user ",true));
    //    $sql  = "SELECT `id`,`First_Name`,`Last_Name`,`Username`,`Privilege`,`Salary` FROM `Staff`";

    $sql  = "SELECT ".self::Columns_Select()." FROM `APEX_Staff` ";
    $sql .= "WHERE `Username` = '{$usernameP}' ";
    $sql .= "AND `Password` = '{$passwordP}' ";
    $sql .= "AND `Blocked`='No' LIMIT 1";
    //$sql = 'SELECT * FROM `Staff` WHERE `Password`="'.$password.'" AND `Username`="'.$username.'"';
   // error_log(print_r($sql, true));

    $result_array = self::find_by_sql($sql,"");
   // unset($result_array["Password"]);
    // error_log(print_r($result_array, true));
		return !empty($result_array) ? array_shift($result_array) : false;
	}

}

?>