Add ability to create taxonomy-filtered daily nodes
EvanDonovan - January 16, 2009 - 22:48
| Project: | Daily |
| Version: | 5.x-2.1 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
We're creating a database of quotes and we have the Daily module set up to display a new random quote each day. However, we'd also like to create Daily Containers which display random quotes from particular individuals, each of which is identified by a particular taxonomy term.
Thus, we'd like it the Daily Container content type to have another field so that you can specify the taxonomy term from the second vocabulary.

#1
I modified the random daily node function to give this functionality. I don't have the time or the skills to integrate this in with the rest of the module, but I present it here in case it's useful to anyone else.
<?php
function _daily_node_by_tid_filter_random($tid1, $tid2) {
// Check whether we have already one today which is still valid
$db_record = db_fetch_object(db_query("SELECT dr.nid FROM {daily_random} dr, {node} n WHERE dr.nid = n.nid AND tid = %d AND n.status = 1 AND dr.date = '%s'", $tid1, _daily_pack_date(_daily_today())));
if ($db_record) {
$nid = $db_record->nid;
}
else {
// If we did not find one, generate a new one
db_query("DELETE FROM {daily_random} WHERE tid = %d", $tid1);
$nr_of_rows = db_result(db_query("SELECT COUNT(*) FROM {term_node} t1, {term_node} t2,{daily} d, {node} n WHERE t1.tid = %d AND t2.tid = %d AND t1.nid = n.nid AND t2.nid = n.nid AND n.vid = d.vid AND n.status = 1", $tid1, $tid2));
if (!$nr_of_rows) {
return;
}
$nid = db_result(db_query("SELECT n.nid FROM {term_node} t1, {term_node} t2, {daily} d, {node} n WHERE t1.tid = %d AND t2.tid = %d AND t1.nid = n.nid AND t2.nid = n.nid AND n.vid = d.vid AND n.status = 1 ORDER BY d.date LIMIT %d,1", $tid1, $tid2, rand(0, $nr_of_rows - 1)));
db_query("INSERT INTO {daily_random} (tid,nid,date) VALUES (%d,%d,'%s')", $tid1, $nid, _daily_pack_date(_daily_today()));
}
$node = node_load($nid);
return $node;
}
?>
#2
Actually, I've determined this code doesn't work the way I wanted it to. It only allows for one Quote of the Day at a time since the tid for "Quote" is the same regardless of author. I tried to modify it to make it possible to have multiple quotes, but it doesn't seem possible without changing the table schema. So far my modifications to the table schema have been without success.
#3
I've gotten this functionality working to my satisfaction now. Below is the main function which implements it. The calling functions need to be changed slightly in order to pass in the right variables. I haven't integrated it in with the interface, however.
<?php
function _daily_node_by_tid_random($tid, $nrid = 0, $tid2 = 0) {
// Check whether we have already one today which is still valid
$db_record = db_fetch_object(db_query("SELECT dr.nid FROM {daily_random} dr, {node} n WHERE dr.nid = n.nid AND tid = %d AND tid2 = %d AND n.status = 1 AND dr.date = '%s'", $tid, $tid2, _daily_pack_date(_daily_today())));
if ($db_record) {
$nid = $db_record->nid;
}
else {
// If we did not find one, generate a new one
if($tid2 == 0) {
db_query("DELETE FROM {daily_random} WHERE tid = %d AND tid2 = 0", $tid);
$nr_of_rows = db_result(db_query("SELECT COUNT(*) FROM {term_node} t,{daily} d, {node} n WHERE t.tid = %d AND t.nid = n.nid AND n.vid = d.vid AND n.status = 1", $tid));
if (!$nr_of_rows) {
return;
}
$nid = db_result(db_query("SELECT n.nid FROM {term_node} t,{daily} d, {node} n WHERE t.tid = %d AND t.nid = n.nid AND n.vid = d.vid AND n.status = 1 ORDER BY d.date LIMIT %d,1", $tid, rand(0, $nr_of_rows - 1)));
db_query("INSERT INTO {daily_random} (nrid,tid,tid2,nid,date) VALUES (%d,%d,%d,%d,'%s')", $nrid, $tid, 'NULL', $nid, _daily_pack_date(_daily_today()));
}
else {
db_query("DELETE FROM {daily_random} WHERE tid = %d AND tid2 = %d", $tid, $tid2);
$nr_of_rows = db_result(db_query("SELECT COUNT(*) FROM {term_node} t1, {term_node} t2, {daily} d, {node} n WHERE t1.tid = %d AND t1.nid = n.nid AND t2.tid = %d AND t2.nid = n.nid AND n.vid = d.vid AND n.status = 1", $tid, $tid2));
if (!$nr_of_rows) {
return;
}
$nid = db_result(db_query("SELECT n.nid FROM {term_node} t1, {term_node} t2, {daily} d, {node} n WHERE t1.tid = %d AND t1.nid = n.nid AND t2.tid = %d AND t2.nid = n.nid AND n.vid = d.vid AND n.status = 1 ORDER BY d.date LIMIT %d,1", $tid, $tid2, rand(0, $nr_of_rows - 1)));
db_query("INSERT INTO {daily_random} (nrid,tid,tid2,nid,date) VALUES (%d,%d,%d,%d,'%s')", $nrid, $tid, $tid2, $nid, _daily_pack_date(_daily_today()));
}
}
$node = node_load($nid);
return $node;
}
?>
In order to get this to work, the table schema must be as follows:
Field Type Null Key Default
nrid int(11) NO PRI 0
tid int(10) unsigned NO 0
tid2 int(11) YES NULL
nid int(10) unsigned NO 0
date char(8) NO NULL