<?php
namespace app\common\lib;

use think\facade\Db;

use JeroenDesloovere\VCard\VCard as VCardApi;

class Vcard{

    public static function genVcf($userid){
        $userInfo = Db::name('user')->where('user_id',$userid)->find();
        if(!$userInfo){
            return false;
        }
        $vcard = new VCardApi();

        $lastname = $userInfo['real_name'];
        $firstname = '';
        $additional = '';
        $prefix = '';
        $suffix = '';

        $vcard->addName($lastname, $firstname, $additional, $prefix, $suffix);
        
        // add work data
        $vcard->addCompany($userInfo['company']);
        $vcard->addJobtitle($userInfo['title']);
        // $vcard->addRole('Data Protection Officer');
        $vcard->addEmail($userInfo['email']);
        $vcard->addPhoneNumber($userInfo['phone'], 'PREF;CELL');
        $vcard->addPhoneNumber($userInfo['tel'], 'WORK');
        // $vcard->addAddress(null, null, 'street', 'worktown', null, 'workpostcode', 'Belgium');
        // $vcard->addLabel('street, worktown, workpostcode Belgium');
        if($userInfo['url']){
            // $vcard->addURL(getUrl().'/card/?userid='.$userInfo['user_id']);
            $vcard->addURL($userInfo['url']);
        }
        
        $vcard->addPhoto(__DIR__.'/../../../public/storage/'.$userInfo['user_id'].'/'.$userInfo['user_id'].'_line.jpg');
        
        // return vcard as a string
        //return $vcard->getOutput();
        
        // return vcard as a download
        // return $vcard->download();        
        $vcard->setSavePath(__DIR__.'/../../../public/storage/'.$userInfo['user_id'].'/');
        $vcard->setFilename($userInfo['user_id']);
        $vcard->save();
        return true;
    }
}