<?php

/**
 * The definition for the utils class.
 *
 * @package CGContacts
 * @category Utility
 * @author  calguy1000 <calguy1000@cmsmadesimple.org>
 * @copyright Copyright 2014 by Robert Campbell
 */

namespace CGContacts;

/**
 * This class provides various static application level utilities.
 */
final class utils
{
    /**
     * @ignore
     */
    private function __construct() {}

    /**
     * @ignore
     */
    public static function load_js($plugin_name)
    {
        $_gCms = \CmsApp::get_instance();
        $mod = \cms_utils::get_module(MOD_CGCONTACTS);

        switch( $plugin_name ) {
        case 'cgcontacts_add':
            $tpl = $mod->CreateSmartyTemplate('jquery.cgcontacts_add.tpl');
            $tpl->assign('mod',$mod);
            $obj = new \StdClass;
            $obj->code = $tpl->fetch();
            return $obj;

        case 'cgcontacts_sel':
            $tpl = $mod->CreateSmartyTemplate('jquery.cgcontacts_sel.tpl');
            $tpl->assign('mod',$mod);
            $obj = new \StdClass;
            $obj->code = $tpl->fetch();
            $tpl = $mod->CreateSmartyTemplate('jquery.cgcontacts_sel.css.tpl');
            $tpl->assign('mod',$mod);
            $obj->styles = $tpl->fetch();
            return $obj;

        default:
            stack_trace();
            die('unknown plugin '.$plugin_name);
        }
    }

    /**
     * Calculate a unique GUID.
     * This method uses the function com_create_guid if it can be found, otherwise
     * it generates a series of random values.
     *
     * @return string
     */
    public static function calc_guid()
    {
        if (function_exists('com_create_guid') === true) {
            return trim(com_create_guid(), '{}');
        }

        return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
    }

    /**
     * @ignore
     */
    public static function get_feu_list()
    {
        static $list;
        if( !is_array($list) ) {
            $list = array();

            // now add in feu users
	    $feu = \cms_utils::get_module('FrontEndUsers');
            if( is_object($feu) ) {
                $query = new \feu_user_query;
                $query->add_and_opt(\feu_user_query_OPT::MATCH_NOTEXPIRED);
                $query->set_sortby(\feu_user_query::RESULT_SORTBY_USERNAME);
                $query->set_sortorder(\feu_user_query::RESULT_SORTORDER_ASC);
                $rs = $query->execute();
                while( !$rs->EOF() ) {
                    $list[$rs->fields['id']] = $rs->fields['username'];
                    $rs->MoveNext();
                }
            }
        }
        return $list;
    }

    /**
     * Generate a vcard for the provided contact.
     *
     * @param contact $contact
     * @return string
     */
    public static function get_vcard(contact $contact)
    {
        $vcard = new vcard_generator;

        // uid
        if( ($uid = $contact->get_uuid()) ) $vcard->setUID($uid);
        // fullname
        if( ($dn = $contact->get_display_name()) ) $vcard->setFullname($dn);
        // nickname
        if( ($nn = $contact->get_nickname()) ) $vcard->setNickname();
        // company
        if( $contact->get_company() ) $vcard->addCompany($contact->get_company());
        // titles
        if( $contact->get_title() ) $vcard->setJobTitle($contact->get_title());
        // role
        if( $contact->get_role() ) $vcard->setProperty('ROLE',$contact->get_role());
        // names
        $name = $contact->get_name();
        if( is_object($name) ) {
            $vcard->setName($name->get_last(),$name->get_first(),$name->get_middle(),$name->get_prefix(),$name->get_suffix());
        }

        // addresses
        $addresses = $contact->get_address_objects();
        for( $i = 0; $i < count($addresses); $i++ ) {
            $adr = $addresses[$i];
            $vcard->addAddress($adr->get_pobox(),$adr->get_street(),$adr->get_city(),
                               $adr->get_state(), $adr->get_zip(), $adr->get_country(),$adr->get_label(),$adr->is_preferred());
        }

        // emails
        $emails = $contact->get_email_objects();
        for( $i = 0; $i < count($emails); $i++ ) {
            $obj = $emails[$i];
            $vcard->addEmail($obj->get_address(),$obj->get_label(),$obj->is_preferred());
        }

        // phone
        $phones = $contact->get_phone_objects();
        for( $i = 0; $i < count($phones); $i++ ) {
            $obj = $phones[$i];
            $types = array($obj->get_type(),$obj->get_label());
            $vcard->addPhone($obj->get_value(),$types,$obj->is_preferred());
        }

        // note
        if( ($note = $contact->get_note()) ) $vcard->addNote($note);

        // url
        $url = $contact->get_preferred_url();
        if( $url ) $vcard->addURL($url);

        // categories
        $categories = $contact->get_categories();
        if( is_array($categories) && count($categories) ) {
            $names = array();
            foreach( $categories as $rec ) {
                $cat_id = $rec['cat_id'];
                $names[] = category::get_category_name($cat_id);
            }
            $vcard->setCategories($names);
        }

        // birthday (todo)

        // photo / logo
        if( ($image = $contact->get_image()) ) $vcard->addPhoto($image);
        if( ($logo = $contact->get_logo()) ) $vcard->addLogo($image);

        return $vcard->get();
    }
} // end of class
?>

