<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog: Raised Eyebrow Web Studio, Inc. &#187; Code and Coding</title>
	<atom:link href="http://blog.raisedeyebrow.com/category/geekery/code-and-coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.raisedeyebrow.com</link>
	<description>What's turning heads at Raised Eyebrow</description>
	<lastBuildDate>Thu, 15 Jul 2010 15:19:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Datadotgc.ca &#8211; A Drupal case study: Part 2</title>
		<link>http://blog.raisedeyebrow.com/2010/06/datadotgc-ca-a-drupal-case-study-part-2/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=datadotgc-ca-a-drupal-case-study-part-2</link>
		<comments>http://blog.raisedeyebrow.com/2010/06/datadotgc-ca-a-drupal-case-study-part-2/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 19:23:41 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[CKAN]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=880</guid>
		<description><![CDATA[This is the second part of Drupal Case Study on integrating the CKAN data repository with Drupal 6. Part 1 covered the following:

What is CKAN?
CKAN’s API
The Foundation
The Build
Theming
Homepage Chart

Caching
API calls are expensive. There&#8217;s no doubt about that. Particularly when you&#8217;re returning large amounts of data. To avoid any issues of the CKAN API being exhausted [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second part of Drupal Case Study on integrating the CKAN data repository with Drupal 6. <a href="/2010/04/datadotgc-ca-a-drupal-case-study/">Part 1 covered the following</a>:</p>
<ul>
<li>What is CKAN?</li>
<li>CKAN’s API</li>
<li>The Foundation</li>
<li>The Build</li>
<li>Theming</li>
<li>Homepage Chart</li>
</ul>
<h3>Caching</h3>
<p>API calls are expensive. There&#8217;s no doubt about that. Particularly when you&#8217;re returning large amounts of data. To avoid any issues of the CKAN API being exhausted from requests and to ensure that the site remained responsive, I decided to leverage Drupals caching mechanisms and pretty much cached everything I could, within reason. <a href="http://www.datadotgc.ca">The Chart</a>, Tag Cloud, <a href="http://www.datadotgc.ca/data/tag">Tag lists</a>, <a href="http://www.datadotgc.ca/data/ministry">Ministry lists</a>, <a href="http://www.datadotgc.ca/data/all">All Packages</a> list and all individual packages are cached. The issue with caching on this site is that if a package gets updated on the CKAN instance, we need to know about that on our Drupal site immediately and then clear the appropriate caches so that the most recent data can be retrieved. </p>
<p>For caching I created a table called &#8216;cache_ckan&#8217;, that stores everything I need. To create this table I used the schema of the existing cache table and put that in my .install file in my module directory.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8808"><td class="code" id="p880code8"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_install().
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_install<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  drupal_install_schema<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_uninstall().
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_uninstall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  drupal_uninstall_schema<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_schema().
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_schema<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$schema</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$schema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> drupal_get_schema_unprocessed<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'system'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$schema</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Whenever this module is enabled this schema will be run and the table will be created.</p>
<h4>What is stored in the ckan_cache table?</h4>
<p>There are various items stored in the cache table.</p>
<ol>
<li>The Homepage chart data</li>
<li>Tag lists</li>
<li>Ministry lists</li>
<li>List of all datasets</li>
</ol>
<p>Let&#8217;s take the <a href="http://www.datadotgc.ca/data/all">list of all packages</a> as an example. I covered how I implemented the paging in <a href="/2010/04/datadotgc-ca-a-drupal-case-study/">my previous post</a>. As this list is paginated it&#8217;s important that every page be cached to improve the speed of the site. As the paging mechanism is already implemented it&#8217;s just a case of creating a cache table entry (<strong>ckan:all{page-number}</strong>) for each page, and then checking for it&#8217;s existence when loading the page.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8809"><td class="code" id="p880code9"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> cache_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:all'</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If cached data exists for this page...</span>
	<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$ckan</span> <span style="color: #339933;">=</span> ckan_ckan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$items_per_page</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_items_per_page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// If we're in a page, we need to set where to start the list</span>
		<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the offset to the number of records in</span>
	<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Limit to the number of items per page </span>
	<span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span>
&nbsp;
	try <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">advancedSearch</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'groups'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'canadagov'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'all_fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'offset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// If the API call worked</span>
	watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Called CKAN API for list of all packages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	cache_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:all'</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This method is very simple and very effective. It means the pages load lightning fast and only one page of data at a time is retrieved.</p>
<h4>How does the cache get cleared/updated</h4>
<p>Datasets/Packages change all the time on the CKAN instance, so how do you make sure that the Drupal site has the most current data. This module has two ways of managing that.</p>
<h4>1. Using <a href="http://api.drupal.org/api/function/hook_form/6">hook_form</a> to redirect to CKAN</h4>
<p>As the CKAN nodes on Drupal are created on the fly and hold very little information, there is really no need to access the EDIT form for these nodes. Whenever an admin user clicks the edit tab on the node, they are automatically redirected to the appropriate CKAN package editing screen. hook_form is called to retrieve the form that is displayed when one attempts to &#8220;create/edit&#8221; an item. For CKAN content types, the user is redirect to the CKAN instance.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88010"><td class="code" id="p880code10"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_form
 *
 * Redirect the user to ca.ckan.net package edit screen on edit
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_form<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #000088;">$form_state</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'ckan'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  	drupal_goto<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://ca.ckan.net/package/edit/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the CKAN form is submitted, CKAN then redirects back to the Drupal site and calls a specific URL that tells Drupal to call CKAN again to get the package information and populate the node. To clarify, the process is</p>
<ol>
<li>Redirect http://www.datadotgc.ca/node/X/edit to http://ca.ckan.net/package/edit/{name of X}</li>
<li>On save of CKAN Package, redirect to http://www.datadotgc.ca/{special_url}/{name_of_X}</li>
<li>Load the node with {name_of_X}</li>
<li>Call CKAN to get the (updated) data for Package {name_of_X}</li>
<li>Save the node with updated data</li>
</ol>
<h4>Using Cron and an Atom Feed</h4>
<p>CKAN provides an Atom feed of recent updates to the Packages. Cron checks this feed every time it runs. If the feed has changed since the last cron run, then we know there have been updates and we clear all of the caches.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code11'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88011"><td class="code" id="p880code11"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_cron()
 *
 **/</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_cron<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Get the md5sum of the current atom feed</span>
	<span style="color: #000088;">$current_feed</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/md5_file"><span style="color: #990000;">md5_file</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://ca.ckan.net/revision/list?format=atom'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Current feed md5: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$current_feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Retrieve the previously stored md5sum</span>
	<span style="color: #000088;">$previous_feed</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_atom_feed_md5'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Previous feed md5: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$previous_feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// If there have been changes</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_feed</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$previous_feed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ATOM feed has updated, clearing caches and deleting nodes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// Flush all the caches</span>
		cache_clear_all<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'*'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  	        <span style="color: #666666; font-style: italic;">// Set the previous feed md5</span>
		variable_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_atom_feed_md5'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Tag cloud creation</h3>
<p>I borrowed some code from the <a href="http://drupal.org/project/tagadelic">Tagadelic</a> module to achieve the tag cloud</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code12'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88012"><td class="code" id="p880code12"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Build a tag cloud based on the settings provided
 *
 * @return	String	A themed list of weighted tags
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_tag_cloud<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// If there is cached data</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> cache_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:tags'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/unserialize"><span style="color: #990000;">unserialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$ckan</span> <span style="color: #339933;">=</span> ckan_ckan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTagCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Called CKAN API for tag cloud'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		cache_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:tags'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/serialize"><span style="color: #990000;">serialize</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Let's sort them by weight first off</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$tag</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$weight</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<a href="http://www.php.net/array_multisort"><span style="color: #990000;">array_multisort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weight</span><span style="color: #339933;">,</span> SORT_DESC<span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Now let's get the top X number of tags</span>
	<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_slice"><span style="color: #990000;">array_slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_tagcloud_total'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Now build the tags</span>
	<span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> ckan_tag_build_weighted<span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Sort them</span>
	<span style="color: #000088;">$tags</span> <span style="color: #339933;">=</span> ckan_tag_sort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// Theme them</span>
	<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> theme<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_weighted_tags'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tags</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Theme function that renders the HTML for the tags
 * @ingroup themable
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> theme_ckan_weighted_tags<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tags</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data/tag/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;tagcloud level&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'weight'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'tag'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Using the CKAN Search API for all lists</h3>
<p>Ok, so what&#8217;s this all about? CKAN has some <a href="http://knowledgeforge.net/ckan/doc/ckan/api.html">nice API calls</a> like <em>/api/rest/package/PACKAGE-REF</em> that return a list of Packages. However these return the name/id of the Package ONLY. In our case, for our listings, we wanted other data, such as the tags attached to the Package as well as a brief description.</p>
<p>The only way to get this data was to do a search API call <em>/api/search/package</em> and pass some extra parameters, in this case <em><strong>all_fields=1</strong></em> and <em><strong>department={name of Ministry}</strong></em>. </p>
<p><em><strong>all_fields=1</strong></em> tells the search to return all Package fields, not just the name/id; just as is if you called  <em>/api/rest/package/PACKAGE-REF</em>.</p>
<p><em><strong>department={name of Ministry}</strong></em> tells the search to return all packages that have a department of {name of Ministry}. <strong>The lovely folks at CKAN added this functionality for us on request</strong>.</p>
<p>What does this look like, well it&#8217;s pretty simple really. Call the <em>advancedSearch()</em> function. Pass it a few parameters and it returns you all the data you need. Here&#8217;s the function itself:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code13'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88013"><td class="code" id="p880code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> advancedSearch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parameters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$querystring</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'='</span><span style="color: #339933;">.</span> <a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transfer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'api/search/package?'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$querystring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> CkanException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Search Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$results</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is that function being called for the list of Ministry Packages. The offset and limit are for the paging mechanism:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p880code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88014"><td class="code" id="p880code14"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Call the function</span>
<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">advancedSearch</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'department'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ministry</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'all_fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'offset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There&#8217;s a lot more functionality in this module, more than I can go through in a blog post, even 5 posts. If you&#8217;re trying to integrate Drupal with a CKAN instance and are not sure where to start then please leave a comment and I&#8217;ll get back in touch.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2010/06/datadotgc-ca-a-drupal-case-study-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Datadotgc.ca &#8211; A Drupal case study</title>
		<link>http://blog.raisedeyebrow.com/2010/04/datadotgc-ca-a-drupal-case-study/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=datadotgc-ca-a-drupal-case-study</link>
		<comments>http://blog.raisedeyebrow.com/2010/04/datadotgc-ca-a-drupal-case-study/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 17:56:20 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Nonprofit Tech]]></category>
		<category><![CDATA[CKAN]]></category>
		<category><![CDATA[Open data]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=863</guid>
		<description><![CDATA[We recently launched http://www.datadotgc.ca, an open data collection portal for Canada, built to help poke the Canadian government in the right direction, towards something like similar sites in the UK (data.gov.uk) and the US (data.gov). Read David Eaves&#8217; explanation of its purpose. For the benefit of the programming and Drupal community, I&#8217;m going to run [...]]]></description>
			<content:encoded><![CDATA[<p>We recently launched <a href="http://www.datadotgc.ca">http://www.datadotgc.ca</a>, an open data collection portal for Canada, built to help poke the Canadian government in the right direction, towards something like similar sites in the UK (<a href="http://data.gov.uk">data.gov.uk</a>) and the US (<a href="http://data.gov">data.gov</a>). <a href="http://eaves.ca/2010/04/15/datadotgc-ca-launched-the-opportunity-and-challenge/">Read David Eaves&#8217; explanation of its purpose</a>. For the benefit of the programming and Drupal community, I&#8217;m going to run through, with the aid of code samples, the development of the Drupal module to communicate with the CKAN API (which is where the data is stored). I&#8217;ll also walk through Theming, integration with Google Charts, Tag Clouds and most importantly, caching.</p>
<h3>What is CKAN?</h3>
<blockquote><p>CKAN is a registry or catalogue system for datasets or other &#8220;knowledge&#8221; resources. CKAN aims to make it easy to find, share and reuse open content and data, especially in ways that are machine automatable.</p></blockquote>
<p>CKAN is a nice big database that is built to accept user input of the type of data we&#8217;re trying to collect for datadotgc.ca. It has a slick front and back end that allows administrative access to the collected data.<br />
You can find out more on <a href="http://www.okfn.org/ckan/">their website</a>. </p>
<h3>CKAN&#8217;s API</h3>
<p>In order to utilize the power of CKAN I needed to link it up to Drupal. CKAN has a <a href="http://knowledgeforge.net/ckan/doc/ckan/api.html">powerful and flexible API</a> that I used extensively in the module.</p>
<h3>The Foundation</h3>
<p>Early on in the project I got in touch with the wonderful team at CKAN and they then put me in touch with Sean Burlington from the <a href="http://data.gov.uk">data.gov.uk</a> development team. They had also built their site in Drupal and Seán had lots of information on how they tweaked their CKAN site to work with Drupal. He worked hard to open source some of the work that they had done, and <a href="http://www.practicalweb.co.uk/blog/10/02/24/drupal-module-ckan-integration">released it</a> just in time for us to get started. Seáns module provided the basic API connectivity we needed to get started and was the foundation for our module.</p>
<h3>The Build</h3>
<p>How do you integrate Drupal with the CKAN API? Let&#8217;s start with the basics:</p>
<p>CKAN stores the <a href="http://www.datadotgc.ca/dataset/geogratisnat_hydrography_v100">individual datasets that you see on Datadotgc.ca</a> as &#8216;<em>Packages</em>&#8216;. It became clear that these &#8216;Packages&#8217; could be directly mapped to the standard node architecture in Drupal. To achieve this I created a content type in the module that stored all the data I needed.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86325"><td class="code" id="p863code25"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Define module-provided node types.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_node_info<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'ckan'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'name'</span>           <span style="color: #339933;">=&gt;</span> t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CKAN Package'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'module'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'description'</span>    <span style="color: #339933;">=&gt;</span> t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A package of Open Data.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'has_title'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'title_label'</span>    <span style="color: #339933;">=&gt;</span> t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'has_body'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'body_label'</span>     <span style="color: #339933;">=&gt;</span> t<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Package Description'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'min_word_count'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'locked'</span>         <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span>
    <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ckan_create_node<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ckan_data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'title'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ckan_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'uid'</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'body'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ckan_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'promote'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'path'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dataset/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ckan_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'type'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'comment'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see from the code, the only data elements to be set when a node is created are <em>Title</em>, <em>Body</em> and <em>Path</em>. The body of the node is set to be the name of the CKAN package, which is in fact a simple string: <em>geogratisnat_hydrography_v100</em>.</p>
<p>The more complex CKAN data was not mapped to any CCK fields as you might think, but instead it is pulled from CKAN when the node is loaded. This simplifies the Drupal side of things by ensuring that we don&#8217;t have to keep track of any changes to the structure or contents of the dataset that may happen on the CKAN side.</p>
<p>Here is an example of some package data:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code26'); return false;">View Code</a> INI</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86326"><td class="code" id="p863code26"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>maintainer<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; Government of Canada, Natural Resources Canada, Centre for Topographic Information <span style="">&#40;</span>Sherbrooke<span style="">&#41;</span></span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>name<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; 1996_population_census_data_canada</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>author<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; Government of Canada, Natural Resources Canada, Canada Centre for Remote Sensing, GeoAccess Division, The Atlas of Canada</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>url<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; ftp://ftp.geogratis.gc.ca/atlas/Population_Ecumene_Census/1996/</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>notes<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; The parts of Canada making up the 1996 Settled Area, <span style="">&#40;</span>or Population Ecumene<span style="">&#41;</span>, represents a selection of the 5984 Census Subdivisions <span style="">&#40;</span>CSD<span style="">&#41;</span> as defined by Statistics Canada for the 1996 Census. The selection process essentially removes those CSDs with very large areas and/or very low populations. Some of British Columbia's CSD boundaries have been further modified to better conform to the distinctive settlement patterns in the Cordilleran regions. The 1996 Settled Area is an attempt to balance the needs of national scale choropleth mapping with the spatial reality that the majority of Canada's land area contains very few people. The Settled Area represents more than 98% of the Canadian population captured in the 1996 Census of Canada.</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>title<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; 1996 Population <span style="">&#40;</span>Ecumene<span style="">&#41;</span> Census Data, Canada</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>download_url<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt; ftp://ftp.geogratis.gc.ca/atlas/Population_Ecumene_Census/1996/1996.zip</span></pre></td></tr></table></div>

<p>When a node is loaded, the package data is pulled from CKAN and then cached for later use.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code27'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86327"><td class="code" id="p863code27"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Load node-type-specific information
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$ckan</span> <span style="color: #339933;">=</span> ckan_ckan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> cache_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Get the cached data</span>
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ckan</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    try <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Call the API to get the package data</span>
      <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ckan</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPackage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      drupal_set_message<span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// Cache this package data for later use</span>
    cache_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">,</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ckan</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
    watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Called CKAN API for '</span><span style="color: #339933;">.</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">body</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' package - ckan_load()'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$node</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once the CKAN data has been added to the node object it&#8217;s relatively easy to output this data in a node template. I created a template file in my theme called node_ckan.tpl.php and here&#8217;s an example of how I displayed some of the CKAN package data in there:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code28'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86328"><td class="code" id="p863code28"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
 <span style="color: #339933;">&lt;</span>h1 id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;page-title&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;title tk-museo-slab&quot;</span><span style="color: #339933;">&gt;&lt;</span> ?php <span style="color: #b1b100;">print</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;package-name&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">print</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
 <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;package-link&quot;</span><span style="color: #339933;">&gt;&lt;</span> ?php <span style="color: #b1b100;">print</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You can see from all of the above examples I&#8217;m using a class object called &#8220;<em>ckan</em>&#8221; to store our data. This came from Sean&#8217;s module and is a simple class that provides the connectivity to the CKAN API. Here&#8217;s a brief synopsis of how it works:</p>
<ol>
<li>First I need a way to connect to the API. That&#8217;s relatively straightforward using the <a href="http://php.net/manual/en/book.curl.php">curl libraries in php</a>.

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86329"><td class="code" id="p863code29"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_getinfo"><span style="color: #990000;">curl_getinfo</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li>Now create some functions that allow various parts of the API to be called. Below is an example of two class functions which call the API.

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86330"><td class="code" id="p863code30"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Get an individual package</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPackage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$package</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$package</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transfer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'api/rest/package/'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$package</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$package</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> CkanException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Package Load Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$package</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get a list of all packages</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPackageList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$list</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transfer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'api/rest/package/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> CkanException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Package List Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$list</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</li>
</ol>
<p>All of these API calls return a JSON object which is then decoded into an object using the PHP function:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code31'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86331"><td class="code" id="p863code31"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/json_decode"><span style="color: #990000;">json_decode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Theming</h3>
<p>Once that data has been successfully retrieved and decoded from JSON into something that&#8217;s easier to handle, it needs to be themed. For this I created two theming functions; one for creating an individual list item, the other to create a formatted list of these individual items. I call the theme function whenever I get results from the API, and pass it the results, along with the title I want to appear on the listings page.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code32'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86332"><td class="code" id="p863code32"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Theme the results retrieved from API call</span>
theme<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_results'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'All Packages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Theme search results
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> theme_ckan_results<span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Two global variables needed by the pager.</span>
  <span style="color: #666666; font-style: italic;">// Taken from pager_query() in pager.inc</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$pager_page_array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pager_total</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Grab the 'page' query parameter.</span>
  <span style="color: #666666; font-style: italic;">// Taken from pager_query() in pager.inc</span>
  <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Convert comma-separated $page to an array, used by other functions.</span>
  <span style="color: #666666; font-style: italic;">// Taken from pager_query() in pager.inc</span>
  <span style="color: #000088;">$pager_page_array</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Generate the data for page the requested and add it to the output.</span>
  <span style="color: #000088;">$items_per_page</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_items_per_page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// If there are less results than the specified number of items per page, reset the number of items per page</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$items_per_page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$items_per_page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Initialize pager</span>
  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// If it's not the first page</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Set the data to start displaying on the correct page</span>
    <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;h1 id=&quot;page-title&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;h3 class=&quot;resultscount&quot;&gt;Your search returned '</span><span style="color: #339933;">.</span><span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">' records&lt;/h3&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Theme the individual results</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span>  theme<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_item'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Put some magic in the two global variables</span>
  <span style="color: #666666; font-style: italic;">// Based on code in pager_query() in pager.inc</span>
  <span style="color: #000088;">$total_results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$pager_total</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ceil"><span style="color: #990000;">ceil</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_results</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$items_per_page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ckan_number_of_pages();</span>
  <span style="color: #000088;">$pager_page_array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span>
    <a href="http://www.php.net/max"><span style="color: #990000;">max</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/min"><span style="color: #990000;">min</span></a><span style="color: #009900;">&#40;</span>
      <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$pager_page_array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$pager_total</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Add the pager to the output.</span>
  <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> theme<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pager'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Theme individual search items
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> theme_ckan_item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Link the title to the dataset</span>
  <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;h2&gt;'</span> <span style="color: #339933;">.</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dataset/'</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span>check_plain<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/h2&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// Truncate the notes field</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;'</span> <span style="color: #339933;">.</span> truncate_utf8<span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notes</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">250</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wordsafe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dots</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// Output any tags	</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tags</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tags</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> l<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'data/tag/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$seperated</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; '</span> <span style="color: #339933;">.</span><span style="color: #000088;">$seperated</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>	
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>One thing you need to consider when displaying a list of results is having a pager built in so that you can break the list into bite-sized chunks. This took quite a while to figure out how to do. The problem was that the API call resulted in a lot of data and that resulted in a significant delay loading the page, due to the amount of time to complete the round trip to the API, along with the time taken to render all of that in a pager. CKAN however is very clever. When you call the API and ask for a list of packages, it returns the packages, but it also returns, as a variable, the count of the records your query generated. As well as that you can use the parameters &#8216;<em>offset</em>&#8216; and &#8216;<em>limit</em>&#8216; just like in SQL. What&#8217;s even more clever here is that it still returns the variable that holds the count of the records the query generated, but it also only returns the number of packages determined by the &#8216;<em>offset</em>&#8216; and &#8216;<em>limit</em>&#8216; parameters. </p>
<p>So if an API call to list all packages for a certain tag would normally return 200 records, and you specify a limit of 10 and an offset of 10, the data returned will contain a count of the number of records normally generated by that call, 200, but will <strong>only return 10 packages</strong> in the data, as specified by the offset and limit. This came in extremely useful for the pager as I just passed an offset and limit each time a page was loaded and then cached the returned data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code33'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86333"><td class="code" id="p863code33"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ckan</span> <span style="color: #339933;">=</span> ckan_ckan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items_per_page</span> <span style="color: #339933;">=</span> variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_items_per_page'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// If we're in a page, we need to set where to start the list</span>
  <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set the offset</span>
<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Limit to the number of items per page </span>
<span style="color: #000088;">$limit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$items_per_page</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the list of tags with their count</span>
try <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">advancedSearch</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'department'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ministry</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'all_fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'offset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$offset</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$limit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  drupal_goto<span style="color: #009900;">&#40;</span>variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_no_results_page'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sorry'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Homepage Chart</h3>
<p>There was a requirement for a graph on the homepage the displayed the number of packages attributed to each Government Ministry. The quickest way to do this was using <a href="http://code.google.com/apis/charttools/">Google Chart Tools</a>. It was relatively straightforward to get the data we needed. I did however have to do some funky sorting to get the data in the correct order. I also <a href="http://psychopyko.com/tutorial/how-to-use-google-charts/">found a wonderful tutorial</a> that really helped to clear up some of the label/legend issues I was having.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p863code34'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86334"><td class="code" id="p863code34"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Function to build a Google Chart
 *
 * @return	  string	HTML code with img tag
 *
 **/</span>
<span style="color: #000000; font-weight: bold;">function</span> ckan_chart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// If there is a cached version of the chart</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> cache_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:chart'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    watchdog<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Called Google API to build chart'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Get the list of ministries</span>
    <span style="color: #000088;">$ministries</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> filter_xss<span style="color: #009900;">&#40;</span>variable_get<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan_ministry_list'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Set up our data array</span>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ministries</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ministry</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$ckan</span> <span style="color: #339933;">=</span> ckan_ckan<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Get the list of tags with their count</span>
      try <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ckan</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">advancedSearch</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'department'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ministry</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'all_fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'offset'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limit'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #666666; font-style: italic;">// Cache the count to use on the Ministry list page '/ministry'</span>
      cache_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:ministry_'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$ministry</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'_count'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ministry</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">' ('</span><span style="color: #339933;">.</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">')'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// Sort the array in reverse order - most packages first and maintain index association</span>
    <a href="http://www.php.net/arsort"><span style="color: #990000;">arsort</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Return all the keys of the data array - the names of the ministries</span>
    <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legend</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Get the range of the chart - highest + a quarter</span>
    <span style="color: #000088;">$range</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/round"><span style="color: #990000;">round</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color:#800080;">1.25</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Grid spacing  100/MaxRange*IntervalAmount</span>
    <span style="color: #000088;">$grid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">/</span><span style="color: #000088;">$range</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Chart size, must be less than 30k pixels</span>
    <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'590'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'380'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Create query</span>
    <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span> <span style="color: #339933;">=</span>
      <span style="color: #0000ff;">'cht=bhg&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// Type</span>
      <span style="color: #0000ff;">'chd=t:'</span><span style="color: #339933;">.</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// Data</span>
      <span style="color: #0000ff;">'chs='</span><span style="color: #339933;">.</span><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// Size</span>
      <span style="color: #0000ff;">'chco=cc0000&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// Color ( Remove # from string )</span>
      <span style="color: #0000ff;">'chxt=x,y&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// X,Y axis labels</span>
      <span style="color: #0000ff;">'chxr=0,0,'</span><span style="color: #339933;">.</span><span style="color: #000088;">$range</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span> <span style="color: #666666; font-style: italic;">// Range</span>
      <span style="color: #0000ff;">'chxs=1,000000,13|0,000000,13&amp;'</span><span style="color: #339933;">.</span> 	<span style="color: #666666; font-style: italic;">// Axis colors and font size</span>
      <span style="color: #0000ff;">'chg='</span><span style="color: #339933;">.</span><span style="color: #000088;">$grid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">',0,5,5&amp;'</span><span style="color: #339933;">.</span> <span style="color: #666666; font-style: italic;">// Grid verticalgridlines, horizontalgridlines, linesize, gapsize</span>
      <span style="color: #0000ff;">'chds=0,'</span><span style="color: #339933;">.</span><span style="color: #000088;">$range</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// Scale</span>
      <span style="color: #0000ff;">'chma=0,0,0,0&amp;'</span><span style="color: #339933;">.</span> <span style="color: #666666; font-style: italic;">//left_margin, right_margin, top_margin, bottom_margin| legend_width, legend_height</span>
      <span style="color: #0000ff;">'chbh=13,0,2&amp;'</span><span style="color: #339933;">.</span>	<span style="color: #666666; font-style: italic;">// bar_width_or_scale, space_between_bars, space_between_groups</span>
      <span style="color: #0000ff;">'chxl=1:|'</span><span style="color: #339933;">.</span><a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array_reverse"><span style="color: #990000;">array_reverse</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legend</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//|Jan|Feb|Mar|Apr|May'</span>
&nbsp;
    <span style="color: #000088;">$api_path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://chart.apis.google.com/chart?'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;img src=&quot;%s&quot; alt=&quot;%s&quot; style=&quot;width:%spx;height:%spx;&quot; /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$api_path</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Who\'s Sharing'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$chart</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">size</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      cache_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ckan:chart'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cache_ckan'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>	
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$image</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So that&#8217;s a brief(not-so) overview of some of the fundamentals of how I integrated Drupal with CKAN and was able to create nodes and listings directly from API calls. </p>
<p><a href="/2010/06/datadotgc-ca-a-drupal-case-study-part-2/">In my next post I&#8217;ll cover some very important areas</a> of the module development such as:</p>
<ul>
<li>Caching</li>
<li>Tag cloud creation</li>
<li>Using the CKAN Search API for all lists</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2010/04/datadotgc-ca-a-drupal-case-study/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rewrite a Views query</title>
		<link>http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rewrite-a-views-query</link>
		<comments>http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 19:20:51 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Views]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=845</guid>
		<description><![CDATA[Recently, due to Views lack of ability to OR it&#8217;s filters together and due to some serious constraints in relation to arguments and relationships, I found myself needing to figure out how to rewrite the SQL of a Drupal view so that I could get the records that I needed.
It seems like there are two [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, due to Views lack of ability to OR it&#8217;s filters together and due to some serious constraints in relation to arguments and relationships, I found myself needing to figure out how to rewrite the SQL of a Drupal view so that I could get the records that I needed.</p>
<p>It seems like there are two ways to do this. You can either rewrite the SQL directly, or try to hack the query object that views provides.</p>
<p>A quick Google search and <a href="http://drupal.org/node/409808">I ended up on Drupal.org</a></p>
<p>Which then led me to the <a href="http://views.doc.logrus.com/group__views__hooks.html ">Views documentation</a></p>
<p>From here I was able to deduce that the</p>
<pre>hook_views_pre_execute()</pre>
<p>hook was what I needed.</p>
<p>I created my module and added the hook:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code41'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84541"><td class="code" id="p845code41"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Implementation of hook_views_pre_execute()
 *
 **/</span>
<span style="color: #000000; font-weight: bold;">function</span> other_views_pre_execute<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// If it's my view</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'my_view'</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_display</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// If it's the block on the author page</span>
	<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'block_5'</span><span style="color: #339933;">:</span>
	  <span style="color: #666666; font-style: italic;">// Set the extra argument</span>
	  <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query_args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query_args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	  <span style="color: #666666; font-style: italic;">// Rewrite the query</span>
	  <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT ... &quot;</span><span style="color: #339933;">;</span>
	  <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lets step through this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code42'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84542"><td class="code" id="p845code42"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> other_views_pre_execute<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>We need to make sure the word hook is replaced with the name of our module, in this case it&#8217;s called &#8216;other&#8217;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code43'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84543"><td class="code" id="p845code43"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// If it's my view</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'my_view'</span><span style="color: #339933;">:</span></pre></td></tr></table></div>

<p>Each view is represented by a unique name. We need to modify the query for a certain view, not all of them, in this case the view is called &#8216;my_view&#8217;.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code44'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84544"><td class="code" id="p845code44"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current_display</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// If it's the block on the author page</span>
  <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'block_5'</span><span style="color: #339933;">:</span></pre></td></tr></table></div>

<p>Views can have multiple displays, such as blocks, pages and attachments. In this case we want to target the block display with the name/id &#8216;block_5&#8242;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code45'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84545"><td class="code" id="p845code45"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Set the extra argument</span>
<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query_args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query_args'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Rewrite the query</span>
<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build_info</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT ... &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this case our query is going to have an extra argument added to the query. These arguments are an array $view-&gt;build_info['query_args']. They appear in the form &#8216;%s&#8217; and &#8216;%d&#8217; in the query text, like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p845code46'); return false;">View Code</a> SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84546"><td class="code" id="p845code46"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">status</span> <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#40;</span>node<span style="color: #66cc66;">.</span>type <span style="color: #993333; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'%s'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>node_node_data_field_publication_centre_authors<span style="color: #66cc66;">.</span>title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'%s'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span>node_node_data_field_publication_first_author<span style="color: #66cc66;">.</span>title <span style="color: #66cc66;">=</span><span style="color: #ff0000;">'%s'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In this case the first &#8216;%s&#8217; = $view->build_info['query_args'][0], the second one = $view->build_info['query_args'][1] and so on.</p>
<p>And that&#8217;s it. Your query is rewritten. One thing to watch our for, do not add any extra fields in the SELECT statement, this can cause all sorts of crazy problems.</p>
<p>The alternative to using the pre_execute hook is to use the <a href="http://views.doc.logrus.com/group__views__hooks.html#gf4d538493930fe0fa0ce6fb3bf42c156">query_alter hook</a>.</p>
<p>This seems a lot trickier and even <a href="http://www.nicklewis.org/hook-views-query-alter-how-programatically-alter-views-queries#comment-52901">Earl Miles himself hates this hook</a>, so I suggest staying away from it, but if you&#8217;re interested in what it looks like here&#8217;s a quick Krumo screenshot of what it looks like. This was achieved using the devel module along with the kpr() command. So</p>
<pre>kpr($view->query); 

<div id="attachment_846" class="wp-caption aligncenter" style="width: 377px"><a href="http://blog.raisedeyebrow.com/wp-content/uploads/2010/04/views_query_krumo.gif"><img class="size-full wp-image-846" title="Anatomy of a View Query" src="http://blog.raisedeyebrow.com/wp-content/uploads/2010/04/views_query_krumo.gif" alt="Anatomy of a View Query" width="367" height="373" /></a><p class="wp-caption-text">Anatomy of a View Query</p></div></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Passing a querystring to FormAPI redirect</title>
		<link>http://blog.raisedeyebrow.com/2010/03/passing-a-querystring-to-formapi-redirect/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=passing-a-querystring-to-formapi-redirect</link>
		<comments>http://blog.raisedeyebrow.com/2010/03/passing-a-querystring-to-formapi-redirect/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 21:36:10 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[FormAPI]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=840</guid>
		<description><![CDATA[Here&#8217;s a really quick and useful trick to know.
Sometimes you might want your form to redirect to somewhere and pass a few bits of information in the querystring, so that you can do something when you get there.
For example, I needed a recent form to redirect depending on which checkboxes were checked to build a [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a really quick and useful trick to know.</p>
<p>Sometimes you might want your form to redirect to somewhere and pass a few bits of information in the querystring, so that you can do something when you get there.</p>
<p>For example, I needed a recent form to redirect depending on which checkboxes were checked to build a certain URL:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p840code48'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84048"><td class="code" id="p840code48"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_state</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'values'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filters'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'open'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'o=1'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$form_state</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'values'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filters'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'down'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d=1'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$form_state</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'redirect'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data/search/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$keyword</span><span style="color: #339933;">,</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If &#8220;open&#8221; checkbox is checked, it will redirect to &#8220;data/search/keyword?o=1&#8243;. I then get the value of this using $_GET['o']. Simple eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2010/03/passing-a-querystring-to-formapi-redirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Job Posting closes soon</title>
		<link>http://blog.raisedeyebrow.com/2010/01/job-posting-closes-soon/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=job-posting-closes-soon</link>
		<comments>http://blog.raisedeyebrow.com/2010/01/job-posting-closes-soon/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 15:30:29 +0000</pubDate>
		<dc:creator>Lauren Bacon</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Raised Eyebrow]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=801</guid>
		<description><![CDATA[Vacation’s only just over, but we are hitting the ground running here at Raised Eyebrow, and there are just two days left to apply for our Front-End Developer posting. If you’re passionate about clean code and building fabulous websites, and you’re looking for a position where you can learn from some of the best code [...]]]></description>
			<content:encoded><![CDATA[<p>Vacation’s only just over, but we are hitting the ground running here at Raised Eyebrow, and there are just two days left to apply for our <a href="http://raisedeyebrow.com/jobs/">Front-End Developer posting</a>. If you’re passionate about clean code and building fabulous websites, and you’re looking for a position where you can learn from some of the best code artisans in town and work for clients who are making the world a better place, look no further.</p>
<p>We offer a friendly and healthy workplace, a <a href="http://raisedeyebrow.com/portfolio/">client list</a> that includes national and local nonprofits, mission-driven businesses, and government agencies, and a very competitive salary &amp; benefits package. If you (or someone you know) might be a fit, please check out our <a href="http://www.raisedeyebrow.com/jobs/">posting</a> &amp; send your resume &amp; cover letter in by 5:00 PM on Thursday, January 7th.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2010/01/job-posting-closes-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Drupal to deliver video</title>
		<link>http://blog.raisedeyebrow.com/2009/12/using-drupal-to-deliver-video/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=using-drupal-to-deliver-video</link>
		<comments>http://blog.raisedeyebrow.com/2009/12/using-drupal-to-deliver-video/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 17:11:51 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Nonprofit Tech]]></category>
		<category><![CDATA[Our Clients]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=103</guid>
		<description><![CDATA[There are many ways to skin the cat when it comes to putting video on a Drupal site. I&#8217;ve tried and tested quite a few methods since my first introduction to Drupal 2 years ago. I&#8217;ve used Embedded Media Field as well as Video Filter but finally settled on the combination of FileField with JWPlayer [...]]]></description>
			<content:encoded><![CDATA[<p>There are many ways to skin the cat when it comes to putting video on a Drupal site. I&#8217;ve tried and tested quite a few methods since my first introduction to Drupal 2 years ago. I&#8217;ve used <a href="http://drupal.org/project/emfield">Embedded Media Field </a>as well as <a href="http://drupal.org/project/video_filter">Video Filter</a> but finally settled on the combination of <a href="http://drupal.org/project/filefield">FileField</a> with <a href="http://www.longtailvideo.com/players/jw-flv-player/">JWPlayer</a> or <a href="http://flowplayer.org/">Flowplayer</a> and in some cases the <a href="http://drupal.org/project/media_mover">Media Mover Module</a> for moving files to <a href="http://aws.amazon.com/s3/">Amazon S3</a> storage. I&#8217;m going to use our recent launch of the <a href="http://www.policyalternatives.ca">CCPA website</a> as a case study for how we currently handle video delivery. So let&#8217;s dissect this a little.</p>
<h3>Uploading files</h3>
<p>The video files need to be uploaded before we display them. This is best achieved using the wonderful <a href="http://drupal.org/project/filefield">Filefield</a> Module. This is quite a simple yet powerful module developed/maintained by <a href="http://www.lullabot.com/">Lullabot</a>, <a href="http://quicksketch.org/">Nate Haug</a> (<a href="http://drupal.org/user/35821">quicksketch</a>), whom I&#8217;ve had the pleasure of being trained by at one of their excellent Drupal Theming workshops. Once you install and enable the module you then add a new CCK field, of type &#8220;filefield&#8221;. In our case we have a content type called &#8220;Multimedia&#8221;. We add the field to this content type. You then need to configure the following:</p>
<h4>1. Permitted upload file extensions</h4>
<p style="padding-left: 30px;">In most cases this is relatively straightforward, it&#8217;s just one file type. If you&#8217;re using JWPlayer or Flowplayer it will be FLV. Both these players are built to play Flash Video files (FLV). If you have Quicktime MOV&#8217;s or AVI&#8217;s that you want to upload then you&#8217;ll need to consider different options for playing video. For the purpose of this case study we&#8217;re just uploading FLV files.</p>
<p style="padding-left: 30px;">
<h4>2. File size restrictions</h4>
<p style="padding-left: 30px;">It&#8217;s very important that you set these, otherwise you may end up with users trying to upload 200MB videos, not a very good idea. I set this low as a learning feature for clients. Any reasonably long FLV file that is over 40MB is probably not optimized as well as it should be.</p>
<p style="padding-left: 30px;">
<h4>3. Path Settings</h4>
<p style="padding-left: 30px;">I like to keep all files that admin/editor users upload in a folder called <strong>uploads</strong> so that it&#8217;s easy to manage them later if they need to be exported etc.</p>
<p style="padding-left: 30px;">
<div id="attachment_729" class="wp-caption aligncenter" style="width: 398px"><a href="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-video-admin.gif"><img class="size-full wp-image-729" title="File Field" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-video-admin.gif" alt="File Field" width="388" height="576" /></a><p class="wp-caption-text">Multimedia File Field Settings</p></div>
<p style="padding-left: 30px; text-align: center;">
<h3>Create a placeholder image</h3>
<p>Most video players require some sort of poster/placeholder image to display before the video plays. In this case I created another FileField for the placeholder image. We&#8217;ll use that later on in conjunction with the <a href="http://ftp.drupal.org/files/projects/imagecache-6.x-2.0-beta9.tar.gz">ImageCache module</a> to achieve our desired results.</p>
<h3>Moving Files to Amazon S3</h3>
<p>We&#8217;ve been using Amazon S3 for storing video files on quite a number of sites recently. One reason is that we were looking for a location off the webserver that could deliver the video, without impacting the performance of the server, so that in the event of a traffic spike the webserver wouldn&#8217;t fall over. We could also have used Amazon EC2 or another CDN service for this, however as most of our clients have a very regional (BC) audience. Most CDN&#8217;s have nodes in various locations across the US and Europe and this would have served no real improvement as the nearest cached version will always be in the same place for everyone.</p>
<p>So if they&#8217;re uploading the files directly to the Drupal site, how to the files get delivered from Amazon S3. That&#8217;s where the <a href="http://drupal.org/project/media_mover">Media Mover module</a> comes in. This module has many purposes, but for our needs it simply harvests all the files uploaded via the &#8220;Multimedia&#8221; content type and moves those files to Amazon S3 so that we can deliver them from there.</p>
<p>Download, install and enable the Media Mover module. You&#8217;ll also need an S3 account and will need to set that up via the module setting page. You then need to add a Configuration via https://www.yoursite.ca/admin/build/media_mover/add.</p>
<p>Media Mover has 4 actions which it performs on your files:</p>
<ul>
<li>Harvest &#8211; Define/collect the files you want to perform actions on</li>
<li>Process &#8211; Perform certain actions on the files</li>
<li>Storage &#8211; Where to store files once the actions have been carried out</li>
<li>Complete &#8211; Final actions to perform on the files</li>
</ul>
<p>So in this case we just want to <strong>harvest</strong> all Multimedia files and <strong>store</strong> them on Amazon S3.</p>
<p>So for clarification here Media Mover does <strong>NOT MOVE</strong> the files to Amazon S3, it simply <strong>COPIES</strong> them over to S3 and the original files remain on your server.</p>
<div id="attachment_730" class="wp-caption aligncenter" style="width: 399px"><a href="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-media-mover-settings.gif"><img class="size-full wp-image-730" title="Media Mover Settings" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-media-mover-settings.gif" alt="Media Mover Settings" width="389" height="572" /></a><p class="wp-caption-text">Media Mover Settings</p></div>
<p style="text-align: center;">
<h3>Delivering the moved Video files</h3>
<p>So this is where the Drupal theming trickery comes in. Flowplayer and JWPlayer are both Flash based FLV video players than can be called using Javascript and that&#8217;s exactly what I do on this site. In plain english this is what happens:</p>
<ol>
<li>Output the placeholder image to screen as a link.</li>
<li>Use Javascript so that when the user clicks on the image the video plays.</li>
<li>Deliver the video from the file on Amazon S3 rather than the file on the webserver (Drupal site).</li>
</ol>
<p>We need to modify three files to achive the above:</p>
<ul>
<li>template.php</li>
<li>multimedia.js (a newly created JS file)</li>
<li>node-multimedia.tpl.php (a custom template file for all multimedia types)</li>
</ul>
<h4>template.php file</h4>
<p>Setting up all the variables we&#8217;re going to need to use as well as making the javascript available</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code55'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10355"><td class="code" id="p103code55"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//If there is a file and there is something to display...</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_aspect_ratio'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Aspect ratio handling</span>
    <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aspect_ratio'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_aspect_ratio'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aspect_ratio'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'normal'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'multimedia_type'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'video'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Set the type of multimedia - we also have audio and interactive...</span>
  custom_theme_get_media_mover_files<span style="color: #009900;">&#40;</span><span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'media_mover'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Set the filepath to the media moved filepath...</span>
  drupal_add_js<span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'videoplayerpath'</span> <span style="color: #339933;">=&gt;</span> path_to_theme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts/plugins/flowplayer/flowplayer-3.1.1.swf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'setting'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Set a JS variable to retrieve later...</span>
  drupal_add_js<span style="color: #009900;">&#40;</span>path_to_theme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts/plugins/flowplayer/example/flowplayer-3.1.1.min.js'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Call the player...</span>
  drupal_add_js<span style="color: #009900;">&#40;</span>path_to_theme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts/multimedia.js'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Call the custom JQuery to handle creating the player...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * A function that takes a file object and a media_mover element array and set the file path to
 * its media moved path on Amazon S3 or wherever it moved to.
 *
 * It uses the unique file_id identifier to match file with media_mover file.
 *
 * $file = $variables['file_image'][0];
 * $media_mover = $variables['media_mover'][{id of media mover configuration}];
 *
 * @param 		&amp;$file A Drupal file array (by reference)
 * @param 		$media_mover A media_mover file/element array
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> custom_theme_get_media_mover_files<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$media_mover</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>module_exists<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'media_mover_api'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$media_mover</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If media mover is installed...</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$media_mover</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$media</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Loop through each media_moved file...</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$media</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fid'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fid'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If they match (file id is a unique identifier...</span>
        <span style="color: #000088;">$file</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filepath'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$media</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'complete_file'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Replace the attached file path with the media moved file path...</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// custom_theme_get_media_mover_files()</span></pre></td></tr></table></div>

<p>Let me explain one thing in regards to line 9. I&#8217;ve created a custom function and I&#8217;m passing</p>
<pre>$variables['media_mover'][3]</pre>
<p>to my custom function. When you create a Media Mover configuration and map it to a CCK field, it creates an array in $variables to keep track of the Media Mover object. The array is called <strong>&#8216;media_mover&#8217;</strong> and the number <strong>3</strong> in this case is the ID of the Media Mover configuration.</p>
<h4>node-multimedia.tpl.php</h4>
<p>Set up the template. Create a wrapper div with the placeholder image as the background image (this is run through imagecache) and display the play button as a link with the path set to the path of the Amazon S3 file. This link will also have an id attribute of<strong> &#8216;multimedia&#8217;</strong>. This is necessary as it allows us to attach the player, via Javascript, to this link.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code56'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10356"><td class="code" id="p103code56"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;containing-block&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;video-wrapper&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php print <span style="color: #006699; font-weight: bold;">$aspect_ratio</span>;?&gt;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> ?php <span style="color: #b1b100;">print</span> l<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;img src=&quot;/'</span><span style="color: #339933;">.</span>path_to_theme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/images/ccpa-button-play-large.png&quot; alt=&quot;Play this video&quot; /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field_file</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filepath'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'multimedia'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$multimedia_type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h4>multimedia.js file</h4>
<p>Hook the Flowplayer to the link, with id  of &#8216;multimedia&#8217;, that we created in the template.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code57'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10357"><td class="code" id="p103code57"><pre class="javascript" style="font-family:monospace;">Drupal.<span style="color: #660066;">behaviors</span>.<span style="color: #660066;">showMultimedia</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> interactive_path <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#multimedia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009966; font-style: italic;">/*Get the path to the video*/</span>
  <span style="color: #003366; font-weight: bold;">var</span> interactive_image <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#multimedia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #009966; font-style: italic;">/*Get the path to the placeholder image*/</span>
  interactive_image <span style="color: #339933;">=</span> interactive_image.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span>interactive_image.<span style="color: #660066;">length</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #006600; font-style: italic;">/*Tidying up the interactive image path*/</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#multimedia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'video'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #006600; font-style: italic;">/*If the link has a class of video*/</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#multimedia'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">flowplayer</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/*Initialize the flowplayer and configure the controls*/</span>
    Drupal.<span style="color: #660066;">settings</span>.<span style="color: #660066;">basePath</span> <span style="color: #339933;">+</span> Drupal.<span style="color: #660066;">settings</span>.<span style="color: #660066;">videoplayerpath</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/*Path to the player, gotten from temaplate.php*/</span>
    <span style="color: #009900;">&#123;</span>
      plugins<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
	controls<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #000066;">stop</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
	  backgroundColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#efefef'</span><span style="color: #339933;">,</span>
	  backgroundGradient<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
	  borderRadius<span style="color: #339933;">:</span> <span style="color: #3366CC;">'0px'</span><span style="color: #339933;">,</span>
	  bufferColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#d2d6ab'</span><span style="color: #339933;">,</span>
	  bufferGradient<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
	  buttonColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#777777'</span><span style="color: #339933;">,</span>
	  buttonOverColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#99a134'</span><span style="color: #339933;">,</span>
	  durationColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#cccccc'</span><span style="color: #339933;">,</span>
	  height<span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
	  opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">,</span>
	  progressColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#99a134'</span><span style="color: #339933;">,</span>
	  sliderColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#9999999'</span><span style="color: #339933;">,</span>
	  sliderGradient<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
	  timeBgColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#777777'</span><span style="color: #339933;">,</span>
	  timeColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
	  tooltipColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#000000'</span><span style="color: #339933;">,</span>
	  tooltipTextColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#ffffff'</span><span style="color: #339933;">,</span>
	  volumeSliderColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#777777'</span><span style="color: #339933;">,</span>
	  volumeSliderGradient<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span>
	<span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I hope that was easy to follow. Now there&#8217;s one more thing to cover and that&#8217;s <strong>Aspect Ratio.</strong></p>
<h3>Aspect Ratio</h3>
<p>The issue of aspect ratio is very important when figuring out how to display video. Not so recently YouTube switched all video display to the 16:9 ratio thus setting the stage for the proliferation of the widescreen aspect ratio across the web. So how do you allow the user to upload a video and choose it&#8217;s aspect ratio. I&#8217;m sure there are other ways to do this via Metadata etc, but for our needs on this site I used a CCK field. This is a simple CCK field set with three options:</p>
<ol>
<li>None (defaults to 4:3)</li>
<li>Normal (4:3)</li>
<li>Widescreen (16:9)</li>
</ol>
<div id="attachment_753" class="wp-caption aligncenter" style="width: 396px"><a href="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-aspect-ratio.gif"><img class="size-full wp-image-753" title="ccpa-aspect-ratio" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-aspect-ratio.gif" alt="ccpa-aspect-ratio" width="386" height="487" /></a><p class="wp-caption-text">Aspect Ratio Field Settings</p></div>
<p style="text-align: center;">
<p>We then check the value of this field in <strong>template.php</strong> above:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code58'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10358"><td class="code" id="p103code58"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_aspect_ratio'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//Aspect ratio handling</span>
    <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aspect_ratio'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_aspect_ratio'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$variables</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'aspect_ratio'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'normal'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and set a variable called &#8216;aspect_ratio&#8221; which we apply as a class to the div wrapping the video in the <strong>node-multimedia.tpl.php</strong>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code59'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10359"><td class="code" id="p103code59"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;containing-block&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;video-wrapper&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php print <span style="color: #006699; font-weight: bold;">$aspect_ratio</span>;?&gt;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">print</span> l<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;img src=&quot;/'</span><span style="color: #339933;">.</span>path_to_theme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/images/ccpa-button-play-large.png&quot; alt=&quot;Play this video&quot; /&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field_file</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'filepath'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'html'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'attributes'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'multimedia'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$multimedia_type</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>We have also created <strong>image cache presets</strong> for the placeholder images to account for both aspect ratios. These are named &#8216;multimedia_normal&#8217; and &#8216;multimedia_widescreen&#8217; and these have the appropriate dimensions associated with them:</p>
<div id="attachment_760" class="wp-caption aligncenter" style="width: 441px"><a href="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-imagecache-presets.gif"><img class="size-full wp-image-760" title="ccpa-imagecache-presets" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/12/ccpa-imagecache-presets.gif" alt="ccpa-imagecache-presets" width="431" height="79" /></a><p class="wp-caption-text">Image Cache Presets</p></div>
<p style="text-align: center;">So using the <a href="http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/">amazing article on A List Apart for creating intrinsic ratios for video</a> we use CSS to resize the player based on the aspect ratio chosen by the user.</p>
<h4>style.css file</h4>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p103code60'); return false;">View Code</a> CSS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10360"><td class="code" id="p103code60"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* -- Multimedia -- */</span>
<span style="color: #cc00cc;">#containing-</span><span style="color: #993333;">block</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#video-wrapper</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#video-wrapper</span> div<span style="color: #00AA00;">,</span>
  <span style="color: #cc00cc;">#video-wrapper</span> <span style="color: #993333;">embed</span><span style="color: #00AA00;">,</span>
  <span style="color: #cc00cc;">#video-wrapper</span> object <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#video-wrapper</span>.<span style="color: #993333;">normal</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75%</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#video-wrapper</span><span style="color: #6666ff;">.widescreen</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">padding-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">56.25%</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #00AA00;">*</span> html <span style="color: #cc00cc;">#video-wrapper</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">45px</span><span style="color: #00AA00;">;</span>
    margin-bot\tom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#video-wrapper</span> <span style="color: #cc00cc;">#multimedia</span><span style="color: #6666ff;">.audio</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">1em</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#video-wrapper</span> <span style="color: #cc00cc;">#multimedia</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#video-wrapper</span>.<span style="color: #993333;">normal</span> <span style="color: #cc00cc;">#multimedia</span> img <span style="color: #00AA00;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/*margin:118.5px 0 0;*/</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">32%</span> <span style="color: #933;">42%</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&nbsp;
  <span style="color: #cc00cc;">#video-wrapper</span><span style="color: #6666ff;">.widescreen</span> <span style="color: #cc00cc;">#multimedia</span> img <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #933;">22%</span> <span style="color: #933;">41%</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And the end result looks something like this <a href="http://www.policyalternatives.ca/multimedia/matthew-poverty-and-looking-after-each-other-tough-times">http://www.policyalternatives.ca/multimedia/matthew-poverty-and-looking-after-each-other-tough-times</a>. They haven&#8217;t added any widescreen content yet, just testing content.</p>
<h4>So what are the advantages of doing things this way?</h4>
<ol>
<li>You can easily use any player to play your flash files (all you need to do  is change the path to your player and a few configuration params in multimedia.js)</li>
<li>All your video content is hosted on and delivered from Amazon S3. But there is also a copy on your local server in the event of something going wrong on Amazon S3</li>
<li>You don&#8217;t have to worry about your video looking skewed due to aspect ratio problems</li>
<li>You can add many other apsect ratios pretty quickly</li>
<li>The video file is still downloadable when javascript is not present or disabled</li>
</ol>
<p>I&#8217;d love to get feedback on how other do this, please leave a comment or send me an email and let me know how you deliver Video content on your site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2009/12/using-drupal-to-deliver-video/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>We&#8217;re hiring…</title>
		<link>http://blog.raisedeyebrow.com/2009/12/were-hiring%e2%80%a6/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=were-hiring%25e2%2580%25a6</link>
		<comments>http://blog.raisedeyebrow.com/2009/12/were-hiring%e2%80%a6/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 23:50:10 +0000</pubDate>
		<dc:creator>Lauren Bacon</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Raised Eyebrow]]></category>
		<category><![CDATA[jobs]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=723</guid>
		<description><![CDATA[We&#8217;re expanding our team here at Raised Eyebrow, and this time we&#8217;re looking for a Front-End Developer/Themer &#8212; someone who loves building websites, and is looking to extend their skills with Drupal and WordPress.
Beyond our roster of fantastic, progressive clients, we offer a positive &#38; healthy work environment, beautifully remodeled heritage office space, and a [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re expanding our team here at Raised Eyebrow, and this time we&#8217;re looking for a Front-End Developer/Themer &#8212; someone who loves building websites, and is looking to extend their skills with Drupal and WordPress.</p>
<p>Beyond our roster of fantastic, progressive clients, we offer a positive &amp; healthy work environment, beautifully remodeled heritage office space, and a team of web experts who love what we do and are keen to share knowledge and produce top-notch work.</p>
<p>Intrigued? Know someone who might be a fit? <a href="http://raisedeyebrow.com/jobs/">Check out the job description and get in touch.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2009/12/were-hiring%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic caching function for Drupal 6</title>
		<link>http://blog.raisedeyebrow.com/2009/06/generic-caching-function-for-drupal-6/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=generic-caching-function-for-drupal-6</link>
		<comments>http://blog.raisedeyebrow.com/2009/06/generic-caching-function-for-drupal-6/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 22:02:43 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Geekery]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=455</guid>
		<description><![CDATA[One of the pieces of the Drupal performance puzzle is caching. After weeding out a couple of very demanding sql queries in a busy Drupal site, we decided to make more use of Drupal&#8217;s caching mechanisms to cut down some of the database load.
Back in 2007, Lullabot&#8217;s Jeff Eaton wrote a blog post about caching [...]]]></description>
			<content:encoded><![CDATA[<p>One of the pieces of the Drupal performance puzzle is caching. After weeding out a couple of very demanding sql queries in a busy Drupal site, we decided to make more use of Drupal&#8217;s caching mechanisms to cut down some of the database load.</p>
<p>Back in 2007, Lullabot&#8217;s Jeff Eaton wrote <a href="http://www.lullabot.com/articles/a_beginners_guide_to_caching_data">a blog post about caching</a> including a sample implementation for a caching function. This function was almost exactly what we wanted, but we wanted something more generic—a function that we could use in multiple places in the same module or template.php file without modification.</p>
<p>But in order to use one function to populate and retrieve different cache entries, we needed a way to call an arbitrary function from <em>inside</em> the caching function. Fortunately, PHP supports <a href="http://php.net/manual/functions.variable-functions.php">variable functions</a>. This meant that we could pass the name and parameters of an arbitrary function to our caching function and, when necessary, populate the cache with the output from that function (alternatively, we could also have used the <a href="http://php.net/manual/function.call-user-func.php">call_user_func()</a> function or, like Drupal&#8217;s own <a href="http://api.drupal.org/api/function/theme/6">theme()</a> function, the <a href="http://php.net/manual/function.func-get-args.php">func_get_args()</a> function).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p455code64'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45564"><td class="code" id="p455code64"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * This function returns the cached value for a given cache entry. If the entry does not exist, or if
 * the entry is empty, this function executes the passed function, caches the result and returns it.
 *
 * RENAME THIS FUNCTION FOR USE IN YOUR THEME OR MODULE'S NAMESPACE!
 *
 * @param string $cache_entry The full name of the cache entry--remember the namespace!
 * @param array $function An array with two keys:
 *			string $function['name'] The name of the function
 *			string $function['parameters'] A comma-separated list of parameters
 * @param string $table The name of the cache table to use (see cache_set())
 * @param CACHE_PERMANENT|CACHE_TEMPORARY|unix timestamp $expire Expiry period for this cache entry
 * @param boolean $reset If true, regenerate cache entry; if false, check first
 * @return string|array|object $cache_data The contents of the relevant row in the cache table
 * @see http://api.drupal.org/api/function/cache_set/6
 * @see http://api.drupal.org/api/function/cache_get/6
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> theme_name_theme_cache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$expire</span> <span style="color: #339933;">=</span> CACHE_PERMANENT<span style="color: #339933;">,</span> <span style="color: #000088;">$reset</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  static <span style="color: #000088;">$cache_data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Establish the $cache_data variable for this page load...</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$reset</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If the variable is not set, or if a cache reset has been requested...</span>
  	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$reset</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span> <span style="color: #339933;">=</span> cache_get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_entry</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If $cache-&amp;gt;data is set and reset not requested...</span>
  		<span style="color: #000088;">$cache_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cache</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>data<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Store the current value of $cache-&amp;gt;data...</span>
  	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// If we need to (re) generate the cache...</span>
  		<span style="color: #000088;">$function_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$function</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the function name to call..</span>
  		<span style="color: #000088;">$function_parameters</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$function</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'parameters'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Get the parameters for the function...</span>
  		<span style="color: #000088;">$cache_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$function_name</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function_parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Set cache data to the return value of the function...</span>
  		cache_set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cache_data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$expire</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Cache the resulting value...</span>
  	<span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$cache_data</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Return the cached value...</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// theme_name_theme_cache()</span></pre></td></tr></table></div>

<p>In the above code sample, we pass an array &#8216;$function&#8217; with two keys, &#8216;name&#8217; and &#8216;parameters&#8217; to the function. If the cache is not already populated, we determine the cacheable value with the following line:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p455code65'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45565"><td class="code" id="p455code65"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cache_data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$function_name</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function_parameters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We might use this function like this (for illustration purposes only!):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p455code66'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p45566"><td class="code" id="p455code66"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cache_entry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'theme_name:website_link'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$function</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'l'</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'parameters'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&quot;Drupal Website&quot;, &quot;http://www.drupal.org/&quot;'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$expire</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/time"><span style="color: #990000;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">360</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> theme_name_theme_cache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_entry</span><span style="color: #339933;">,</span> <span style="color: #000088;">$function</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$expire</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This would result in $output being set to the output of the call to Drupal&#8217;s <a href="http://api.drupal.org/api/function/l/6">l()</a> function, <em>and</em> the cache entry &#8216;theme_name:website_link&#8217; in the &#8216;cache&#8217; table being set (or updated) to that value, with an expiry time one week in the future.</p>
<p>Of course the use of a function like this&mdash;like the use of the cache in general&mdash;should be limited to queries or calculations that are very resource intensive. The example above would actually <em>add</em> overhead compared to simply calling l() directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2009/06/generic-caching-function-for-drupal-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strange permissions on Coda file uploads</title>
		<link>http://blog.raisedeyebrow.com/2009/06/strange-permissions-on-coda-file-uploads/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=strange-permissions-on-coda-file-uploads</link>
		<comments>http://blog.raisedeyebrow.com/2009/06/strange-permissions-on-coda-file-uploads/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 15:55:18 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[Coda]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=445</guid>
		<description><![CDATA[I&#8217;ve been working with Coda now for a few weeks and it&#8217;s been going pretty smoothly. There are a few bugs in the software, or they may even be feature requests :) When creating a file locally and subsequently uploading it to the server the file permissions are set to 700. This was causing all [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with <a href="http://www.panic.com/coda">Coda</a> now for a few weeks and it&#8217;s been going pretty smoothly. There are a few bugs in the software, or they may even be feature requests :) When creating a file locally and subsequently uploading it to the server the file permissions are set to <strong>700</strong>. This was causing all sorts of weirdness on a Drupal site that I&#8217;m currently theming. This is definitely a bug, however it&#8217;s pretty easy to overcome. In &#8220;Preferences -&gt; Transfers&#8221; there is an option to &#8220;Set permissions on upload&#8221;. Set this to <strong>644</strong>. This is the recommended permission for files in your theme folder in Drupal. You should now have no more problems when uploading files. If you&#8217;ve already uploaded files, it&#8217;s pretty easy to change via the command line using the <a href="http://en.wikipedia.org/wiki/Chmod">chmod</a> command:</p>
<pre>chmod 644 filename.php</pre>
<p><img class="aligncenter size-full wp-image-447" title="coda-pref-screen" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/06/coda-pref-screen.gif" alt="coda-pref-screen" width="391" height="105" /></p>
<p><img class="aligncenter size-full wp-image-446" title="coda-file-perms" src="http://blog.raisedeyebrow.com/wp-content/uploads/2009/06/coda-file-perms.jpg" alt="coda-file-perms" width="480" height="206" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2009/06/strange-permissions-on-coda-file-uploads/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Theming multiple value CCK fields in Views</title>
		<link>http://blog.raisedeyebrow.com/2009/06/theming-multiple-value-cck-fields-in-views/#utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=theming-multiple-value-cck-fields-in-views</link>
		<comments>http://blog.raisedeyebrow.com/2009/06/theming-multiple-value-cck-fields-in-views/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 20:44:21 +0000</pubDate>
		<dc:creator>Colin Calnan</dc:creator>
				<category><![CDATA[Code and Coding]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Geekery]]></category>
		<category><![CDATA[CCK]]></category>
		<category><![CDATA[Theming]]></category>
		<category><![CDATA[Views]]></category>

		<guid isPermaLink="false">http://blog.raisedeyebrow.com/?p=423</guid>
		<description><![CDATA[I&#8217;m working on a site right now that has a Publications content type, which in turn has a multiple value CCK text field for Author. If more than one author was input I  needed them to display as a comma separated list in a view. How do you go about modifying this? My first [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a site right now that has a <strong>Publications</strong> content type, which in turn has a multiple value CCK text field for <strong>Author</strong>. If more than one author was input I  needed them to display as a comma separated list in a view. How do you go about modifying this? My first stop was the theming info link in Views, and that helped me narrow it down to a template file to use, <code>views-view-field.tpl.php()</code>. But that file didn&#8217;t allow me to modify the individual fields without getting a whole load of other HTML in the bargain.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p423code69'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42369"><td class="code" id="p423code69"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #666666; font-style: italic;">// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $</span>
 <span style="color: #009933; font-style: italic;">/**
  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row-&gt;{$field-&gt;field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">print</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> Using the brilliant &#8220;Theme developer&#8221; extension of the <a href="http://drupal.org/project/devel">Devel module</a> I was able to pin point the theme function &#8211; <code>theme_content_view_multiple_field</code> that would allow me to modify the actual value of the field without the wrapper div&#8217;s etc (I&#8217;m not getting into the <a href="http://www.angrydonuts.com/views-panels-economy-of-front-end-code-and-classes-and-namespace">views divitis argument</a>. </p>
<p>Here&#8217;s a quick snippet of code to place in your template.php file of your theme to allow you to modify multiple value fields:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p423code70'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42370"><td class="code" id="p423code70"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> custom_theme_content_view_multiple_field<span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #339933;">,</span> <span style="color: #000088;">$field</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$field</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'field_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// If this is the author field then we need to comma separate the authors</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'field_author'</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;div class=&quot;field-item&quot;&gt;'</span><span style="color: #339933;">.</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$item</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;div class=&quot;field-item&quot;&gt;'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$item</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I used a <code>switch</code> statement because I have a lot of other multiple fields that need to be themed in different ways and it&#8217;s nicer than a whole load of if statements. Also <code>switch</code> provides a default state if a condition isn&#8217;t met.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.raisedeyebrow.com/2009/06/theming-multiple-value-cck-fields-in-views/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
