You are here

spaces_taxonomy.module in Spaces 6.3

File

spaces_taxonomy/spaces_taxonomy.module
View source
<?php

/**
 * Implementaton of hook_enable().
 */
function spaces_taxonomy_enable() {

  // Weight spaces_taxonomy to be after taxonomy.
  $weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'taxonomy'"));
  db_query("UPDATE {system} SET weight = %d WHERE name = 'spaces_taxonomy'", $weight + 1);
}

/**
 * Implementation of hook_ctools_plugin_api().
 */
function spaces_taxonomy_ctools_plugin_api($module, $api) {
  if ($module == 'spaces' && $api == 'plugins') {
    return array(
      'version' => 3,
    );
  }
}

/**
 * Implementation of hook_purl_provider().
 */
function spaces_taxonomy_purl_provider() {
  $items = array();
  $items["spaces_taxonomy"] = array(
    'name' => t('Term space'),
    'description' => t('Sets a space as the active space.'),
    'callback' => 'spaces_init_space',
    'callback arguments' => array(
      'taxonomy',
    ),
    'example' => 'my-space',
  );
  return $items;
}

/**
 * Implementation of hook_spaces_plugins().
 */
function spaces_taxonomy_spaces_plugins() {
  $plugins = array();
  $plugins['space_taxonomy'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'spaces_taxonomy') . '/plugins',
      'file' => 'space_taxonomy.inc',
      'class' => 'space_taxonomy',
      'parent' => 'space_type_purl',
    ),
  );
  return $plugins;
}

/**
 * Implementation of hook_spaces_registry().
 */
function spaces_taxonomy_spaces_registry() {
  return array(
    'types' => array(
      'taxonomy' => array(
        'title' => t('Term space'),
        'plugin' => 'space_taxonomy',
        'path' => 'taxonomy/term/%',
      ),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function spaces_taxonomy_menu() {
  $items = array();
  $items['admin/build/spaces/taxonomy'] = array(
    'title' => 'Taxonomy',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'spaces_taxonomy_settings',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer spaces',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_menu_alter().
 */
function spaces_taxonomy_menu_alter(&$items) {
  if (isset($items['taxonomy/term/%'])) {
    $items['taxonomy/term/%/view'] = $items['taxonomy/term/%'];
    $items['taxonomy/term/%/view']['type'] = MENU_DEFAULT_LOCAL_TASK;
    $items['taxonomy/term/%/view']['title'] = 'View';
    $items['taxonomy/term/%/view']['weight'] = -10;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function spaces_taxonomy_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'node-form' && arg(0) . '/' . arg(1) != 'admin/content') {
    $vid = variable_get('spaces_taxonomy_vid', 0);
    $space = spaces_get_space();
    if ($vid && !empty($form['taxonomy'][$vid]) && $space->type == 'taxonomy') {
      $form['taxonomy'][$vid]['#disabled'] = TRUE;
      $form['taxonomy'][$vid]['#default_value'] = $space->id;
    }
  }
}

/**
 * Implementation of hook_form_alter() for taxonomy_form_term.
 */
function spaces_taxonomy_form_taxonomy_form_term_alter(&$form, $form_state) {
  $vid = variable_get('spaces_taxonomy_vid', 0);
  if ($vid && $form['vid']['#value'] == $vid) {
    $tid = isset($form['tid']['#value']) ? $form['tid']['#value'] : 0;
    $space = spaces_load('taxonomy', $tid);
    if ($space) {
      $space
        ->activate();
      $form['spaces'] = array(
        '#type' => 'fieldset',
        '#title' => t('Spaces'),
        '#tree' => FALSE,
        '#weight' => 0,
      );

      // Add PURL modifier form
      $modifier = purl_load(array(
        'id' => $tid,
        'provider' => 'spaces_taxonomy',
      ));
      $form['spaces']['purl'] = purl_form('spaces_taxonomy', $tid, isset($modifier['value']) ? $modifier['value'] : '');

      // Add presets form
      $presets = spaces_preset_load(NULL, 'taxonomy');
      if (count($presets) > 1) {
        module_load_include('inc', 'spaces', 'spaces.admin');
        $form['spaces']['spaces_preset'] = spaces_preset_form($presets, 'taxonomy');
        $space = spaces_load('taxonomy', $tid);
        $default_value = $space->controllers->variable
          ->get('spaces_preset_taxonomy', 'space');
        if (isset($default_value, $form['spaces']['spaces_preset']['spaces_preset_taxonomy']['#options'][$default_value])) {
          $form['spaces']['spaces_preset']['spaces_preset_taxonomy']['#default_value'] = $default_value;
        }
      }
    }
  }
}

/**
 * Implementation of hook_taxonomy().
 */
function spaces_taxonomy_taxonomy($op, $type, $array = NULL) {
  if ($type === 'term') {
    $term = $array;
    switch ($op) {
      case 'insert':
      case 'update':
        $space = spaces_load('taxonomy', $term['tid']);
        if ($space) {

          // Save preset values.
          if (!empty($term['spaces_preset_taxonomy'])) {
            $space->controllers->variable
              ->set('spaces_preset_taxonomy', $term['spaces_preset_taxonomy']);
          }

          // Save PURL modifier.
          $modifier = array(
            'provider' => 'spaces_taxonomy',
            'id' => $term['tid'],
            'value' => $term['purl']['value'],
          );
          purl_save($modifier);
        }
        break;
      case 'delete':
        spaces_delete('taxonomy', $term['tid']);
        $modifier = array(
          'provider' => 'spaces_taxonomy',
          'id' => $term['tid'],
        );
        purl_delete($modifier);
        break;
    }
  }
}

/**
 * Spaces Taxonomy settings form.
 */
function spaces_taxonomy_settings(&$form_state) {
  $form = array();

  // Collect an array of valid vocab options
  $vocabs = array(
    0 => '---',
  );
  foreach (taxonomy_get_vocabularies() as $vocab) {

    // Vocab may not be multiple
    if (!$vocab->multiple && !$vocab->tags) {
      $vocabs[$vocab->vid] = $vocab->name;
    }
  }
  $form['spaces_taxonomy_vid'] = array(
    '#type' => 'select',
    '#title' => t('Spaces vocabulary'),
    '#description' => t('Choose one of the following vocabularies to enable for use with Spaces.'),
    '#options' => $vocabs,
    '#default_value' => variable_get('spaces_taxonomy_vid', 0),
  );
  $form = system_settings_form($form);
  return $form;
}