By Guus , 1 August 2009

For the address book on our site I used CiviCRM, an open source CRM system. I recently upgraded from CiviCRM 1.9 to 2.0, and since the APIs changed I had to update the block with birthdays that I wrote two years ago.

The primary change in CiviCRM 2 that impacted the block was the merge of civicrm_individual into civicrm_contact, a sensible schema change. Secondly, the public API's method calls are now organized in smaller modules and were renamed.

Here's the updated birthday block for CiviCRM 2.0:

<?php
// Check if CiviCRM is installed here.
if (!module_exists('civicrm')) return false;

// Initialization call is required to use CiviCRM APIs.
civicrm_initialize(true);
require_once('api/v2/Contact.php');

$select = "SELECT id, birth_date, CONCAT(((RIGHT(birth_date,5) 
< RIGHT(CURRENT_DATE,5)) + YEAR(CURRENT_DATE)), RIGHT(birth_date,6)) AS bday,
concat(concat(month(birth_date), '/'), day(birth_date)) as displaydate,  
(TO_DAYS(CONCAT(((RIGHT(birth_date,5) < RIGHT(CURRENT_DATE,5)) + YEAR(CURRENT_DATE)), 
RIGHT(birth_date,6))) - TO_DAYS(CURRENT_DATE)) AS toBday FROM civicrm_contact WHERE 
(TO_DAYS(CONCAT(((RIGHT(birth_date,5) < RIGHT(CURRENT_DATE,5)) + YEAR(CURRENT_DATE)), 
RIGHT(birth_date,6))) - TO_DAYS(CURRENT_DATE) < 7) ORDER BY bday, RIGHT(birth_date,5);";

$query  = $select;
$params = array( );

$dao =& CRM_Core_DAO::executeQuery( $query, $params );

echo "<div class=\"item-list\"><ul>\n";
while ( $dao->fetch( ) ) {

// Contact API returns contact info

$params = array('contact_id' => $dao->id);
$contact = &civicrm_contact_get( $params );

if ( civicrm_error( $contact ) ) {
    echo $contact['error_message'];
}

echo "<li><a href=\"/civicrm/contact/view?reset=1&cid=" . $dao->id . "\">" . $contact['display_name'] . 
"</a>, " . $dao->displaydate;

echo "</li>\n";

}
echo "</div></ul>\n";

?>

While developing this database query was useful; it disables the block.

update blocks set status = 0 where title = 'Birthdays';
Topic