<?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>Dan Polant :: Web Interaction Designer &#187; Code</title>
	<atom:link href="http://danpolant.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://danpolant.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 May 2012 04:24:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Dynamically creating views in Drupal</title>
		<link>http://danpolant.com/dynamically-creating-views-in-drupal/</link>
		<comments>http://danpolant.com/dynamically-creating-views-in-drupal/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 00:09:34 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[how to]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=547</guid>
		<description><![CDATA[The Views module can be broken down into three main parts: the query builder, the display builder and the user interface. Usually, we use the interface to control the other two parts, but sometimes situations call for query and display logic that the interface cannot provide.
A good introduction to dynamic views is the function views_embed_view(). [...]]]></description>
			<content:encoded><![CDATA[<p>The Views module can be broken down into three main parts: the query builder, the display builder and the user interface. Usually, we use the interface to control the other two parts, but sometimes situations call for query and display logic that the interface cannot provide.</p>
<p>A good introduction to dynamic views is the function <code><a href="http://drupalcontrib.org/api/drupal/contributions--views--views.module/function/views_embed_view/6">views_embed_view()</a></code>. Using this, one can render the finished display of a view stored in the database or in code. It returns rendered HTML:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$display</span> = views_embed_view<span class="br0">&#40;</span><span class="st0">&quot;name_of_view&quot;</span>, <span class="st0">&quot;display_id&quot;</span>, <span class="re0">$arg1</span>, <span class="re0">$arg2</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Often this function is a good alternative to using views blocks, for situations where the view display should be more tightly integrated in a certain area. By using <code>func_get_args()</code>, <code>views_embed_view</code> lets you pass arguments into the view that would normally have to be in the URL. </p>
<p>However, there are situations where views_embed_view just won&#8217;t cut it. Here are some examples:</p>
<p>1) The user needs be able to choose what filters/fields exist in the view.<br />
2) Groupings need to be dynamically generated based on user input</p>
<p>The functionality listed above cannot be achieved merely by passing arguments into the view. Rather, one must employ methods of the View object ([views module]/includes/view.inc) in order to build up elements of the view from the foundation.</p>
<p>This approach starts with the function <code><a href="http://drupalcontrib.org/api/drupal/contributions--views--views.module/function/views_get_view/6">views_get_view()</a></code>. This function loads a view from any storage, but does not execute or render it. </p>
<p>Now comes the most important function to master, <code>View::add_item()</code>. The code is in views/includes/view.inc. The following snippet demonstrates how to add a node id filter to a view named &#8220;example.&#8221;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span> = views_get_view<span class="br0">&#40;</span><span class="st0">&#8216;example&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$options</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="st0">&#8216;value&#8217;</span> =&gt; <span class="nu0">5</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span>-&gt;<span class="me1">add_item</span><span class="br0">&#40;</span><span class="st0">&#8216;default&#8217;</span>, <span class="st0">&#8216;filter&#8217;</span>, <span class="st0">&#8216;node&#8217;</span>, <span class="st0">&#8216;nid&#8217;</span>, <span class="re0">$options</span>, <span class="st0">&#8216;nid_identifier&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Here is a breakdown of arguments found in <code>View::add_item</code>:</p>
<p>1) display id<br />
2) type &#8211; can be filter, relationship, field, sort<br />
3) table &#8211; can also be table alias<br />
4) options &#8211; see below<br />
5) identifier &#8211; an internal reference to this filter, useful for establishing predictable dynamic keys for fields/filters/relationships that may get added twice. If you don&#8217;t pass anything for this argument, Views will create a unique id using an incremental variable if necessary.</p>
<p>Here is a breakdown of the <code>$options</code> argument, at least that of it which I have used:</p>
<p>1) value &#8211; meaningful only to filters. Specifies the conditions that will pass the filter. This can be an array in order to imply the IN operator<br />
2) operator &#8211; may assume any of the standard db operators. It is not necessary to specify IN if you provide an array as value.<br />
3) relationship &#8211; specifies a relationship for this item to use. Remember that doing this will require that you change the table alias to reflect the relationship table alias.<br />
4) group &#8211; filters only. Specifies a group for this filter to be a part of. Numeric or string values will work.</p>
<p>The majority of the work can be done with the <code>add_item</code> function. If I were to choose one more important method to learn within this subject it would be <code>set_option()</code>. Suppose you have defined several filters as being part of groups 0 and 1. Here is how you would use <code>set_option</code> to tell Views what to do with those groupings:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$groups</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="nu0">0</span> =&gt; <span class="st0">&#8216;OR&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="nu0">1</span> =&gt; <span class="st0">&#8216;AND&#8217;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$view</span>-&gt;<span class="me1">display_handler</span>-&gt;<span class="me1">set_option</span><span class="br0">&#40;</span><span class="st0">&#8216;filter_groups&#8217;</span>, <span class="re0">$groups</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>This snippet would create two conditional groups, 0 conjoined by OR and 1 conjoined by AND.</p>
<p>Set_option is a method of the views handler, not the view. It can be used to set any of the other characteristics defined the in the options element of the handler. It can do the same things as <code>add_item</code> but <code>add_item</code> performs additional procedures that help things come out the way you want (in most cases) and is definitely more useful for adding filters, relationships, fields and sorts.</p>
<p>Once you have your view prepared the way you want it, you need to execute and probably render it. The following code will do just that:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">init_display</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">pre_execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// at this point, your view will have results added</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$view</span>-&gt;<span class="me1">post_execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co1">// this is the final output &nbsp;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re0">$output</span> = <span class="re0">$view</span>-&gt;<span class="me1">display_handler</span>-&gt;<span class="me1">preview</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>There is a lot more you can do with dynamic views than I have outlined here. Generally, the best way to learn more about this is to first use the UI to build a view that contains the elements and characteristics that you want to dynamically add in your function. Then look at the export of that view. You will gain a good understanding of what the $view object and its display handler need to look like in code. Having a PHP debugger is also immensely helpful, but short of that, just use <code>dpm</code> liberally.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/dynamically-creating-views-in-drupal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use the output buffer to debug a SOAP server</title>
		<link>http://danpolant.com/use-the-output-buffer-to-debug-a-soap-server/</link>
		<comments>http://danpolant.com/use-the-output-buffer-to-debug-a-soap-server/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 03:55:16 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ob_start]]></category>
		<category><![CDATA[output buffer]]></category>
		<category><![CDATA[quickbooks]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=519</guid>
		<description><![CDATA[I&#8217;m working on a Quickbooks module for Drupal Ubercart. By implementing a SOAP server, it is possible to allow the Quickbooks Web Connector (QWC) to pull down XML snippets representing store information. We&#8217;re using the SOAP server module for Drupal, which extends services.
Quickbooks is the event horizon &#8211; you have know idea what it does with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a Quickbooks module for <a href="http://www.ubercart.org/">Drupal Ubercart</a>. By implementing a SOAP server, it is possible to allow the <a href="http://developer.intuit.com/qbsdk-current/doc/pdf/qbwc_proguide.pdf">Quickbooks Web Connector</a> (QWC) to pull down XML snippets representing store information. We&#8217;re using the <a href="http://drupal.org/project/soap_server">SOAP server</a> module for Drupal, which extends <a href="http://drupal.org/project/services">services</a>.</p>
<p>Quickbooks is the event horizon &#8211; you have know idea what it does with your SOAP responses once it takes over. It is poorly documented and the Intuit developer forums are essentially derelict. Before we could even tackle the problem of getting the response data structures right, we had to make sure our SOAP server was working right. Packet sniffing was not an option for investigating our server&#8217;s responses because QWC needs to use SSL. Instead we wrote the output of the SOAP server instance to the output buffer, and logged it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/ob_start"><span class="kw3">ob_start</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$server</span> = <span class="kw2">new</span> SoapServer;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$server</span>-&gt;<span class="me1">handle</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$buffer</span> = <a href="http://www.php.net/ob_get_contents"><span class="kw3">ob_get_contents</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">watchdog<span class="br0">&#40;</span><span class="st0">&#8216;modulename&#8217;</span>, <span class="st0">&#8216;server output: &#8216;</span> . <span class="re0">$buffer</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/ob_end_flush"><span class="kw3">ob_end_flush</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Output buffers direct the http response away from its destination and into a &#8220;buffer&#8221; where it can be retrieved later with ob_get_contents(). Using this method we were able to verify that our SOAP server was sending proper (looking) SOAP requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/use-the-output-buffer-to-debug-a-soap-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Figuring out the Facebook API</title>
		<link>http://danpolant.com/figuring-out-the-facebook-api/</link>
		<comments>http://danpolant.com/figuring-out-the-facebook-api/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 03:35:12 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=496</guid>
		<description><![CDATA[A lot has been said about Facebook&#8217;s service oriented API, most of it bad. Recently I used the PHP Facebook SDK to make an app that gathers Facebook photos from users who want to make an online collage. The project is scrapwalls.com, and the Facebook import feature is now live. Anyway, I didn&#8217;t think the [...]]]></description>
			<content:encoded><![CDATA[<p>A lot has been said about Facebook&#8217;s service oriented API, most of it bad. Recently I used the <a href="https://github.com/facebook/php-sdk/">PHP Facebook SDK</a> to make an app that gathers Facebook photos from users who want to make an online collage. The project is <a href="http://scrapwalls.com">scrapwalls.com</a>, and the Facebook import feature is now live. Anyway, I didn&#8217;t think the Facebook API was all that bad. However, there were definitely some problematic areas which I&#8217;ll go over too. Here is what I learned:</p>
<p><strong>The random Javascript in example.php is important</strong></p>
<p>It took me a while to realize this, but you will have problems detecting a browser&#8217;s Facebook login status if you don&#8217;t have something like the following in your page:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp;window.<span class="me1">fbAsyncInit</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; FB.<span class="me1">init</span><span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appId &nbsp; : <span class="st0">&#8216;&lt;!&#8211;?php echo $facebook&#8212;&gt;getAppId(); ?&gt;&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session : &lt;!&#8211;?php echo json_encode<span class="br0">&#40;</span>$session<span class="br0">&#41;</span>; ?&#8211;&gt;, <span class="co1">// don&#8217;t refetch the session when PHP already has it</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">status</span> &nbsp;: <span class="kw2">true</span>, <span class="co1">// check login status</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cookie &nbsp;: <span class="kw2">true</span>, <span class="co1">// enable cookies to allow the server to access the session</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xfbml &nbsp; : <span class="kw2">true</span> <span class="co1">// parse XFBML</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// whenever the user logs in, we refresh the page</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; FB.<span class="me1">Event</span>.<span class="me1">subscribe</span><span class="br0">&#40;</span><span class="st0">&#8216;auth.login&#8217;</span>, <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.<span class="me1">location</span>.<span class="me1">reload</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> e = document.<span class="me1">createElement</span><span class="br0">&#40;</span><span class="st0">&#8216;script&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">src</span> = document.<span class="me1">location</span>.<span class="me1">protocol</span> + <span class="st0">&#8216;//connect.facebook.net/en_US/all.js&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">async</span> = <span class="kw2">true</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&#8216;fb-root&#8217;</span><span class="br0">&#41;</span>.<span class="me1">appendChild</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This is from<a href="https://github.com/facebook/php-sdk/blob/master/examples/example.php"> example.php</a> in the PHP SDK. Basically this detects if the login status has changed, and reloads the page if it has. Your site&#8217;s server can&#8217;t see Facebook&#8217;s login cookies, so this snippet ensures that when the browser session changes, the pages gets reloaded. This process isn&#8217;t asynchronous (that would be cool) but simply adds an extra refresh to a page load where it detects a changed FB session. It is a bit weird, but as far as I could tell this is the only way to get it to work.</p>
<p><strong>FQL is faster than the Graph API</strong></p>
<p>Facebook&#8217;s <a href="http://developers.facebook.com/docs/reference/api/">graph API</a> reflects their current &#8220;no-sql&#8221; storage and retrieval approach. Through the graph API you see Facebook information as objects connected non-hierarchically through common id references. It is great for jumping from node to node but not so great for getting sets of nodes of one type attached to a user &#8211; e.g. &#8220;get name and url of all photos belonging to user&#8221; or &#8220;get all friends of user with IDs in a set.&#8221; For these &#8220;old school&#8221; queries, a SQL-like language is best, and that is why Facebook still supports <a href="http://developers.facebook.com/docs/reference/fql/">FQL</a>.</p>
<p>If you understand the basics of SQL, you understand FQL. FQL uses the simplest faculties of SQL (no joins) to query for specific field values within Facebook&#8217;s data store. The big advantage is it returns only as much info as you need. Responses from graph API calls are often huge and can cause slowdowns when requests are fired in a long loop, which is often necessary due to the nature of the graph API.</p>
<p><strong>Don&#8217;t forget to install the cert</strong></p>
<p>This is probably in the directions somewhere, but I wasted some time not realizing you have to put <a id="b92d7190e9fa44296b9812f507efa71213ec4b53" href="https://github.com/facebook/php-sdk/blob/master/src/fb_ca_chain_bundle.crt">fb_ca_chain_bundle.crt</a> in the same dir with <a id="d2d2e866bf0b1e6e1f030c06c5eebd87f4da1fc9" href="https://github.com/facebook/php-sdk/blob/master/src/facebook.php">facebook.php</a>.</p>
<h2>Overall impressions</h2>
<p>The most annoying thing I ran into was right at the beginning of the project. Over the span of a few days, I tried to register the app five or six times and was unsuccessful. At the time there was a lot of buzz about this. I don&#8217;t know how long it took for them to fix, because fortunately I realized one of the other developers had registered an app for us a year ago. Old apps seemed to still work fine, but the registration bug kept new ones from working for at least two or three days.</p>
<p>But hey, you have to take the bad with the good. The SDK is a well written class and provides everything you need to make API requests efficiently. I couldn&#8217;t imagine using the REST API without the SDK. On the browser side, the <a href="https://github.com/facebook/connect-js">Javascript SDK</a> has what it needs, and most importantly <a href="https://github.com/facebook/connect-js/blob/master/examples/jquery/login.html">work well with jQuery</a>.</p>
<p>Mostly I was just struck by the richness of the information that these APIs expose. I can see clever people writing apps that gather tons of information and analyze users&#8217; lives on Facebook, which today reflect quite accurately the ways in which people perceive themselves and are perceived. The security/privacy situation <a href="http://www.allfacebook.com/facebook-photos-warning-2009-02">is attrocious</a>, and it made making the app a lot easier. I worry about the potential for powerful corporate and political entities to gather information about individuals and groups &#8211; if the governments of Libya or Egypt had been effectively monitoring the revolutionary mentality as it developed on Facebook, they could have probably mounted a preemptive crackdown.</p>
<p>But insofar as code is apolitical, I say the Facebook APIs are a hit. Check out Facebook import on <a href="http://scrapwalls.com">scrapwalls.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/figuring-out-the-facebook-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSON output from Javascript</title>
		<link>http://danpolant.com/json-output-from-javascript/</link>
		<comments>http://danpolant.com/json-output-from-javascript/#comments</comments>
		<pubDate>Mon, 28 Feb 2011 04:11:40 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=491</guid>
		<description><![CDATA[Have you ever wished that html forms could send more than just key value pairs to the server? While working with the Facebook API, I found that I needed to send an array of objects back to the server for processing when the user submits the form. JSON was the ideal solution, but I soon [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wished that html forms could send more than just key value pairs to the server? While working with the Facebook API, I found that I needed to send an array of objects back to the server for processing when the user submits the form. JSON was the ideal solution, but I soon discovered that neither Javascript nor jQuery have built in libraries for converting variables into JSON.</p>
<p>Fortunately, I came upon <a href="https://github.com/douglascrockford/JSON-js/blob/master/json2.js">https://github.com/douglascrockford/JSON-js/blob/master/json2.js</a>, a handy library that gives you variable.stringify(). This will output a string of JSON representing your variable. I simply used this method to create a JSON string for the data I wanted, and used jQuery to set this as the value of a hidden input. Adding a json_decode() on the server allowed me to read this data directly as a variable.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> myJSONText = JSON.<span class="me1">stringify</span><span class="br0">&#40;</span>myObject, replacer<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>This method can be pretty useful if you are collecting object oriented data asynchronously and hoping to send that data back to the server. It seems like it ought to be built into jQuery at some point.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/json-output-from-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML playground for drupal</title>
		<link>http://danpolant.com/html-playground-for-drupal/</link>
		<comments>http://danpolant.com/html-playground-for-drupal/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 19:52:05 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drupal]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=430</guid>
		<description><![CDATA[Recently I had to build this. It&#8217;s a simple little app that uses the forms API. Think w3schools.com &#8220;try it yourself.&#8221;



function sg_games_menu&#40;&#41;&#123;


&#160;


&#160; &#160; &#160; &#160; $items&#91;&#8216;html-sandbox&#8217;&#93; = array&#40;


&#160; &#160; &#160; &#160; &#160; &#160; &#160;&#8216;title&#8217; =&#62; t&#40;&#8216;HTML sandbox&#8217;&#41;,


&#160; &#160; &#160; &#160; &#160; &#160; &#160;&#8216;page callback&#8217; =&#62; &#8216;sg_games_html&#8217;,


&#160; &#160; &#160; &#160; &#160; &#160; &#160;&#8216;access arguments&#8217; =&#62; array&#40;&#8216;access [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to build this. It&#8217;s a simple little app that uses the forms API. Think <a href="w3schools.com">w3schools.com</a> &#8220;try it yourself.&#8221;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> sg_games_menu<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$items</span><span class="br0">&#91;</span><span class="st0">&#8216;html-sandbox&#8217;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;title&#8217;</span> =&gt; t<span class="br0">&#40;</span><span class="st0">&#8216;HTML sandbox&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;page callback&#8217;</span> =&gt; <span class="st0">&#8216;sg_games_html&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;access arguments&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;access content&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;type&#8217;</span> =&gt; PAGE_CALLBACK,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$items</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> sg_games_html<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$output</span> = drupal_get_form<span class="br0">&#40;</span><span class="st0">&#8216;sg_games_html_form&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$output</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> sg_games_html_form<span class="br0">&#40;</span> <span class="re0">$form_state</span> <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> !<span class="re0">$output</span> = <span class="re0">$form_state</span><span class="br0">&#91;</span><span class="st0">&#8216;storage&#8217;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8216;values&#8217;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8216;editor&#8217;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$output</span> = <span class="st0">&#8216;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&lt;h1&gt;Try entering some html!&lt;/h1&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&#8216;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span><span class="br0">&#91;</span><span class="st0">&#8216;editor&#8217;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#type&#8217;</span> =&gt; <span class="st0">&#8216;textarea&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#title&#8217;</span> =&gt; t<span class="br0">&#40;</span><span class="st0">&#8216;Enter your HTML code here&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#default_value&#8217;</span> =&gt; <span class="re0">$output</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span><span class="br0">&#91;</span><span class="st0">&#8216;submit&#8217;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#type&#8217;</span> =&gt; <span class="st0">&#8216;submit&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#value&#8217;</span> =&gt; <span class="st0">&#8216;Try it!&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form</span><span class="br0">&#91;</span><span class="st0">&#8216;output&#8217;</span><span class="br0">&#93;</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#value&#8217;</span> =&gt; <span class="re0">$output</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#prefix&#8217;</span> =&gt; <span class="st0">&#8216;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&lt;div id=&quot;sg-games-output&quot;&gt;&#8217;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;#suffix&#8217;</span> =&gt; <span class="st0">&#8216;&lt;/div&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&#8216;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$form</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> sg_games_html_form_submit<span class="br0">&#40;</span><span class="re0">$form</span>, &amp;amp;<span class="re0">$form_state</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form_state</span><span class="br0">&#91;</span><span class="st0">&#8216;storage&#8217;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8216;values&#8217;</span><span class="br0">&#93;</span> = <span class="re0">$form_state</span><span class="br0">&#91;</span><span class="st0">&#8216;values&#8217;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$form_state</span><span class="br0">&#91;</span><span class="st0">&#8216;rebuild&#8217;</span><span class="br0">&#93;</span> = <span class="kw2">TRUE</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>All it really needs to do is hand off the form_state back into storage, rebuild the same form, and use part of the storage array as the default value of the editor field. </p>
<p>This one is really going to send shockwaves through the Drupal community <img src='http://danpolant.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/html-playground-for-drupal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: Force download of links in Magento</title>
		<link>http://danpolant.com/how-to-force-download-of-links-in-magento/</link>
		<comments>http://danpolant.com/how-to-force-download-of-links-in-magento/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 19:02:34 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[.htaccess]]></category>
		<category><![CDATA[Magento]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=413</guid>
		<description><![CDATA[It's an easy fix. I'll tell you about the hack way to do it, if you like doing it the right way have fun creating 7-8 files.]]></description>
			<content:encoded><![CDATA[<p>Real quick: find DownloadController.php and look at lines 78 &#8211; 81:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$contentDisposition</span> = <span class="re0">$helper</span>-&gt;<span class="me1">getContentDisposition</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">getResponse</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-&gt;<span class="me1">setHeader</span><span class="br0">&#40;</span><span class="st0">&#8216;Content-Disposition&#8217;</span>, <span class="re0">$contentDisposition</span> . <span class="st0">&#8216;; filename=&#8217;</span>.<span class="re0">$fileName</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Change line 80 (line 3 in this excerpt) to:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">-&gt;<span class="me1">setHeader</span><span class="br0">&#40;</span><span class="st0">&#8216;Content-Disposition&#8217;</span>, <span class="st0">&#8216;attachment&#8217;</span> . <span class="st0">&#8216;; filename=&#8217;</span>.<span class="re0">$fileName</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>That should make it work. As far as I know, the .htaccess solutions to this problem don&#8217;t work in Magento, because Magento encrypts the filename and extension, and the path does not represent a real location. </p>
<p>Note: I usually don&#8217;t go around hacking the core files of a CMS, but sometimes I make an exception with deep extensions within Magento because it just takes so darn long to follow the variables through to their various sources in the code. Not that I don&#8217;t know how to do it &#8230;</p>
<p>By the way, if you&#8217;re not using Magento and just have regular paths to your files, put this in your .htaccess:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">AddType application/octet-stream .mov .mp3 .zip</div>
</li>
</ol>
</div>
<p><a href="http://www.askapache.com/htaccess/htaccess.html">This</a> is one of the best .htaccess resources I&#8217;ve seen and it explains by example.</p>
<p>You can of course change the list of extension if you want. Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-force-download-of-links-in-magento/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pseudo Random Algorithm</title>
		<link>http://danpolant.com/pseudo-random-algorithm/</link>
		<comments>http://danpolant.com/pseudo-random-algorithm/#comments</comments>
		<pubDate>Thu, 27 May 2010 18:29:05 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=367</guid>
		<description><![CDATA[Ever wanted to program something that looks like a random outcome, but comes out the same way every time based on some kind of input? Here is one way. The modulus (%) operator is the key because it produces an output that seems to vary randomly. I had to implement something like this in Javascript.



function [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to program something that looks like a random outcome, but comes out the same way every time based on some kind of input? Here is one way. The modulus (%) operator is the key because it produces an output that seems to vary randomly. I had to implement something like this in Javascript.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> pseudoRandom<span class="br0">&#40;</span> seed, maximum <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; n = <span class="br0">&#40;</span><span class="nu0">314159265</span> * seed + <span class="nu0">2718281</span><span class="br0">&#41;</span> % <span class="nu0">65536</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; c = Math.<span class="me1">round</span><span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>n / <span class="nu0">65536</span><span class="br0">&#41;</span> * <span class="nu0">100</span><span class="br0">&#41;</span> % <span class="nu0">100</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; output = Math.<span class="me1">floor</span><span class="br0">&#40;</span> c/<span class="nu0">100</span> * maximum <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> output;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Simply provide a seed argument that is an integer that varies based on user input, or some state of your application. This function will output a number between 0 and the &#8216;maximum&#8217; argument. </p>
<p>I used this to create a simple Javascript game. It could be applied to any situation where you want to present  the user with outcomes that feel random but are consistent based on what they do. Of course there are limits to how random these outcomes actually are. But in general it works well enough to give the experience of randomness.</p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/pseudo-random-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R integration with PHP</title>
		<link>http://danpolant.com/r-integration-with-php/</link>
		<comments>http://danpolant.com/r-integration-with-php/#comments</comments>
		<pubDate>Mon, 17 May 2010 18:10:07 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=329</guid>
		<description><![CDATA[Recently, I needed to do some statistical tests on data from within a Drupal Module. Having found no PHP libraries suitable for the task, I decided to try and run code in R from within PHP. This post helped out a great deal.
As far as I can tell, the only way to do this is [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I needed to do some statistical tests on data from within a Drupal Module. Having found no PHP libraries suitable for the task, I decided to try and run code in R from within PHP. <a href="http://http://www.stanford.edu/~mjockers/cgi-bin/drupal/node/25">This post</a> helped out a great deal.</p>
<p>As far as I can tell, the only way to do this is to write the R output to a temporary file. So to start off, I create a new temp file with:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$path</span> = <a href="http://www.php.net/tempnam"><span class="kw3">tempnam</span></a><span class="br0">&#40;</span><span class="st0">&#8216;/tmp&#8217;</span>, <span class="st0">&#8216;tmp&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This function, in case you&#8217;ve never seen it, creates a file with a random name in the directory specified by the first argument, with the extension specified by the second argument.</p>
<p>Next, I loaded up some R. Here&#8217;s what I did:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$rcode</span> = &lt;&lt;&lt;EOD</div>
</li>
<li class="li1">
<div class="de1">library<span class="br0">&#40;</span>RMySQL<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">con &lt;- dbConnect<span class="br0">&#40;</span>dbDriver<span class="br0">&#40;</span><span class="st0">&#8216;MySQL&#8217;</span><span class="br0">&#41;</span>, dbname = <span class="st0">&#8216;sg_drupal&#8217;</span>,</div>
</li>
<li class="li2">
<div class="de2">username=<span class="st0">&#8216;xxxx&#8217;</span>, password=<span class="st0">&#8216;xxxxxx&#8217;</span>,host=<span class="st0">&#8216;localhost&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">data &lt;- dbGetQuery<span class="br0">&#40;</span>con,statement=<span class="st0">&#8216;$sql&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">x = data<span class="br0">&#91;</span>,<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#91;</span>data<span class="br0">&#91;</span><span class="st0">&#8216;x&#8217;</span><span class="br0">&#93;</span>==<span class="re0">$a</span><span class="br0">&#93;</span></div>
</li>
<li class="li2">
<div class="de2">y = data<span class="br0">&#91;</span>,<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#91;</span>data<span class="br0">&#91;</span><span class="st0">&#8216;x&#8217;</span><span class="br0">&#93;</span>==<span class="re0">$b</span><span class="br0">&#93;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">x = <span class="kw1">as</span>.numeric<span class="br0">&#40;</span>x<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">y = <span class="kw1">as</span>.numeric<span class="br0">&#40;</span>y<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">ttest = t.test<span class="br0">&#40;</span>x,y<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">library<span class="br0">&#40;</span>rjson<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">ttest = toJSON<span class="br0">&#40;</span>ttest<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">cat<span class="br0">&#40;</span>ttest, <a href="http://www.php.net/file"><span class="kw3">file</span></a>=<span class="st0">&#8216;$path&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">EOD;</div>
</li>
</ol>
</div>
<p>First, you connect to your database in R. This is a lot easier than trying to pass in a result set &#8211; you could use JSON to do this if you really needed to. But R runs MySQL queries just fine, and puts them into neatly accessible vectors. Don&#8217;t forget to load the RMySQL library before doing your queries. I believe this comes with the standard package of R. </p>
<p>One word of warning &#8211; be careful of using the $ sign in your R code string. There may be a way to escape it, but I couldn&#8217;t (quickly) find a way to use this character without PHP trying to parse it as a variable. Fortunately, R has substitute ways to access vectors of a dataset.</p>
<p>Here I&#8217;d like to point out that R has a unique functionality that makes it better than PHP for manipulating and traversing raw data. Without having to write a loop, R has functions that can create new vectors out of the matching values of existing vectors, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">x = data<span class="br0">&#91;</span>,<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#91;</span>data<span class="br0">&#91;</span><span class="st0">&#8216;x&#8217;</span><span class="br0">&#93;</span>==<span class="re0">$a</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>This command means &#8220;assign the second vector of dataset &#8216;data&#8217; where the &#8216;x&#8217; vector equals $a, to variable x.&#8221;</p>
<p>After loading the rjson library, you can turn your output, in this case a T-test, into JSON notation. JSON is a special format that makes data readable by PHP and Javascript as an object or array. Finally, </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">cat<span class="br0">&#40;</span>ttest, <a href="http://www.php.net/file"><span class="kw3">file</span></a>=<span class="st0">&#8216;$path&#8217;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>writes the JSON output to the temp file we created. But none of this has been executed yet. So let&#8217;s say:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/exec"><span class="kw3">exec</span></a><span class="br0">&#40;</span><span class="st0">&quot;echo <span class="es0">\&quot;</span>$rcode<span class="es0">\&quot;</span> | /usr/bin/R &#8211;vanilla&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$back</span> = <a href="http://www.php.net/file_get_contents"><span class="kw3">file_get_contents</span></a><span class="br0">&#40;</span><span class="re0">$path</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$back</span> = json_decode<span class="br0">&#40;</span><span class="re0">$back</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>First, this runs the R code as a UNIX command on your server. Then, it reads the contents of the temp file and JSON decodes them. The second &#8220;true&#8221; argument of json_decode returns the data as an array rather than an object. This is important because R will often name its properties with spaces and other illegal characters for object properties in PHP. There is probably a way to escape or change these characters, but I couldn&#8217;t find it. The array will work just as well as the object though.</p>
<p>I thought this was pretty cool. Using this method, you could run statistics on anything in your database and display it on a web page. This seems like a clever way to display usage statistics to site admins, and actually give them an idea of whether the results are statistically significant. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/r-integration-with-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to: Make sure Buddypress loads first</title>
		<link>http://danpolant.com/how-to-make-sure-buddypress-loads-first/</link>
		<comments>http://danpolant.com/how-to-make-sure-buddypress-loads-first/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 22:18:31 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[class]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=259</guid>
		<description><![CDATA[Often, it doesn&#8217;t matter whether your Buddypress plugin loads before or after Buddypress. However, if you are using groups API, or extending other Buddypress plugins, your class extensions require that Buddypress be loaded beforehand. Functions hooked into actions that occur before Buddypress loads that use Buddypress functions will cause problems as well if the load [...]]]></description>
			<content:encoded><![CDATA[<p>Often, it doesn&#8217;t matter whether your Buddypress plugin loads before or after Buddypress. However, if you are using groups API, or extending other Buddypress plugins, your class extensions require that Buddypress be loaded beforehand. Functions hooked into actions that occur before Buddypress loads that use Buddypress functions will cause problems as well if the load order is wrong.</p>
<p>WordPress loads plugins in alphabetical order. So the simplest (and most janky) solution is to name your plugin something that comes after &#8220;buddypress&#8221; in the alphabet. But there are better methods that you should use instead.</p>
<h4>For versions before BP 1.2</h4>
<p>The goal here is to check whether Buddypress has been loaded at the point when your plugin loads, and if it has not, load it manually. The manual load will prevent the normal load from happening later, so you won&#8217;t get double what you need.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//hack required to load BP first</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> bpgc_load_buddypress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//buddypress is loaded</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <a href="http://www.php.net/function_exists"><span class="kw3">function_exists</span></a><span class="br0">&#40;</span> <span class="st0">&#8216;bp_core_setup_globals&#8217;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Get the list of active sitewide plugins </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$active_sitewide_plugins</span> = maybe_unserialize<span class="br0">&#40;</span> get_site_option<span class="br0">&#40;</span> <span class="st0">&#8216;active_sitewide_plugins&#8217;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$bp_activated</span> = <span class="re0">$active_sitewide_plugins</span><span class="br0">&#91;</span><span class="st0">&#8216;buddypress/bp-loader.php&#8217;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//bp is not activated</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> !<span class="re0">$bp_activated</span> <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//bp is activated but not yet loaded</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="re0">$bp_activated</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//load bp if its not activated</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">if</span> <span class="br0">&#40;</span> bpgc_load_buddypress<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require_once</span><span class="br0">&#40;</span> WP_PLUGIN_DIR . <span class="st0">&#8216;/buddypress/bp-loader.php&#8217;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h4>If you are using BP 1.2+</h4>
<p>Fortunately, they made this much easier to do in BP 1.2. For my plugin, all I needed to do was:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> bpgc_bp_loaded<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">require</span> <span class="br0">&#40;</span> WP_PLUGIN_DIR . <span class="st0">&quot;/BP-Group-Control/bpgc-classes.php&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">add_action<span class="br0">&#40;</span><span class="st0">&#8216;bp_init&#8217;</span>, <span class="st0">&#8216;bpgc_bp_loaded&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>&#8230; in order to load my classes only once Buddypress has finished loading. Probably the safest way to ensure that your plugin completely loads after Buddypress is to create a loader.php file that contains this action and loads all of your other files. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-make-sure-buddypress-loads-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to: use email addresses as usernames in Buddypress</title>
		<link>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/</link>
		<comments>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 01:13:43 +0000</pubDate>
		<dc:creator>dan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[buddypress]]></category>
		<category><![CDATA[email address]]></category>

		<guid isPermaLink="false">http://danpolant.com/?p=202</guid>
		<description><![CDATA[Just spent about 5 hours on this one, enjoy &#8230;
I&#8217;ve seen this question come up on a lot of forums &#8211; people want to use email addresses as usernames in Buddypress. It makes sense &#8211; emails are easy to remember. But Buddypress has made this difficult to do. Andy Peatling added the following constant to [...]]]></description>
			<content:encoded><![CDATA[<p>Just spent about 5 hours on this one, enjoy &#8230;</p>
<p>I&#8217;ve seen this question come up on a lot of forums &#8211; people want to use email addresses as usernames in Buddypress. It makes sense &#8211; emails are easy to remember. But Buddypress has made this difficult to do. Andy Peatling added the following constant to the core:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span> <span class="st0">&#8216;BP_ENABLE_USERNAME_COMPATIBILITY_MODE&#8217;</span>, <span class="kw2">true</span> <span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>in order to deal with part of this problem. But this did not solve the problem completely. See <a href="http://codex.buddypress.org/how-to-guides/changing-internal-configuration-settings/">this article</a> for how to use this. This changed allowed users to navigate to an @-containing username&#8217;s profile, but that user could not do things like create groups or edit his profile.</p>
<p>It turns out there&#8217;s another step to getting this to work. You have to override <code>wp_sanitize_redirect</code> in wp-includes/pluggable.php and add &#8216;@&#8217; to the list of OK characters, like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> wp_sanitize_redirect<span class="br0">&#40;</span><span class="re0">$location</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span><span class="st0">&#8216;|[^a-z0-9-~+_.?#=&amp;;,/:%!@]|i&#8217;</span>, <span class="st0">&#8221;</span>, <span class="re0">$location</span><span class="br0">&#41;</span>; <span class="co1">//this line needs to have an @</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = wp_kses_no_null<span class="br0">&#40;</span><span class="re0">$location</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// remove %0d and %0a from location</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$strip</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%0d&#8217;</span>, <span class="st0">&#8216;%0a&#8217;</span>, <span class="st0">&#8216;%0D&#8217;</span>, <span class="st0">&#8216;%0A&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$location</span> = _deep_replace<span class="br0">&#40;</span><span class="re0">$strip</span>, <span class="re0">$location</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$location</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now it should work. </p>
]]></content:encoded>
			<wfw:commentRss>http://danpolant.com/how-to-use-email-addresses-as-usernames-in-buddypress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

