<?php
require "vendor/autoload.php";
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
use Dompdf\Dompdf;
class Sendreceipt{
    public function generate($donation_id){
        $CI = & get_instance();

        //Fetch Setting
        $site_setting=$CI->db->get('app_site_setting')->row_array();

        //Fetch Donation
        $CI->db->select('app_donation.*,app_donation_category.title as category_title');
        $CI->db->where('app_donation.id',$donation_id);
        $CI->db->join('app_donation_category', 'app_donation_category.id=app_donation.category_id');
        $donation=$CI->db->get('app_donation')->row_array();

        if(isset($donation) && $donation!=null) {

            $first_name=$donation['first_name'];
            $last_name=$donation['last_name'];
            $email=$donation['email'];

            $dompdf = new Dompdf();
            $site_setting['logo']=$this->imageToBase64(dt_get_CompanyLogo());
            $site_setting['donation']=$donation;

            $html = $CI->load->view('admin/print/one', $site_setting,true);
            $dompdf->loadHtml($html);
            $dompdf->render();

            $output = $dompdf->output();
            $uploadPath = dirname(BASEPATH) . "/" . uploads_path;
            $filename=$uploadPath."/donation_receipt/"."donation_receipt_".$donation_id.".pdf";

            @file_put_contents($filename, $output);

            $pdf_path=FCPATH.uploads_path."/donation_receipt/"."donation_receipt_".$donation_id.".pdf";

            if (file_exists($pdf_path)){
                //Send email
                $subject = dt_get_CompanyName()." | ".dt_translate('donation');
                $define_param['to_name'] = $first_name." ".$last_name;
                $define_param['to_email'] =$email;

                $parameter['NAME'] = $first_name." ".$last_name;
                $parameter['AMOUNT'] =$donation['amount'];

                $html = $CI->load->view("email_template/donation", $parameter, true);
                $CI->sendmail->send($define_param, $subject, $html,$pdf_path);
            }
        }
    }

    private function imageToBase64($path) {
        $path = $path;
        $type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        return $base64;
    }
}