<?php

class CATEGORY {

    private $db;

    function __construct($DB_con) {
        $this->db = $DB_con;
    }

    public function insert($cat_title, $cat_desc, $cat_parentId, $cat_status, $cat_add_by) {
        try {
            //echo "here";


            $stmt = $this->db->prepare("INSERT INTO categories(
                            cat_title,
                            cat_desc,
                            cat_parentId,
                            cat_status,
                            cat_add_by 
                            ) 
		            VALUES(
                            :cat_title,
                            :cat_desc,
                            :cat_parentId,
                            :cat_status,
                            :cat_add_by
                            )");
            $stmt->bindparam(":cat_title", $cat_title);
            $stmt->bindparam(":cat_desc", $cat_desc);
            $stmt->bindparam(":cat_parentId", $cat_parentId);
            $stmt->bindparam(":cat_status", $cat_status);
            $stmt->bindparam(":cat_add_by", $cat_add_by);
            
            $stmt->execute();

            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function update($cat_id, $cat_title, $cat_desc, $cat_parentId, $cat_status) {
        try {
            //echo "here";

            $stmt = $this->db->prepare("update categories
                            Set 
                               cat_title = :cat_title,
                               cat_desc  = :cat_desc,
                               cat_parentId = :cat_parentId,
                               cat_status = :cat_status,
                               last_update = :last_update
                            where cat_id = :cat_id ");
            
            $stmt->bindparam(":cat_title", $cat_title);
            $stmt->bindparam(":cat_desc", $cat_desc);
            $stmt->bindparam(":cat_parentId", $cat_parentId);
            $stmt->bindparam(":cat_status", $cat_status);
            $stmt->bindparam(":last_update", date("Y-m-d H:i:s"));
            $stmt->bindparam(":cat_id", $cat_id); 
            
            
            $stmt->execute();
            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function getParentCategories(){
        try {
            
            $return = array();
            
                $stmt = $this->db->prepare("SELECT * FROM categories where cat_parentId = 0" );
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    
    public function getCategories($id=''){
        try {
            
            $return = array();
            if(!empty($id)){
                $stmt = $this->db->prepare("SELECT * FROM categories WHERE cat_id=:uId ");
                $stmt->execute(array(':uId' => $id));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
                
            }else {
                $stmt = $this->db->prepare("SELECT * FROM categories" );
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row;  
                }
            }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    
    public function getCategoryByName($category=''){
        try {
            
            $return = array();
            if(!empty($category)){
                $category = trim(strtolower($category));
                $stmt = $this->db->prepare("SELECT cat_id FROM categories WHERE LOWER(cat_title) =:cat ");
                $stmt->execute(array(':cat' => $category));
                $return = $stmt->fetch(PDO::FETCH_ASSOC);
                if(count($return)>0){
                    $return = $return['cat_id'];
                }
            }
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    

    public function getAllCategories(){
        try {
            
            $return = array();
            
                $stmt = $this->db->prepare("SELECT * FROM categories WHERE cat_parentId = 0" );
                $stmt->execute();
                $i=0;
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      //$return[] = $Row;
                      $stmt2 = $this->db->prepare("SELECT * FROM categories WHERE cat_parentId = ".$Row['cat_id'] ."  " );
                      $stmt2->execute();
                      
                        $return[$i]['cat_id'] =$Row['cat_id'];
                        $return[$i]['category'] =$Row['cat_title'];
                        $return[$i]['desc'] =$Row['cat_desc'];
                        $return[$i]['status'] =$Row['cat_status'];
                        $j = 0 ;
                      while($Row2 = $stmt2->fetch(PDO::FETCH_ASSOC)){
                          
                                $return[$i]['sub_cat'][$j]['category'] =$Row2['cat_title'];
                                $return[$i]['sub_cat'][$j]['desc'] =$Row2['cat_desc'];
                                $return[$i]['sub_cat'][$j]['status'] =$Row2['cat_status'];    
                                $return[$i]['sub_cat'][$j]['main'] =$Row['cat_title'];
                                $return[$i]['sub_cat'][$j]['cat_id'] =$Row2['cat_id'];
                                
                            $j++;
                      }
                     $i++; 
                }
            
            return $return;
            
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }
    

    
    
public function getCategoriesName($ids){
    try { 
            $ids = trim($ids);
            $cooma = substr($ids, strlen($ids-1),strlen($ids) );
            if($cooma== ","){
                $ids = substr($ids, 0, strlen($ids-1));
            }
            
            $return = array();
            if(!empty($ids)){
                $stmt = $this->db->prepare("SELECT * FROM categories WHERE cat_id in (".$ids.")");
                $stmt->execute();
                
                while($Row = $stmt->fetch(PDO::FETCH_ASSOC)){
                      $return[] = $Row['cat_title'];
                }
            }else{}
            
            //print_r($return);
            $return = implode(", ", $return);
            
            return $return;

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
   }
    
    
    
    /****************************************************************************************************************************/
    
    
    
    public function getProfile($id) {
        try {
            $stmt = $this->db->prepare("SELECT * FROM users WHERE uId=:uId");
            $stmt->execute(array(':uId' => $id));
            $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($stmt->rowCount() > 0) {
                return $userRow;
            }
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function is_loggedin() {
        if (isset($_SESSION['user_session'])) {
            return true;
        }
    }

    public function redirect($url) {
        header("Location: $url");
    }

    public function logout() {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
    }

}

?>