Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 20 revisions

Category:Microformats Category:Helper

[h1]Microformats[/h1] Designed for humans first and machines second, [url=http://microformats.org/]microformats[/url] are a set of simple, open data formats built upon existing and widely adopted standards.

This helper is meant to ease the creation of various well known microformats. Currently, the most popular [url=http://microformats.org/wiki/hcard]hCard format[/url] is supported.

[h2]hCard()[/h2] hCard is a simple, open, distributed format for representing people, companies, organizations, and places, using a 1:1 representation of the properties and values of the vCard standard (RFC2426 (http://www.ietf.org/rfc/rfc2426.txt)) in semantic XHTML. hCard is one of several open microformat standards suitable for embedding in (X)HTML, Atom, RSS, and arbitrary XML. [h3]Usage[/h3] The hCard help will accept data either in distinct variable references (for example hCard ("Firstname", "Middlename", "Lastname", "Organizationname"), or more commonly in an array (see below). [code] $hCard_data = array ( 'given_name' => 'Firstname', 'middle_name' => 'Middlename', 'family_name' => 'Lastname', 'organization' => 'OrganizationName', 'street' => '123 Street', 'city' => 'City', 'province' => 'Province/State', 'postal_code' => 'Postal/Zip', 'country' => 'Country', 'phone' => 'phonenumber', 'email' => '[email protected]', 'url' => 'http://yoursite.com', 'aim_screenname' => 'aimname', 'yim_screenname' => 'yimname' );

echo hCard($hCard_data); [/code] The output of the above will be: [code] //output would be

Firstname class="additional_name">Middlename Lastname
OrganizationName

<a class="email"

href="mailto:[email protected]">[email protected]

123 Street
City
Province/State
Postal/Zip
Country
</div>
<div id="tel"></div>
<a class="url" 

href="aim:goim?screenname=aimname">AIM YIM

[/code]

Save the following as system/helpers/microformats_helper.php

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

// ------------------------------------------------------------------------

/**

// ------------------------------------------------------------------------

/**

  • hCard
  • Generates an microformats hCard. Accepts either distinct variable references
  • (for example hCard ("Firstname", "Middlename", "Lastname", "Organizationname")
  • or more commonly an array such as
  • $hCard_data = array (
  • 'given_name' => 'Firstname',
  • 'middle_name' => 'Middlename',
  • 'family_name' => 'Lastname',
  • 'organization' => 'OrganizationName',
  • 'street' => '123 Street',
  • 'city' => 'City',
  • 'province' => 'Province/State',
  • 'postal_code' => 'Postal/Zip',
  • 'country' => 'Country',
  • 'phone' => 'phonenumber',
  • 'email' => '[email protected]',
  • 'url' => 'http://yoursite.com',
  • 'aim_screenname' => 'aimname',
  • 'yim_screenname' => 'yimname' *);
  • @access public
  • @param array
  • @return string */

function hCard($given_name = '', $middle_name = '', $family_name = '', $organization = '', $street = '', $city = '', $province = '', $postal_code = '', $country = '', $phone = '', $email = '', $url = '', $aim_screenname = '', $yim_screenname = '') {

if (is_array($given_name)) {        
    foreach (array('middle_name', 'family_name', 'organization', 'street', 'city', 'province', 'postal_code', 'country', 'phone', 'email', 'url', 'aim_screenname', 'yim_screenname', 'given_name') as $item) {
        if (isset($given_name[$item])) {
            $$item = $given_name[$item];
        }
    }
}

$middle_name != '' ? $name_class = 'fn n' : $name_class = 'fn';
if ( $url != '' ) {
    $name_wrapper_start = "<a href=\"$url\" class=\"$name_class\">";
    $name_wrapper_end = "</a>";
} else {
    $name_wrapper_start ="<span class=\"$name_class\">";
    $name_wrapper_end = "</span>";
}

$hCard = "<div class=\"vcard\">\n";
$hCard .= "\t$name_wrapper_start\n";
if ($given_name != '') {
    $hCard .= "\t\t<span class=\"given-name\">$given_name</span>\n";    
}
if ($middle_name != '') {
    $hCard .= "\t\t<span class=\"additional_name\">$middle_name</span>\n";    
}
if ($family_name != '') {
    $hCard .= "\t\t<span class=\"family_name\">$family_name</span>\n";    
}
$hCard .= "\t$name_wrapper_end\n";
if ($organization != '') {
    $hCard .= "\t<div class=\"org\">$organization</div>\n";    
}
if ($email != '') {
    $hCard .= "<a class=\"email\" href=\"mailto:$email\">$email</a>\n";
}
if ($street !== '' || $city !== '' || $province != '' || $postal_code != '' || $country != '') {
    $hCard .= "\t<div class=\"adr\">\n";
    if ($street !== '') {
        $hCard .= "\t\t<div class=\"street-address\">$street</div>\n";
    }
    if ($city !== '') {
        $hCard .= "\t\t<div class=\"locality\">$city</div>\n";
    }
    if ($province !== '') {
        $hCard .= "\t\t<div class=\"region\">$province</div>\n";
    }
    if ($postal_code !== '') {
        $hCard .= "\t\t<div class=\"postal-code\">$postal_code</div>\n";
    }
    if ($country !== '') {
        $hCard .= "\t\t<div class=\"country-name\">$country</div>\n";
    }
    $hCard .= "\t</div>\n";
}
if ($phone != '') {
    $hCard .= "\t<div id=\"tel\">$phone</div>\n";
}
if ($aim_screenname != '') {
    $hCard .= "\t<a class=\"url\" href=\"aim:goim?screenname=$aim_screenname\">AIM</a>\n";
}
if ($yim_screenname != '') {
    $hCard .= "\t<a class=\"url\" href=\"ymsgr:sendIM?$yim_screenname\">YIM</a>\n";
}
$hCard .= "</div>\n";
return $hCard;

}

?> [/code]

Clone this wiki locally