You are here

function faq_ask_taxonomy in FAQ_Ask 6.2

Same name and namespace in other branches
  1. 6 faq_ask.module \faq_ask_taxonomy()

Implementation of hook_taxonomy().

@param: op: 'insert', 'update, 'delete' type: 'term', 'vocabulary' array: depends on other two.

File

./faq_ask.module, line 1341
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_taxonomy($op, $type, $edit = NULL) {
  global $user;
  $default_expert = variable_get('faq_ask_default_expert', 1);
  $my_vocs = variable_get('faq_ask_vocabularies', array());
  $vid = $edit['vid'];

  // See if it's one of our vocabularies.
  $our_vocab = in_array($vid, $my_vocs);
  switch ($op) {
    case 'insert':
      switch ($type) {
        case 'term':

          // term: set default expert.
          if ($our_vocab) {
            $values = array(
              $default_expert,
              $edit['tid'],
            );
            $insert = db_query("INSERT INTO {faq_expert} (uid, tid) VALUES (" . db_placeholders($values) . ")", $default_expert, $edit['tid']);
            if ($insert === FALSE) {
              drupal_set_message(t('Attempt to assign expert failed.'), 'error');
            }
            else {
              drupal_set_message(t('Assigned expert @expert to @name (@tid).', array(
                '@expert' => $default_expert,
                '@name' => $edit['name'],
                '@tid' => $edit['tid'],
              )), 'status');
            }
          }
          break;
        case 'vocabulary':

          // New vocabulary created. It will not show on the ask page until the user
          // goes to the settings page, so we don't need to do anything.
          break;
      }

      // End insert switch type.
      break;
    case 'delete':
      switch ($type) {
        case 'term':

          // Delete term: remove experts.
          if ($our_vocab) {
            _faq_ask_delete_expert($edit['tid'], $edit['name']);
          }
          break;
        case 'vocabulary':

          // Each term gets deleted first, so all we have to do is remove it from our vocab list.
          if ($our_vocab) {
            _faq_ask_delete_vocabulary($vid, $edit, $my_vocs);
          }
          break;
      }

      // End delete switch type.
      break;
    case 'update':

      //  Two cases for vocabulary:
      //    1) FAQ is added to the vocab. -- see insert comment.
      //    2) FAQ is removed from the vocab. -- need to delete all experts for all terms and remove from voc list?
      //  $edit['nodes'] contains an array of content types for the vocab.
      switch ($type) {
        case 'term':

          // Term update: nothing to do.
          break;
        case 'vocabulary':
          if (in_array('faq', $edit['nodes'])) {

            // If it's there now, we're done.
            break;
          }

          // Not there now, so we need to see if it was.
          if ($our_vocab) {
            $tree = taxonomy_get_tree($vid);
            foreach ($tree as $term) {
              $my_tid = $term->tid;
              $my_tname = $term->name;
              _faq_ask_delete_expert($my_tid, $my_tname);
            }

            // End foreach tree.
            _faq_ask_delete_vocabulary($vid, $edit, $my_vocs);
          }
          break;
      }

      // End update switch type.
      break;
    default:
      drupal_set_message(t('Faq_ask_taxonomy: Unknown op (@op) encountered', array(
        '@op' => $op,
      )), 'status');
  }

  // End switch $op
}