You are here

function simplenews_enable in Simplenews 5

Implementation of hook_enable().

If required create vocabulary for simplenews newsletter and one newsletter term

File

./simplenews.install, line 102

Code

function simplenews_enable() {
  if ($vocabulary = taxonomy_get_vocabulary(variable_get('simplenews_vid', ''))) {

    // Existing install. Add back newsletter node type, if the forums
    // vocabulary still exists. Leave all other node types unchanged.
    $vocabulary = (array) $vocabulary;
    $vocabulary['nodes']['simplenews'] = 1;

    // If it exists we remove the 0 key. It is the indication that no node types are selected.
    unset($vocabulary['nodes'][0]);
    taxonomy_save_vocabulary($vocabulary);
  }
  else {

    // Create the simplenews vocabulary if it does not exist.
    $vocabulary = array(
      'name' => t('Newsletter'),
      'multiple' => '0',
      'required' => '0',
      'hierarchy' => '0',
      'relations' => '0',
      'module' => 'simplenews',
      'nodes' => array(
        'simplenews' => 1,
      ),
    );
    taxonomy_save_vocabulary($vocabulary);
    variable_set('simplenews_vid', $vocabulary['vid']);
  }

  // Check to see if at least 1 term exists, else create one
  $tid = db_result(db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vocabulary['vid']));
  if (!$tid) {
    $form_values = array(
      'name' => variable_get('site_name', 'Drupal') . ' ' . t('newsletter'),
      'vid' => $vocabulary['vid'],
      'weight' => 0,
    );
    switch (taxonomy_save_term($form_values)) {
      case SAVED_UPDATED:
        drupal_set_message(t('Updated term %name.', array(
          '%name' => $form_values['name'],
        )));
        break;
      case SAVED_DELETED:
        drupal_set_message(t('Deleted term %name.', array(
          '%name' => $form_values['name'],
        )));
        break;
    }
  }
}