<?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>Joey Rivera &#187; Flash</title>
	<atom:link href="http://www.joeyrivera.com/category/categories/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joeyrivera.com</link>
	<description>Blogging about PHP, Actionscript, MySQL, and other interests.</description>
	<lastBuildDate>Fri, 02 Dec 2011 03:55:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to build an Adobe AIR Badge</title>
		<link>http://www.joeyrivera.com/2010/how-to-build-adobe-air-badge/</link>
		<comments>http://www.joeyrivera.com/2010/how-to-build-adobe-air-badge/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 14:52:07 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[badge]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=593</guid>
		<description><![CDATA[NOTE: This article was written with AIR 1.5 in mind. I can&#8217;t say if the following information will work as intended on AIR 2. If you already know how to build you own Adobe AIR badge, you won&#8217;t find much new information here. This post is more to inform users that you can in fact [...]]]></description>
			<content:encoded><![CDATA[<p>NOTE: This article was written with AIR 1.5 in mind. I can&#8217;t say if the following information will work as intended on AIR 2. If you already know how to build you own Adobe AIR badge, you won&#8217;t find much new information here. This post is more to inform users that you can in fact create your own AIR badge if you didn&#8217;t already know. When I first started working with AIR, I learned about the badge but as far as I was concerned, the badge was a black box who&#8217;s functionality was designed to remain mysterious.  The badge I used was the one <a title="Grant Skinner's website" href="http://www.gskinner.com/">Grant Skinner</a> created and you can get more information from <a title="About the Badge" href="http://www.adobe.com/devnet/air/articles/badge_for_air.html">Adobe</a> on how to use it. For a long time this badge worked well but finally the time has come where I need to make it behave a bit differently so I decided to do a bit of research.</p>
<p>After doing a few Google searches, I found a link to Adobe&#8217;s site with all the technical information on how to create your own AIR badge! I was completely surprised that I never even thought about the possibility that I could write my own badge&#8230; not sure what I was thinking. Nothing is more fun that reinventing the wheel right? After looking at the <a title="Adobe AIR 1.5 Badge" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7e15.html">specifications</a>, I realized it wasn&#8217;t too bad and started to plan out a logical flow on how my badge was supposed to work.</p>
<h2>Requirements</h2>
<p>My current badge has the following issue: a user installs my app through the badge, every time after that point that the user comes back to the badge, they have to &#8216;install&#8217; the app again even thought it was already installed. Instead, I want the badge to sense if the application is installed and if so, launch it (I haven&#8217;t spend much time looking into Grants Badger app but I think it&#8217;ll let you do this as well if used correctly&#8230; I think). The logic for the new badge should be:</p>
<ol>
<li>Check if AIR is installed (If not, install)</li>
<li>Check if my app is installed (If not install)</li>
<li>Launch my app</li>
</ol>
<h2>Setup</h2>
<p>I&#8217;ll be using Flash CS3 since that&#8217;s what I have available to me for this tutorial and I am making the assumption you already know how to use Flash, understand ActionScript 3, and have an AIR app to test. Open Flash and create a new AS3 file and save it in some folder. Now attach a class called &#8216;Badge&#8217; to your file that extends MovieClip. Your class should look like the following:</p>
<div class="dean_ch" style="white-space: wrap;">package<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Badge <span class="kw3">extends</span> <span class="kw3">MovieClip</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Badge<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&#8216;hi&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I added a trace in the constructor so I can make sure my class is working. When you test your badge you should see &#8216;hi&#8217; in your output window. Now we are ready to continue.<span id="more-593"></span></p>
<h2>air.swf</h2>
<p>Adobe created a file called air.swf that resides on their server at http://airdownload.adobe.com/air/browserapi/air.swf. This file contains all the functionality required to interact with AIR files through your badge. It can check to see if you have air installed on the local machine and install it, it can check to see if an air app is installed on the local machine and install it or launch. It does it all so the first step in creating a badge is to load this file so you can call it&#8217;s methods.</p>
<p>All we need to do is create a Loader instance, add an event listener to alert us when it&#8217;s done loading the air.swf file and call the load method passing in the url as a URLRequest. The code is below with all the required imports:</p>
<div class="dean_ch" style="white-space: wrap;">package<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">Event</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">Loader</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">net</span>.<span class="me1">URLRequest</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Badge <span class="kw3">extends</span> <span class="kw3">MovieClip</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _air:<span class="kw3">Object</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Badge<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> loader:Loader = <span class="kw2">new</span> Loader<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">contentLoaderInfo</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">INIT</span>, airSwfLoaded<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="kw3">load</span><span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span><span class="st0">&#8216;http://airdownload.adobe.com/air/browserapi/air.swf&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> airSwfLoaded<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._air = <span class="kw3">e</span>.<span class="kw3">target</span>.<span class="me1">content</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="kw3">this</span>._air.<span class="me1">getStatus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Test the above code and in your output you should see one of the following <a title="Different status responses" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7e15.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7c98">status responses</a> (taken from Adobe&#8217;s site):</p>
<table border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
<th id="d17e28828" width="NaN%" valign="top">String value</th>
<th id="d17e28831" width="NaN%" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td width="NaN%" valign="top"><samp>&#8220;available&#8221;</samp></td>
<td width="NaN%" valign="top">The runtime can be installed on this computer but currently it is not installed.</td>
</tr>
<tr>
<td width="NaN%" valign="top"><samp>&#8220;unavailable&#8221;</samp></td>
<td width="NaN%" valign="top">The runtime cannot be installed on this computer.</td>
</tr>
<tr>
<td width="NaN%" valign="top"><samp>&#8220;installed&#8221;</samp></td>
<td width="NaN%" valign="top">The runtime is installed on this computer.</td>
</tr>
</tbody>
</table>
<h2>Handling the response</h2>
<p>Now that we have a status, we can tell the badge to handle it in the appropriate manner. The first status we need to handle is &#8216;available&#8217;. This means AIR is not installed on the machine but can be. By default, AIR needs to be installed before installing an AIR app so in this scenario, all we need to call is install on our app and AIR will be installed first automatically. Here is the <strong>tricky part</strong>, we can&#8217;t automatically call install on an AIR app, it is not allowed. <span style="text-decoration: underline;">You can only call install via a user initiated event such as a button click</span>.</p>
<p>If the status is unavailable, then we can&#8217;t do much other than let the user know they can&#8217;t install AIR on their machine. If the status is installed, then the user already has AIR installed and we need to check if they have our app installed. If they do not, we install it and let them launch it. If they do have it installed, we let them launch it. Below is the complete code that does all the above:</p>
<div class="dean_ch" style="white-space: wrap;">package<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">Event</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">Loader</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">MouseEvent</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">net</span>.<span class="me1">URLRequest</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> fl.<span class="me1">controls</span>.<span class="kw3">Button</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Badge <span class="kw3">extends</span> <span class="kw3">MovieClip</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _air:<span class="kw3">Object</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _myButton:<span class="kw3">Button</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> const APPID:<span class="kw3">String</span> = <span class="st0">&#8216;YOUAPPID&#8217;</span>; <span class="co1">// replace with your app id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> const PUBID:<span class="kw3">String</span> = <span class="st0">&#8217;123123123123123123123123.1&#8242;</span>; <span class="co1">// replace with your publisher id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> const APPURL:<span class="kw3">String</span> = <span class="st0">&#8216;http://www.yourdomain.com/YOUAIRAPP.air&#8217;</span>; <span class="co1">// replace with your app url</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> const APPRUNTIME:<span class="kw3">String</span> = <span class="st0">&#8217;1.5&#8242;</span>; <span class="co1">// I&#8217;m using 1.5 as a minimum requirement for this example</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> const AIRSWF:<span class="kw3">String</span> = <span class="st0">&#8216;http://airdownload.adobe.com/air/browserapi/air.swf&#8217;</span>; <span class="co1">// adobe&#8217;s file</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Badge<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> loader:Loader = <span class="kw2">new</span> Loader<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">contentLoaderInfo</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">INIT</span>, airSwfLoaded<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="kw3">load</span><span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">AIRSWF</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._myButton = <span class="kw2">new</span> <span class="kw3">Button</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addChild</span><span class="br0">&#40;</span><span class="kw3">this</span>._myButton<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> airSwfLoaded<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._air = <span class="kw3">e</span>.<span class="kw3">target</span>.<span class="me1">content</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span><span class="br0">&#40;</span><span class="kw3">this</span>._air.<span class="me1">getStatus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;available&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// install app</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">installApp</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;unavailable&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// AIR not supported on this system</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;installed&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// check if app is installed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">checkApp</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> checkApp<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._air.<span class="me1">getApplicationVersion</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">APPID</span>, <span class="kw3">this</span>.<span class="me1">PUBID</span>, checkAppCallback<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> checkAppCallback<span class="br0">&#40;</span><span class="kw3">version</span>:<span class="kw3">String</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">version</span> == <span class="kw2">null</span><span class="br0">&#41;</span> <span class="co1">// app is not installed</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._myButton.<span class="me1">label</span> = <span class="st0">&quot;Install&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._myButton.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">CLICK</span>, installApp<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._myButton.<span class="me1">label</span> = <span class="st0">&quot;Launch&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._myButton.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">CLICK</span>, launchApp<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> installApp<span class="br0">&#40;</span><span class="kw3">e</span>:Event = <span class="kw2">null</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._air.<span class="me1">installApplication</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">APPURL</span>, <span class="kw3">this</span>.<span class="me1">APPRUNTIME</span>, <span class="kw2">new</span> <span class="kw3">Array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> launchApp<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>._air.<span class="me1">launchApplication</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">APPID</span>, <span class="kw3">this</span>.<span class="me1">PUBID</span>, <span class="kw2">new</span> <span class="kw3">Array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I added a button that depending on the status will display the appropriate label and call the corresponding method. If the status is available we know that my app is probably not installed (since AIR is not installed) so I tell my button to call the install app method. The install app method calls the installApplication method from the air.swf file and passes the following 3 paramets: the url of the app you are trying to install, the AIR runtime required for this app, and an array of parameters required for this app if any (make sure to modify those variables to match your needs). When a user clicks on the button, they will be asked to install AIR, install the AIR application, and allowed to start it.</p>
<p>If the status is installed, we call the check app method to see if our app is already installed on the users machine by seeing what version of the app is installed. A null version means that the app is not installed. If installed, our button will launch the application and if not, we call the install app method that I just described above. The launch application method takes in three parameters: the application id, the publisher id and a array of parameters if the application requires it. <span style="text-decoration: underline;">NOTE</span>: Your AIR application MUST have <a title="BrowserInvocation Information from Adobe" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118676a5d46-8000.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7e19">allowBrowserInvocation</a> set to true in it&#8217;s AIR application descriptor xml file for the badge to be able to launch your AIR application.</p>
<h5>Finding your application id</h5>
<p>Simply go to your AIR application&#8217;s folder and open the descriptor xml file. Look for the id node and that&#8217;s the id you want to use.</p>
<h5>Finding your publisher id</h5>
<p>This one was a bit trickier. First you need to install your AIR application on your system. Once installed, go to the folder where your application was installed and inside there will be a META-INF folder. Inside that folder there is a AIR folder with a publisherid file. Open that file and the string inside is your publisher id. For example: c:\program files\myairapp\META-INF\AIR\publisherid</p>
<p>That should do it. At this point you should have a basic working AIR badge. Now you can customize it any ways you like. Hope you all find this helpful! Feel free to leave any questions or comments.</p>
<h2>Resources:</h2>
<p><a title="Adobe Links" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7e15.html">http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7e15.html</a></p>
<p><a title="Adobe Links" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff1.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7c9c">http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff1.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7c9c</a></p>
<p><a title="Adobe Links" href="http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff0.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7cca">http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff0.html#WS5b3ccc516d4fbf351e63e3d118666ade46-7cca</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2010/how-to-build-adobe-air-badge/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Automate Db Model Creation with Zend_CodeGenerator_Php_Class</title>
		<link>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/</link>
		<comments>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:47:35 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[codegenerator]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend_codegenerator_php_class]]></category>
		<category><![CDATA[zend_db_table_abstract]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=462</guid>
		<description><![CDATA[I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework. I&#8217;ll be using [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a new tool at work that will automate several processes for a few employees so they don&#8217;t have to spend too much time doing very repetitive tasks. This tool has to do a good bit of database manipulation so I&#8217;ve decided I&#8217;ll build it in PHP using Zend Framework.</p>
<p>I&#8217;ll be using <a title="Zend Db Table Docs" href="http://framework.zend.com/manual/en/zend.d.table.html">Zend_Db_Table_Abstract</a> to communicate with the db tables from my project and I&#8217;ll be creating a model for each table as well to store and manipulate data. I&#8217;ll be working with lots of tables in the database and many have lots of fields.</p>
<p>I start by opening up Zend Studio on one monitor and SQL Query Analyzer on the other and get to work. The first table I want to work with is the &#8216;Student&#8217; table. I create a new file in my project called Student.php. Place it on my models/DbTable folder and inside I simply have to declare &#8216;_name&#8217; as a protected property with the value &#8216;Student&#8217; and extend &#8216;Zend_Db_Table_Abstract&#8217;. Easy enough but now I want to create the model I will be using to convert the database data into workable objects through my mapper class.</p>
<h4>Problem</h4>
<p>I create a new file called &#8216;Student.php&#8217; and save it to my models folder. I open the file up and now I have to create a property (it&#8217;s actually an array _data with all properties defined as keys inside) for each field in the Student table&#8230; all 50 of them! I have to be careful to name each correctly as well as to not accidentally miss some field. It ends up being a time consuming process and inefficient so I start looking for a better way to accomplish this.<span id="more-465"></span></p>
<h4>Solution</h4>
<p>This is where <a title="Zend CodeGenerator Php Class" href="http://framework.zend.com/manual/en/zend.codegenerator.html">Zend_CodeGenerator_Php_Class</a> comes in. This component of Zend allows you to create php code on the fly. My theory was, instead of manually creating all these files and typing all the field names (very time consuming with lots of room for error), I could use Zend_CodeGenerator_Php to create all the content of my classes and echo it on the screen so I could copy/paste. Once I had this working, I took it to the next step and included the process of saving that content to files on my project folder. After working on this last night, everything worked like a charm and my classes are easily and quickly created, mapped directly to my database structure, by simply calling one method. No room for error either and I can run this at any time!</p>
<h4>Code</h4>
<p>I&#8217;ll explain the code below. <a title="File of code below" href="http://www.joeyrivera.com/blog_files/462/file.txt">You can download the file if you like</a>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$db</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$paths</span> = Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;paths&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$temp_path</span> = <span class="re0">$paths</span><span class="br0">&#91;</span><span class="st0">&#8216;temp&#8217;</span><span class="br0">&#93;</span>;</p>
<p><span class="co1">// get all tables in db</span><br />
<span class="re0">$tables</span> = <span class="re0">$db</span>-&gt;<span class="me1">listTables</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$tables</span> <span class="kw1">as</span> <span class="re0">$table</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// need to remove underline first, ucwords, and then remove space</span><br />
&nbsp; &nbsp; <span class="re0">$name</span> = <a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216; &#8216;</span>, <span class="st0">&#8221;</span>, <a href="http://www.php.net/ucwords"><span class="kw3">ucwords</span></a><span class="br0">&#40;</span><a href="http://www.php.net/str_replace"><span class="kw3">str_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216;_&#8217;</span>, <span class="st0">&#8216; &#8216;</span>, <span class="re0">$table</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// create new class generator</span><br />
&nbsp; &nbsp; <span class="re0">$class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// configure docblock</span><br />
&nbsp; &nbsp; <span class="re0">$docblock</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; model&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// set name and docblock</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="re0">$docblock</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// get all fields</span><br />
&nbsp; &nbsp; <span class="re0">$fields</span> = <span class="re0">$db</span>-&gt;<span class="me1">describeTable</span><span class="br0">&#40;</span><span class="re0">$table</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// want to track primary ids for table</span><br />
&nbsp; &nbsp; <span class="re0">$primary</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// add to columns each field with a default value</span><br />
&nbsp; &nbsp; <span class="re0">$columns</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$fields</span> <span class="kw1">as</span> <span class="re0">$field</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// if int field default to 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$columns</span><span class="br0">&#91;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span><span class="br0">&#93;</span> =<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;DATA_TYPE&#8217;</span><span class="br0">&#93;</span>, <span class="st0">&#8216;int&#8217;</span><span class="br0">&#41;</span> !== <span class="kw2">false</span> ? <span class="nu0">0</span> : <span class="st0">&#8221;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// track primary field(s) for table</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;PRIMARY&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$primary</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$field</span><span class="br0">&#91;</span><span class="st0">&#8216;COLUMN_NAME&#8217;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="co1">// add data array property to class</span><br />
&nbsp; &nbsp; <span class="re0">$class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_data&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$columns</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;array Maps to all fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create zend_db_table_abstract</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span> = <span class="kw2">new</span> Zend_CodeGenerator_Php_Class<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&#8216;Ct_Model_DbTable_&#8217;</span> . <span class="re0">$name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setDocblock</span><span class="br0">&#40;</span><span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;shortDescription&#8217;</span> =&gt; <span class="re0">$name</span> . <span class="st0">&#8216; db table abstract&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;author&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;Joey Rivera&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setExtendedClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Zend_Db_Table_Abstract&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_name&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <span class="re0">$table</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;string Name of db table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db_class</span>-&gt;<span class="me1">setProperty</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;_primary&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;visibility&#8217;</span> =&gt; <span class="st0">&#8216;protected&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;defaultValue&#8217;</span> =&gt; <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$primary</span><span class="br0">&#41;</span> &gt; <span class="nu0">1</span> ? <span class="re0">$primary</span> : <span class="re0">$primary</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;docblock&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;tags&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_CodeGenerator_Php_Docblock_Tag<span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;name&#8217;</span> =&gt; <span class="st0">&#8216;var&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;description&#8217;</span> =&gt; <span class="st0">&#8216;string or array of fields in table&#8217;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> . PHP_EOL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents<span class="br0">&#40;</span><span class="re0">$temp_path</span> . <span class="st0">&#8216;DbTable/&#8217;</span> . <span class="re0">$name</span> . <span class="st0">&#8216;.php&#8217;</span>, <span class="st0">&#8216;&lt;?php&#8217;</span> . PHP_EOL . <span class="re0">$db_class</span>-&gt;<span class="me1">generate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I start out by getting some needed variables such as my $db adapter and paths that I will use to store my files in. For this example I am using MySQL (5.1.x) database and Zend Framework 1.9.5. If you are not familiar on how to setup your database adapter <a title="Zend Db Adapter Doc" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor');" href="http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.connecting.constructor">here is the documentation</a>.</p>
<p>The next step is to call listTables on the db adapter to get an array of all the tables in the database. We want to loop through the array and create two files for each table: an object model and a db table abstract. Each file has a corresponding instance of Zend_CodeGenerator_Php_Class. The first one we create in the code is for the object model. The docblock is the optional comments for the class if you want them. setName is the name you want for the class. In this case I&#8217;m using &#8216;Ct_Model_$name&#8217; where name is the name of the table without any underscores and with the first letter of each word in uppercase. For example, if the table name is: &#8216;user_info&#8217;, name is &#8216;UserInfo&#8217;.</p>
<p>The next information I need is all the fields for that table so I can add them to my data array for each model. Calling describeTable(&#8216;tableName&#8217;) returns that. Now that I have my array with all the fields information for this first table, so I loop through to:</p>
<ul>
<li>assign names and default value(s) of 0 for an int field type or null for anything else into the columns array.</li>
<li>track the primary key(s) for each table. Some tables can have more than one.</li>
</ul>
<p>setProperty is where we create the protected data property array for this class. The name of the property is &#8216;_data&#8217; and the default value is the array we just created &#8216;columns&#8217;. You can also add a docblock to the property which I did here. Since there is no Zend_CodeGenerator_Php_Docblock_Tag_Property I just used the default Tag class passing it name &#8216;var&#8217;. Had I used Zend_CodeGenerator_Php_Docblock_Tag_Param, my comments would have shown &#8216;@param&#8217; when it is really &#8216;@var&#8217;. Try it and you&#8217;ll see what I mean.</p>
<p>Our first class for the model is now complete! I&#8217;m echoing it to the screen so I can see it when I view-source or just use Zend_Debug::dump() instead to see it formatted. The next line saves the newly generated code to a file at a temp_path location. It&#8217;s important to add &#8216;&lt;?php&#8217; before class-&gt;generate() so your file have the php tag in them.</p>
<p>The next part of the code creates the second class that extends <a title="Zend Db Table Abstract Doc" href="http://framework.zend.com/manual/en/zend.db.table.html">Zend_Db_Table_Abstract</a>. I won&#8217;t go into detail with what is happening there since it&#8217;s very similar to the above. If you have any questions do feel free to ask. That&#8217;s pretty much it. Run this code, make sure you have your directories created and after executing you should now have two files for each table in your database.</p>
<p>Here is an example of what you should see after running the page:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="coMULTI">/**<br />
&nbsp;* Bio model<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_Bio<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var array Maps to all fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_data</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;user_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;bio_type_id&#8217;</span> =&gt; <span class="nu0">0</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;title&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;content&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;last_update&#8217;</span> =&gt; <span class="kw2">null</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;enabled&#8217;</span> =&gt; <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="coMULTI">/**<br />
&nbsp;* Bio db table abstract<br />
&nbsp;*<br />
&nbsp;* @author Joey Rivera<br />
&nbsp;*/</span><br />
<span class="kw2">class</span> Ct_Model_DbTable_Bio <span class="kw2">extends</span> Zend_Db_Table_Abstract<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string Name of db table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_name</span> = <span class="st0">&#8216;bio&#8217;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* @var string or array of fields in table<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span class="re0">$_primary</span> = <span class="st0">&#8216;bio_id&#8217;</span>;<br />
<span class="br0">&#125;</span></div>
<p>This is a work in progress since I&#8217;m still trying different things. After I finished writing this code I went back to the zend docs and noticed there is <a title="Zend CodeGenerator Php File Doc" href="http://framework.zend.com/manual/en/zend.codegenerator.examples.html#zend.codegenerator.examples.file">Zend_CodeGenerator_Php_File</a>. Seems to do the same thing I did above with file_put_contents. Feel free to try that instead.</p>
<h4>Conclusion</h4>
<p>I really enjoy the flexibility of using this method instead of typing everything out myself. It&#8217;s quicker with a lot less room for error. I&#8217;m sure there are already other programs/apps out there that do this but hey, it&#8217;s more fun to reinvent the wheel! Plus I learned about another Zend Framework module that I had not used before.</p>
<p>EDIT:</p>
<p>I had to make to small edits to this post to work more efficiently. The first was to change the line where we set the default value of each field for the columns array to &#8221; instead of null. The second was to set the primary property only if there was a primary field found on that table since not every table will always have one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/automate-model-creation-with-zend_codegenerator_php_class/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>AMF,XMLRPC,JSON,REST Zend Web Services Tutorial</title>
		<link>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/</link>
		<comments>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 20:17:07 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[amf]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[xmlrpc]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=356</guid>
		<description><![CDATA[I&#8217;ve been using the Zend for the last few months and I&#8217;m loving it. Here at work we were having a discussion about implementing some web services in the future so I decided to see what it takes to create some web services in PHP using Zend. I was pleasantly surprise (well not really surprised) [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using the Zend for the last few months and I&#8217;m loving it. Here at work we were having a discussion about implementing some web services in the future so I decided to see what it takes to create some web services in PHP using Zend. I was pleasantly surprise (well not really surprised) that it was extremely quick and easy to get things up and running.</p>
<p>In this post, I&#8217;m going to explain how to create web services that can be accessed via AMF, XMLRPC, JSON, and REST using the Zend Framework. Hopefully these should cover most uses out there. I left SOAP out since I don&#8217;t really see myself using it any time soon. I will be communicating with a database as well to make the tutorial more informative. The db will consist of one table with information about courses such as mathematics courses.</p>
<p>I&#8217;m going to show you the final product first, hopefully to catch your attention so you&#8217;ll read the rest <img src='http://www.joeyrivera.com/blog_new/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . This is what the final product will look like. If you look at the following links in Chrome, do a view source to see the formatted response.</p>
<h5>XMLRPC</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/xml-rpc/course-info/abbr/math101/">http://www.joeyrivera.com/ZendWebServices/xml-rpc/course-info/abbr/math101/</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/xml-rpc/courses-info/">http://www.joeyrivera.com/ZendWebServices/xml-rpc/courses-info/</a></p>
<h5>JSON</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/json/course-info/abbr/math101/">http://www.joeyrivera.com/ZendWebServices/json/course-info/abbr/math101/</a><br />
All courses: <a href="http://www.joeyrivera.com/ZendWebServices/json/courses-info/">http://www.joeyrivera.com/ZendWebServices/json/courses-info/</a></p>
<h5>REST</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/rest/course-info/?method=getCourseInfo&amp;abbr=math102">http://www.joeyrivera.com/ZendWebServices/rest/course-info/?method=getCourseInfo&amp;abbr=math102</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/rest/courses-info/?method=getCoursesInfo">http://www.joeyrivera.com/ZendWebServices/rest/courses-info/?method=getCoursesInfo</a></p>
<h5>AMF</h5>
<p>One course:  <a href="http://www.joeyrivera.com/ZendWebServices/amf/course-info/abbr/math101">http://www.joeyrivera.com/ZendWebServices/amf/course-info/abbr/math101</a><br />
All courses:  <a href="http://www.joeyrivera.com/ZendWebServices/amf/courses-info/">http://www.joeyrivera.com/ZendWebServices/amf/courses-info/</a><span id="more-356"></span></p>
<p>Now let me show you how to make this work. First thing is setting up the database. If you already have a database you can communicate with just use that, there&#8217;s nothing specific to the database I&#8217;ll be using that requires you to create it. But if you want to follow along here are the details. I&#8217;ll be creating a database called &#8216;sample&#8217; with a table called &#8216;course&#8217;.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">DROP TABLE</span> IF <span class="kw1">EXISTS</span> `sample`.`course`;<br />
<span class="kw1">CREATE TABLE</span>  `sample`.`course` <span class="br0">&#40;</span><br />
`course_id` <span class="kw2">INT</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span> <span class="kw3">AUTO_INCREMENT</span>,<br />
`abbr` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`name` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">45</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`title` <span class="kw2">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">45</span><span class="br0">&#41;</span> <span class="kw3">NOT NULL</span>,<br />
`credits` <span class="kw2">SMALLINT</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span> <span class="kw3">UNSIGNED</span> <span class="kw3">NOT NULL</span>,<br />
<span class="kw1">PRIMARY KEY</span>  <span class="br0">&#40;</span>`course_id`<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span> ENGINE=<span class="kw1">INNODB</span> <span class="kw3">AUTO_INCREMENT</span>=<span class="nu0">3</span> <span class="kw3">DEFAULT</span> <span class="kw3">CHARSET</span>=utf8;</div>
<p>Now that you have the table created, add some sample data:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> `course` <span class="kw1">VALUES</span><br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>,<span class="st0">&#8216;math101&#8242;</span>,<span class="st0">&#8216;Math 101&#8242;</span>,<span class="st0">&#8216;Beginner Mathematics&#8217;</span>,<span class="st0">&#8217;3&#8242;</span><span class="br0">&#41;</span>,<br />
<span class="br0">&#40;</span><span class="kw3">NULL</span>,<span class="st0">&#8216;math102&#8242;</span>,<span class="st0">&#8216;Math 102&#8242;</span>,<span class="st0">&#8216;Advanced Mathematics&#8217;</span>,<span class="st0">&#8217;4&#8242;</span><span class="br0">&#41;</span>;</div>
<p>Now the code. I have one Model class called Service. This is the only class that interacts with the database. For each format such as XMLRPC, JSON, etc. I created a Controller to handle that formats needs. All these controllers initialize a Zend server class if needed, call the method needed from the Service class, and return the formatted response to the browser.</p>
<p>This is what the Service class looks like:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* Class with all the calls to the db<br />
* IMPORTANT: The methods needs comments with params and return for XMLRPC to work!!!<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> Service<br />
<span class="br0">&#123;</span><br />
<span class="coMULTI">/**<br />
* Get all courses<br />
*<br />
* @return array<br />
*/</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> getCoursesInfo<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">return</span> Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchAll</span><span class="br0">&#40;</span><span class="st0">&#8216;select * from course&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="coMULTI">/**<br />
* Gets info for a course<br />
*<br />
* @param string $abbr<br />
* @return array<br />
*/</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> getCourseInfo<span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="kw1">return</span> Zend_Registry::<span class="me2">get</span><span class="br0">&#40;</span><span class="st0">&#8216;db&#8217;</span><span class="br0">&#41;</span>-&gt;<span class="me1">fetchRow</span><span class="br0">&#40;</span><span class="st0">&quot;select * from course where abbr = &#8216;$abbr&#8217;&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Two methods make up this class. &#8216;getCoursesInfo&#8217; (plural) gets the information from all the courses in the &#8216;course&#8217; table. &#8216;getCourseInfo&#8217; takes in one parameter &#8216;abbr&#8217; and returns the information for the course who&#8217;s abbreviation matches &#8216;abbr&#8217;. You can create as many methods as you like here and these are the methods your controllers will call. One thing to note, you MUST add comments to the methods for XMLRPC calls to work. It uses the variable declaration and return declaration in your comments to know what data type to expect in and out.</p>
<p>Now lets move on to the first format: <strong>XMLRPC</strong>.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* XmlRpcController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> XmlRpcController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_XmlRpc_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span>, <span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_request = <span class="kw2">new</span> Zend_XmlRpc_Request<span class="br0">&#40;</span><span class="st0">&#8216;Service.getCoursesInfo&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="re0">$this</span>-&gt;_request = <span class="kw2">new</span> Zend_XmlRpc_Request<span class="br0">&#40;</span><span class="st0">&#8216;Service.getCourseInfo&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">setParams</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The init function gets called for any action in the controller. I am turning off the layout and view since I won&#8217;t be using them for these examples. Then I initialize the proper zend server class, in this case the &#8216;Zend_XmlRpc_Server&#8217; class. The next line &#8216;setClass&#8217; tells the server what classes will be handling incoming requests. You can add several classes and/or functions. For this sample, I&#8217;m simply attaching one class &#8216;Service&#8217; which I described earlier. &#8216;postDispatch&#8217; gets called after the action for the controller is done executing. In my &#8216;postDispatch&#8217; I tell the server instance to handle the incoming request.</p>
<p>&#8216;coursesInfoAction&#8217; creates a request object that calls the Service class &#8216;getCoursesInfo&#8217; (plural) method. Likewise, &#8216;courseInfoAction&#8217; does the same calling &#8216;getCourseInfo&#8217; (singular) but makes sure the abbr variable is set. Which ever of these two is called, a server instance gets initialized, the request variable is populated, then the server handle method is called so the request is returned to the browser with the correct format. I&#8217;m doing request objects here so I can easily test and make sure these methods are working correctly by just loading the url.</p>
<p>All the other controller classes look very similar, each initializing a server instance if necessary and then telling the server instance to display the results correctly.</p>
<p>Here&#8217;s the <strong>JSON</strong> controller:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* JsonController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> JsonController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_return</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> Zend_Json::<span class="me2">encode</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_return<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$request</span> = <span class="kw2">new</span> Service<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_return = <span class="re0">$request</span>-&gt;<span class="me1">getCoursesInfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="re0">$request</span> = <span class="kw2">new</span> Service<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_return = <span class="re0">$request</span>-&gt;<span class="me1">getCourseInfo</span><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>You&#8217;ll notice there is no JSON server class. All we are doing here is grabbing the data set from the Service class and calling Zend&#8217;s Zend_Json::encode to convert the data into a properly formatted JSON string and echoing it out.</p>
<p>Here you can see the implementation for <strong>REST</strong>:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* RestController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> RestController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_Rest_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span>, <span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$abbr</span> = Zend_Filter::<span class="me2">get</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_request-&gt;<span class="me1">getParam</span><span class="br0">&#40;</span><span class="st0">&#8216;abbr&#8217;</span><span class="br0">&#41;</span>, <span class="st0">&#8216;StripTags&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>!<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$abbr</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <a href="http://www.php.net/exit"><span class="kw3">exit</span></a>;<br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Starting to look repetitive? Once you figure out how to make one format work, the others are very similar. Part of using REST is passing a method parameter in the query string. The method you pass is the method of the service that will be called. This is why coursesInfo and courseInfo don&#8217;t do anything but validate if necessary.</p>
<p>Finally if you work with Flash or Flex like I do some times, you&#8217;ll want to use AMF if you are working with complex or large data sets. Here&#8217;s the <strong>AMF</strong> example:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;?php</span><br />
<span class="coMULTI">/**<br />
* AmfController<br />
*<br />
* @author Joey Rivera<br />
*/</span><br />
<span class="kw2">class</span> AmfController <span class="kw2">extends</span> Zend_Controller_Action<br />
<span class="br0">&#123;</span><br />
protected <span class="re0">$_server</span> = <span class="kw2">null</span>;<br />
protected <span class="re0">$_request</span> = <span class="kw2">null</span>;<br />
<span class="kw2">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">layout</span>-&gt;<span class="me1">disableLayout</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_helper-&gt;<span class="me1">viewRenderer</span>-&gt;<span class="me1">setNoRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server = <span class="kw2">new</span> Zend_Amf_Server<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">setClass</span><span class="br0">&#40;</span><span class="st0">&#8216;Service&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> postDispatch<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="re0">$this</span>-&gt;_server-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> coursesInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">public</span> <span class="kw2">function</span> courseInfoAction<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>If you try to access the url for amf in your browser you&#8217;ll be prompted to download a file because AMF is not a text format but a binary format that Flash and Flex read. You&#8217;ll just have to take my word on that it&#8217;s working :p or create a quick Flash/Flex app that calls this service.</p>
<p>This is pretty much all I have for you all today. My next post will be on creating the different format clients to call these services. Thanks for reading. If you have any questions or comments feel free to post.</p>
<p>EDIT: Added a new post on how to use Zend_Json_Server and how to call it: <a title="How to use Zend_Json_Server and calling it post" href="http://www.joeyrivera.com/2011/zend_json_server-and-how-to-call-it-via-json-rpc-2-0/">http://www.joeyrivera.com/2011/zend_json_server-and-how-to-call-it-via-json-rpc-2-0/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2009/amfxmlrpcjsonrest-zend-web-services-tutorial/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Uploader Phase 2 complete!</title>
		<link>http://www.joeyrivera.com/2008/uploader-phase-2-complete/</link>
		<comments>http://www.joeyrivera.com/2008/uploader-phase-2-complete/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 03:45:52 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uploader]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=165</guid>
		<description><![CDATA[So today after work I decided it was about time to start working on phase two of my Uploader tool. Phase two consists of allowing me to drag and drop an image URL into the app so the app can then download the image to a folder in my server, create a thumbnail of this [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 157px"><img title="Uploader screen shot" src="/blog_files/96/uploader_ss.gif" alt="Here is a screen shot of the Uploader 0.1.0" width="147" height="172" /><p class="wp-caption-text">Uploader 0.2.0</p></div>
<p>So today after work I decided it was about time to start working on phase two of my Uploader tool. Phase two consists of allowing me to drag and drop an image URL into the app so the app can then download the image to a folder in my server, create a thumbnail of this image, and update the WordPress database with information about the new image. The new image would then show up the next time a person visits my blog. If you notice, there is now a &#8220;Pics of Interest&#8221; section on the sidebar to the right of the page (similar to what people are doing with the flickr plugin). Every time I drag an image URL into the Uploader, the new image thumbnail will show in that area and if you click on the thumbnail you&#8217;ll see the full size image.</p>
<p>It was actually much easier than I originally thought to make this work. Technically, the Uploader already has the drag/drop functionality and already takes in a URL. The only extra <a title="Main.as" href="http://www.joeyrivera.com/blog_files/165/Main.as" target="_blank">AS3 code</a> was a check to see if the URL was an image and if so call the PHP page with a code (I&#8217;m passing &#8211;@IMAGE@&#8211; to know it&#8217;s an image &#8211; it was just the first thing that came to my head) so then PHP knows this URL should be treated as an image not just a URL. If you remember, phase 1 for the Uploader allows a person to drag/drop a URL into the app so it creates a new link under the &#8220;Links of Interest&#8221;.<span id="more-165"></span></p>
<p>Most of the changes were on the PHP end. The file that handles the request from the AIR app now does a check to see if this URL is an image. If so, it has to download the original image to the local server, make a thumbnail copy of it, and finally update the wp_images table (this is a new table I created for this - <a title="MySQL create script" href="http://www.joeyrivera.com/blog_files/165/wp_images.sql" target="_blank">MySQL script</a>) in WordPress. I&#8217;m using two PEAR packages to do this: HTTP_Request and Image_Transform. The same can be done just as easily without them but I like using PEAR on my projects. Here&#8217;s the code that does this (<a title="index.php file as text" href="http://www.joeyrivera.com/blog_files/165/file.txt" target="_blank">file.txt</a>):</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$name</span> == <span class="st0">&#8216;&#8211;@IMAGE@&#8211;&#8217;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// later use unique names?</span><br />
    <span class="re0">$name</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$url</span>, strripos<span class="br0">&#40;</span><span class="re0">$url</span>,<span class="st0">&#8216;/&#8217;</span><span class="br0">&#41;</span> + <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
        <br />
&nbsp; &nbsp; <span class="kw1">require</span> <span class="st0">&#8216;HTTP/Request.php&#8217;</span>;<br />
    <span class="re0">$req</span> = <span class="kw2">new</span> HTTP_Request<span class="br0">&#40;</span><span class="re0">$url</span><span class="br0">&#41;</span>;<br />
    <span class="re0">$req</span>-&gt;<span class="me1">sendRequest</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
    <br />
&nbsp; &nbsp; <span class="re0">$fp</span> = <a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="st0">&#8216;images/&#8217;</span>.<span class="re0">$name</span>, <span class="st0">&#8216;w&#8217;</span><span class="br0">&#41;</span>;<br />
    <a href="http://www.php.net/fwrite"><span class="kw3">fwrite</span></a><span class="br0">&#40;</span><span class="re0">$fp</span>, <span class="re0">$req</span>-&gt;<span class="me1">getResponseBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;   <a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$fp</span><span class="br0">&#41;</span>;<br />
    <br />
&nbsp; &nbsp; <span class="co1">// create thumbnail</span><br />
    <span class="kw1">require</span> <span class="st0">&#8216;Image/Transform.php&#8217;</span>;</p>
<p>&nbsp;   <span class="co1">//create transform driver object</span><br />
&nbsp;   <span class="re0">$it</span> = Image_Transform::<span class="me2">factory</span><span class="br0">&#40;</span><span class="st0">&#8216;GD&#8217;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp;   <span class="co1">//load the original file</span><br />
&nbsp;   <span class="re0">$it</span>-&gt;<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">&#8216;images/&#8217;</span>.<span class="re0">$name</span><span class="br0">&#41;</span>;</p>
<p>&nbsp;   <span class="co1">//scale it to 100px</span><br />
&nbsp;   <span class="re0">$it</span>-&gt;<span class="me1">scaleMaxLength</span><span class="br0">&#40;</span><span class="nu0">100</span><span class="br0">&#41;</span>;</p>
<p>&nbsp;   <span class="co1">//save it into a different file</span><br />
&nbsp;   <span class="re0">$tn</span> = <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$name</span>,<span class="nu0">0</span>,strripos<span class="br0">&#40;</span><span class="re0">$name</span>,<span class="st0">&#8216;.&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="st0">&#8216;-tn.&#8217;</span>.<a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$name</span>,strripos<span class="br0">&#40;</span><span class="re0">$name</span>,<span class="st0">&#8216;.&#8217;</span><span class="br0">&#41;</span><span class="nu0">+1</span><span class="br0">&#41;</span>;<br />
&nbsp;   <span class="re0">$it</span>-&gt;<span class="me1">save</span><span class="br0">&#40;</span><span class="st0">&#8216;images/&#8217;</span>.<span class="re0">$tn</span><span class="br0">&#41;</span>;</p>
<p>&nbsp;   <span class="re0">$query</span> = <span class="st0">&#8216;<br />
&nbsp;           Insert Into wp_images<br />
&nbsp;           Values (null, &#8216;</span>.<span class="re0">$db</span>-&gt;<span class="me1">quote</span><span class="br0">&#40;</span><span class="re0">$name</span>, <span class="st0">&#8216;text&#8217;</span><span class="br0">&#41;</span>.<span class="st0">&#8216;, &#8216;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$db</span>-&gt;<span class="me1">quote</span><span class="br0">&#40;</span><span class="re0">$tn</span>, <span class="st0">&#8216;text&#8217;</span><span class="br0">&#41;</span>.<span class="st0">&#8216;, &#8216;</span>.<span class="re0">$db</span>-&gt;<span class="me1">quote</span><span class="br0">&#40;</span><span class="re0">$url</span>, <span class="st0">&#8216;text&#8217;</span><span class="br0">&#41;</span>.<span class="st0">&#8216;);<br />
}</span></div>
<p>Then I had to add some code to the sidebar.php for my template in WordPress to show the new section &#8220;Pics of Interest&#8221;. As you read this keep in mind this is just a quick beta and I&#8217;ll be doing refactoring later. This is by no means a finished product but it works fine and it&#8217;s a good start for anyone who wants to try it out. This is the new code I added to my <a title="sidebar.php" href="http://www.joeyrivera.com/blog_files/165/sidebar.txt" target="_blank">sidebar.php</a>:</p>
<div class="dean_ch" style="white-space: wrap;">&lt;div <span class="kw2">class</span>=<span class="st0">&quot;block poi&quot;</span>&gt;<br />
&nbsp;       &lt;h3&gt;Pics of Interest&lt;/h3&gt;<br />
&nbsp;       <span class="kw2">&lt;?php</span><br />
&nbsp;               <span class="kw1">require</span> <span class="st0">&#8216;./uploader/lib/DB.php&#8217;</span>;<br />
&nbsp;               DB::<span class="me2">conn</span><span class="br0">&#40;</span><span class="st0">&#8216;live&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp;               <span class="re0">$db</span> = DB::<span class="me2">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp;               <span class="re0">$query</span> = <span class="st0">&#8216;<br />
&nbsp;                       Select image_file, image_thumbnail, image_origin<br />
&nbsp;                       From wp_images<br />
&nbsp;                       Order By image_id desc<br />
&nbsp;                       Limit 6;<br />
&nbsp;                       &#8217;</span>;</p>
<p>&nbsp;               <span class="re0">$rs</span> = <span class="re0">$db</span>-&gt;<span class="me1">query</span><span class="br0">&#40;</span><span class="re0">$query</span><span class="br0">&#41;</span>;<br />
&nbsp;               <span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$row</span> = <span class="re0">$rs</span>-&gt;<span class="me1">fetchRow</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;&lt;a href=&quot;/uploader/images/&#8217;</span>.<span class="re0">$row</span><span class="br0">&#91;</span><span class="st0">&#8216;image_file&#8217;</span><span class="br0">&#93;</span>.<span class="st0">&#8216;&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title=&quot;From: &#8216;</span>.<span class="re0">$row</span><span class="br0">&#91;</span><span class="st0">&#8216;image_origin&#8217;</span><span class="br0">&#93;</span>.<span class="st0">&#8216;&quot;&gt;&lt;img<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src=&quot;/uploader/images/&#8217;</span>.<span class="re0">$row</span><span class="br0">&#91;</span><span class="st0">&#8216;image_thumbnail&#8217;</span><span class="br0">&#93;</span>.<span class="st0">&#8216;&quot; /&gt;&lt;/a&gt;&#8217;</span>;<br />
&nbsp;       <span class="kw2">?&gt;</span><br />
&lt;/div&gt;</div>
<p>The last thing I had to do was to add the style for my images in the styles.css file and I was done. Make sure your images folder has the proper privileges so you can upload an image into it. The next phase will be allowing me to drag files from my local file system into the AIR app so it&#8217;ll upload it to my server. Right now the Uploader only does something when a URL is dropped on it: adds a link if it&#8217;s not a image, adds the image if it&#8217;s an image.</p>
<p>Question, comments, conerns are always welcome!</p>
<p>Edit:</p>
<p><a title="Info about Uploader Phase 1" href="http://www.joeyrivera.com/2008/uploader-phase-1/" target="_self">Uploader Phase 1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2008/uploader-phase-2-complete/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&lt;head&gt; conference 2008</title>
		<link>http://www.joeyrivera.com/2008/head-conference-2008/</link>
		<comments>http://www.joeyrivera.com/2008/head-conference-2008/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 18:39:38 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[head]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=83</guid>
		<description><![CDATA[So I attended the &#60;head&#62; conference a couple weekends ago and I&#8217;m glad I did.  There were a ton of knowledgeable speakers covering many interesting topics. The following are the sessions I enjoyed the most so far: Building Personal Brand with Social Media (Gary Vaynerchuk) I really enjoyed this session. It was all about why [...]]]></description>
			<content:encoded><![CDATA[<p>So I attended the <a title="&lt;head&gt; conference website" href="http://www.headconference.com/" target="_blank">&lt;head&gt;</a> conference a couple weekends ago and I&#8217;m glad I did.  There were a ton of knowledgeable speakers covering many interesting topics. The following are the sessions I enjoyed the most so far:</p>
<p><span id="more-83"></span></p>
<p><strong>Building Personal Brand with Social Media (<a title="Gary Vaynerchuk" href="http://www.headconference.com/speakers/gary-vaynerchuk/" target="_blank">Gary Vaynerchuk</a>)</strong><br />
I really enjoyed this session. It was all about why personal branding is so important and various ways to get a person started. A lot of this was stuff I&#8217;ve been hearing for years (specially from my friend <a title="Moses website" href="http://www.mospired.com" target="_blank">Moses</a>) but I never did anything about. Gary said things that made sense and put things on a different perspective for me. As you call tell, it worked. I now have a blog and even a twitter account!</p>
<p><strong>Google App Engine: An Overview (<a title="Pete Koomen" href="http://www.headconference.com/speakers/pete-koomen/" target="_blank">Pete Koomen</a>)</strong><br />
This was a good introduction to <a title="Google App Engine website" href="http://code.google.com/appengine/" target="_blank">Google App Engine</a> for me. I first learned about Google App Engine from <a title="Aral Balkan" href="http://aralbalkan.com/">Aral Balkans</a> blog but I never did much research on it to find out what it&#8217;s all about. After this session I have a better understanding on what this does and how I could use it at some point.</p>
<p><strong>Beyond Web Standards: Crafting Design and Development Standards (<a title="Kimberly Blessing" href="http://www.headconference.com/speakers/kimberly-blessing/" target="_blank">Kimberly Blessing</a>)</strong><br />
This was another good one. All about trying to create/use standards. This should be common sense but the reality is I haven&#8217;t worked anywhere yet were we had any kind of standards. I&#8217;m going to make it a personal goal to create some sort of standards to implement at my current company. We do so much and all our documentation, code, naming conventions, folder structures, everything is different. Every project seems to be completely different from the next. Things would be much more efficient at least for us (the developers) if we all followed some sort of standards.</p>
<p><strong>Architecting Human Behaviour (<a title="Andy Budd" href="http://www.headconference.com/speakers/andy-budd/" target="_blank">Andy Budd</a>)</strong><br />
This was one of my favorite.  Andy did a great job on making a very fun and interesting presentation. I was 100% engaged the entire time wanting to hear more on what he had to say. It was very interesting to hear the tricks being used out there in the way buildings are structured to guide customers through certain paths increasing the amount of time customers stay in the building and increasing the chances the customers will purchase more items. The coolest example was all the levels of details put on how things are laid out on a grocery store and why they are built the way they are. There was a ton of neat info that I now have to think about how I can put these ideas into practice on my own projects. Looking forward to hearing more on what Andy has to say.</p>
<p><strong>Building Pyramids out of Bits: How can we build software that lasts? (<a title="Cliff Hall" href="http://www.headconference.com/speakers/cliff-hall/" target="_blank">Cliff Hall</a>)</strong><br />
This was an interesting one too. Cliff shared his experiences on how over time, things change and the code you were so proud off can eventually fade away. Technologies change, new languages surface and old languages sometimes stop being used. It&#8217;s important to develop code in a way that will be easily transferable from one application to the next. He talked about <a title="PureMVC" href="http://puremvc.org/" target="_blank">PureMVC</a> and I&#8217;m definately going to look more into it. The idea behind it is having a single framework that is platform independent based on MVC. If you code in AS3 using PureMVC, you should be able to transfer the same code to Java or PHP without too many problems since the structure is the same.</p>
<p>Now I need to look at some of the recordings for the sessions I missed. I&#8217;m sure there are a few other great ones out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2008/head-conference-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploader Phase 1</title>
		<link>http://www.joeyrivera.com/2008/uploader-phase-1/</link>
		<comments>http://www.joeyrivera.com/2008/uploader-phase-1/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 15:49:47 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uploader]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wordpress tools]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=96</guid>
		<description><![CDATA[If you missed the previous post explaining what Uploader is go check it out. So after having some fun with code I think I&#8217;m close to being done with phase 1.  I currently have a working beta (0.1.0) that I&#8217;m going to share with you all and explain what I&#8217;ve done in case anyone else [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 157px"><img title="Uploader screen shot" src="/blog_files/96/uploader_ss.gif" alt="Here is a screen shot of the Uploader 0.1.0" width="147" height="172" /><p class="wp-caption-text">Uploader 0.1.0</p></div>
<p>If you missed the previous post explaining what Uploader is go <a title="About the uploader" href="http://www.joeyrivera.com/2008/uploader-automating-wordpress-links-and-file-uploads/" target="_self">check it out</a>.</p>
<p>So after having some fun with code I think I&#8217;m close to being done with phase 1.  I currently have a working beta (0.1.0) that I&#8217;m going to share with you all and explain what I&#8217;ve done in case anyone else wants to do something similar.  For the most part this was pretty straight forward.  There were a few hiccups here and there that I&#8217;ll try to address in this post. The link to the code will be at the end.</p>
<p> <span id="more-96"></span></p>
<p>First I looked online for some info on Drag/Drop in AIR and I found these two posts that I used for reference:</p>
<ul>
<li><a href="http://www.senocular.com/air/tutorials/dragdropimageviewer/" target="_blank">http://www.senocular.com/air/tutorials/dragdropimageviewer/</a></li>
<li><a href="http://www.mikechambers.com/blog/2007/11/07/air-example-native-drag-and-drop/" target="_blank">http://www.mikechambers.com/blog/2007/11/07/air-example-native-drag-and-drop/</a></li>
</ul>
<p>I should have read the comments in those post since they answer the question of using &#8216;NativeDragManager&#8217; instead of &#8216;DragManager&#8217;.  I had that issue and had to figure out why it wasn&#8217;t working.</p>
<p>This is an AIR app made in Flash.  So far I only have one &#8216;Main&#8217; class taking care of most of the action.  I&#8217;ll clean the code up later and comment it but here&#8217;s the <a title="Main.as file" href="http://www.joeyrivera.com/blog_files/96/com/joeyrivera/uploader/Main.as" target="_blank">class</a> so far. Using the &#8216;NativeDragEvent&#8217;, Uploader waits for something to be dragged over it.  When it does, &#8216;dragEnter&#8217; is called. Usually you can do some validation here to see if this is a file type you want to accept but I just do that later.  Before a file/URL can be dropped you need to call &#8216;NativeDragManager.acceptDragDrop(<em>object</em>)&#8217;. By doing this, you let AIR know that the object is ready to accept the drop.</p>
<p>Once an item is dropped, &#8216;dragDrop&#8217; is called to see if this is a file or a URL.  If URL, &#8216;urlDropped&#8217; is called which creates a listener for the loader var and sends the request to the URL that was dropped on the app.  When the request is complete, &#8216;urlLoaded&#8217; is called.  The reason we are loading the dropped URL is so we can grab the title of that page in between the &lt;title&gt; tag of the html.  This is going to be used as the display name when we insert the new link in WordPress. &#8216;grabTitle&#8217; does a quick substr and when all is done we do one last request.  I had to make sure to remove the listener on &#8216;loader&#8217; for &#8216;urlLoaded&#8217; since later when I make a new listener and request, both the new and old listeners were triggering (so make sure to remove a listener from a loader if you are going to reuse that var with a different event call back).</p>
<p>&#8216;SERVER&#8217; is the constant that needs to by modified to point to the correct PHP page that will insert the link to the WordPress DB.  &#8217;urlLoaded&#8217; finishes by sending two variables to the SERVER page, &#8216;name&#8217; and &#8216;url&#8217;. After the request is loaded, the &#8216;serverUpdated&#8217; function will display if the process was successful or not based on what is returned by the PHP page.</p>
<p>That&#8217;s pretty much it for the AS3 code.  The PHP file is much more light weight.  At first I had PHP request the HTML for the URL and strip out the title but after thinking about it, it makes more sense to put that burden on the client and not the server so I moved the code to Flash.</p>
<p>The <a title="PHP Code file" href="http://www.joeyrivera.com/blog_files/96/file.html" target="_blank">PHP code</a> starts out by checking for the two needed variables &#8216;url&#8217; and &#8216;name&#8217;.  There&#8217;s a tiny bit of validation going on, definitely more to come as well as implementing some security features since at the moment anyone can submit variables to this page if they know the URL.  (<em>Before posting this, I decided to add a quick security check for now which will see if the remote IP address matches a couple different valid IP address &#8211; such as my home or work PC &#8211; and if it does, it will continue executing</em> ) Then we make a DB connection to the WordPress DB. I have a standard DB class I use, just use regular <a title="php.net mysql_connect info" href="http://us2.php.net/mysql_connect" target="_blank">mysql_connect</a> instead (ask if you need help with this). Next is the query to insert the data into the &#8216;wp_links&#8217; table.  I kept the name default during my WordPress install, so the table name might be different for others. And at last, the query is executed and true is returned. The assumption is if we make it down that far everything worked correctly.  Like I said, I need to add more validation and security but that will be later&#8230; maybe phase 2 or 3.</p>
<p>The only issue I&#8217;m having now that I can&#8217;t seem to figure out is where the WordPress DB tracks what category a link is attached to?  When I log into the WordPress admin area and go to manage links, all the links I&#8217;m inserting using this method don&#8217;t show anything under &#8216;Categories&#8217;. I&#8217;m assuming this information isn&#8217;t in the &#8216;wp_links&#8217; table?  If anyone has an answer to this, please let me know.</p>
<p>Here is a <a title="FLA, Main.as, PHP file, and AIR xml file" href="http://www.joeyrivera.com/blog_files/96/Uploader_0.1.0.zip">zip</a> of all the files (FLA, PHP file, Main class, and AIR xml).  Make sure to edit the ActionScript &#8216;SERVER&#8217; const and DB info in the PHP file.</p>
<p>* Quick side note, I need to give credit to my fiance Ashley who created a quick graphic for me to use last night. Visit her <a title="Ashley Fleishel" href="http://www.ashleyfleishel.com" target="_blank">site</a> to see some of her work.</p>
<p>EDIT:</p>
<p>I forgot to mention I made some edits to the Uploader-app.xml file. These settings were to remove the system chrome and make the app window transparent. More information about these settings can be found at <a title="Information about AIR" href="http://help.adobe.com/en_US/AIR/1.5/devappsflash/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff1.html" target="_blank">help.adobe.com</a>.</p>
<p><a title="Uploader Phase 2 done!" href="http://www.joeyrivera.com/2008/uploader-phase-2-complete/" target="_self">Uploader Phase 2 complete</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2008/uploader-phase-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Uploader: Automating WordPress Links and File Uploads</title>
		<link>http://www.joeyrivera.com/2008/uploader-automating-wordpress-links-and-file-uploads/</link>
		<comments>http://www.joeyrivera.com/2008/uploader-automating-wordpress-links-and-file-uploads/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 01:12:41 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uploader]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wordpress links]]></category>

		<guid isPermaLink="false">http://www.joeyrivera.com/?p=93</guid>
		<description><![CDATA[So now that I&#8217;m starting to get my WordPress and new hosting server settled in I&#8217;m trying to think of what I can create to help me automate some tasks. One of the things I know I&#8217;ll be doing quite often is adding new links to the &#8216;Links of Interest&#8217; on the front page.  I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>So now that I&#8217;m starting to get my WordPress and new hosting server settled in I&#8217;m trying to think of what I can create to help me automate some tasks. One of the things I know I&#8217;ll be doing quite often is adding new links to the &#8216;Links of Interest&#8217; on the front page.  I&#8217;ll also be uploading files/images to use on posts or to share with others but I&#8217;m too lazy to be ftping files constantly. So&#8230; time for a new tool!</p>
<p>The Uploader (yeah I know, I&#8217;m very creative) is going to do all the above for me. This will be a app I can keep running on my desktop somewhere and when I need to upload a file or link I can just drag it over and drop it in. The tool will then figure out what kind of item was dropped in and what to do with it.</p>
<p>Phase 1 will handle the links. The way the tool should work for links is the following.  I visit a page that I find interesting and want to add to the &#8216;Links of Interest&#8217;.  Instead of logging into WordPress to create it, I highlight the link from the address bar of the browser, drag it over to the app, and drop it. Now the app needs to figure out the &lt;title&gt; of that page to use as the name since we need a name and url to create a link. Once it has that, the tool will send the information to a PHP page which will do an insert to the links table in the WordPress DB.  </p>
<p>Phase 2 will deal with images and files. I may break phase 2 into two since I may do some stuff differently with images versus other files.  I&#8217;m thinking I may want to upload images, create a thumbnail, and insert to a DB to display in the front page the same way some people do with their flickr images. Other files I would just upload and store somewhere else.  I&#8217;ll give this more thought when I get to it.</p>
<p>I&#8217;m going to be writing the tool in Flash CS3/AIR with PHP in the backend using the current WordPress MySQL DB.  Stay tuned for updates!</p>
<p>EDIT:</p>
<p><a title="Uploader Phase 1 done" href="http://www.joeyrivera.com/2008/uploader-phase-1/" target="_self">Uploader Phase 1 completed</a> <br />
<a title="Uploader Phase 2 done" href="http://www.joeyrivera.com/2008/uploader-phase-2-complete/" target="_self"> Uploader Phase 2 completed</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.joeyrivera.com/2008/uploader-automating-wordpress-links-and-file-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

