<?php

/* CLASS FILE
----------------------------------*/

class mswsys extends db {

  public $settings;
  public $dt;
  public $social;

  public function load($d = []) {
    switch($d['section']) {
      case 'product':
        $Q = db::db_query("SELECT * FROM `" . DB_PREFIX . "products`
             WHERE `en` = 'yes'
             AND `id` = '{$d['id']}'
             ");
        return db::db_object($Q);
        break;
      case 'promo':
        $Q = db::db_query("SELECT * FROM `" . DB_PREFIX . "products_promo`
             WHERE `product` = '{$d['product']}'
             LIMIT 1
             ");
        return db::db_object($Q);
        break;
      case 'new-page':
        $Q = db::db_query("SELECT * FROM `" . DB_PREFIX . "pages`
             WHERE `en` = 'yes'
             AND `id` = '{$d['id']}'
             ");
        return db::db_object($Q);
        break;
    }
  }

  public function theme() {
    if ($this->settings->entheme == 'yes') {
      $Q = db::db_query("SELECT `theme` FROM `" . DB_PREFIX . "themes`
           WHERE `from` <= '" . $this->dt->ts() . "' AND `to` >= '" . $this->dt->ts() . "'
           LIMIT 1
           ");
      $T = db::db_object($Q);
      if (isset($T->theme)) {
        return $T->theme;
      }
    }
    return 'none';
  }

  public function modules($js = [], $area = 'header', $params = []) {
    $str = [];
    switch($area) {
      case 'header':
        if (in_array('date-picker', $js)) {
          $str[] = '<link rel="stylesheet" href="' . $this->settings->ifolder . THEME_FOLDER . '/assets/css/datepicker.css" type="text/css">';
        }
        break;
      case 'footer':
        if (in_array('ibox', $js)) {
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/jquery.ibox.js"></script>';
        }
        if (in_array('textarea', $js)) {
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/jquery.textareafullscreen.js"></script>';
          $tmp = mswTmp(PATH . THEME_FOLDER . '/html/js/textarea.htm', 'yes');
          if ($tmp) {
            $str[] = str_replace(array(
              '{theme_folder}'
            ), array(
              THEME_FOLDER
            ), $tmp);
          }
        }
        if (in_array('sort-table', $js)) {
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/jquery.stupidtable.js"></script>';
          $tmp = mswTmp(PATH . THEME_FOLDER . '/html/js/sort-table.htm', 'yes');
          if ($tmp) {
            $str[] = str_replace(array(
              '{theme_folder}'
            ), array(
              THEME_FOLDER
            ), $tmp);
          }
        }
        if (in_array('date-picker', $js)) {
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/jquery.datepicker.js"></script>';
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/jquery.datepicker.js"></script>';
          $str[] = '<script src="' . $this->settings->ifolder . THEME_FOLDER . '/assets/js/plugins/i18n/datepicker.' . $params['lang'] . '.js"></script>';
          $date = mswTmp(PATH . THEME_FOLDER . '/html/js/date-picker.htm', 'yes');
          $str[] = str_replace(array(
            '{calformat}',
            '{first_day}',
            '{lang}',
            '{theme_folder}'
          ),array(
            $this->settings->calformat,
            ($this->settings->weekstart == 'sun' ? 0 : 1),
            mswJSClean($params['lang']),
            THEME_FOLDER
          ), $date);
        }
        break;
    }
    return (!empty($str) ? implode(mswNL(), $str) : '');
  }

  public function enable() {
    if ($this->settings->autoenable> 0 && ($this->settings->autoenable <= $this->dt->ts())) {
      db::db_query("UPDATE `" . DB_PREFIX . "settings` SET `sysstatus` = 'yes', `autoenable` = '0'");
      header("Location: index.php");
      exit;
    }
  }

  public function unicode($len = 30, $add = '') {
    return substr(mswEnc(uniqid(rand(),1) . $add), 3, $len);
  }

}

?>