<?php
/*-------------------------------------------------------------------------
# com_layer_slider - Creative Slider
# -------------------------------------------------------------------------
# @ author    John Gera, George Krupa, Janos Biro, Balint Polgarfi
# @ copyright Copyright (C) 2017 Offlajn.com  All Rights Reserved.
# @ license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# @ website   http://www.offlajn.com
-------------------------------------------------------------------------*/
?><?php
defined('_JEXEC') or die('Restricted access');

class com_layer_slider extends JObject {

	function install() {
    $db = JFactory::getDBO();
    $db->setQuery("
      CREATE TABLE IF NOT EXISTS `#__layerslider` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `author` int(11) NOT NULL DEFAULT 0,
        `name` varchar(100) NOT NULL,
        `slug` varchar(100) NOT NULL,
        `data` mediumtext NOT NULL,
        `date_c` int(11) NOT NULL,
        `date_m` int(11) NOT NULL,
        `flag_hidden` tinyint(1) NOT NULL DEFAULT 0,
        `flag_deleted` tinyint(1) NOT NULL DEFAULT 0,
        `schedule_start` int(11) NOT NULL,
        `schedule_end` int(11) NOT NULL,
        `flag_popup` tinyint(1) NOT NULL DEFAULT 0,
        PRIMARY KEY (`id`)
      )
    ")->query();

    $db->setQuery("SHOW COLUMNS FROM `#__layerslider`");
    $fields = $db->loadAssocList('Field');

    if (!isset($fields['schedule_start'])) {
      $db->setQuery("
        ALTER TABLE `#__layerslider`
        ADD `schedule_start` int(11) NOT NULL,
        ADD `schedule_end` int(11) NOT NULL
      ")->query();
    }
    if (!isset($fields['flag_popup'])) {
      $db->setQuery("ALTER TABLE `#__layerslider` ADD `flag_popup` tinyint(1) NOT NULL DEFAULT 0")->query();
    }

    $db->setQuery("
      CREATE TABLE IF NOT EXISTS `#__layerslider_revisions` (
        id int(10) NOT NULL AUTO_INCREMENT,
        slider_id int(10) NOT NULL,
        author int(10) NOT NULL DEFAULT 0,
        data mediumtext NOT NULL,
        date_c int(10) NOT NULL,
        PRIMARY KEY (id)
      )
    ");

    $db->setQuery("
      CREATE TABLE IF NOT EXISTS `#__layerslider_options` (
        `option_id` int(11) NOT NULL AUTO_INCREMENT,
        `option_name` varchar(64) NOT NULL,
        `option_value` text NOT NULL,
        PRIMARY KEY (`option_id`),
        UNIQUE KEY `option_name` (`option_name`)
      )
    ")->query();


    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    $query->select('*');
    $query->from($db->quoteName('#__layerslider_options'));
    $query->where($db->quoteName('option_name')." = 'ls-google-fonts'");
    $db->setQuery($query);
    $row = $db->loadRow();
    if(!$row){
      // Create an object.
      $datas = new stdClass();
      $datas->option_name = "ls-google-fonts";
      $datas->option_value = '[{"param":"Lato:100,300,regular,700,900","admin":false},{"param":"Open+Sans:300","admin":false},{"param":"Indie+Flower:regular","admin":false},{"param":"Oswald:300,regular,700","admin":false}]';

      $result = JFactory::getDbo()->insertObject('#__layerslider_options', $datas);
    }

    $sql = preg_split('/;\r?\n/', file_get_contents(dirname(__FILE__).'/install.sql'));
    foreach ($sql as $query) {
      if ($query) {
        $db->setQuery($query);
        $db->query();
      }
    }


	}

	function uninstall() {
  }

}