<?php

/**
 * VCard generator test - can save to file or output as a download
 */
error_reporting(E_ALL); 
ini_set('display_errors', 1);

session_start();


if (! isset($_SESSION["user_name"]) ){
    //echo "Not Logged in";
    header("Location: login.php");
    exit();
}

if($_SESSION["user_group_id"] != 1) {
    echo("Access denied! Please contact Administrator");
exit();
}
    


//require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/vcard-master/src/VCard.php';
include_once __DIR__ . '/include/db_login.php';


use JeroenDesloovere\VCard\VCard;

// select contact details from database where phone number is present
$sql = "SELECT p.pers_firstname 
, p.pers_lastname 
, a.address_place 
, a.address_email 
, a.address_phone 
, p.pers_gedcomnumber 
FROM humo_persons p JOIN humo_addresses a ON p.pers_gedcomnumber = a.address_connect_id 
WHERE a.address_phone != '' ";

// firstname lastname city country phone email additional=RIN
// for each row create a vcard
// zip the .vcf into a file
// trigger a download to local machine

        $result = $dbh->prepare($sql); 
        //$result->bindParam(1,$main_person,PDO::PARAM_STR);
        $result->execute();
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
		    
		    //echo $row['pers_firstname'];
		    
		    
		    
            // define vcard
            $vcard = new VCard();
            
            
            // define variables
            $firstname = $row['pers_firstname'];
            $lastname = $row['pers_lastname'];
            $additional = '';
            $prefix = '';
            $suffix = '';
            $gedcomnbr = $row['pers_gedcomnumber'];
            
            // add personal data
            $vcard->addName($lastname, $firstname, $additional, $prefix, $suffix, $gedcomnbr);
            
            // add work data
            //$vcard->addCompany('Siesqo');
            //$vcard->addJobtitle('Web Developer');
            $vcard->addEmail($row['address_email']);
            $vcard->addPhoneNumber($row['address_phone'], 'PREF;WORK');
            //$vcard->addPhoneNumber(123456789, 'WORK');
            $vcard->addAddress($row['address_place']);
            //$vcard->addURL('http://www.jeroendesloovere.be');
            
            $vcard->addNote($gedcomnbr);
            //$vcard->addPhoto(__DIR__ . '/assets/landscape.jpeg');
            //$vcard->addPhoto('https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg');
            $gedcomnbr = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]"."/admin/index.php?page=editor&tree=humo_&person=".$gedcomnbr;
            $vcard->addFamilyTreeURL($gedcomnbr);
            // return vcard as a string
            //return $vcard->getOutput();
            
            // return vcard as a download
            //return $vcard->download();
            
            // echo message
            //echo 'A personal vCard is saved in this folder: ' . __DIR__;
            
            // or
            
            // save the card in file in the current folder
              $vcard->setSavePath (__DIR__ . '/tmp_files');
              $vcard->save();
            
            // echo message
            // echo 'A personal vCard is saved in this folder: ' . __DIR__;
             
            
    }
    
//     $zip = new ZipArchive();
//     $zipname = __DIR__ . '/tmp_files/addr_book.zip';
// //    echo $zipname;
//     if ($zip->open($zipname, ZipArchive::CREATE)!==TRUE) {
//     exit("cannot open addr_book.zip \n");
//     }
    
    // foreach (glob(__DIR__ . '/tmp_files/'."*.vcf") as $filename) {
    // //echo "$filename\n";
    // $new_filename = substr($filename,strrpos($filename,'/') + 1);
    // $zip->addFile($filename,$new_filename);
    // }
    
    //echo "Number of Contacts: " . $zip->numFiles . "\n";
    //echo "status:" . $zip->status . "\n";
    
    // if ($zip->status == 0)
    // {
    //     //echo "Contacts are successfully zipped";
    //     $zip->close();
    // }
    
    
    // header('Content-Type: application/zip');
    // header('Content-disposition: attachment; filename='.basename($zipname));
    // header('Content-Length: ' . filesize($zipname));
    // readfile($zipname);
    
    
    
    $code = '';
    foreach (glob(__DIR__ . '/tmp_files/'."*.vcf") as $filename) {
        $code.=file_get_contents("$filename");
    }
    file_put_contents(__DIR__ . '/tmp_files/'."all_in_one.vcf",$code);
    
    header('Content-Type: text/x-vcard');  
    header('Content-Disposition: inline; filename= "'."all_in_one.vcf".'"'); 
    header('Content-Length: '.filesize(__DIR__ . '/tmp_files/'."all_in_one.vcf"));  
    readfile(__DIR__ . '/tmp_files/'."all_in_one.vcf"); 
    
    //Deletes all vcf files
    foreach (glob(__DIR__ . '/tmp_files/'."*.vcf") as $filename) {
    //echo "$filename\n";
    unlink($filename);
    }
    
?>