<?php
/*************************************************************
* Mambo Community Builder
* Author MamboJoe
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
*************************************************************/

function com_install() {
  global $database, $mosConfig_absolute_path;

  # Show installation result to user
  ?>
  <center>
  <table width="100%" border="0">
    <tr>
      <td>
	<img src="../components/com_comprofiler/images/smcblogo.gif" />
      </td>
    </tr>
    <tr>
      <td background="F0F0F0" colspan="2">
        <code>Installation Process:<br />
        <?php

          # Set up new icons for admin menu
          echo "Start correcting icons in administration backend.<br />";
          $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/content.png' WHERE admin_menu_link='option=com_comprofiler&task=showLists'");
          $iconresult[0] = $database->query();
          $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/content.png' WHERE admin_menu_link='option=com_comprofiler&task=showField'");
          $iconresult[1] = $database->query();
          $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/content.png' WHERE admin_menu_link='option=com_comprofiler&task=showTab'");
          $iconresult[2] = $database->query();
          $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/config.png' WHERE admin_menu_link='option=com_comprofiler&task=showconfig'");
          $iconresult[3] = $database->query();
          $database->setQuery("UPDATE #__components SET admin_menu_img='js/ThemeOffice/users.png' WHERE admin_menu_link='option=com_comprofiler&task=showusers'");
          $iconresult[4] = $database->query();
          foreach ($iconresult as $i=>$icresult) {
            if ($icresult) {
              echo "<font color='green'>FINISHED:</font> Image of menu entry $i has been corrected.<br />";
            } else {
              echo "<font color='red'>ERROR:</font> Image of menu entry $i could not be corrected.<br />";
            }
          }
	 $database->setQuery("SELECT COUNT(*) FROM #__components WHERE link = 'option=com_comprofiler'");
         $components = $database->loadresult();
	 IF($components > 1) {
		$database->setQuery("SELECT id FROM #__components WHERE link = 'option=com_comprofiler' ORDER BY id DESC LIMIT 1");
         	$comid = $database->loadresult();
		$database->setQuery("DELETE FROM #__components WHERE link  = 'option=com_comprofiler' AND id != $comid  ");
         	$database->query();
		//print $database->getquery();
		$database->setQuery("DELETE FROM #__components WHERE #__components.option = 'com_comprofiler' AND parent != $comid AND id != $comid ");
         	$database->query();
		//print $database->getquery();
              	echo "<font color='green'>FINISHED:</font> Administrator Menu Corrected.<br />";
	} 

	//Manage Database Upgrades
	$MCBUpgrades = array();
	
	//Beta 3 Upgrade
	$MCBUpgrades[0]['test'] = "SELECT `default` FROM #__comprofiler_lists";
	$MCBUpgrades[0]['updates'][0] = "ALTER TABLE `#__comprofiler_lists`"
					."\n ADD `default` TINYINT( 1 ) DEFAULT '0' NOT NULL,"
					."\n ADD `usergroupids` VARCHAR( 255 ),"
					."\n ADD `sortfields` VARCHAR( 255 ),"
					."\n ADD `ordering` INT( 11 ) DEFAULT '0' NOT NULL AFTER `published`";
	$MCBUpgrades[0]['updates'][1] = "UPDATE #__comprofiler_lists SET `default`=1 WHERE published =1";
	$MCBUpgrades[0]['updates'][2] = "UPDATE #__comprofiler_lists SET usergroupids = '29, 18, 19, 20, 21, 30, 23, 24, 25', sortfields = '`username` ASC'";
	$MCBUpgrades[0]['updates'][3] = "ALTER TABLE `#__comprofiler` ADD `acceptedterms` TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER `bannedreason`";
	$MCBUpgrades[0]['message'] = "1.0 Beta 2 to 1.0 Beta 3";


	//Beta 4 Upgrade
	$MCBUpgrades[1]['test'] = "SELECT `firstname` FROM #__comprofiler";
	$MCBUpgrades[1]['updates'][0] = "ALTER TABLE #__comprofiler ADD `firstname` VARCHAR( 100 ) AFTER `user_id` ,"
					."\n ADD `middlename` VARCHAR( 100 ) AFTER `firstname` ,"
					."\n ADD `lastname` VARCHAR( 100 ) AFTER `middlename` ";
	$MCBUpgrades[1]['updates'][1] = "ALTER TABLE `#__comprofiler_fields` ADD `readonly` TINYINT( 1 ) DEFAULT '0' NOT NULL AFTER `profile`";
	$MCBUpgrades[1]['updates'][3] = "ALTER TABLE `#__comprofiler_tabs` ADD `width` VARCHAR( 10 ) DEFAULT '.5' NOT NULL AFTER `ordering` ,"
					."\n ADD `enabled` TINYINT( 1 ) DEFAULT '1' NOT NULL AFTER `width` ," 
					."\n ADD `plugin` VARCHAR( 255 ) DEFAULT NULL AFTER `enabled`" ;

	$MCBUpgrades[1]['message'] = "1.0 Beta 3 to 1.0 Beta 4";




	//Apply Upgrades
	foreach ($MCBUpgrades AS $MCBUpgrade) {
		$database->setQuery($MCBUpgrade['test']);
		//if it fails test then apply upgrade
		if (!$database->query()) {
			foreach($MCBUpgrade['updates'] as $MCBScript) {
				$database->setQuery($MCBScript);
				if(!$database->query()) {
					//Upgrade failed
					print("<font color=red>".$MCBUpgrade['message']." failed! SQL error:" . $database->stderr(true)."</font><br />");
					return;
				}
			}
			//Upgrade was successful
			print "<font color=green>".$MCBUpgrade['message']." Upgrade Applied Successfully!</font>";			
		} 
	}

	$sql="SELECT listid FROM #__comprofiler_lists ORDER BY published desc";
	$database->setQuery($sql);
	$lists = $database->loadObjectList();
	$order=0;
	foreach($lists AS $list) {
		$database->setQuery("UPDATE #__comprofiler_lists SET ordering = $order WHERE listid='".$list->listid."'");
		$database->query();
		$order++;
	}
        ?>

      </td>
    </tr>
    <tr>
	<td>
	<?php
if(is_writable($mosConfig_absolute_path . "/images/")) {
	$galleryFiles = array("airplane.gif"
			,"ball.gif"
			,"butterfly.gif"
			,"car.gif"
			,"dog.gif"
			,"duck.gif"
			,"fish.gif"
			,"frog.gif"
			,"guitar.gif"
			,"kick.gif"
			,"pinkflower.gif"
			,"redflower.gif"
			,"skater.gif"
			,"index.html"); 
	  if(!file_exists($mosConfig_absolute_path . "/images/comprofiler/")){
	    if(mkdir($mosConfig_absolute_path . "/images/comprofiler/")) {
	    		print "<font color=green>".$mosConfig_absolute_path . "/images/comprofiler/ Successfully added!</font><br />";
		} else {
			print "<font color=red>".$mosConfig_absolute_path . "/images/comprofiler/ Failed to be to be created, please do so manually!</font><br />";
		}
	  }  else {
	    print "<font color=green>".$mosConfig_absolute_path . "/images/comprofiler/ already exists!</font><br />";
	  }
	  if(!file_exists($mosConfig_absolute_path . "/images/comprofiler/gallery/")){
	    if(mkdir($mosConfig_absolute_path . "/images/comprofiler/gallery/")) {
	    		print "<font color=green>".$mosConfig_absolute_path . "/images/comprofiler/gallery/ Successfully added!</font><br />";
		} else {
			print "<font color=red>".$mosConfig_absolute_path . "/images/comprofiler/gallery/ Failed to be to be created, please do so manually!</font><br />";
		}
	  }  else {
	    print "<font color=green>".$mosConfig_absolute_path . "/images/comprofiler/gallery/ already exists!</font><br />";
	  }
	  if(!is_writable($mosConfig_absolute_path . "/images/comprofiler/")){
	    if(!chmod($mosConfig_absolute_path . "/images/comprofiler/", 0777)) {
			print "<font color=red>".$mosConfig_absolute_path . "/images/comprofiler/ Failed to be chmod'd to 777 please do so manually!</font><br />";
		}
	  }
	  if(!is_writable($mosConfig_absolute_path . "/images/comprofiler/gallery/")){
	    if(!chmod($mosConfig_absolute_path . "/images/comprofiler/gallery/", 0777)) {
			print "<font color=red>".$mosConfig_absolute_path . "/images/comprofiler/gallery/ Failed to be chmod'd to 777 please do so manually!</font><br />";
		}
	  }
	  foreach($galleryFiles AS $galleryFile) {
		IF(copy($mosConfig_absolute_path . "/components/com_comprofiler/images/gallery/".$galleryFile,$mosConfig_absolute_path . "/images/comprofiler/gallery/".$galleryFile)) {
			print "<font color=green>".$galleryFile." Successfully added to the gallery!</font><br />";

		} ELSE {
			print "<font color=red>".$galleryFile." Failed to be added to the gallery please do so manually!</font><br />";
		}
	  }
} else {
	print "<font color=red>".$mosConfig_absolute_path . "/images/ is not writable!<br />  Manually do the following:<br /> 1.) create ".$mosConfig_absolute_path . "/images/comprofiler/ directory <br /> 2.) chmod it to 777 <br /> 3.) create ".$mosConfig_absolute_path . "/images/comprofiler/gallery/ <br /> 4.) chmod it to 777 <br />5.) copy ".$mosConfig_absolute_path . "/components/com_comprofiler/images/gallery/ and its contents to ".$mosConfig_absolute_path . "/images/gallery/  </font><br />";
}
	?>
        <font color="green"><b>Installation finished.</b></font></code>
      </td>
    </tr>
  </table>
  </center>
  <?php
}
?>