<?php

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

class Myaesse_library {

    protected $CI;

    // We'll use a constructor, as you can't directly call a function
    // from a property definition.
    public function __construct() {
        // Assign the CodeIgniter super-object
        $this->CI = & get_instance();
        $this->CI->load->database();
        $this->CI->load->helper('url');
        $this->CI->load->library('session');
        $this->CI->config->item('base_url');
    }

    function randomPassword() {
        $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
        $pass = array(); //remember to declare $pass as an array
        $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
        for ($i = 0; $i < 8; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }
        return implode($pass); //turn the array into a string
    }
    
    public function scrivi_log($testo) {
        $data = date("d/m/Y H:i:s") . " - " . $testo . "\n";
        if (!write_file('./assets/log/log.txt', $data, 'a+')) {
            echo 'Unable to write the file';
        } else {
            //echo 'File written!';
        }
    }
    
    public function utf8ize($d) {
        if (is_array($d)) 
            foreach ($d as $k => $v) 
                $d[$k] = $this->utf8ize($v);
    
         else if(is_object($d))
            foreach ($d as $k => $v) 
                $d->$k = $this->utf8ize($v);
    
         else 
            return utf8_encode($d);
    
        return $d;
    }
    
    public function raggruppamento_date($date) {
        $haystack=array();
        foreach ($date as $key => $data) {
            for ($i = 1; $i < 12; $i++) {
                if ($i < 10) {
                    $j = '0' . $i;
                } else {
                    $j = $i;
                }
                if (!in_array($data->{'SMME' . $j}, $haystack) && $data->{'SMME' . $j}!=0) {
                    $haystack []=(int)$data->{'SMME' . $j};
                }
            }
        }
        asort($haystack);
        return $haystack;
    }

}
