<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) {
	die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
}

/**
*
* @package simpleMembership
* @copyright Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Anton Getman(ljanton@mail.ru);
* Homepage: http://www.ordasoft.com
* @version: 1.2 FREE
*
**/



$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;

require_once($mosConfig_absolute_path."/administrator/components/com_simplemembership/compat.joomla1.5.php");
require_once($mosConfig_absolute_path."/administrator/components/com_simplemembership/install.simplemembership.helper.php");
if (version_compare(JVERSION, "1.6.0", "lt")){
  function com_install(){
    return com_install2();
  }
}
function com_install2()
{
global $database,$mosConfig_absolute_path,$mosConfig_live_site;
//*******************************   begin check version PHP   **********************************
$is_warning = false;

if ( (phpversion()) < 5 ) { 
$is_warning = true;
?>
<center>
<table width="100%" border="0">
  <tr>
    <td>
      <code>Installation status: <font color="red">fault</font></code>
    </td>
  </tr>
  <tr>
    <td>
      <code><font color="red">This component works correctly under PHP version 5.0 and higher.</font></code>
    </td>
  </tr>
</table>
</center>

<?php
return '<h2><font color="red">Component installation fault</font></h2>';
}
$tableList = $database->getTableList();


$table_prefix = $database->getPrefix(); // for 1.6

echo 'cheking database<br/>';

 $query= "DROP TABLE IF EXISTS `#__simplemembership_users` "; // for 1.6
      $database->setQuery($query);
      $database->query();
        echo $database->getErrorMsg();

 echo "Creating table for main_categories... ";
      $query= "CREATE TABLE IF NOT EXISTS `#__simplemembership_users` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `joomid` int(11) DEFAULT NULL,
			  `name` varchar(255) NOT NULL DEFAULT '',
			  `username` varchar(150) NOT NULL DEFAULT '',
			  `email` varchar(100) NOT NULL DEFAULT '',
			  `usertype` varchar(25) NOT NULL DEFAULT '',
			  `block` tinyint(4) NOT NULL DEFAULT '0',
			  `gid` tinyint(3) unsigned NOT NULL DEFAULT '1',
			  `approveDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
			  `user_type` varchar(50) NOT NULL,
			  `last_approved` date NOT NULL,
			  `want_group` varchar(100) NOT NULL,
			  `want_gid` int(11) NOT NULL,
			  `approved` tinyint(4) NOT NULL DEFAULT '1',
			  `days_last` int(11) NOT NULL,
			  `advgroup_id` int(11) NOT NULL,
			  `sent_approved` int(11) DEFAULT '0',
        PRIMARY KEY (`id`)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8; ";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();
      $query= "CREATE TABLE IF NOT EXISTS `#__simplemembership_groups` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `name` varchar(150) NOT NULL,
		  `acl_group` varchar(150) NOT NULL,
		  `acl_gid` int(11) NOT NULL,
		  `days_last` int(11) NOT NULL,
		  `product_id` int(11) NOT NULL,
		  `product_name` varchar(250) NOT NULL,
		  `link` text NOT NULL,
		  `notes` text NOT NULL,
		  `published` tinyint(4) DEFAULT '1',
		  PRIMARY KEY (`id`),
		  UNIQUE KEY `name` (`name`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8; ";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();
        
   $query= "CREATE TABLE IF NOT EXISTS `#__simplememberships` (
			  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
			  `invitee_name` varchar(100) NOT NULL DEFAULT '',
			  `invitee_email` varchar(50) NOT NULL DEFAULT '',
			  `invited_by_name` varchar(100) NOT NULL DEFAULT '',
			  `invited_by_email` varchar(50) NOT NULL DEFAULT '',
			  `to_be_invited` smallint(6) NOT NULL DEFAULT '0',
			  `last_sent` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
			  `msg` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
			  `checked_out` int(11) NOT NULL DEFAULT '0',
			  `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
			  PRIMARY KEY (`id`),
			  UNIQUE KEY `invitee_email` (`invitee_email`,`invited_by_email`)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();
        
        
          $query= "CREATE TABLE IF NOT EXISTS `#__simplemembership_config` (
			  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
			  `msg` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
			  `auto_invites` smallint(6) NOT NULL DEFAULT '0',
			  `send_after` smallint(6) NOT NULL DEFAULT '30',
			  `email_from_user` tinyint(4) NOT NULL DEFAULT '1',
			  `bcc_admin` tinyint(4) NOT NULL DEFAULT '0',
			  `custom_subject` varchar(150) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
			  `use_custom_msg` tinyint(4) NOT NULL DEFAULT '1',
			  PRIMARY KEY (`id`)
			)ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();

  $query= "CREATE TABLE IF NOT EXISTS `#__simplemembership_check` (
			  `id` tinyint(4) NOT NULL,
			  `last_check` datetime NOT NULL,
			  UNIQUE KEY `id` (`id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();
    
    $query= "CREATE TABLE IF NOT EXISTS `#__simplemembership_group_joomgroup` (
      `mgroup_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Foreign Key to #__simlemembership_groups.id',
      `jgroup_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Foreign Key to #__usergroups.id',
      PRIMARY KEY (`mgroup_id`,`jgroup_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
        $database->setQuery($query);
        $database->query();
        echo $database->getErrorMsg();

//  $query= "INSERT INTO `#__simplemembership_check` ( `last_check`) VALUES ('0000-00-00 00:00:00');";
//         $database->setQuery($query);
//         $database->query();
//         echo $database->getErrorMsg();

   echo "end install database<br/>";

$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;
$db_OK=true;

if( !$is_warning ) echo "<h2 style='color:#0f0;'>Import simple data - OK</h2>";


////

$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path']	= JPATH_SITE;
$db_OK=true;
// $database->setQuery("SELECT COUNT(1) FROM #__virtuemart_orders" );
// if (!$database->query()) {
// 	$db_OK=FALSE;
// }

//end of creating trigger


# Show installation result to user
?>
<center>
<table width="100%" border="0">
  <tr>
    <td>
      <strong>simplemembership</strong><br/>
      <br/>
      This component is published under the <a href="<?php echo $mosConfig_live_site."administrator/components/com_simplemembership/doc/LICENSE.txt"; ?>"
       target="new">License</a>.
    </td>
  </tr>
  <tr>
    <td>
      <code>Installation: <font color="green">succesful</font></code>
    </td>
  </tr>
</table>
</center>
<?php
if($is_warning) return '<h2><font color="red">The simplemembershipComponent installed with a warning about a missing PHP extension! Please read carefully and uninstall simplemembership. Next fix your PHP installation and then install simplemembership again.</font></h2>';
}
?>
