theoleschool - 6.x http://theoleschool.com/tags/6x en Using hook_views_alter to Add a GROUP BY Statement http://theoleschool.com/blog/using-hookviewsalter-add-group-statement <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p><em>I should note this post deals with Views 2.x on Drupal 6.x</em></p> <p>When Views doesn't quite spit out the query we are looking for we can turn to <span class="geshifilter"><code class="php geshifilter-php">hook_views_query_alter<span style="color: #009900;">(</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$view</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$query</span><span style="color: #009900;">)</span></code></span> to nudge it in the right direction. This is by no means news, and there are many a blog post detailing various ways to alter the query using this function. However, recently, I ran into an odd issue when attempting to add a <span class="geshifilter"><code class="sql geshifilter-sql"><span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span></code></span> statement.</p> <p>The <a href="http://drupal.org/node/385158">issue</a> can be observed when attempting to use the <a href="http://views.doc.logrus.com/classviews__query.html#089b7eab81ec9cb4b24aa489ff4ac448"><span class="geshifilter"><code class="php geshifilter-php">add_groupby</code></span> function</a> to a field in the query. As an example, say we have a node view with a few fields (node id, node title, and user id). This will give a simple query:</p> <div class="geshifilter"><div class="sql geshifilter-sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> node<span style="color: #66cc66;">.</span>nid <span style="color: #993333; font-weight: bold;">AS</span> nid<span style="color: #66cc66;">,</span> <br />        node<span style="color: #66cc66;">.</span>title <span style="color: #993333; font-weight: bold;">AS</span> node_title<span style="color: #66cc66;">,</span> <br />        users<span style="color: #66cc66;">.</span>uid <span style="color: #993333; font-weight: bold;">AS</span> users_uid <br /><span style="color: #993333; font-weight: bold;">FROM</span> node node  <br /><span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> users users <span style="color: #993333; font-weight: bold;">ON</span> node<span style="color: #66cc66;">.</span>uid <span style="color: #66cc66;">=</span> users<span style="color: #66cc66;">.</span>uid</div></div> <p>Now, say we want to do something silly and get this query to group by user id. A node view does not allow for this, so we turn to <span class="geshifilter"><code class="php geshifilter-php">hook_views_query_alter</code></span>.</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_module_views_query_alter<span style="color: #66cc66;">(</span><span style="color: #66cc66;">&amp;</span><span style="color: #0000ff;">$view</span>, <span style="color: #66cc66;">&amp;</span><span style="color: #0000ff;">$query</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$view</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">name</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />     <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'my_view_name'</span>:<br />       <span style="color: #808080; font-style: italic;">//add the group by  on the user id field </span><br />       <span style="color: #0000ff;">$query</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">add_groupby</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'users_uid'</span><span style="color: #66cc66;">)</span>;<br />       <span style="color: #b1b100;">break</span>;<br />   <span style="color: #66cc66;">}</span><br /><span style="color: #66cc66;">}</span></div></div> <p>We expect to see a nice <span class="geshifilter"><code class="sql geshifilter-sql"><span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> users_uid</code></span> tacked on to the end of our query, but instead there is a disturbing <span class="geshifilter"><code class="sql geshifilter-sql"><span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> nid<span style="color: #66cc66;">,</span> node_title<span style="color: #66cc66;">,</span> users_uid</code></span>. What gives? Merlinofchaos <a href="http://drupal.org/node/385158#comment-1296924">clarifies</a> for us:</p> <p><em>"When using GROUP BY items that appear in the SELECT must either use aggregate functions OR appear in the GROUP BY. This is enforced by postgres, therefore Views must enforce this."</em></p> <p>It is good that Views is looking out for the rules of postgres; it's what helps make it the powerful and flexible tool it is. In this application though, I am not worried about postgres and simply want to alter a single Views query on an sql database. I know that I can pass an 'aggregate' flag to the fields in the query object to denote that they use an aggregate function, even if they do not. Worth a try...</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> dsic_judging_views_query_alter<span style="color: #66cc66;">(</span><span style="color: #66cc66;">&amp;</span><span style="color: #0000ff;">$view</span>, <span style="color: #66cc66;">&amp;</span><span style="color: #0000ff;">$query</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #b1b100;">switch</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$view</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">name</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />     <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'dsic_judge_incomplete'</span>:<br />       <span style="color: #808080; font-style: italic;">//loop through the fields and add the aggregate indicator</span><br />       <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$query</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">fields</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$key</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$field</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />         <span style="color: #0000ff;">$query</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">fields</span><span style="color: #66cc66;">[</span><span style="color: #0000ff;">$key</span><span style="color: #66cc66;">]</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'aggregate'</span><span style="color: #66cc66;">]</span> = <span style="color: #000000; font-weight: bold;">TRUE</span>;<br />       <span style="color: #66cc66;">}</span><br />       <span style="color: #0000ff;">$query</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">add_groupby</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'users_uid'</span><span style="color: #66cc66;">)</span>;<br />       <span style="color: #b1b100;">break</span>;<br />   <span style="color: #66cc66;">}</span><br /><span style="color: #66cc66;">}</span></div></div> <p>Turns out this works! Jumping back to our view we see the query we are after:</p> <div class="geshifilter"><div class="sql geshifilter-sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> node<span style="color: #66cc66;">.</span>nid <span style="color: #993333; font-weight: bold;">AS</span> nid<span style="color: #66cc66;">,</span> <br />        node<span style="color: #66cc66;">.</span>title <span style="color: #993333; font-weight: bold;">AS</span> node_title<span style="color: #66cc66;">,</span> <br />        users<span style="color: #66cc66;">.</span>uid <span style="color: #993333; font-weight: bold;">AS</span> users_uid <br /><span style="color: #993333; font-weight: bold;">FROM</span> node node  <br /><span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> users users <span style="color: #993333; font-weight: bold;">ON</span> node<span style="color: #66cc66;">.</span>uid <span style="color: #66cc66;">=</span> users<span style="color: #66cc66;">.</span>uid<br /><span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> users_uid</div></div> <p>If it looks like a hack, and quacks like a hack, then it's probably a... well ya know. However, I don't have big qualms with implementing this. Views behavior is in place to accommodate a database that, in this instance, is not being used. Further, I am already using a hook function to modify a view beyond what that view is capable of spitting out on its own. Lastly, I am able to accomplish this without modifying Views core. All of that adds up to a valid use case and helps me to sleep at night.</p></div></div></div> Mon, 12 Mar 2012 15:09:21 +0000 rho 159 at http://theoleschool.com Viewing Users Who Have Not Submitted a Webform http://theoleschool.com/blog/viewing-users-who-have-not-submitted-webform <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>On a recent Drupal 6 site, I received a request to build a view displaying a list of users who had not submitted a specific webform. <a href="http://drupal.org/project/webform&quot;">Webform</a> of course, has some very nice reporting as well as decent views integration for submissions. However, to view users that have yet to make a submission takes some creative views argument handling.</p> <p>I began by building a basic user view. I then set the style and fields to my liking, and filtered on the specific role I was looking for. So now we have views pulling a list of users that we wish to check against. To find out who has not submitted the webform, we first have to take a look at who has.</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;">webform_get_submissions<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$filters</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>, <span style="color: #0000ff;">$header</span> = <span style="color: #000000; font-weight: bold;">NULL</span>, <span style="color: #0000ff;">$pager_count</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">)</span>;</div></div> <p>Webform has the above function in the <span class="geshifilter"><code class="php geshifilter-php">webform<span style="color: #339933;">.</span>submission<span style="color: #339933;">.</span>inc</code></span> file that does just this. You can pass a node id to <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #000088;">$filters</span></code></span> and the function will return a nice array of submission objects, detailing who has submitted the form. We can then add an argument of type <em>"User: UID"</em> to our view, select <em>"Provide default argument"</em> below "Action to take if argument is not present," and select <em>"PHP Code"</em> under "Default argument type." Here we can provide a code snippet to return a list of User ids of users that have submitted the form. </p><div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><a href="http://api.drupal.org/api/function/module_load_include/6"><span style="color: #000066;">module_load_include</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'inc'</span>, <span style="color: #ff0000;">'webform'</span>, <span style="color: #ff0000;">'includes/webform.submissions'</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #0000ff;">$submission_array</span> = webform_get_submissions<span style="color: #66cc66;">(</span><span style="color: #808080; font-style: italic;">/*node id of webform we are checking for*/</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #0000ff;">$uids</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$submission_array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$submission</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #0000ff;">$uids</span><span style="color: #66cc66;">[</span><span style="color: #66cc66;">]</span> = <span style="color: #0000ff;">$submission</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">uid</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #b1b100;">return</span> <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">','</span>, <span style="color: #0000ff;">$uids</span><span style="color: #66cc66;">)</span>;</div></div> <p>This will gives us a comma separated string of user ids. The default "Validator options" are fine but <strong>be sure</strong> to check <em>"Allow multiple terms per argument"</em> and <em>"Exclude the argument."</em> After updating, your view now filters down to users that have not submitted the webform! To double check that this is happening, you can look at the query below the preview. Near the end of the query in the <span class="geshifilter"><code class="sql geshifilter-sql"><span style="color: #993333; font-weight: bold;">WHERE</span></code></span> clause, you should see something like <span class="geshifilter"><code class="sql geshifilter-sql">users<span style="color: #66cc66;">.</span>uid <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">(</span><span style="color: #cc66cc;">110</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">13028</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">210</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">107</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">35329</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">35330</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">35331</span><span style="color: #66cc66;">)</span></code></span>. The list of numbers are the user ids that are being filtered. If no one has submitted the form, then this of course will be empty.</p> <p>Now I admit, this is kinda gross. I shudder any time I include php in configuration. It is, however, a very straight forward way to get the results requested while staying in views, therefore leaving the output in the hands of a site builder/administrator. There may be some clever ways to achieve the same thing using something like <a href="http://drupal.org/project/webform_report">Webform Report</a>, or even a different views configuration, but nothing jumped out at me. However, if you have solved this in a different way I would love to hear about it.</p></div></div></div> Sun, 04 Mar 2012 18:25:05 +0000 rho 158 at http://theoleschool.com Using the Data Module's "Relate to Node" Functionality http://theoleschool.com/blog/using-data-modules-relate-node-functionality <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>While I'm <a href="/blog/adopting-table-schema-using-data-module">on the topic</a> of the <a href="http://drupal.org/project/data">Data module</a>, I'd like to share my thoughts on some of the confusion surrounding use of the Data Node module included with the package. (see issues <a href="http://drupal.org/node/976292">976292</a> and <a href="http://drupal.org/node/898562">898562</a>)</p> <p>It's my opinion that the bulk of the functionality in the Data Node module has been intentionally "left undone." Instead, the module is giving us the tools to allow us to implement node relationships as needed, ourselves (think API). My thinking is that this was done to allow the maximum amount of flexibility in relating other data to nodes by minimizing assumptions about what the relationship should be. <em>I have in no way validated this with any of the module maintainers, nor is it denoted in any documentation that I am aware of.</em> What follows is my "average" use case for this module, and some thoughts on more advanced implementations.</p> <img src="/sites/default/files/u3/relate-to-node-form.png" style="float:right; margin: 0 0 0 10px; width:250px;" /><p>When enabled, this functionality is apparent in Drupal's admin UI at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>edit<span style="color: #339933;">/%</span>table_name<span style="color: #339933;">/</span>node</code></span> by clicking on the edit link next to a table at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>overview</code></span> then clicking the "Relate to Nodes" tab. This page presents a form asking you which content type you would like the table related to, and the column name of the id key in the table you are referencing. Simple right? Well sorta... When you submit this form and happily skip over to views to reference the node table, it quickly becomes apparent that nothing has happened. Checking the <span class="geshifilter"><code class="php geshifilter-php">data_table_node</code></span> table in the database will reveal it to be empty. So what's up here? Is this just an unfinished piece?</p> <p>After taking a look in the data_node.module things start to make a bit more sense. At first glance we see some tantalizing functions, especially:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**<br />  * Add a relationship between a data table and a node.<br />  */</span><br /><span style="color: #000000; font-weight: bold;">function</span> data_node_add<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #0000ff;">$save</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><br />     <span style="color: #ff0000;">'data_table_name'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$table</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">)</span>,<br />     <span style="color: #ff0000;">'id'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$id</span>,<br />     <span style="color: #ff0000;">'nid'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$nid</span>,<br />   <span style="color: #66cc66;">)</span>;<br />   <span style="color: #b1b100;">return</span> <a href="http://api.drupal.org/api/function/drupal_write_record/6"><span style="color: #000066;">drupal_write_record</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'data_table_node'</span>, <span style="color: #0000ff;">$save</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #808080; font-style: italic;">/**<br />  * Remove a relationship between a data table and a node.<br />  */</span><br /><span style="color: #000000; font-weight: bold;">function</span> data_node_remove<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <a href="http://api.drupal.org/api/function/db_query/6"><span style="color: #000066;">db_query</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"DELETE FROM {data_table_node} WHERE data_table_name = '%s' AND id = %d AND nid = %d"</span>, <span style="color: #0000ff;">$table</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">)</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span></div></div> <p>Typically I'll stop right here. I really only use the Data module for custom reporting, and when doing so I handle the CRUD functions for the new data type myself. My data type's row reference to nodes are nearly always a 1 to 1 mapping. To further simplify things for myself, the <span class="geshifilter"><code class="php geshifilter-php"><span style="color: #000088;">$id</span></code></span> I use in my custom data type is often the node id of it's intended reference. This makes my calls to these functions look something like:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;">  <span style="color: #808080; font-style: italic;">//insert my_table with a nid key</span><br />   ...<br />   <span style="color: #808080; font-style: italic;">//inform Data Node of the reference</span><br />   <span style="color: #0000ff;">$my_table_object</span> = data_get_table<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'my_table'</span><span style="color: #66cc66;">)</span>;<br />   data_node_add<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$my_table_object</span>, <span style="color: #0000ff;">$nid</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>;</div></div> <p>This will populate the <span class="geshifilter"><code class="php geshifilter-php">data_table_node</code></span> table so that when we use the relationship in out data type view it will return results. Of course deletion will be handled similarly.</p> <p>This is admittedly a somewhat basal interaction with this module, though it is often enough to satisfy my needs. However Data Node allows for much more complexity and has a certain level of user interaction built in just below the surface.</p> <p>It's not until we start thinking about the data structures involved that the scope of the allowed complexity becomes apparent. It is easy to assume that referencing a data type to a node is always going to be 1 to 1 (much like the typical use of CCK's Node Reference.) However, this may not always be the case. It is not required that the data table's id be the nid of the node it is referencing. It only has to be unique within the table. Because of this, a large field of possibilities emerge. You may have data type rows referencing many nodes each (1 to many), many data type rows referencing a single node (many to 1), or, of course, any number of data type rows referencing any number of nodes (many to many). The <span class="geshifilter"><code class="php geshifilter-php">data_table_node</code></span> table along with the provided CRUD functions, like the ones mentioned above, will allow for this. I think that this flexibility is a big reason that the module has been left so open.</p> <p>Lastly, a closer look at the Data Node module will reveal some rather fancy display functions for the underlying relationship CRUD.</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**<br />  * Render an add link for a given item..<br />  */</span><br /><span style="color: #000000; font-weight: bold;">function</span> data_node_render_add_link<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <a href="http://api.drupal.org/api/function/drupal_add_css/6"><span style="color: #000066;">drupal_add_css</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data_node'</span><span style="color: #66cc66;">)</span> . <span style="color: #ff0000;">'/data_node.css'</span><span style="color: #66cc66;">)</span>;<br />   <a href="http://api.drupal.org/api/function/drupal_add_js/6"><span style="color: #000066;">drupal_add_js</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data_node'</span><span style="color: #66cc66;">)</span> . <span style="color: #ff0000;">'/data_node.js'</span><span style="color: #66cc66;">)</span>;<br /><br />   <span style="color: #0000ff;">$title</span> = _data_node_get_title<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #0000ff;">$table_name</span> = <span style="color: #0000ff;">$table</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">"data_node_link-{$table_name}-{$id}-{$nid}"</span>;<br />   <span style="color: #b1b100;">return</span> <a href="http://api.drupal.org/api/function/l/6"><span style="color: #000066;">l</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/t/6"><span style="color: #000066;">t</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Add to !title'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'!title'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$title</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>, data_node_add_path<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'attributes'</span> =<span style="color: #66cc66;">&gt;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'class'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #ff0000;">"data-node-add $class"</span><span style="color: #66cc66;">)</span>, <span style="color: #ff0000;">'query'</span> =<span style="color: #66cc66;">&gt;</span> <a href="http://api.drupal.org/api/function/drupal_get_destination/6"><span style="color: #000066;">drupal_get_destination</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span><br /><br /><span style="color: #808080; font-style: italic;">/**<br />  * Render a remove link for a given item.<br />  */</span><br /><span style="color: #000000; font-weight: bold;">function</span> data_node_render_remove_link<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <a href="http://api.drupal.org/api/function/drupal_add_css/6"><span style="color: #000066;">drupal_add_css</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data_node'</span><span style="color: #66cc66;">)</span> . <span style="color: #ff0000;">'/data_node.css'</span><span style="color: #66cc66;">)</span>;<br />   <a href="http://api.drupal.org/api/function/drupal_add_js/6"><span style="color: #000066;">drupal_add_js</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data_node'</span><span style="color: #66cc66;">)</span> . <span style="color: #ff0000;">'/data_node.js'</span><span style="color: #66cc66;">)</span>;<br /><br />   <span style="color: #0000ff;">$title</span> = _data_node_get_title<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #0000ff;">$table_name</span> = <span style="color: #0000ff;">$table</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">get</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #0000ff;">$class</span> = <span style="color: #ff0000;">"data_node_link-{$table_name}-{$id}-{$nid}"</span>;<br />   <span style="color: #b1b100;">return</span> <a href="http://api.drupal.org/api/function/l/6"><span style="color: #000066;">l</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/t/6"><span style="color: #000066;">t</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Remove from !title'</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'!title'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #0000ff;">$title</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>, data_node_remove_path<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$table</span>, <span style="color: #0000ff;">$id</span>, <span style="color: #0000ff;">$nid</span><span style="color: #66cc66;">)</span>, <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'attributes'</span> =<span style="color: #66cc66;">&gt;</span> <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'class'</span> =<span style="color: #66cc66;">&gt;</span> <span style="color: #ff0000;">"data-node-remove $class"</span><span style="color: #66cc66;">)</span>, <span style="color: #ff0000;">'query'</span> =<span style="color: #66cc66;">&gt;</span> <a href="http://api.drupal.org/api/function/drupal_get_destination/6"><span style="color: #000066;">drupal_get_destination</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span></div></div> <p>These are the two pertinent functions. They both return a link that will hit a page callback to do just what you would expect. Add or remove a node relationship. This allows you "expose" the functionality to the user. The link could be placed on a view or node page to allow users to add or remove the relation to a specific data type themselves, "on the fly". This, in combination with the flexibility of the relationship itself, lends to a huge variety of possible use cases with relatively few function calls.</p> <p>So wrapping this up. Data Node module is best thought of as an API. It doesn't do so much right out of the box, but with minimal implementation it becomes a fairly powerful tool.</p></div></div></div> Sun, 04 Mar 2012 03:44:18 +0000 rho 157 at http://theoleschool.com Adopting a Table with Schema using Data Module http://theoleschool.com/blog/adopting-table-schema-using-data-module <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>When working with medium to larger sized web applications, inevitably the requirement comes up for some advanced data reporting. The spectrum of requests here can obviously be pretty broad and is often beyond what you can accomplish with the default tables provided by Drupal and the installed modules. This tends to lead us to creating custom tables to track and manage the requested reports. Along with writing all of the necessary <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> functions to make sure our new table is managing data appropriately, we also have to provide some sort of display or an export. Views can handle this latter requirement quite nicely. So it is here that we can turn to modules like <a href="http://drupal.org/project/data">Data</a> or the now depricated <a href="http://drupal.org/project/tw">Table Wizard</a> to bring our new table under views "control." This gives us the added bonus of taking the display configuration out of the hands of the developer and placing it into the site builder/administrator's domain.</p> <p>In a recent project just such a reporting request came up. The report was fairly complex and was to be visible to a larger subset of users. I wound up managing this report with a custom table and I turned to Data module to allow views to handle the output. After installing my custom module which created the custom table, I navigated to <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>adopt</code></span>. Expecting to see my table in the list of "adoptable" tables, I was a bit confused when it was nowhere to be found. After <a href="http://drupal.org/node/888946#comment-3360326">a bit of digging</a> I realized that tables declared using <a href="http://drupal.org/node/146843">Schema API</a> were considered "owned" by the module and not included in the list of adoptable tables. This makes some sense as you don't want just any table adopted by Data. Just imagine when an admin who's not quite sure what he or she is doing, decides to adopt and then drop the node table. I'd rather not...</p> <p>So this leaves us with the question, <em>"How do I get Data module to adopt my table?"</em> From what I can tell, the creators of the data module expect us to use the Data API to handle table creation and CRUD. Personally I don't like the idea of this. I already understand and like working with Drupal's core Schema API, and don't want to adopt another layer of abstraction. I only want to utilize Data module to throw my table up to views. So after looking through how adoption is handled in the module, I found that really there is nothing preventing Data from adopting these schema declared tables. They are simply being made unavailable to the form at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>adopt</code></span>. This lead me to attempt to adopt my table right after it is generated in my module's .install file. This wound up working quite nicely and is implemented as follows:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_module_install<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #808080; font-style: italic;">//install schema as normal</span><br />   <a href="http://api.drupal.org/api/function/drupal_install_schema/6"><span style="color: #000066;">drupal_install_schema</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'my_table'</span><span style="color: #66cc66;">)</span>;<br />   <br />   <span style="color: #808080; font-style: italic;">//make sure we have the data module</span><br />   <span style="color: #b1b100;">include_once</span><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data'</span><span style="color: #66cc66;">)</span> .<span style="color: #ff0000;">'/data.module'</span><span style="color: #66cc66;">)</span>;<br />   data_include<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'DataTable'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #808080; font-style: italic;">//instanciate the table we just created with the DataTable class</span><br />   <span style="color: #0000ff;">$my_table_object</span> = DataTable::<span style="color: #006600;">instance</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'my_table'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #808080; font-style: italic;">//tell Data module to adopt the table</span><br />   <span style="color: #0000ff;">$my_table_object</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">adopt</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br />   DataTable::<span style="color: #006600;">clearCaches</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span></div></div> <p>After installing the module you should be able to see your custom table at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data</code></span>. Data module is now obviously a dependency so be sure that it is installed and enabled before installing your custom module.</p> <p>I realize that I could write my own views handlers for the custom table data I am providing, but I have found that to be a fairly time consuming (and often esoteric) task. When all we are after is a single, somewhat unique report, I feel that integration with the Data module and it's corresponding integration with views is simple, quick, and more than adequate.</p></div></div></div> Sat, 03 Mar 2012 20:19:36 +0000 rho 156 at http://theoleschool.com Quick Stock Quote on Page Refresh http://theoleschool.com/blog/quick-stock-quote-page-refresh-0 <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>I thought I would throw a post up, demonstrating a "quick and easy" use case of the <a href="http://drupal.org/project/stockapi">Stock API module</a>, and simultaneously, give this site's <a href="http://qbnz.com/highlighter/">GeSHi filter</a> a quick test run.</p> <p>A few days ago I encountered a site requirement asking for a simple informative stock quote block on a Drupal 6.x installation. The format required was something like: <br /><em>NFLX 208.75 -1.30 Sep 13 7:30 EST</em> <br /> My initial thought was to enable the <a href="http://drupal.org/project/stock">Stock module</a> and simply configure and theme the built in block. The stock module will allow the management of multiple stocks on a per-user basis, and provides a block that displays this information in a table. There is no doubt that stock module is useful but in this instance it is overkill. All we are after is a simple quote from a single unchanging stock symbol displayed in a single line of text.</p> <p>So let's take a short step back to the stock API module. There is really one "go to" function when grabbing a quote from the stock api and that is <span class="geshifilter"><code class="php geshifilter-php">stockapi_load<span style="color: #009900;">(</span><span style="color: #000088;">$symbol</span><span style="color: #009900;">)</span></code></span>. This function is defined like so:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> stockapi_load<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />     <span style="color: #0000ff;">$stock</span> = <a href="http://api.drupal.org/api/function/db_fetch_array/6"><span style="color: #000066;">db_fetch_array</span></a><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/db_query/6"><span style="color: #000066;">db_query</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"SELECT * FROM {stockapi} WHERE symbol = '%s'"</span>, <span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;<br />     <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><span style="color: #0000ff;">$stock</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />       <span style="color: #0000ff;">$stock</span> = stockapi_fetch<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span>;<br />       <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$stock</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />         <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">]</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'N/A'</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />           <span style="color: #808080; font-style: italic;">// Date is 'N/A' means an invalid stock symbol</span><br />           stockapi_save<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$stock</span><span style="color: #66cc66;">)</span>;<br /><br />           <span style="color: #808080; font-style: italic;">// Load again so it can show for the first time</span><br />           <span style="color: #0000ff;">$stock</span> = stockapi_load<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span>;<br />         <span style="color: #66cc66;">}</span><br />       <span style="color: #66cc66;">}</span><br />     <span style="color: #66cc66;">}</span><br />   <span style="color: #66cc66;">}</span><br />   <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$stock</span>;<br /><span style="color: #66cc66;">}</span></div></div> <p>As you can see the stock API module is wanting to store stock information in the database to be updated on cron runs. It does this as a type of database caching. This way if a site is pulling information on many stocks, potentially every page load, compounded by many users the API can retrieve the information with a simple query to the local database. Not only is this method much faster than hitting the third party service (yahoo) every time, it is able to serve saved information (especially handy if that service happens to be down or a connection cannot be established). This feature along with returning a nice keyed array of stock information is reason enough to love this function. However, I think we could go a bit simpler still. We only need information on one stock site wide, and in this instance that information should be as current as possible. Furthermore, I would like to avoid setting up a cron job to run every half hour just to keep stock information current. Rather, we look further into the Stock API and make a more basal call to <span class="geshifilter"><code class="php geshifilter-php">stockapi_fetch<span style="color: #009900;">(</span><span style="color: #000088;">$symbol</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></code></span>. This function is a direct call to the 3rd party service, in this case yahoo:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**<br />  * Fetch the stock info for a given ticker symbol from Yahoo!<br />  */</span><br /><span style="color: #000000; font-weight: bold;">function</span> stockapi_fetch<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><a href="http://www.php.net/empty"><span style="color: #000066;">empty</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />     <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">FALSE</span>;<br />   <span style="color: #66cc66;">}</span><br /><br />   <span style="color: #0000ff;">$fields</span> = <span style="color: #ff0000;">'snl1c1ohgvd1t1'</span>;<br />   <span style="color: #808080; font-style: italic;">/*<br />     "s"  =&gt; "Symbol",<br />     "n"  =&gt; "Name",<br />     "l1" =&gt; "Last",<br />     "c1" =&gt; "Change",<br />     "o"  =&gt; "Opening",<br />     "h"  =&gt; "High",<br />     "g"  =&gt; "Low",<br />     "v"  =&gt; "Volume",<br />     "d1" =&gt; "Date",<br />     "t1" =&gt; "Time"<br />    */</span><br /><br />   <span style="color: #0000ff;">$host</span> = <span style="color: #ff0000;">'http://download.finance.yahoo.com'</span>;<br />   <span style="color: #0000ff;">$url</span> = <span style="color: #0000ff;">$host</span> .<span style="color: #ff0000;">'/d/quotes.csv?s='</span>. <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$symbol</span><span style="color: #66cc66;">)</span> .<span style="color: #ff0000;">'&amp;f='</span>.<span style="color: #0000ff;">$fields</span>.<span style="color: #ff0000;">'&amp;e=.csv'</span>;<br /><br />   <span style="color: #0000ff;">$result</span> = <a href="http://api.drupal.org/api/function/drupal_http_request/6"><span style="color: #000066;">drupal_http_request</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$url</span><span style="color: #66cc66;">)</span>;<br />   ...</div></div> <p>This ensures fresh data, without a cron run to update the database. This function is not quite as nice as the <span class="geshifilter"><code class="php geshifilter-php">stockapi_load<span style="color: #009900;">(</span><span style="color: #009900;">)</span></code></span> function in that the array it returns is not keyed. The commented section above (from the module) comes to the rescue. It is in the same sequence as the array returned so it can be used as a reference. So to return something like our requirement example above:<br /><em>NFLX 208.75 -1.30 Sep 13 7:30 EST</em><br /> We would write something like:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #0000ff;">$output</span> = <span style="color: #ff0000;">''</span>;<br /><span style="color: #0000ff;">$my_stock</span> = stockapi_fetch<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'NFLX'</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #b1b100;">if</span><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #0000ff;">$output</span> .= <span style="color: #ff0000;">'&lt;span class="my-stock-quote"&gt;'</span>.<span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">]</span>.<span style="color: #ff0000;">' '</span>.<span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">]</span>.<span style="color: #ff0000;">' '</span>.<span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">]</span>.<span style="color: #ff0000;">' '</span>.<span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">]</span>.<span style="color: #ff0000;">' '</span>.<span style="color: #0000ff;">$my_stock</span><span style="color: #66cc66;">[</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">]</span>.<span style="color: #ff0000;">' EST&lt;/span&gt;'</span>;<br /><span style="color: #66cc66;">}</span><br /><span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$output</span>;</div></div> <p>I wound up throwing a variant of the snippet above into a block created in my module, allowing me to then jump to panels and throw the output wherever I wanted.</p> <p>Now I know... the example is somewhat contrived. There is no "secret" here. Everything outlined in this post would quickly be revealed by only a few minutes of looking through the Stock API module. I wanted to demonstrate that by making a simple call to the Stock API I was able to save significant time in themeing, as well as gain flexibility in function. The alternative would be wrangling the output of the Stock module's block, and making concessions for it's functional behavior. It is easy to fall into the thinking that Drupal site building is just finding a module that is a closest fit and wrestling with what it gives us. Indeed sometimes that may be the case (Organic Groups comes to mind), but to follow that line of thinking blindly is most certainly a mistake.</p></div></div></div> Fri, 16 Sep 2011 04:28:56 +0000 rho 154 at http://theoleschool.com