<?php
/**
 * Joomla! 1.5 component NS Pro
 *
 * @version $Id: install.nspro.php 2009-04-28 01:47:12 svn $
 * @author Christopher Mavros
 * @package Joomla
 * @subpackage NS Pro
 * @license GNU/GPL
 *
 * Pro Version of Newsletter Subscriber.
Provides:
Keep subscriptions in database
Unsubscription Page
Optional Confirmation on subscribe/unsubscribe
Subscription Manager
Send Newsletters
Newsletter Manager
Subscription Statistics
Unique Emails (does not allow duplicates)
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Initialize the database
$db =& JFactory::getDBO();
$update_queries = array (
'CREATE TABLE IF NOT EXISTS `#__nspro_subs` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `email` varchar(200) NOT NULL,
  `datetime` DATETIME NOT NULL,
  `notes` TEXT NULL,
  `confirmed` BOOL NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletters` (
  `id` int(11) NOT NULL auto_increment,
  `fName` varchar(100) NOT NULL,
  `fEmail` varchar(200) NOT NULL,
  `subject` TEXT NULL,
  `message` TEXT NULL,
  `datetime` DATETIME NOT NULL,
  `sent` BOOL NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_lists` (
  `id` int(11) NOT NULL auto_increment,
  `lname` varchar(100) NOT NULL,
  `published` BOOL NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_attachments` (
  `id` int(11) NOT NULL auto_increment,
  `newsletter_id` int(11) NOT NULL,
  `filename` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;');

$alternative_queries = array (
'CREATE TABLE IF NOT EXISTS `#__nspro_subs` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `email` varchar(200) NOT NULL,
  `datetime` DATETIME NOT NULL,
  `notes` TEXT NULL,
  `confirmed` BOOL NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletters` (
  `id` int(11) NOT NULL auto_increment,
  `fName` varchar(100) NOT NULL,
  `fEmail` varchar(200) NOT NULL,
  `subject` TEXT NULL,
  `message` TEXT NULL,
  `datetime` DATETIME NOT NULL,
  `sent` BOOL NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_lists` (
  `id` int(11) NOT NULL auto_increment,
  `lname` varchar(100) NOT NULL,
  `published` BOOL NOT NULL,
  `notes` TEXT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;',
'CREATE TABLE IF NOT EXISTS `#__nspro_newsletter_attachments` (
  `id` int(11) NOT NULL auto_increment,
  `newsletter_id` int(11) NOT NULL,
  `filename` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0;');

$additional_queries = array (
'ALTER TABLE `#__nspro_subs` ADD COLUMN `mailing_lists` varchar(60) NOT NULL;',
'ALTER TABLE `#__nspro_newsletters` ADD COLUMN `mailing_lists` varchar(60) NOT NULL;');

// Perform all queries - we dont care if it fails
$try_alternatives = false;
foreach( $update_queries as $query ) {
    $db->setQuery( $query );
    if (!$db->query()) {
      $try_alternatives = true;
    }
}

if ($try_alternatives) {
  foreach( $alternative_queries as $query ) {
      $db->setQuery( $query );
      if (!$db->query()) {
        print $db->getErrorMsg();
      }
  }
}

foreach( $additional_queries as $query ) {
    $db->setQuery( $query );
    if (!$db->query()) {
      //print $db->getErrorMsg();
      // I would print errors, but this always tries to insert duplicate columns, so... :)
    }
}

?>