<?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>CoffeePowered &#187; PHP</title>
	<atom:link href="http://coffeepowered.co.uk/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeepowered.co.uk</link>
	<description>The online ramblings of Paul Stanton</description>
	<lastBuildDate>Mon, 09 Jan 2012 08:36:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Smarter images in Jadu CMS</title>
		<link>http://coffeepowered.co.uk/2011/09/smarter-images-in-jadu-cms/</link>
		<comments>http://coffeepowered.co.uk/2011/09/smarter-images-in-jadu-cms/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 18:17:35 +0000</pubDate>
		<dc:creator>Stanton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Jadu]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://coffeepowered.co.uk/?p=665</guid>
		<description><![CDATA[An exploration into low impact development within a CMS, building lightweight functionality to improve how we use imagery within web pages that can be replaced as and when the platform evolves.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been looking at ways to enhance certain content types that we deliver through <a href="http://jadu.net">Jadu CMS</a>, my focus at the moment has been through the use of images within content, whether that be News, Events, Documents or Homepages. This started with a re-alignment of the news section on the <a href="http://www.leeds.ac.uk">University of Leeds&#8217; corporate website</a>.</p>
<h3>Multiple image sizes</h3>
<p>We&#8217;ve traditionally been hampered by the fact that Jadu doesn&#8217;t yet have the ability to generate multiple image sizes from a single base image so if I wanted to have a large document level image, and a smaller thumbnail within a list view, I&#8217;d either have to resize with CSS which results in degraded image quality in some browsers, or have the editors upload multiple versions of each image to the CMS and figure some way to switch between these programatically. Neither of these options were desirable, so we built an image helper that, instead of inserting the <img alt="" /> element directly into the front end script, we pass the image name to the helper, along with the desired attributes such as a specific height and/or width, the helper then resizes the native image, and caches it for future use.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">print</span> imageGenerator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$imageURL</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'alt'</span> <span style="color: #339933;">=&gt;</span> getImageProperty<span style="color: #009900;">&#40;</span><span style="color: #000088;">$imageURL</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'altText'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'contentimage'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">340</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Take the <a href="http://www.leeds.ac.uk/news/30292/press_releases">UoL Press Releases news section</a>, for example, we can now elegantly deliver multiple image sizes depending on our needs, without either the bandwidth overhead of resizing large images, or the administration headache of managing multiple images within the CMS, and the image helper can be used anywhere in our front end templates.</p>
<blockquote><p>we can now elegantly deliver multiple image sizes depending on our needs, without either the bandwidth overhead of resizing large images, or the administration headache of managing multiple images within the CMS</p></blockquote>
<p style="text-align: center;"><a href="http://coffeepowered.co.uk/wp-content/uploads/2011/09/Screen-Shot-2011-09-02-at-18.48.53.png"><img class="size-full wp-image-694 aligncenter" style="border-style: initial; border-color: initial; border-width: 0px;" title="Screen Shot 2011-09-02 at 18.48.53" src="http://coffeepowered.co.uk/wp-content/uploads/2011/09/Screen-Shot-2011-09-02-at-18.48.53.png" alt="" width="520" height="236" /></a></p>
<h3>Image attribution</h3>
<p>The next issue I&#8217;ve been looking into recently is that of image attribution, again, Jadu doesn&#8217;t currently have any options within the multimedia gallery module to attach attribution data to multimedia items. If the UoL wants to use a Creative Commons licensed image, there&#8217;s no way, other than adding the attribution data into the body content. I figured there was a better way to do this.</p>
<p>The Multimedia Module within Jadu is the central place to manage all types of multimedia content, for this example I&#8217;ll focus on images. One of the core concepts of a CMS is that an image only needs to exist once, but can then be included in any other piece of content. The multimedia manager is a relatively recent addition to Jadu CMS, and as such, provides the bare minimum of features required so I needed to find a way to link attribution data to an image without modifying the core CMS code.</p>
<h3>Enter machine tags</h3>
<p>As we can apply tags to each image, this was a perfect place to use machine tags &#8211; a piece of text which contains machine-readable semantic information &#8211; to include the relevant licensing information. Machine tags are composed of three parts; a namespace, a predicate, and a value. For example:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">license:creativecommons=&quot;© 2009 Greg Grossmeier, used under a Creative <br />
Commons Attribution-ShareAlike license: <br />
http://creativecommons.org/licenses/by-sa/3.0/&quot;</div></td></tr></tbody></table></div>
<p>Initially, I hit a brick wall in that the &#8216;tag&#8217; column within the JaduMultimediaTags table was limited to varchar(50), a sensible default for most english word based tags but kills the chance of using machine tags which are typically longer. Frustrated, <a href="http://twitter.com/#!/stanton/status/108904319063048193">I moaned a bit on Twitter</a>, and within minutes had an email from <a href="http://twitter.com/ap49">@ap49</a> at Jadu asking what they could do to help, and later that night they&#8217;d modified the Jadu trunk to change the tag column to varchar(255) which is much more useful, for me at least!</p>
<p>So, armed with our enlarged tag column, I set about writing a function which would parse an images&#8217; tags for our license machine tag, extract the relevant components, and spit out some meaningful markup to the browser which we could style as needed. You can <a href="https://gist.github.com/599db38634c9ba2418d0">get the Image License function from GitHub</a>, in case it&#8217;s of use to anyone.</p>
<p>For the University of Leeds&#8217; corporate site, I wanted to include a small copyright, or creative commons symbol in the corner of the image, which would then expand to show the license detail when hovered over. I chose to use CSS for the hover animations rather than javascript and I&#8217;m quite pleased with the result. Modern browsers get a nicely animated transition and older browsers simply display the information on hover. The license function can be extended to provide the data in different formats depending on the content&#8217;s needs. For a homage widget like the one below, a subtle copyright icon works well, for images, illustrations or figures within a document we may prefer to list all of the attribution data at the foot of the document in a list of references.</p>
<p>Here&#8217;s the attribution implemented on our document image component:<br />
<video src="http://coffeepowered.co.uk/wp-content/uploads/2011/09/attribution.flv.video_.mp4" controls="controls"></video></p>
<h3>Low impact development</h3>
<p>Working in this way, we&#8217;re able to extend the functionality of the CMS without modifying the core product (which would complicate our upgrade cycle) because we&#8217;re making the assumption that these features will eventually be provided natively within the CMS. These functions enhance how we manage and use images, yet are lightweight enough to be stripped out and replaced by native functions if and when they become available.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeepowered.co.uk/2011/09/smarter-images-in-jadu-cms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://coffeepowered.co.uk/wp-content/uploads/2011/09/attribution.flv.video_.mp4" length="211086" type="video/mp4" />
		</item>
		<item>
		<title>Using PHP&#8217;s alternate syntaxes</title>
		<link>http://coffeepowered.co.uk/2010/01/using-phps-alternate-syntaxes-to-aid-code-readability/</link>
		<comments>http://coffeepowered.co.uk/2010/01/using-phps-alternate-syntaxes-to-aid-code-readability/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 08:00:57 +0000</pubDate>
		<dc:creator>Stanton</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[p52]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[control structures]]></category>
		<category><![CDATA[elseif]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://coffeepowered.co.uk/?p=348</guid>
		<description><![CDATA[If you&#8217;ve ever worked with PHP you&#8217;ll be more than familiar with the use of if/elseif/else conditional statements, these statements often become large and unweildy, especially when HTML is mixed in with PHP, causing a problems with code readability. There are a couple of alternate ways to write these statements which can improve your code.
if..endif [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever worked with PHP you&#8217;ll be more than familiar with the use of if/elseif/else conditional statements, these statements often become large and unweildy, especially when HTML is mixed in with PHP, causing a problems with code readability. There are a couple of alternate ways to write these statements which can improve your code.</p>
<h3>if..endif syntax</h3>
<p>We&#8217;ll start with a simple example of a &#8216;regular&#8217; statement which includes a foreach and an if statement which is extremely common among PHP scripts.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_expression</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$expression1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Result1'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Result2'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Now, just for funzies, let&#8217;s make this a tiny bit more complex and mix in some HTML markup in there for good measure, just like you&#8217;d see in, say, a WordPress template where we&#8217;re jumping back and forth between HTML and PHP mode.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_expression</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$expression1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Result1&lt;/p&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?</span> &nbsp;<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Result2&lt;/p&gt; <br />
<span style="color: #000000; font-weight: bold;">&lt;?</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span> <br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>See the closing braces on lines 9 &#038; 10? With a relatively small example like this you can scan back through the code to see wether they close the if or the elseif. Now imagine a much larger statement, the only way to find out what the closing brace actually closes is to either pay attention to the tab levels (providing the tab levels have been maintained properly), or to read back through the whole statement and match them up. There is an alternate syntax which makes life a little easier. </p>
<h4>Alternate syntax</h4>
<p>By using the alternate if..elseif syntax we can replace those closing braces with something a little more useful.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$array_expression</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$expression1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Result1&lt;/p&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?</span> &nbsp;<span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;Result2&lt;/p&gt; <br />
<span style="color: #000000; font-weight: bold;">&lt;?</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>This alternate syntax is expecially useful when mixing PHP and HTML and works for <strong>if/while/for</strong> control structures.</p>
<h3>Ternary operator</h3>
<p>For simple evaluations you can use the ternary operator which is a slimline version of an if/else statement, for example, the following statement which pluralises a string based on an integer variable.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$comments</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'comment'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$comments</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'comments'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$comments</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h4>Alternate syntax</h4>
<p>Using the ternary operator this can be condensed to a single line:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?</span><br />
<span style="color: #000088;">$comments</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'comment'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'comments'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$comments</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h3>Have a try!</h3>
<p>By learning and adopting these alternate syntaxes you will ensure that your code remains readable and maintainable, especially if you&#8217;re writing code to be used by other people. Give them a try!</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeepowered.co.uk/2010/01/using-phps-alternate-syntaxes-to-aid-code-readability/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

