<?php
class ModelToolEntegrasyonApi extends Model {
    public function addEntegrasyonApi($code,$api) {

        $query=$this->db->query("select * from database_entegrasyon de left join `entegrasyon` e ON(de.entegrasyon_id=e.entegrasyon_id)  where e.code='".$code."' and de.database_id='".$this->user->getDbId()."'  ");
        if(!$query->num_rows){
            $this->db->query("INSERT INTO database_entegrasyon SET database_id='".$this->user->getDbId()."', api='".serialize($api)."',status=1, entegrasyon_id=(select entegrasyon_id from entegrasyon where code='".$code."') ");
        }else {

            $this->db->query("UPDATE database_entegrasyon SET api='".serialize($api)."' where  entegrasyon_id=(select entegrasyon_id from entegrasyon where code='".$code."') and database_id='".$this->user->getDbId()."'  ");

        }

        return true;

    }
    public function deleteEntegrasyonApi($code) {


            $this->db->query("DELETE FROM database_entegrasyon where database_id='".$this->user->getDbId()."' and entegrasyon_id=(select entegrasyon_id from entegrasyon where code='".$code."') ");

        return true;

    }

    public function getEntegrasyonApi($code,$entegrasyon_group_id=1)
    {
        try {
            $query=$this->db->query("select * from database_entegrasyon de left join `entegrasyon` e ON(de.entegrasyon_id=e.entegrasyon_id)  where e.code='".$code."' and de.database_id='".$this->user->getDbId()."'  and entegrasyon_group_id='".$entegrasyon_group_id."' ");

            return $query->row;
        }catch (Exception $exception){

          echo  $exception->getMessage();
        }

    }

    public function getEntegrasyonApiInfo($code)
    {


       $api=$this->getEntegrasyonApi($code);

       if(isset($api['api'])){
        return unserialize($api['api']);

    }else {
           return array();
       }
    }






}