Hey guys

I really need this module for a job I am currently doing so I started converting some of the sql but got stuck.

I really don't know how to deal with the %s in the case

Here is my progress any help would be great.

model/hs_taxonomy.module

<?php
function hs_taxonomy_term_count_nodes($tid, $type = 0) {
  static $count;

  $term = taxonomy_term_load($tid);
  $tree = _hs_taxonomy_hierarchical_select_get_tree($term->vid, $tid);
  $tids = array($tid);
  foreach ($tree as $descendant) {
    $tids[] = $descendant->tid;
  }

  if (!isset($count[$type][$tid])) {
    if (is_numeric($type)) {
      // TODO Please convert this statement to the D7 database API syntax.
      
      //Drupal 6
     // $count[$type][$tid] = db_query(
     //   db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.tid IN (%s)"),
     //   implode(',', $tids))->fetchField();
  
      // Drupal 7
      $query = db_select('taxonomy_index' ,'t');
        $query->fields('t');
        $query->join('node', 'n',  't.nid = n.nid');
        $query->distinct();
        $query->addExpression('COUNT(n.nid)', 'count');
        $query->condition('n.status', '1');
         // ->condition('t.tid', array($tid), 'IN');
      $result = $query->execute();
        
        //print_r($query->__toString());
       // print '<pre>';
      foreach($result as $row){
        //print_r($row);
        $count[$type][$tid] = $row->tid;
      }
       // print '</pre>';

    }
    else {
      // TODO Please convert this statement to the D7 database API syntax.
      
      //Drupal 6
      //$count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids))->fetchField();
      
      // Drupal 7
      $query = db_select('taxonomy_index' ,'t');
        $query->fields('t');
        $query->join('node', 'n',  't.nid = n.nid');
        $query->distinct();
       $query->addExpression('COUNT(n.nid)', 'count');
        $query->condition('n.status', '1');
//          ->condition('n.type', '%s');
      $result = $query->execute();
        
        //print_r($query->__toString());
       // print '<pre>';
      foreach($result as $row){
        //print_r($row);
        $count[$type][$tid] = $row->tid;
      }
       // print '</pre>';
    }
  }
  return $count[$type][$tid];
}

CommentFileSizeAuthor
#2 hs_taxonomy.module.zip6.97 KBgarethhallnz

Comments

lahode’s picture

Hi,

I would rather write it:

$query = db_select('taxonomy_index' ,'t');
$query->join('node', 'n', 't.nid = n.nid');
$query->addExpression('COUNT(DISTINCT(n.nid))', 'count');
$query->condition('n.status', 1);
$query->condition('n.type', $type);
$query->condition('t.tid', $tids, 'IN');
$result = $query->execute();

Didn't test it, hope it helps. If ever you have some spare time to waist, have a look to "add term" option, I would be motivate to help this function to work on D7.

garethhallnz’s picture

Status: Active » Needs review
StatusFileSize
new6.97 KB

Hey

Here is the updated hs_taxonomy.module file with some of the sql updated to D7. Sorry guys I still have no idea how to use git from terminal (I use a gui called git-tower) and I don't really get how to make a patch.

wim leers’s picture

Status: Needs review » Needs work

It's very simple:

cd /path/to/hierarchical_select
git diff . > 1190028.patch

That's all.

wim leers’s picture

Status: Needs work » Closed (duplicate)