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

Category:Microformats | Category:Helper

download the helper file

[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@yoursite.com', 'url' => 'http://yoursite.com', 'aim_screenname' => 'aimname', 'yim_screenname' => 'yimname', 'avatar' => '/images/your_photo.png', 'title' => 'title', );

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

avatar Firstname Middlename Lastname
OrganizationName
title
email@yoursite.com
123 Street
City
Province/State
Postal/Zip
Country
AIM YIM
[/code]

[h2]license()[/h2] Rel-License is a simple, open, format for indicating content licenses which is embedable in (X)HTML, Atom, RSS, and arbitrary XML. Currently, the following licenses can be automatically generated:

Code Igniter License Agreement (of course), every Creative Commons 2.5 license, GNU General Public License (version 2), GNU Lesser General Public License (version 2.1), Mozilla Public License (Version 1.1), Apache License (version 2.0), and W3C SOFTWARE NOTICE AND LICENSE.

[h3]Usage[/h3] license ('license type', 'optional text'); [code] echo license('lgpl'); // will output GNU Lesser General Public License (version 2.1)

echo 'We release our code under a ' . license('ci', 'Code Igniter') . ' license.'; // will output We release our code under a Code Igniter license. [/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@yoursite.com',
  • 'url' => 'http://yoursite.com',
  • 'aim_screenname' => 'aimname',
  • 'yim_screenname' => 'yimname'
  • 'avatar' => '/images/your_photo.png',
    
  • 'title' => 'title',
    

*); *

  • @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 = '', $avatar = '', $title = '' ) {

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', 'avatar','title','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 ($avatar != '') {
    $hCard .="\t\t<img src=\"$avatar\" alt=\"avatar\" class=\"photo\" />\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 ($title !=''){
    $hCard .= "\t<div class=\"title\">$title</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;

}

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

/**

  • license
  • Generates an microformats license. Accepts one of the following licenses
  • and an optional text string.
  • using 'ci' gives Code Igniter License Agreement
  •                http://www.codeigniter.com/user_guide/license.html
    
  • using 'cc_by' gives Creative Commons Attribution (version 2.5)
  •                 http://creativecommons.org/licenses/by/2.5/
    
  • using 'cc_by-nd' gives Creative Commons Attribution-NoDerivs (version 2.5)
  •                http://creativecommons.org/licenses/by-nd/2.5/
    
  • using 'cc_by-nc-nd' gives Creative Commons Attribution-NonCommercial-NoDerivs (version 2.5)
  •                http://creativecommons.org/licenses/by-nc-nd/2.5/
    
  • using 'cc_by-nc' gives Creative Commons Attribution-NonCommercial (version 2.5)
  •                http://creativecommons.org/licenses/by-nc/2.5/
    
  • using 'cc_by-nc-sa' gives Creative Commons Attribution-NonCommercial-ShareAlike (version 2.5)
  •                http://creativecommons.org/licenses/by-nc-sa/2.5/
    
  • using 'cc_by-sa' gives Creative Commons Attribution-ShareAlike (version 2.5)
  •                http://creativecommons.org/licenses/by-sa/2.5/
    
  • using 'gpl' gives GNU General Public License (version 2)
  •                http://www.gnu.org/copyleft/gpl.html
    
  • using 'lgpl' gives GNU Lesser General Public License (version 2.1)
  •                http://www.gnu.org/licenses/lgpl.html
    
  • using 'mpl' gives Mozilla Public License (Version 1.1)
  •                http://www.mozilla.org/MPL/MPL-1.1.html
    
  • using 'apache' gives Apache License (version 2.0)
  •                http://www.apache.org/licenses/LICENSE-2.0
    
  • using 'w3c' gives W3C SOFTWARE NOTICE AND LICENSE
  •                http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
    
  • Using any string but one of these generates an error.
  • @access public
  • @param string
  • @param string
  • @return string */

function license ($license_choice, $text = '') { $license_types = array( array( 'ci', 'Code Igniter License Agreement', 'http://www.codeigniter.com/user_guide/license.html' ), array( 'cc_by', 'Creative Commons Attribution (version 2.5)', 'http://creativecommons.org/licenses/by/2.5/' ), array( 'cc_by-nd', 'Creative Commons Attribution-NoDerivs (version 2.5)', 'http://creativecommons.org/licenses/by-nd/2.5/' ), array( 'cc_by-nc-nd', 'Creative Commons Attribution-NonCommercial-NoDerivs (version 2.5)', 'http://creativecommons.org/licenses/by-nc-nd/2.5/' ), array( 'cc_by-nc', 'Creative Commons Attribution-NonCommercial (version 2.5)', 'http://creativecommons.org/licenses/by-nc/2.5/' ), array( 'cc_by-nc-sa', 'Creative Commons Attribution-NonCommercial-ShareAlike (version 2.5)', 'http://creativecommons.org/licenses/by-nc-sa/2.5/' ), array( 'cc_by-sa', 'Creative Commons Attribution-ShareAlike (version 2.5)', 'http://creativecommons.org/licenses/by-sa/2.5/' ), array( 'gpl', 'GNU General Public License (version 2)', 'http://www.gnu.org/licenses/lgpl.html' ), array( 'lgpl', 'GNU Lesser General Public License (version 2.1)', 'http://www.gnu.org/licenses/lgpl.html' ), array( 'mpl', 'Mozilla Public License (Version 1.1)', 'http://www.mozilla.org/MPL/MPL-1.1.html' ), array( 'apache', 'Apache License (version 2.0)', 'http://www.apache.org/licenses/LICENSE-2.0' ), array( 'w3c', 'W3C SOFTWARE NOTICE AND LICENSE', 'http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231' ) );

if ( array_search_recursive($license_choice, $license_types) ) {
    $license_array = array_search_recursive($license_choice, $license_types);
    if ($text == '') {
        $text = $license_types[$license_array[0]][1];
    }
    $license_url = $license_types[$license_array[0]][2];
    return '<a rel="license" href="' . $license_url . '">' . $text . '</a>';
} else {
    show_error ('unknown license type');
}

}

/**

  • Just a quick handy function to search down through multi-dimensional arrays
  • it returns an array within which the $needle was found
  • needed for license() function. */ function array_search_recursive($needle, $haystack, $key_lookin="") { $path = NULL; if (!empty($key_lookin) && array_key_exists($key_lookin, $haystack) && $needle === $haystack[$key_lookin]) { $path[] = $key_lookin; } else { foreach($haystack as $key => $val) { if (is_scalar($val) && $val === $needle && empty($key_lookin)) { $path[] = $key; break; } elseif (is_array($val) && $path = array_search_recursive($needle, $val, $key_lookin)) { array_unshift($path, $key); break; } } } return $path; }

?> [/code]

Clone this wiki locally