<?php
// $Id: imagex.install,v 1.6 2009/08/12 09:21:51 sdrycroft Exp $
/**
 * hook_install
 */
function imagex_install() {  
  // If we have the image_gallery module installed, we'll use it to our
  // advantage
  if(function_exists('_image_gallery_get_vid')){
    // Set a message to tell users that, because they have image_gallery
    // installed, all images will be placed in to a default image gallery, which
    // can of course be changed.
    drupal_set_message(t('Imagex will work with the Image Gallery module to add all uploaded images to a default gallery called "Library".  This can be changed from the Imagex settings page'));
    // Create an image gallery called Library (unless one already exists).
    $gallery_vid = _image_gallery_get_vid();
    // Search for Library
    $library_word = t('Library');
    /*kprint_r($library_word);
    kprint_r(array($library_word));*/
    $num_rows = array_pop(db_fetch_array(db_query(db_rewrite_sql("SELECT COUNT(t.tid) FROM {term_data} t WHERE t.name = '%s'", 't', 'tid'), $library_word)));
    if(!$num_rows){
      drupal_set_message('Creating new gallery');
      $form_values = array(
          'vid' => $gallery_vid,
          'name' => $library_word,
          'description' => t('Default image gallery for images uploaded via imagex'),
          'weight' => 100);
      taxonomy_save_term($form_values);
    }
    // We should now have our Library image gallery, lets get its tid (this is
    // ugly)
    $tid = array_pop(db_fetch_array(db_query(db_rewrite_sql("SELECT t.tid FROM {term_data} t WHERE t.name = '%s'", 't', 'tid'), $library_word)));
    // Set the imagex_gallery_tid variable
    variable_set('imagex_gallery_tid', $tid);
  } else {
    drupal_set_message(t('Imagex will work with the Image Gallery module to add all uploaded images to a default gallery called "Library".  Reinstalling this module, after installing "Image Gallery" will help users locate images uploaded using Imagex'), 'error');
  }
}

/**
 * hook_uninstall
 */
function imagex_uninstall(){
  db_query("DELETE FROM {variable} WHERE name = 'imagex_gallery_tid'");
}