<?php

Class Functions{
    
    private $con;
    public $records;
    public $numlinks;
    public $item_per_page =10;
    
    public $africas_username = 'sandbox'; // use 'sandbox' for development in the test environment
    public $africas_talk_apiKey   = 'ce613c96a34de96be089ab54486b543c9fdc4a82f682b40451bff514a8595277'; // use your sandbox app 
    
    function __construct(){
        
        require_once dirname(__FILE__).'/connection.php';
        
        
        $database = new Connection();
        
        $this->con = $database->openConnection();
        
        // Require https
        // if ($_SERVER['HTTPS'] != "on") {
        //     $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
        //     header("Location: $url");
        //     exit;
        // }
    }
        
    
       //count total records
    function countAll($query){
        $stmt = $this->con->prepare($query);
        $stmt->execute();
        return $stmt->rowCount();
    }
    //pagination
    function pagenate($query, $page_number){
        $per_page = $this->item_per_page;
        //$this->item_per_page = $per_page;
        
        $numrecords = $this->countAll($query);
        
        $this->pages = $numrecords;  
        
        $position = (($page_number-1) * $per_page);
        
        $sql_pagination = $this->con->prepare($query." LIMIT ".  $position.",".$per_page);
        //echo "SELECT id,fullname FROM users LIMIT $start,$numperpage";
        $sql_pagination->execute();
        
        return $sql_pagination->fetchAll();
    }
	 
    
    //RETRIEVE SINGLE RECORD
    function retrieveSingle($query){
            $stmt = $this->con->prepare($query);
            $stmt->execute();
            return $stmt->fetch();
            
    }
    
    //RETRIEVE SINGLE RECORD
    function retrieveMany($query){
            $stmt = $this->con->prepare($query);
            $stmt->execute();
            return $stmt->fetchAll();
            
    }
	
     /* function to UPDATE sql data*/
    function updateData($table, $data, $where){
        $cols = array();

        foreach($data as $key=>$val) {
            $cols[] = "$key = '$val'";
        }
        $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where";
        
        
        $stmt = $this->con->prepare($sql);
        if($stmt->execute()){
            return 1;
        }else{
            return 2;
        }

        
    }
    
     /* function to insert into table */
    function insertData($table, $data) {
        $key = array_keys($data);
        $val = array_values($data);
        $sql = "INSERT INTO $table (" . implode(', ', $key) . ") "
             . "VALUES ('" . implode("', '", $val) . "')";

        $stmt = $this->con->prepare($sql);
        
        if($stmt->execute()){
            return 1;
        }else{
            return 2;
        }
    }
    
    function deleteData($table,$where){
        
        $sql = "DELETE FROM $table WHERE $where";
        
        
        $stmt = $this->con->prepare($sql);
        if($stmt->execute()){
            return 1;
        }else{
            return 2;
        }
    }
    

}

?>