<?php if( $_GET['token'] !== 'parolaoneappdocs') die('access is not permitted');

/**
 * 
 * Controller pentru Check files.
 * 
 */
class Check extends CI_Controller{
    
    function __construct() 
    {   
        parent::__construct();
    }

    function files()
    {    
        $doc_root = dirname($_SERVER['DOCUMENT_ROOT'], 1);

        $this->load->library('login_library');
        $this->load->model('generale_model');
        $this->load->model('client_model');

        $infoEmail  = $this->generale_model->getInfoEmail();
        $infoConfig = $this->generale_model->getInfoConfig();

        //path unde sunt docs sync de pe desktop
        $path       = (isset($infoConfig) && $infoConfig->path)?$doc_root.$infoConfig->path:$doc_root."/docs.devsrv01.oneit.cloud/nextclouddata/dev/files/DE_TRIMIS/";
        //path pt docs trimise catre client
        $new_path   = (isset($infoConfig) && $infoConfig->new_path)?$doc_root.$infoConfig->new_path:$doc_root."/docs.devsrv01.oneit.cloud/nextclouddata/dev/files/TRIMISE/";
        //separator identificare cod client din denumire document
        $separator  = (isset($infoConfig) && $infoConfig->separator)?$infoConfig->separator:"_";

        $subiect_default    = (isset($infoEmail) && $infoEmail->subject_email)?$infoEmail->subject_email:'Docs - ONE-IT';
        $body_default       = (isset($infoEmail) && $infoEmail->body_email)?$infoEmail->body_email:"Document trimis din app ".base_url();
        
        //echo $path;
        
        //scan folder path
        $files = scandir($path);
        if(count($files) > 0) 
        {
            //check all files from folder
            foreach ($files as $key => $value) 
            {        
                //if ext is pdf    
                $ext = pathinfo($value, PATHINFO_EXTENSION);
                if($ext == 'pdf')
                {
                    // echo '<pre>'; 
                    // print_r("https://docs.devsrv01.oneit.cloud/nextclouddata/dev/files/DE_TRIMIS/".$value); 
                    // echo '</pre>';
                    //preluare cod client din denumire fisier
                    $infoName    = explode("$separator", $value);
                    //exist client dupa cod doc
                    $existClient = $this->client_model->existClientCode($infoName[0]);
                    
                    //only file name explode
                    $fileName    = explode(".", $value);
                    $value_name  = (isset($fileName))? $fileName[0]:$value; 
                    
                    if(isset($existClient) && $existClient)
                    {
                        //echo 'Trimis '.$value;

                        $destination = $new_path.$value;
                        rename($path.$value, "$destination");

                        //responsabil client
                        $to_email       = (isset($existClient) && $existClient->email)?$existClient->email:"dev.voisea@gmail.com";

                        //setari email template
                        $subiect_email  = (isset($existClient) && $existClient->subiect)?$existClient->subiect." ".$value_name:$subiect_default." ".$value_name;

                        $content        = (isset($existClient) && $existClient->template)?$existClient->template:$body_default;

                        $result         = $this->login_library->sendEmailFile($to_email, $subiect_email, $subiect_email, $content, $value);
                        
                        //insert info doc sended
                        $content_doc = array(
                            'id_client'          => $existClient->id_client,
                            'denumire'           => $value,
                            'responsabil_client' => $existClient->responsabil,
                            'email_client'       => $existClient->email,
                            'data_adaugare'      => date("Y-m-d H:i:s")
                        );

                        $this->db->insert('document', $content_doc);    
                        $id_doc = $this->db->insert_id();
                        
                        //sleep
                        sleep(30);
                        //echo "result: ".$result;
                    }   
                }
            }
        }
    }
}