[development] Returning OR with hook_db_rewrite_sql
Ron Parker
sysop at scbbs.com
Wed Apr 23 20:05:16 UTC 2008
Hi. My module requires additional sql to return the correct content on
taxonomy queries. I've used hook_db_rewrite_sql() to construct the sql I
need, and it returns what I need. The problem is that my hook is AND'd
with the taxonomy_access_db_rewrite_sql hook, and therefore voided.
Example:
In function taxonomy_term_page():
$result = db_query(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data}
t WHERE t.tid IN (%s)', 't', 'tid'), implode(',', $terms['tids']));
Once ran through the TAC hook and my hook, this query becomes:
SELECT t.tid, t.name FROM term_data t WHERE (t.tid IN ('1')) AND (t.tid
IN ('1','3')) AND ( t.tid IN (3))
TAC hook_db_rewrite_sql returns: (t.tid IN ('1'))
My hook_db_rewrite_sql is returning: (t.tid IN ('1','3')) (correct)
What I need is for my clause to be returned as OR rather than AND:
SELECT t.tid, t.name FROM term_data t WHERE (t.tid IN ('1')) OR (t.tid
IN ('1','3')) AND ( t.tid IN (3))
That would do the trick. Question is, how to do it? I've included my
rewrite of the TAC db_rewrite_sql so you can see what I'm working with,
but I'm hoping you might have some way for me to accomplish this short
of hacking TAC. Thanks!
-ron
<?php
/**
* Implementation of hook_db_rewrite_sql()
*
* Used to include OGR roles in taxonomy/term
*
*/
function og_user_roles_db_rewrite_sql($query, $table, $field) {
if (!user_access('administer taxonomy') && ($field =='vid' || $field
=='tid')) {
global $user;
$op = (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit')) ?
'create' : 'list';
// let's cache
static $taxonomy_access_sql_clause;
$clause = array();
if (!isset($taxonomy_access_sql_clause)) {
$taxonomy_access_sql_clause = array();
}
if (!isset($taxonomy_access_sql_clause[$op][$field])) {
if (isset($user) && is_array($user->roles)) {
$rids = array_keys($user->roles);
}
else {
$rids[] = 1;
}
if (isset($user)) {
$query = db_query('SELECT ogr_id FROM {og_users_roles} where uid
= %d', $user->uid);
while($row = db_fetch_array($query)){
$ogr[] = $row['ogr_id'];
}
}
else {
$ogr[] = 0;
}
$sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS
grant_$op FROM {term_access} t INNER JOIN {term_data} d ON t.tid = d.tid
LEFT OUTER JOIN {og_users_roles} ogr ON ogr.rid = t.rid WHERE (t.rid IN
('".implode("','",$rids)."') OR ogr.uid = ".$user->uid.") AND grant_$op
= 1 GROUP BY t.tid, d.vid");
while ($result = db_fetch_object($sql)) {
$tids[]= $result->tid;
$vids[$result->vid]= $result->vid;
}
$clause[$op]['tid'] = isset($tids) ? implode("','",$tids) : '';
$clause[$op]['vid']= isset($vids) ? implode("','",$vids) : '';
$taxonomy_access_sql_clause = $clause;
}
else {
$clause[$op][$field] = $taxonomy_access_sql_clause[$op][$field];
}
$return['where'] = ($clause[$op][$field]) ? "$table.$field IN
('".$clause[$op][$field]."')" : "$table.$field IS NULL";
return $return;
}
else {
return array();
}
}
?>
--
Ron Parker
Software Creations http://www.scbbs.com
Self-Administration Web Site http://saw.scbbs.com
SDSS Subscription Mgmt Service http://sdss.scbbs.com
Central Ave Dance Ensemble http://www.centralavedance.com
R & B Salsa http://www.randbsalsa.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20080423/1c4236e8/attachment-0001.htm
More information about the development
mailing list