<?php
/*
* @name simplerating 1.0
* Created By Chris grVulture Michaelides
* http://www.axxis.gr
* @copyright  Copyright (C) 2011 AXXIS INTERNET SOLUTIONS / All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

defined('_JEXEC') or die(dirname(__FILE__).DS.'Restricted access');

function com_install() {
$database = JFactory::getDBO();
$echo = false;
echo "<img src=\"http://www.axxis.gr/images/simplerating_logo.jpg\"><br/>AXXIS Simple Rating (and Reviews) written by Chris Michaelides<br /><br />";
echo "For any questions, bug reporting, etc. visit <a href='http://www.axxis.gr'>Axxis Internet Solutions</a><br /><br />";
echo "<small>Copyright © 2011 www.axxis.gr	All rights reserved.</small><br /><br />";
//==============================================================================
    $query = "
CREATE TABLE IF NOT EXISTS `#__simplerating_extravote` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `extra_id` int(11) NOT NULL,
  `option` varchar(50) NOT NULL,
  `lastip` varchar(50) NOT NULL,
  `rating_sum` int(11) NOT NULL,
  `rating_count` int(11) NOT NULL,
  `review_text` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  `published` tinyint NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `extravote_idx` (`user_id`)
)
";
		$database->setQuery($query);
		if (!$database->query()) {
      echo "<br/>WARNING: Error creating simple rating table. Please contact support.<br/>";
      return FALSE;
    }

    $query = "
CREATE TABLE IF NOT EXISTS `#__simplerating_extravote_reports` (
  `id` int(11) NOT NULL auto_increment,
  `report_user` int(11) NOT NULL,
  `review_id` int(11) NOT NULL,
  `report_text` text NOT NULL,
  `date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`),
  KEY (`report_user`)
)
";
		$database->setQuery($query);
		if (!$database->query()) {
      echo "<br/>WARNING: Error creating reports table. Please contact support.<br/>";
      return FALSE;
    }

  if ($echo) echo $echo;
  return TRUE;
}  

// ************************
// UPDATE TABLE FOR UPGRADES
// ************************
function newUpdate($field,$type,$table) {
$db = JFactory::getDBO();
		$query = "SHOW COLUMNS FROM `$table` LIKE '".$field."'";
		$db->setQuery($query);
	  $data = $db->loadObjectList();
    if (count($data)==0) {
      $query = "ALTER TABLE `$table` ADD `$field` $type";
		  $db->setQuery($query);
  		$db->query();
    } else {
  		$query = "DESCRIBE `$table`";
  		$db->setQuery($query);
  	  $data = $db->loadObjectList();
  	  foreach ($data as $row) {
        if ($row->Field == $field) {
          //echo "Field $field found. ";
          if ($row->Type != $type) {
            //echo "Changing ".$row->Type." to $type <br />";
            $query = "ALTER TABLE `$table` MODIFY `$field` $type";
      		  $db->setQuery($query);
        		$db->query();
          }
        }
      }
    }
}
// ************************
?>