You are here

function _ed_classified_get_vid in Classified Ads 6.2

Same name and namespace in other branches
  1. 5.2 ed_classified_utils.inc \_ed_classified_get_vid()
  2. 5 ed_classified_utils.inc \_ed_classified_get_vid()
  3. 7.2 ed_classified_utils.inc \_ed_classified_get_vid()

Returns (and possibly creates) a primary vocabulary for classified.

Repairs association with primary vocabulary if $detect is TRUE.

8 calls to _ed_classified_get_vid()
ed_classified_admin_settings in ./ed_classified_settings.inc
Implementation of hook_settings().
ed_classified_cron in ./ed_classified.module
Implements hook_cron().
ed_classified_install in ./ed_classified.install
Implements hook_install().
ed_classified_page in ./ed_classified.module
Display a page of classified ads, as appropriate.
ed_classified_view in ./ed_classified.module
Implements hook_view().

... See full list

File

./ed_classified_utils.inc, line 334
Simple text-based classified ads module.

Code

function _ed_classified_get_vid($detect = FALSE) {
  $vid = _ed_classified_variable_get('vocabulary', '');
  if ($detect || empty($vid)) {
    $taxonomy_name = _ed_classified_displayname() . t(' Category');

    // Check to see if classified ads vocabulary exists
    $dbvid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='%s'", EDI_CLASSIFIED_MODULE_NAME));
    if ($dbvid != $vid) {
      _edi_wd(t('Classified Ads vocabulary was not remembered but detected - storing.'));
      _ed_classified_variable_set('vocabulary', $vid = $dbvid);
    }
    if (empty($vid)) {

      // Perhaps we're just not associated, look for vocabulary by name
      $dbvid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE name='%s'", $taxonomy_name));
      if ($dbvid) {
        db_query("UPDATE {vocabulary} SET module='%s' WHERE vid='%d'", EDI_CLASSIFIED_MODULE_NAME, $vid);
        _edi_wd(t('Classified Ads vocabulary was detected - associating and storing.'));
        _ed_classified_variable_set('vocabulary', $vid = $dbvid);
      }
    }
    if (empty($vid)) {

      // No, still no vocabulary, we're creating it
      $version = explode('.', DRUPAL_VERSION);
      switch (reset($version)) {
        case 5:
        case 6:
          $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='%s'", EDI_CLASSIFIED_MODULE_NAME));
          if (!$vid) {
            $vocabulary = array(
              'name' => _ed_classified_displayname() . t(' Category'),
              'description' => t('Vocabulary required by Classified Ads (' . EDI_CLASSIFIED_MODULE_NAME . ') module.  <strong>Warning:  You should not delete this vocabulary unless you intend to uninstall (or have already uninstalled) the Classified Ads module.</strong>'),
              'multiple' => '0',
              'required' => '1',
              'hierarchy' => '1',
              'relations' => '0',
              'module' => EDI_CLASSIFIED_MODULE_NAME,
              'nodes' => array(
                EDI_CLASSIFIED_MODULE_NAME => 1,
              ),
            );
            taxonomy_save_vocabulary($vocabulary);
            $vid = $vocabulary['vid'];
            $term = array();
            $term['name'] = 'For sale';
            $term['description'] = 'Items for sale';
            $term['vid'] = $vid;
            $term['weight'] = 0;
            taxonomy_save_term($term);
            $term['name'] = 'Wanted';
            $term['description'] = 'Wanted items';
            taxonomy_save_term($term);
          }
          _ed_classified_variable_set('vocabulary', $vid);
          $msg = t('Install: Classified Ad vocabulary !vid and default terms created.', array(
            '!vid' => $vid,
          ));
          _edi_wd($msg);
          drupal_set_message($msg);
          break;
        case 7:
          $vid = db_result(db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module='%s'", EDI_CLASSIFIED_MODULE_NAME));
          if (!$vid) {
            $vocabulary = (object) array(
              'name' => 'Classified Ad Category',
              'description' => t('Vocabulary required by Classified Ads (' . EDI_CLASSIFIED_MODULE_NAME . ') module.  <strong>Warning:  You should not delete this vocabulary unless you intend to uninstall (or have already uninstalled) the Classified Ads module.</strong>'),
              'multiple' => '0',
              'required' => '1',
              'hierarchy' => '1',
              'relations' => '0',
              'module' => EDI_CLASSIFIED_MODULE_NAME,
              'nodes' => array(
                EDI_CLASSIFIED_MODULE_NAME => 1,
              ),
            );
            taxonomy_vocabulary_save($vocabulary);
            $vid = db_result(db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module='%s'", EDI_CLASSIFIED_MODULE_NAME));

            // TODO - create default terms in a Drupal 7 manner
          }
          _ed_classified_variable_set('vocabulary', $vid);
          $msg = t('Install: Classified Ad vocabulary !vid created.', array(
            '!vid' => $vid,
          ));
          _edi_wd($msg);
          drupal_set_message($msg);
          break;
        default:
          drupal_set_message('Unknown Drupal version - unable to initialise vocabulary.');
          break;
      }
    }
    if (!empty($vid)) {

      // Check that the Classified Ads vocabulary is associated with ed_classified
      $dbvid = db_result(db_query("SELECT vid FROM {vocabulary_node_types} WHERE vid='%d' AND type='%s'", $vid, EDI_CLASSIFIED_MODULE_NAME));
      if (empty($dbvid)) {

        // Association not found
        db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES ('%d', '%s')", $vid, EDI_CLASSIFIED_MODULE_NAME);
        _edi_wd(t('Classified Ads vocabulary - associating and storing.'));
      }
    }
    else {
      _edi_wd(t('Classified Ads vocabulary - not found!'));
    }
  }
  return $vid;
}