You are here

function faq_ask_update_7002 in FAQ_Ask 7

Add node/term data to faq_ask maintained term index for unpublished nodes

File

./faq_ask.install, line 162
This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.

Code

function faq_ask_update_7002() {
  $node_count = '';
  $term_pairs = '';

  // Get unpublished node nids
  $unpub = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->condition('n.status', '0')
    ->condition('n.type', 'faq')
    ->execute()
    ->fetchCol('nid');
  if (empty($unpub)) {
    return t('No unpublished nodes found.');
  }
  foreach ($unpub as $nid) {
    $node = node_load($nid);
    $node_count++;
    foreach (_faq_ask_get_term_field_name($node) as $field) {
      foreach ($node->{$field}[$node->language] as $term) {

        // Check if the node/term pair is loaded from before
        $result = db_select('faq_ask_term_index', 'ti')
          ->fields('ti')
          ->condition('tid', $term['tid'])
          ->condition('nid', $node->nid)
          ->execute()
          ->fetchAll();

        // If not found and the term is actually a term (not 0) - add it to the table
        if (empty($result) && $term['tid']) {
          db_insert('faq_ask_term_index')
            ->fields(array(
            'nid' => $node->nid,
            'tid' => $term['tid'],
            'sticky' => $node->sticky,
            'created' => $node->created,
          ))
            ->execute();
          $term_pairs++;
        }
      }
    }
  }
  return t('Parsed @count nodes adding @num node id/term pairs to the faq_ask index table.', array(
    '@count' => $node_count,
    '@num' => $term_pairs,
  ));
}