<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

//clasa info 


class Global_library {

	//create select array
	function createSelect($array, $key1, $key2, $null_option = true)
    {
        if(isset($array) && $array)
        {
            $row = $array[0];
        
            if(!key_exists($key1, $row) || !key_exists($key2, $row))
                return "";
        }
        else
            return "";
        
       $str = "";
       if($null_option)
           if(!is_string($null_option))
                $str.="<option value=\"0\"></option>";
           else
               $str.="<option value=\"0\">$null_option</option>";
       foreach($array as $row)
       {
           $id = $row->$key1;
           $denumire = $row->$key2;
           $str.="<option value=\"$id\">$denumire</option>";
       }
        
       return $str;
    }

    //create auto select from array list
    function createAutoSelect($array, $key1, $key2, $selected_key, $null_option = true)
    {        
        if(count($array))
        {
            $row = $array[0];

            if(!key_exists($key1, $row) || !key_exists($key2, $row))
                return "";
        }
        else
            return "";
        
        $str = "";
        $selected = 'selected';
        
        if($null_option)
            if(!is_string($null_option))
            {
                if($selected_key==0)
                    $str.="<option value=\"0\" $selected></option>";
                else
                    $str.="<option value=\"0\"></option>";
            }
            else
            {
                if($selected_key==0)
                    $str.="<option value=\"0\" $selected>$null_option</option>";
                else
                    $str.="<option value=\"0\">$null_option</option>";
            }
        foreach($array as $row)
        {
            $id = $row->$key1;
            $denumire = $row->$key2;
            
            if($row->$key1 == $selected_key)
                $str.="<option value=\"$id\" $selected>$denumire</option>";
            else
                $str.="<option value=\"$id\">$denumire</option>";
        }
        
        return $str;
    }

    //create multi select from array list
    function createMultiSelect($array, $key1, $key2, $selected_key, $null_option = true)
    {        
        if(count($array))
        {
            $row = $array[0];

            if(!key_exists($key1, $row) || !key_exists($key2, $row))
                return "";
        }
        else
            return "";
        
        $str = "";
        $selected = 'selected';
        
        foreach($array as $k => $row)
        {
            $id = $row->$key1;
            $denumire = $row->$key2;

            $sel = FALSE;
            if(isset($selected_key) && $selected_key)
                $sel = in_array($row->$key1, $selected_key);

            
            if($sel)
                $str.="<option value=\"$id\" $selected>$denumire</option>";
            else
                $str.="<option value=\"$id\">$denumire</option>";
        }
        
        return $str;
    }

    function setDateFormat($data)
    {
        $format = "d-m-Y";
        
        $data_set = date($format, strtotime($data));
        
        return $data_set;
    }
    function setMySQLDateFormat($data)
    {
        $format = "Y-m-d";
        
        $data_set = date($format, strtotime($data));
        
        return $data_set;
    }

    function getDenumire($table, $column, $table_id)
    {
        $TABLE_ID_NAME = 'id_'.strtolower($table);

        $ci = &get_instance();
        $query = 
                "
                    SELECT $column
                    FROM $table
                    WHERE $TABLE_ID_NAME = '$table_id'
                ;";

        $q = $ci->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result()[0]->$column;

        return false;
    }
	
	function enc($string) 
    {
        $result = '';
        $key = 'keyetask';
        for($i=0; $i<strlen($string); $i++) {
          $char = substr($string, $i, 1);
          $keychar = substr($key, ($i % strlen($key))-1, 1);
          $char = chr(ord($char)+ord($keychar));
          $result.=$char;
        }

        return base64_encode($result);
    }

    function dec($string) 
    {
        $result = '';
        $string = base64_decode($string);
        $key = 'keyetask';
        for($i=0; $i<strlen($string); $i++) {
          $char = substr($string, $i, 1);
          $keychar = substr($key, ($i % strlen($key))-1, 1);
          $char = chr(ord($char)-ord($keychar));
          $result.=$char;
        }

        return $result;
    }
}

?>